Skip to main content
goliver
Staff
Staff
January 3, 2017

Technical Tip: How to perform a search and replace in HTML Body text

  • January 3, 2017
  • 0 replies
  • 2142 views

Description

 

This article describes that when implementing FortiWeb as a proxy, there may be a need to replace text in the HTML body so that the HTML from FortiWeb is presented correctly to the user.

For example, when offloading to the FortiWeb, the server can often return the URL in the form HTTP rather than HTTPS in the HTML, as the FortiWeb is connected as HTTP.

 

Scope

 

FortiWeb.


Solution

 

The following steps can be used to enable a rewrite of the HTML body text from HTTP to HTTPS:

 

  1. Configure the maximum cache for FortiWeb to be able to manipulate the text from the back-end server.
 
config system advanced
    set max-cache-size 1024
end

 

  1. Set up an uncompress rule to enforce that the HTML is received as text and not compressed.  (A common reason why the text is not detected and changed).
 
config waf web-protection-profile inline-protection
    edit "rewrite"
        set http-session-management enable
        [deleted ....]   
     set file-compress-rule CompressZ
     set file-uncompress-rule UnCompressZ
    next
end
 
  1. Collect a packet capture matching the client's IP address at Network > Packet Capture to check the traffic that needs to be replaced.

Note: If traffic is encrypted using HTTPs, the keys for decryption can be captured by enabling while collecting the PCAP and decrypting traffic. For the required steps, see the following documents:

  1. Create a URL rewrite rule to have a regex to detect the 'http://' and replace it with the 'https://'.
 
config waf url-rewrite url-rewrite-rule
    edit "rewrite"
        set location http://
        set body_replace https://
        set action http-body-rewrite
            config  match-condition
                edit 1
                 set object http-body
                 set reg-exp "http:\\/\\/"
                 set protocol-filter enable
                 set content-type-filter enable
                 set content-type-set text/html text/plain text/javascript application/xml(or)text/xml application/javascript application/soap+xml application/x-javascript application/json application/rss+xml
                next
            end
        next
    end
 
  1. Create a URL Rewrite Policy and assign the Rule.
 
config waf url-rewrite url-rewrite-policy
    edit "rewrite"
        config  rule
            edit 1
                set url-rewrite-rule-name rewrite
            next
        end
    next
end
 
It is also possible to configure a URL rewrite rule and policy from the GUI as follows:
 
URL rewrtie.png
 
  1. Assign the URL Rewrite Policy to the Web Protection Profile and then to the Server Policy.

Note:

When testing the problem, it appears not to work because of the Cache of the Browser.

Thought Leadership Security Summit. Outpace New Threats with AI - enhanced defense. Tuesday, Septmeber 15, 8:30 AM - 2:30 PM PT. The Golf Club at Newcastle, WA.
Fortinet Flag the Hack. Wednesday, August 26, 9:00 AM - 5:00 PM ET, COSM, Atlanta, GA.