From 140d1eae64a33cb8862411c7dc8cf7294eeaae65 Mon Sep 17 00:00:00 2001 From: Andrew Kesterson Date: Mon, 18 May 2026 12:43:29 -0400 Subject: [PATCH] Import old code, circa 2005 --- logger | 198 +++++++++++++++++++++++++++++++++++++++++++++++++ portscan.pl | 94 +++++++++++++++++++++++ updateHosts.pl | 96 ++++++++++++++++++++++++ 3 files changed, 388 insertions(+) create mode 100755 logger create mode 100755 portscan.pl create mode 100755 updateHosts.pl diff --git a/logger b/logger new file mode 100755 index 0000000..65b1eda --- /dev/null +++ b/logger @@ -0,0 +1,198 @@ +#!/usr/bin/perl -w +#extract lines in a given date range from an xchat logfile +#is a bit too hardcoded to be translated to other clients (gaim, etc) +#sorry! Future update will see this more modular. +use Getopt::Std; +use strict; + +#extractDate(\%hash, $line) +#extract the contents of the date in line and put it in a hash +#(hash is keyed by month, day, hour, minute, second) +sub extractDate; +#buildRegex($line) +#build a regex from an input string of the form MM HH MM:SS:DD and return it +sub buildRegex; + +my %args; +my $buffer; + +#regexes for quick and easy pattern matching +my $startregex; +my $endregex; +#start and end date hashes for more complex range matching +my %startdate; +my %enddate; +# additional regex to check for in lines of output +my $addlRegex = ".*"; + +#Yeah this is a bit hackish. Whaddya want from me? This is, like, my second perl script. +my %monthvals = ( + Jan => 0, + Feb => 1, + Mar => 2, + Apr => 3, + May => 4, + Jun => 5, + Jul => 6, + Aug => 7, + Sep => 8, + Oct => 9, + Nov => 10, + Dec => 11, + jan => 0, + feb => 1, + mar => 2, + apr => 3, + may => 4, + jun => 5, + jul => 6, + aug => 7, + sep => 8, + oct => 9, + nov => 10, + dec => 11, + ); + +my $help; + +$help = < inputfile + -h : this help + -s : Starting date + -e : Ending date + -r : Further restrict the search to lines matching this regex (search, not replace) + + inputfile defaults to stdin if no file provided + + Dates are expected in "Month Day Hour:Minute:Second" format. Months + are abbreviated to 3 letters, non-case sensitive. You must provide + AT LEAST one regex. (If one is missing, then it is assumed that both -s + and -e are equal.) + + To search for all lines on the 13th of May: + logger -s "May 13 *:*:*" xchatlogfile + + To search for all lines between the 13th of May and the 20th of June: + logger -s "May 13 *:*:*" -e "Jun 20 *:*:*" xchatlogfile + + To search for all lines written between 5 and 10 pm in the entire log: + logger -s "* * 17:*:*" -e "* * 22:*:*" xchatlogfile + + As above, but only match lines containing "somenick" + logger -s "* * 17:*:*" -e "* * 22:*:*" -r somenick xchatlogfile + +EOT + +getopt ( "hxs:e:f:r:", \%args ); + +# there's quite a bit of redundancy in the argument checking. Sue me. + +if ( $args{h} ) { + print $help; + exit; +} +if ( $args{r} ) { + $addlRegex = $args{r}; +} +if ( $args{s} ) { + $startregex = buildRegex($args{s}); + extractDate(\%startdate, $args{s}); +} +if ( $args{e} ) { + $endregex = buildRegex($args{e}); + extractDate(\%enddate, $args{e}); +} + +if ( !$args{e} && !$args{s} ) { + print $help; + exit; +} +elsif ( !$args{e} || !$args{s} ) { + if ( $args{s} ) { + $endregex = $startregex; + extractDate(\%enddate, $args{s}); + } + elsif ( $args{e} ) { + $startregex = $endregex; + extractDate(\%startdate, $args{e}); + } +} + + +while ( <> ) { + my $toPrint; + if ( $_ =~ m/^\*\*\*\*/i || $_ =~ m/^\s*\n/i ) { + # this is a xchat log status line (effectively a comment for our purpose) + next; + } + if ( $_ =~ m/$startregex/i || + $_ =~ m/$endregex/i ) { + $toPrint = $_; + } + else { + # not lucky enough for an exact match ... check the date + my %linedate; + extractDate(\%linedate, $_); + # check the dates to see if the date in the line is between startdate + # and end date + if ( !($enddate{month} eq "*") && + $monthvals{$linedate{month}} <= $monthvals{$enddate{month}} && + $monthvals{$linedate{month}} >= $monthvals{$startdate{month}} ) { + + if ( !($enddate{day} eq "*") && + $linedate{day} <= $enddate{day} && + $linedate{day} >= $startdate{day} ) { + + if ( !($enddate{hour} eq "*") && + $linedate{hour} <= $enddate{hour} && + $linedate{hour} >= $startdate{hour} ) { + + if (!($enddate{minute} eq "*") && + $linedate{minute} <= $enddate{minute} && + $linedate{minute} >= $startdate{minute} ) { + + if (!($enddate{second} eq "*") && + $linedate{second} <= $enddate{second} && + $linedate{second} >= $enddate{second} ) { + $toPrint = $_; + } + } + } + } + } + } + + if ( $toPrint && ($toPrint =~ m/$addlRegex/) ) { + print $toPrint; + } +} + +sub buildRegex { + my $orig = shift; + # make sure to not muck up the original + my $regex = $orig; + + $regex =~ s/\s/\\s/g; + $regex =~ s/\*/\.\*/g; + + return $regex; +} + +sub extractDate { + my $finalDate = shift; #expects a reference to a hash + my $buff = shift; #expects a line of text from the logfile + #expects buff in the form MM DD HH:MM:SS + my @date = split " ", $buff; + my @time = split ":", $buff; + + $time[0] =~ s/$date[0]\s$date[1]\s//; + + $finalDate->{month} = $date[0]; + $finalDate->{day} = $date[1]; + $finalDate->{hour} = $time[0]; + $finalDate->{minute} = $time[1]; + + my @splitline = split " ", $time[2]; + $finalDate->{second} = $splitline[0]; +} diff --git a/portscan.pl b/portscan.pl new file mode 100755 index 0000000..4729072 --- /dev/null +++ b/portscan.pl @@ -0,0 +1,94 @@ +#!/usr/bin/perl -w +#This could really stand to be improved, but it works for basic purposes +#right now of identifying open ports on the given host. +#The big improvement would be a hash containing basic jibberish to +#send to given ports (eg 22, 21, 25, 80, 110) to see if they're +#running standard services. + +use IO::Socket; +use Getopt::Std; +use strict; + +my %args; +my $socket; +my $host = "127.0.0.1"; +my $lowport = 0; +my $highport = 65535; +my $wait = 10; +my $outfile; +my $jibberish = "\n"; +my $usage = <$args{f}") + or die "Couldn't open output file $args{f}"; +} + +foreach ( $lowport ... $highport ) { + my $response = ""; + if ( $args{v} ) { + $response .= "trying $host port $_\n"; + } + $socket = IO::Socket::INET->new(PeerAddr => $host, + PeerPort => $_, + Proto => "tcp", + Type => SOCK_STREAM, + Timeout => $wait) + or next; + $response .= "Got socket on $host port $_\n"; + if ( $socket && $args{v} ) { + # got a connection, let's throw some jibberish at it and + # see what happens! + print $socket $jibberish; + my $response .= <$socket>; + close($socket); + } + if ( $outfile ) { + print $outfile $response; + } + else { + print $response; + } +} + +if ( $outfile ) { + close($outfile); +} diff --git a/updateHosts.pl b/updateHosts.pl new file mode 100755 index 0000000..07e4e4e --- /dev/null +++ b/updateHosts.pl @@ -0,0 +1,96 @@ +#!/usr/bin/perl -w +# updateHosts.pl Andrew Kesterson andrew@aklabs.net +# update the system hosts file with Mike's ad blocking file +# patch it up to include the newest entries +# this obviously needs to run as root if you're working on /etc/hosts + +# I'm still learning perl. Don't harsh me too much. + +use strict; +use LWP::Simple; +use Socket; +use Getopt::Std; +use Time::localtime; + +my $URL = "http://everythingisnt.com/hosts"; +my $hostfile = "/etc/hosts"; +my $buffer; +my %args; +my $updating = 0; +my $usage = <>$hostfile") or die "Couldn't open $hostfile for writing."; + +if (defined $args{v} ) { + print "Beginning update; due to calls to gethostbyname, the program might appear to hang while waiting to resolve.\n"; +} + +HOSTS: foreach my $host ( @newHosts ) { + $host =~ m/(127.0.0.1\s\w.*)/; + if ( defined $1 ) { + $host = $1; + $host =~ s/127.0.0.1\s//; + $host =~ s/\s.*//; + } + else { + # this should've returned us a hostname... + next; + } + + my @addresses = gethostbyname($host); + # just checking to see if it already exists in etc-hosts as 127.0.0.1 + @addresses = map { inet_ntoa($_) } @addresses[ 4 ... $#addresses]; + foreach my $addr ( @addresses ) { + if ( $args{v} ) { + print "checking 127.0.0.1 against address for $host : $addr\n"; + } + if ( $addr eq "127.0.0.1" ) { + # skip this one. + next HOSTS; + } + else { + if ( $args{v} ) { + print "adding $host\n"; + } + if ( !$updating ) { + # add a comment with the date if this is the first entry + my $time = localtime; + print hostsFile "\n# Updates from ", $time->year+1900 , + "-", $time->mon+1, "-", $time->mday, "\n"; + $updating = 1; + } + print hostsFile "127.0.0.1\t", $host, "\n"; + } + } +} + +close hostsFile;