<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#!/usr/bin/perl

use Net::RawIP qw(:pcap);
system("clear");
$SIG{INT} = \&amp;quit;

my ($ip,$seq_nr,$count);

$my_ip = "123.123.123.123";	# change value
$count = 0;
$|=1;

$dev='eth0';
$ip_addr=${ifaddrlist()}{$dev};

$packet_tcp=new Net::RawIP({tcp=&gt;{}});
$filt_tcp="ip proto \\tcp";
$pcap_tcp=$packet_tcp-&gt;pcapinit($dev,$filt_tcp,1500,60);
$offset_tcp = linkoffset($pcap_tcp);


if (fork){ loop $pcap_tcp,-1,\&amp;check_tcp,\@packet_tcp;} 


sub check_tcp{
	
	my $time = timem();
	$packet_tcp-&gt;bset($_[2],$offset_tcp);
	$proto=$packet_tcp-&gt;proto;
	
	my ($saddr,$daddr,$sport,$dport,$seq,$ack_seq,$urg,$ack,$psh,$rst,$syn,$fin,$data)= $packet_tcp-&gt;get({ip=&gt;['saddr','daddr'],tcp=&gt;['source','dest','seq','ack_seq','urg','ack','psh','rst','syn','fin','data']});
	
	if ($sport == "22" ) {
 		# vorerst ssh ignorieren
	} elsif ( $dport == "22" ) {
	} else {
		
		$seq =~ s/-//g;
		
		
		if ($syn){	# &amp; !$ack) {	
			
			print "PACKET: $seq "; 
	
			if ($urg) {print "URG "};
			if ($ack) {print "ACK "};
			if ($psh) {print "PSH "};
			if ($rst) {print "RST "};
			if ($syn) {print "SYN "};
			if ($fin) {print "FIN "};	
		
		
			if ($saddr &gt; $daddr) {
				
				print "\t",ip2name($saddr), ":$sport \t -&gt; \t",ip2name($daddr),":$dport \n";
				
				push(@ips, ip2name($daddr) . "#$seq");
				
			} else {
				
				print "\t",ip2name($daddr), ":$dport \t &lt;- \t",ip2name($saddr),":$sport \n";
				
				# ATTACKER_IP - SEQ - ATTACK_PORT - MY_PORT
				push(@content, ip2name($saddr) . "#$seq#$sport#$dport");
				
			
				if ($#content &gt;= "120"){
					foreach $entry(@content){
						($ip,$seq_nr,$attacker_port,$my_port) = split('#', $entry);
						if ($ip == ip2name($saddr) ){
							$count++;
						}
						if ($count &gt;= "100"){
							print "INCOMING SYNFLOOD DETECTED!\n";
							$attacker = $ip;
							
							foreach $bad (@content) {
								($ipX,$seq_nrX,$attacker_portX,$my_portX) = split('#', $bad);
								if ($ipX == $attacker){
									print "KILLING BAD CONNECTION ATTEMPT!\n";
									
									# send FIN to abort connection
									my $new_packet= new Net::RawIP;
									$new_packet-&gt;set({
										ip=&gt;{	saddr=&gt;$my_ip,
											daddr=&gt;$attacker
											},
										tcp=&gt;{
											source=&gt;$my_port,
											dest=&gt;$attacker_portX,
											fin=&gt;"1",
											seq=&gt;$seq_nrX
										}
										});
									$new_packet-&gt;send;
								}
							}	
						}
					}
				$count = 0;	
				@content = ();
				}
				
			}
		}
	
	}
}

sub ip2name {
my $addr = shift;
(gethostbyaddr(pack("N",$addr),AF_INET))[0] || ip2dot($addr);
}

sub ip2dot {
sprintf("%u.%u.%u.%u",unpack "C4", pack "N1", shift);
}

sub quit {
	exit(0);
}</pre></body></html>