File: snortifyprads.pl

package info (click to toggle)
prads 0.3.3-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, stretch
  • size: 2,400 kB
  • ctags: 1,564
  • sloc: ansic: 10,215; perl: 1,689; sql: 204; sh: 167; makefile: 152
file content (278 lines) | stat: -rwxr-xr-x 6,581 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#!/usr/bin/perl -w

# Convert PRADS db to SNORT host-attribute table
#
# Usage: 
# 
# Work-in-progress: services!
# XXX: 
#  - figure out how to filter client ports (not a "service" per se
#  - what is confidence and how does it affect the resulting rules?
#  - why oh why attribute maps?
#
# Copyright 2010 Kacper Wysocki <kwy@redpill-linpro.com>

use strict;
use warnings;

use DBI;

our $DATABASE = 'dbi:SQLite:dbname=prads.db';
our $DB_USERNAME;
our $DB_PASSWORD;
our $DB_TABLE = 'asset';
our $DBH;

our $SQL_IP = 'ipaddress';
our $SQL_FP = 'os_fingerprint';
our $SQL_MAC = 'mac_address';
our $SQL_DETAILS = 'os_details';
our $SQL_HOSTNAME = 'hostname';
our $SQL_TIME = 'timestamp';

our $SW = 4;
our $IL = 0;

# start attributes at this number
our $ATTR_NUM = 1337;
our %attr;

sub tab {
    $IL++;
}
sub bat {
    $IL--;
}

sub out_hat {
    out ('<SNORT_ATTRIBUTES>');
    tab;
    db_suck($DATABASE,$DB_USERNAME,$DB_PASSWORD,$DB_TABLE);
    bat;
    out ('</SNORT_ATTRIBUTES>');
}

sub out {
    print " " x ($SW * $IL);
    for (@_){
        print;
    }
    print "\n";
}

sub out_tag {
    my ($tag, $value) = @_;
    out ("<$tag>$value</$tag>");
}

sub out_ai {
    my ($value) = @_;
    out_tag ('ATTRIBUTE_ID', $value);
}
sub out_av {
    my ($value) = @_;
    out_tag ('ATTRIBUTE_VALUE', $value);
}
sub db_suck {
    my ($db, $user, $pass, $table) = @_;
    $DBH = DBI->connect($db, $user, $pass);


    if ($db =~ /dbi:sqlite/i) {
        $SQL_IP = 'ip';
        $SQL_FP = 'fingerprint';
        $SQL_MAC = 'mac';
        $SQL_TIME = 'time';
        $SQL_DETAILS = 'details';
        $SQL_HOSTNAME = 'reporting';
    }

    # do attribute map
    out ('<ATTRIBUTE_TABLE>');
    out_hosts($table);
    out ('</ATTRIBUTE_TABLE>');
    out_attribute_map();
}

sub out_hosts {
    my ($table) = @_;
    #todo : should be uniq'ed somehhow!
    #my $sql = "SELECT $SQL_IP,$SQL_TIME, service, $SQL_MAC, os, $SQL_DETAILS FROM $table WHERE service = 'SYN' OR service = 'SYNACK'";
    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'";

    my $sth = $DBH->prepare_cached($sql);
    $sth->execute();
    my ($ip, $time, $service, $mac, $os, $details);
#meh 
#while ( ($ip, $time, $service, $mac, $os, $details) = $sth->fetchrow_array()) {
#    print "$ip $time $service $mac $os $details\n";
#}

    my $ref;
    while ($ref = $sth->fetchrow_hashref()) {
        out ('<HOST>');
        tab;
        out_tag('IP', $ref->{'ip'});
        tab;
        out ('<OPERATING_SYSTEM>');
        tab;
        out_os($ref);
        bat;
        out ('</OPERATING_SYSTEM>');
        out ('<SERVICES>');
        tab;
        out_services($ref->{'ip'});
        bat;
        out ('</SERVICES>');
        bat;
        out ('</HOST>');
        bat;
    }
}

sub out_os {
    my ($ref) = @_;
    out ('<NAME>');
    tab;
    out_ai(gen_attribute_id($ref->{'os'}));
    bat;
    out ('</NAME>');
    out ('<VENDOR>');
    tab;
    out_av (gen_vendor($ref->{'os'}));
    bat;
    out ('</VENDOR>');
    out ('<VERSION>');
    tab;
    out_av (gen_version($ref->{'os'}, $ref->{$SQL_DETAILS}));
    bat;
    out ('</VERSION>');
    out_tag('FRAG_POLICY', gen_fragpolicy($ref->{'os'}, $ref->{$SQL_DETAILS}));
    out_tag('STREAM_POLICY', gen_streampolicy($ref->{'os'}, $ref->{$SQL_DETAILS}));
}

sub out_attribute_map {
    out('<ATTRIBUTE_MAP>');
    for (keys %attr){
        out('<ENTRY>');
        out_tag('ID', $attr{$_});
        out_tag('VALUE', $_);
        out('</ENTRY>');
    }
    out('</ATTRIBUTE_MAP>');
}


sub gen_attribute_id {
    ($_) = @_;
    my $name;
    /SSH/i and $name = 'ssh' or
    $name = $_;
    if(not defined $attr{$name}){
        $attr{$name} = $ATTR_NUM++;
    }

    return $attr{$name};
}


sub gen_vendor {
    my ($os) = @_;
    $_ = $os;
    /windows/i and return "Microsoft" or
    /linux/i and return "Linux" or
    return ucfirst $os;
}
sub gen_version {
    my ($os, $details) = @_;
    return $details;
}
sub gen_fragpolicy {
    my ($os, $details) = @_;
    /windows/i and return 'windows' or
    /linux/i and return 'linux' or
    /openbsd/i and return 'linux' or
    /bsd/i and return 'BSD' or
    /jetdirect/i and return 'BSD-right' or
    /hp-ux/i and $details =~ /11/ and return 'First' or
    /hp/i and return 'BSD' or
    /mac/i and return 'First' or
    /irix/i and return 'BSD' or
    /aix/i and return 'BSD' or
    /cisco/i and return 'Last' or
    /vms/i and return 'BSD' or
    /os\/2/i and return 'BSD' or
    /osf/i and return 'BSD' or
    /sun/i and $details =~ /4/ and return 'BSD' or
    /sun/i and return 'First' or
    /tru64/i and return 'BSD' or
    /vms|vax/i and return 'BSD' or
    return 'last';
}

sub gen_streampolicy {
    my ($os, $details) = @_;
    gen_fragpolicy($os, $details);
}

# generate PORT IPPROTO PROTOCOL (CONFIDENCE) APPLICATION (VERSION)_
sub out_services {
    my ($ip) = @_;
    my $sql = "SELECT $SQL_IP,$SQL_TIME, fingerprint, service, $SQL_MAC, os, $SQL_DETAILS FROM $DB_TABLE WHERE service LIKE 'SERVICE_%' AND $SQL_IP = ?";

    my $sth = $DBH->prepare($sql);
    $sth->execute($ip);

    my ($ref, $port, $t, $proto, @r);
    while ($ref = $sth->fetchrow_hashref()) {
        out('<SERVICE>');
        tab;
        ($t, $port, @r) = split /:/, $ref->{'fingerprint'};
        $ref->{'service'} =~ s/SERVICE_//;
        $proto = lc $ref->{'service'};
        if($port){
            out('<PORT>');
            tab;
            out_av($port);
            bat;
            out('</PORT>');
        }
        out ("<!-- service $ref->{'service'} with fp $ref->{'fingerprint'} for $ref->{'ip'} are forthcoming: -->");
        out ("<!-- $ref->{$SQL_MAC} $ref->{'os'}, '$ref->{$SQL_DETAILS}' -->");
        out('<IPPROTO>');
        tab;
        out_av($proto);
        bat;
        out('</IPPROTO>');

        if(defined $ref->{'os'}){
            out('<APPLICATION>');
            tab;
            out_av($ref->{'os'});
            if(defined $ref->{$SQL_DETAILS}){
                out('<VERSION>');
                tab;
                out_av($ref->{$SQL_DETAILS});
                bat;
                out('</VERSION>');
            }
            bat;
            out('</APPLICATION>');
        }

        bat;
        out('</SERVICE>');

    }
}


# extract <operating_system>ip, os Vendor, Version, frag_policy, stream_policy,
# & all services: 
# <services><service><port><attribute_value/>
# IPPROTO, PROTOCOL <Confidence>

if(@ARGV){
    $DATABASE = $ARGV[0];
}
out_hat;