Category: Networking
Author/Contact Info
Description: this code open the port 22 with iptables if you try to connect to the port 7, 3 5, 10 in the correct order.
#!/usr/bin/perl -w
use strict;
use Net::Pcap;
use NetPacket::Ethernet qw(:strip);
use NetPacket::IP qw(:protos :strip);
use NetPacket::TCP;
use NetPacket::UDP;

# Knock Knock Neo.... matrixz hasz youz :D

#################################################
my $ethx = $ARGV[0];                            #
my $address = $ARGV[1];                         #
#                                               #
my @fil = ("7", "3", "5", "10");                # List of port to be k
+nocked
my $MAXCO = @fil;                               #
#                                               #
my %sex;                                        # Glob Vars
#################################################

sub exe {
    system("iptables -A INPUT -p tcp -s 0.0.0.0 --dport 22 -j ACCEPT\n
+");
    }

sub check {
   my ($user_data, $header, $packet) = @_;
    # Via Ethernet Quindi Ip (Ip)
    $packet = NetPacket::IP->decode(eth_strip($packet));
    my $d_ip = $packet->{dest_ip};
    my $p_ip = $packet->{src_ip};
        if ($d_ip eq  $address) {
                my $s_ip=$packet->{src_ip};
                # Qui va via un altro incapsulamento
                $packet=NetPacket::TCP->decode($packet->{data});
                my  $port = $packet->{dest_port};
                print "$port\n";
                if (!$sex{$s_ip}) {
                    print"uNdEf\n";
                    $sex{$s_ip}++;
                }
                if ($packet->{dest_port} == $fil[$sex{$s_ip}]) {
                       $sex{$s_ip}++;
                       print"...\n";
                       if ($sex{$s_ip}= $MAXCO) {
                            exe;
                       }
                }
        }
}

sub create_obj {
    my $err;
    my $objz = Net::Pcap::open_live($ethx, 1500, 1, 0, \$err) || die "
+Cannot create packet descriptor: \$err, $!\n";
    return $objz;
}

sub noparam {
    print "\n";
    print "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n";
    print "\n";
    print "usage: perl portknockerz.pl <interface>\n";
    print "\n";
    print "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n";
    print "-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_\n";
    print "\n";
    exit;
   }

sub main {
    if (!defined $ARGV[0]) { noparam;}
    while (1) {
        my $pid=fork();
        if (not defined $pid) { die "Unable to fork. $!\n";}
        if ($pid) {
            wait();
            sleep(1);
            }else {
                print"fork success\n";
                my $pack=create_obj();
                print"Packet descriptor created\n";
                Net::Pcap::loop($pack, -1, \&check, "");
                Net::Pcap::close($pack);
            }
       }
}

main;


######################################################################
+#####
# Need to improve TCP or UDP packet flow..  ---> I don't know how to d
+o it
##########