Skip to content

Instantly share code, notes, and snippets.

@marmarek
Created April 25, 2016 08:46
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marmarek/1d0a296930b7784327aaf9a801ec5585 to your computer and use it in GitHub Desktop.
Save marmarek/1d0a296930b7784327aaf9a801ec5585 to your computer and use it in GitHub Desktop.
Qubes firewall learning based on denied connections
#!/usr/bin/perl
# usage:
# sudo tcpdump -vni eth0 port 53 or icmp | perl ./firewall-learn.pl
use strict;
use Data::Dumper;
use Sys::Hostname;
my %dns_cache;
my $host = hostname();
if (defined($ARGV[0])) {
$host = $ARGV[0];
}
my $denied_ip = undef;
my $denied_host = undef;
while (<>) {
if (defined($denied_ip)) {
if (m/ > $denied_ip\.(\S+): Flags/) {
print "qvm-firewall -a $host $denied_host tcp $1\n";
$denied_ip = undef;
} elsif (m/^[0-9]/) {
# next packet
print STDERR "Unrecognised packet to $denied_ip ($denied_host)\n";
$denied_ip = undef;
}
}
if (m/\.domain > 10\..*: \d+ \d+\/\d+\/\d+ (.*) \(\d+\)$/) {
# DNS response
foreach (split(/, /, $1)) {
if (m/(\S+) A ([0-9.]+)/) {
$dns_cache{$2} = $1;
}
}
}
if (m/ICMP host ([0-9.]+) unreachable - admin prohibited/) {
$denied_ip = $1;
if (defined($dns_cache{$1})) {
$denied_host = $dns_cache{$1};
} else {
$denied_host = $denied_ip;
}
}
}
#print Dumper(%dns_cache);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment