Skip to content

Using Perl to bypass UserAgent blocks

Let’s say you need to download/see a web content but the server is blocking your request due to your UserAgent.
For instance, an attacker has forged an web page where the malicious content is only available for Firefox running on Windows.

We can use LWP::UserAgent or urllib.
I coded in Perl due to project requirement.

The following code list all URLs inside this “malicious” domain ;)

use LWP::UserAgent;
use HTTP::Response;
use Data::Dumper;
use HTML::TreeBuilder 5 -weak; # Ensure weak references in use
use strict;
use warnings;

my %ua_config = (
	agent => "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1",
	timeout => 60

my $ua = LWP::UserAgent->new(%ua_config);
my $urladdr = "";
my $out_file="/tmp/";

my $response = $ua->get($urladdr);

open (FH, '>', $out_file) or die "Could not open";
# if ($respon.) {save file }
print FH $response->decoded_content if $response->is_success;

## Parsing File by href
my $tree = HTML::TreeBuilder->new;

for ( @{ $tree->extract_links("a", "href") } ) {
	my($link, $element, $attr, $tag) = @$_;
	print $link, "\n";

For additional UserAgent:

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload the CAPTCHA.