3 # Convert PRADS db to SNORT host-attribute table
7 # Work-in-progress: services!
9 # - figure out how to filter client ports (not a "service" per se
10 # - what is confidence and how does it affect the resulting rules?
11 # - why oh why attribute maps?
13 # Copyright 2010 Kacper Wysocki <kwy@redpill-linpro.com>
20 our $DATABASE = 'dbi:SQLite:dbname=prads.db';
23 our $DB_TABLE = 'asset';
26 our $SQL_IP = 'ipaddress';
27 our $SQL_FP = 'os_fingerprint';
28 our $SQL_MAC = 'mac_address';
29 our $SQL_DETAILS = 'os_details';
30 our $SQL_HOSTNAME = 'hostname';
31 our $SQL_TIME = 'timestamp';
36 # start attributes at this number
48 out
('<SNORT_ATTRIBUTES>');
50 db_suck
($DATABASE,$DB_USERNAME,$DB_PASSWORD,$DB_TABLE);
52 out
('</SNORT_ATTRIBUTES>');
56 print " " x
($SW * $IL);
64 my ($tag, $value) = @_;
65 out
("<$tag>$value</$tag>");
70 out_tag
('ATTRIBUTE_ID', $value);
74 out_tag
('ATTRIBUTE_VALUE', $value);
77 my ($db, $user, $pass, $table) = @_;
78 $DBH = DBI
->connect($db, $user, $pass);
81 if ($db =~ /dbi:sqlite/i) {
83 $SQL_FP = 'fingerprint';
86 $SQL_DETAILS = 'details';
87 $SQL_HOSTNAME = 'reporting';
91 out
('<ATTRIBUTE_TABLE>');
93 out
('</ATTRIBUTE_TABLE>');
99 #todo : should be uniq'ed somehhow!
100 #my $sql = "SELECT $SQL_IP,$SQL_TIME, service, $SQL_MAC, os, $SQL_DETAILS FROM $table WHERE service = 'SYN' OR service = 'SYNACK'";
101 my $sql = "SELECT DISTINCT $SQL_IP,$SQL_TIME, service, $SQL_MAC, os, $SQL_DETAILS FROM $table WHERE service = 'SYN' OR service = 'SYNACK' OR service = 'UDP' OR service = 'ICMP'";
103 my $sth = $DBH->prepare_cached($sql);
105 my ($ip, $time, $service, $mac, $os, $details);
107 #while ( ($ip, $time, $service, $mac, $os, $details) = $sth->fetchrow_array()) {
108 # print "$ip $time $service $mac $os $details\n";
112 while ($ref = $sth->fetchrow_hashref()) {
115 out_tag
('IP', $ref->{'ip'});
117 out
('<OPERATING_SYSTEM>');
121 out
('</OPERATING_SYSTEM>');
124 out_services
($ref->{'ip'});
137 out_ai
(gen_attribute_id
($ref->{'os'}));
142 out_av
(gen_vendor
($ref->{'os'}));
147 out_av
(gen_version
($ref->{'os'}, $ref->{$SQL_DETAILS}));
150 out_tag
('FRAG_POLICY', gen_fragpolicy
($ref->{'os'}, $ref->{$SQL_DETAILS}));
151 out_tag
('STREAM_POLICY', gen_streampolicy
($ref->{'os'}, $ref->{$SQL_DETAILS}));
154 sub out_attribute_map
{
155 out
('<ATTRIBUTE_MAP>');
158 out_tag
('ID', $attr{$_});
159 out_tag
('VALUE', $_);
162 out
('</ATTRIBUTE_MAP>');
166 sub gen_attribute_id
{
169 /SSH/i and $name = 'ssh' or
171 if(not defined $attr{$name}){
172 $attr{$name} = $ATTR_NUM++;
182 /windows/i and return "Microsoft" or
183 /linux/i and return "Linux" or
187 my ($os, $details) = @_;
191 my ($os, $details) = @_;
192 /windows/i and return 'windows' or
193 /linux/i and return 'linux' or
194 /openbsd/i and return 'linux' or
195 /bsd/i and return 'BSD' or
196 /jetdirect/i and return 'BSD-right' or
197 /hp-ux/i and $details =~ /11/ and return 'First' or
198 /hp/i and return 'BSD' or
199 /mac/i and return 'First' or
200 /irix/i and return 'BSD' or
201 /aix/i and return 'BSD' or
202 /cisco/i and return 'Last' or
203 /vms/i and return 'BSD' or
204 /os\/2/i
and return 'BSD' or
205 /osf/i and return 'BSD' or
206 /sun/i and $details =~ /4/ and return 'BSD' or
207 /sun/i and return 'First' or
208 /tru64/i and return 'BSD' or
209 /vms|vax/i and return 'BSD' or
213 sub gen_streampolicy
{
214 my ($os, $details) = @_;
215 gen_fragpolicy
($os, $details);
218 # generate PORT IPPROTO PROTOCOL (CONFIDENCE) APPLICATION (VERSION)_
221 my $sql = "SELECT $SQL_IP,$SQL_TIME, fingerprint, service, $SQL_MAC, os, $SQL_DETAILS FROM $DB_TABLE WHERE service LIKE 'SERVICE_%' AND $SQL_IP = ?";
223 my $sth = $DBH->prepare($sql);
226 my ($ref, $port, $t, $proto, @r);
227 while ($ref = $sth->fetchrow_hashref()) {
230 ($t, $port, @r) = split /:/, $ref->{'fingerprint'};
231 $ref->{'service'} =~ s/SERVICE_//;
232 $proto = lc $ref->{'service'};
240 out
("<!-- service $ref->{'service'} with fp $ref->{'fingerprint'} for $ref->{'ip'} are forthcoming: -->");
241 out
("<!-- $ref->{$SQL_MAC} $ref->{'os'}, '$ref->{$SQL_DETAILS}' -->");
248 if(defined $ref->{'os'}){
249 out
('<APPLICATION>');
251 out_av
($ref->{'os'});
252 if(defined $ref->{$SQL_DETAILS}){
255 out_av
($ref->{$SQL_DETAILS});
260 out
('</APPLICATION>');
270 # extract <operating_system>ip, os Vendor, Version, frag_policy, stream_policy,
272 # <services><service><port><attribute_value/>
273 # IPPROTO, PROTOCOL <Confidence>
276 $DATABASE = $ARGV[0];