home January 01, 2017

Squid User-Agent Randomizer


If you are using Squid as an anonymous proxy then you might like this perl script. It was made to change the User-Agent string in the squid.conf configuration file before squid was restarted or reconfigured.

The idea is to set a random user agent string every time squid is restarted. For example, in the script we could have the words "Stargate" and "Carter" and the user agent could turn out to be "Stargate/1.0 (Carter)" or "Carter/1.0 (Stargate)" randomly. This is _not_ a security tool, just something to keep those webalizer tools busy and the site admins amused. Instead of sending a bunch of boring Mozilla/5.0 strings as the user-agent you could send the words of your choice. Be creative.

If your User-Agent normally was sent as thus:
"Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.6) Gecko/20061201 Firefox/2.0.0.6 (Ubuntu-feisty)"

You would now be seen as the anonymous User-Agent string:
"Stargate/1.0 (Carter)" or even "Carter/1.0 (Stargate)"
NOTE: If you need assistance with Squid check out the moneyslow.com Squid "how to"

Getting Started

Take a look at the following perl script.

#!/usr/bin/perl -w
#
### moneyslow.com Squid User-Agent Randomizer
### squid_ua_random.pl
#

my $infile = "/etc/squid/squid.conf";

open F,$infile or die "Could not open $infile ($!)";
undef $/; # Tell perl to slurp whole file in one piece.
$txt = <F>;
close F;

my $useragent = &ruseragent;

$txt =~ s/^(header_replace User-Agent ).*/$1$useragent/m;

open F,">$infile.$^T.$$" or die "Could not create $infile.$^T.$$ ($!)";
print F $txt;
close F;

rename "$infile.$^T.$$", $infile
or die "Error renaming $infile.$^T.$$ to $infile ($!)";

sub rdig{
  my @digits = qw/name1 name2 name3 name4 name5/;
  srand(time ^ $$ ^ unpack "%32L*", `ps -ael | gzip`);
  $digits[int(rand(@digits))];
}

sub ruseragent{
  join '',&rdig,"/1.0 (",&rdig,")";
}

To use the script...

Step 1: Make sure the line that reads "my $infile = "/etc/squid/squid.conf";" actually points to your squid.conf file.

Step 2: Take a look at the line "my @digits = qw/name1 name2 name3 name4 name5/;". This is the line you will enter all of the words that will be randomly picked to be your user agent. As place holders the words name1, name2, etc are here. You can have as many words as you want as long as they are space separated and on a single line.

Step 3: Now, just execute the script before starting squid _OR_ before you execute a "squid -k reconfigure". You might want to setup a cron job to execute this script and reconfigure squid every hour on the hour.

Questions?

How do I test if the User-Agent is being changed by squid correctly?You can test the browser headers at Xhaus's header test page.