Block specific User-Agent using FortiADC script
Description
There is a requirement to block a specific User-Agent. FortiADC Scripting feature supports this requirement with a custom HTTP script.
Refer to the documentation at the end of this article for more information on the Scripting feature and the respective configuration.
Scope
FortiADC and FortiADC VM.
Solution
There are 2 options with the respective HTTP Response Code.
Option 1: HTTP Response Code 403 Forbidden.
This script will check if the User-Agent matches the listed User-Agent and reply with the 403 HTTP Response Code.
when HTTP_REQUEST {
-- Get User-Agent information
agent = HTTP:header_get_value("User-Agent")
-- Check if User-Agent match value listed below
if agent:lower():find("ua1", 1, true)
or agent:lower():find("ua2", 1, true)
or agent:lower():find("ua3", 1, true) then
t = {}
t["code"] = 403
t["content"] = "HTTP/1.1 403 Forbidden\r\nConnection: close\r\nContent-Type: text/plain\r\n\r\n403 Forbidden - Access Denied"
-- If matched response with 403 Forbidden
HTTP:respond(t)
end
}Information in the traffic logs:

Option 2: HTTP Response Code 503 Service Unavailable.
This script will check if the User-Agent matches the listed User-Agent and reply with the 503 HTTP Response Code.
when HTTP_REQUEST {
-- Get User-Agent information
agent = HTTP:header_get_value("User-Agent")
-- Check if User-Agent match value listed below
if agent:lower():find("ua1", 1, true)
or agent:lower():find("ua2", 1, true)
or agent:lower():find("ua3", 1, true) then
-- If matched response with 503 Server Unavailable
HTTP:close()
end
}Information in the traffic logs:

Related documents:
