Advertisement
access2godzilla

hosts2abe.plx - HOSTS file to ABE rule converter

Feb 25th, 2014
433
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 2.13 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. # HOSTS file to ABE rule converter
  4. # Updated 27/2/2014 with Bugfix (Thanks Miller at Stackoverflow!)
  5.  
  6. use strict;
  7. use warnings;
  8. use Regexp::Assemble;
  9. use File::Slurp;
  10.  
  11. sub printhelp
  12. {
  13.     print "Usage: hosts2abe.plx [hostsfile] [aberuleset]\nConvert a HOSTS file to ABE ruleset.\n";
  14.     exit(0);
  15. }
  16.  
  17. sub printargerror
  18. {
  19.     print "Wrong arguments, please type `hosts2abe.plx -h' to get help.\n";
  20.     exit(1);
  21. }
  22.  
  23. printhelp if (@ARGV == 0 || $ARGV[0] eq "-h");
  24. printargerror if (@ARGV != 2);
  25.  
  26. die "Cannot open input file $ARGV[0] : $!" unless (my $hostsfile = read_file ($ARGV[0], err_mode => "quiet"));
  27.  
  28. print "Preliminarily processing the hosts file ...\n";
  29.  
  30. $hostsfile =~ s/#.*//mg;            # Strip comments
  31. $hostsfile =~ s/(?:^\s+|\s+$)//mg;  # Strip unnecessary whitespace
  32. $hostsfile =~ s/^[0-9.:]+\s+//mg;   # Strip IP addresses
  33. $hostsfile =~ s/^\s*\n//g;          # Strip blank lines
  34. $hostsfile =~ s/\s+/\n/g;           # Put every whitespace seperated element on its own line
  35.  
  36. # Whitelisted hosts
  37. $hostsfile =~ s/(?:localhost|360\.yahoo\.com|92x\.tumblr\.com|\*.*)\n//mg;
  38.  
  39. # Generalise rules
  40. print "Generalising rulesets with heuristics...\n";
  41. $hostsfile =~ s/^(?:www|[a-z-]*[0-9.-]+|[0-9]+[a-z]+)\.(.{4,}\..{2,})/$1/mg;
  42.  
  43. # Remove redundancies
  44. print "Removing redundancies...\n";
  45. my @hostnames = split ("\n", $hostsfile);
  46.  
  47. # Thanks Hynek -Pichi- Vychodil!
  48. # https://stackoverflow.com/questions/22008651/removing-redundant-array-elements
  49. my @result = do {
  50.     my $p;
  51.     map scalar reverse, grep {
  52.         my $x = !defined $p || substr($_, 0, length($p)) ne $p;
  53.         $p = $_ if $x;
  54.         $p .= '.' if $p !~ /\.$/;
  55.         $x
  56.     } sort map scalar reverse, @hostnames;
  57. };
  58.  
  59. # Form regular expressions
  60. print "Forming regexes...\n";
  61. my $regex = Regexp::Assemble -> new;
  62.  
  63. for (my $i = 0; $i < @result; $i++)
  64. {
  65.     $_ = $result[$i] =~ s/\./\\./gr; # Non destructive modifier 'r' - Perl >= 5.13 required!
  66.     $regex -> add("^https?://(?:[a-z0-9_-]+\\.)*".$_."/");
  67. }
  68.  
  69. print "Writing to file...";
  70. open (ABEFILE, '>', $ARGV[1]) or die "Cannot open output file $ARGV[1] : $!\n";
  71. print ABEFILE "Site " . substr($regex->re, 4, -1) . "\nDeny INC";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement