User Tools

Site Tools


ubuntu:squid:modify_content

Ubuntu - Squid - Modify Content

Since the Linux proxy server is between the browser and the internet, this is a very good position to alter the delivered content.

You can change images or ads or whatever. This can be done using the url_rewrite_program module.

Actually, you can do more than that, but we don’t want to be evil.

In our example, we will flip the images and surf the flipped images instead of the original.


Install ImageMagick

sudo apt install imagemagick

Then we will write the script that will do the magic.

The script will be written in Perl.

You can find the script here

#!/usr/bin/perl
$|=1;
$count = 0;
$pid = $$;
while (<>) {
        chomp $_;
        if ($_ =~ /(.*\.jpg)/i) {
                $url = $1;
                system("/usr/bin/wget", "-q", "-O","/space/WebPages/images/$pid-$count.jpg", "$url");
                system("/usr/bin/mogrify", "-flip","/space/WebPages/images/$pid-$count.jpg");
                print "http://127.0.0.1/images/$pid-$count.jpg\n";
        }
        elsif ($_ =~ /(.*\.gif)/i) {
                $url = $1;
                system("/usr/bin/wget", "-q", "-O","/space/WebPages/images/$pid-$count.gif", "$url");
                system("/usr/bin/mogrify", "-flip","/space/WebPages/images/$pid-$count.gif");
                print "http://127.0.0.1/images/$pid-$count.gif\n";
 
        }
        else {
                print "$_\n";;
        }
        $count++;
}

This Perl script searches for JPG, GIF, and PNG, images in the carried content, once it is found, it uses mogrify utility that shipped with ImageMagick to flip the images and put the flipped image in /var/www/html/ which is the root directory for Apache server and apache service should be running of course, then send the flipped images as a response.


Add Ownership

Add ownership for squid for this folder:

sudo usermod -aG www-data squid

Configure Squid

Tell squid about this script. Open the configuration file and type the following:

/etc/squid/squid.conf
...
url_rewrite_program /home/likegeeks/flip.pl
...

Restart Squid

sudo systemctl restart squid

The web has a lot of Perl scripts that play with the content, some of them are good, and some others are evil.

ubuntu/squid/modify_content.txt · Last modified: 2020/07/15 09:30 by 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki