#!/user/bin/perl ########################################################################### # # # GateKeeper # # # # (C) 2016 Domero, Groningen, NL # # chaosje@gmail.nl # # # # ALL RIGHTS RESERVED # # # ########################################################################### use gserv; use grand qw(rand randomhex); use gutil qw(validip gettime); my $ESQUIT=0; $SIG{INT}=\&stopserver; $SIG{PIPE}=\&clientquit; $SIG{__DIE__}=\&fatalerror; $SIG{__WARN__}=\&warning; my @ALLOWED=(); my $SERVPASS = "Vg5ks4d7JuBnWqsTFFsLpQYkGffW67Gbc9m0feEg65"; my $serv=gserv::init(\&handleinput,\&handleloop); $serv->{name}="GateKeeper v1.0.1 (C) 2016 Domero"; $serv->{pingtime}=10; $serv->{pingtimeout}=10; $serv->{server}{port}=7010; $serv->{idletimeout}=30; $serv->{allowedip}= [ '127.0.0.1','94.212.45.34','141.138.137.123' ]; open OUTPUT, '>>', "gatekeeper.log" or die "Can't create filehandle: $!"; $serv->start; while (!$ESQUIT) { $serv->takeloop(); } sub fatalerror { print OUTPUT "!!!! FATAL ERROR !!!! $!"; $ESQUIT=1 } sub warning { print OUTPUT "!! WARNING !! $!"; } sub stopserver { print OUTPUT "** Shutting down server!!!!\n"; $ESQUIT=1 } sub clientquit { if ($serv->{activeclient}) { $serv->{activeclient}{killme}=1; } } sub addclient { my ($ip,$pass,$verify) = @_; my $client={ ip => $ip, pass => $pass, verify => $verify }; push @ALLOWED,$client } sub delclient { my ($ip,$port) = @_; my $i=0; while ($i<=$#ALLOWED) { print $ALLOWED[$i]{ip}."\n"; if (($ALLOWED[$i]{ip} eq $ip) && ($ALLOWED[$i]{port} eq $port)) { splice(@ALLOWED,$i,1); return } $i++ } } sub search { my ($verify) = @_; foreach my $client (@ALLOWED) { if ($client->{verify} eq $verify) { return $client } } return {} } sub checkip { my ($ip) = @_; foreach my $client (@ALLOWED) { if ($client->{ip} eq $ip) { return 1 } } return 0 } sub handleinput { my ($command,$client,$data) = @_; if ($command eq 'connect') { if (!checkip($client->{ip})) { print OUTPUT gettime()." !! ATTACK $client->{ip}:$client->{port}\n"; $client->{killme}=1 } } elsif ($command eq 'input') { if (!length($data)) { return } if ($data =~ /^login ([a-zA-Z0-9]+) ([0-9\.]+)$/) { if (($1 eq $SERVPASS) && (validip($2))) { my $pass=randomhex(rand(100)+150); my $verify=randomhex(20); addclient($2,$pass,$verify); gserv::out($client,$pass." ".$verify); print OUTPUT gettime()." CREATE $pass for $2\n" } } elsif ($data =~ /^getpass ([0-9A-Z]{20})$/) { my $verify=$1; my $check=search($verify); if ($check->{ip} eq $client->{ip}) { gserv::out($client,$check->{pass}); print OUTPUT gettime()." SERVED $client->{ip}:$client->{port}\n"; $client->{killme}=1 } else { print OUTPUT gettime()." ** ILLEGAL REQUEST $client->{ip}:$client->{port} $verify\n"; $client->{killme}=1 } } } elsif ($command eq 'error') { print OUTPUT gettime()." ERROR $client->{ip}:$client->{port} $data\n" } elsif ($command eq 'quit') { print OUTPUT gettime()." QUIT $client->{ip}:$client->{port}\n"; delclient($client->{ip},$client->{port}) } } sub handleloop { my ($client) = @_; }