Skip to content

Instantly share code, notes, and snippets.

@waffle2k
Last active August 29, 2015 13:58
Show Gist options
  • Save waffle2k/10022611 to your computer and use it in GitHub Desktop.
Save waffle2k/10022611 to your computer and use it in GitHub Desktop.
Given a list of URLs, check if they redirect elsewhere, and provide the abuse contact for the IP network on which the domain resides.
#!/usr/bin/perl
use strict;
use LWP::UserAgent;
use LWP::Simple;
sub contact_lookup_ip {
my $ip = shift;
my $q = join( ".", reverse( split( /\./, $ip ) ) ) . ".abuse-contacts.abusix.org";
my @response = `dig txt $q +short`;
chomp for @response;
return \@response;
}
sub resolve_link {
my $link = shift;
if ($link =~ /https?:\/\/(\S+?)\// ){
my @r = `dig $1 +short`;
chomp for @r;
for (@r){
return $_ if /\d+\.\d+\.\d+\.\d+/;
}
}
return undef;
}
sub check_for_302 {
my $link = shift;
my $ua = LWP::UserAgent->new();
$ua->timeout(10);
$ua->requests_redirectable(undef);
my $req = HTTP::Request->new( 'GET', $link );
my $res = $ua->request($req);
return $res->code();
}
sub check_for_meta_refresh {
my $link = shift;
my $ua = LWP::UserAgent->new();
$ua->timeout(10);
my $res = $ua->get( $link );
return undef unless ($res->is_success);
my $content = $res->decoded_content;
if ( $content =~ /meta http-equiv="refresh"/m ){
my $landing = $1 if ( $content =~ /url=(\S+)/m );
return $landing;
}
return undef;
}
while( my $link = <> ){
chomp($link);
print STDERR "I: [$link]\n";
my $code = check_for_302 $link;
my $landing = '';
print STDERR "I: Returncode: $code\n";
next unless ( $code == 301 || $code == 302 || $code == 200);
if ($code == 200){
$landing = check_for_meta_refresh( $link )
or next;
}
print "I: $link redirects\n";
my $a = resolve_link($link);
my $contacts = contact_lookup_ip( $a );
print "A: Send report to [" . join( ", ", @$contacts ) . "] regarding $link => $landing\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment