| 1 | package TCP::Rebuild; | ||
| 2 | |||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | use Net::LibNIDS 0.04; | ||
| 6 | use Socket qw(inet_ntoa); | ||
| 7 | use IO::File; | ||
| 8 | use Getopt::Long; | ||
| 9 | use Date::Format; | ||
| 10 | |||
| 11 | =head1 NAME | ||
| 12 | |||
| 13 | TCP::Rebuild - Rebuild TCP streams to files on disk. | ||
| 14 | |||
| 15 | =head1 VERSION | ||
| 16 | |||
| 17 | Version 0.01 | ||
| 18 | |||
| 19 | =cut | ||
| 20 | |||
| 21 | our $VERSION = '0.01'; | ||
| 22 | |||
| 23 | =head1 SYNOPSIS | ||
| 24 | |||
| 25 | Rebuilds TCP streams to plain text files on disk, one file per connection. | ||
| 26 | |||
| 27 | use TCP::Rebuild; | ||
| 28 | |||
| 29 | my $r = TCP::Rebuild->new(); | ||
| 30 | $r->rebuild('/path/to/file.pcap'); | ||
| 31 | |||
| 32 | =head1 SUBROUTINES/METHODS | ||
| 33 | |||
| 34 | =head2 rebuild | ||
| 35 | |||
| 36 | $r->rebuild('/path/to/file.pcap'); | ||
| 37 | |||
| 38 | This method rebuilds a specific pcap file using the currently set options. | ||
| 39 | |||
| 40 | Will die if the file is not readable or if Net::LibNIDS cannot be initialised. | ||
| 41 | |||
| 42 | =cut | ||
| 43 | |||
| 44 | sub rebuild { | ||
| 45 | my ($self, $filename) = @_; | ||
| 46 | |||
| 47 | # Exception if we can't read the file | ||
| 48 | if (!-r $filename) { | ||
| 49 | die "File $filename is not readable"; | ||
| 50 | } | ||
| 51 | |||
| 52 | # Net::LibNIDS is not currently object oriented, so this is the best we | ||
| 53 | # can do | ||
| 54 | if ($self->{filter} ne '') { | ||
| 55 | my $filter = $self->{filter} . ' or (ip[6:2] & 0x1fff != 0)'; | ||
| 56 | Net::LibNIDS::param::set_pcap_filter($filter); | ||
| 57 | } | ||
| 58 | Net::LibNIDS::param::set_filename($filename); | ||
| 59 | |||
| 60 | if (!Net::LibNIDS::init) { | ||
| 61 | die "libnids failed to initialise"; | ||
| 62 | } | ||
| 63 | |||
| 64 | # Without this closure, the collector has no idea about $self | ||
| 65 | my $callback = sub { | ||
| 66 | $self->_collector(@_); | ||
| 67 | }; | ||
| 68 | Net::LibNIDS::tcp_callback($callback); | ||
| 69 | Net::LibNIDS::run; | ||
| 70 | |||
| 71 | $self->_cleanup; | ||
| 72 | |||
| 73 | return 1; | ||
| 74 | } | ||
| 75 | |||
| 76 | =head2 new | ||
| 77 | |||
| 78 | my $r = TCP::Rebuild->new; | ||
| 79 | |||
| 80 | This method constructs a new TCP::Rebuild object. | ||
| 81 | |||
| 82 | =cut | ||
| 83 | |||
| 84 | sub new { | ||
| 85 | my $class = shift; | ||
| 86 | my %defaults = ( | ||
| 87 | separator => 0, | ||
| 88 | filter => '' | ||
| 89 | ); | ||
| 90 | |||
| 91 | my $self = bless { %defaults, @_ } => $class; | ||
| 92 | |||
| 93 | $self->{connections} = {}; | ||
| 94 | |||
| 95 | return $self; | ||
| 96 | } | ||
| 97 | |||
| 98 | sub _end_connection { | ||
| 99 | my ($self, $key, $conn, $message) = @_; | ||
| 100 | |||
| 101 | my $connections = $self->{connections}; | ||
| 102 | |||
| 103 | # _print 1, "Connection from " . $conn->client_ip . " " . $message; | ||
| 104 | # _print 1, " (C->S: " . $connections{$key}{'client_bytes'} . " bytes, C<-S " . $connections{$key}{'server_bytes'} . " bytes)\n"; | ||
| 105 | |||
| 106 | # Close the output file, if appropriate | ||
| 107 | undef $connections->{$key}{'fh'}; | ||
| 108 | |||
| 109 | delete $connections->{$key}; | ||
| 110 | } | ||
| 111 | |||
| 112 | sub _save_data { | ||
| 113 | my ($self, $key, $conn, $direction) = @_; | ||
| 114 | |||
| 115 | my $connections = $self->{connections}; | ||
| 116 | |||
| 117 | # Extract the current connection object | ||
| 118 | my $active = ($direction eq "server") ? $conn->client : $conn->server; | ||
| 119 | |||
| 120 | my $data = substr($active->data, 0, $active->count_new); | ||
| 121 | my $length = length $data; | ||
| 122 | |||
| 123 | my $fh = $connections->{$key}{'fh'}; | ||
| 124 | |||
| 125 | # Print a separator delimiting packets, this could be customisable | ||
| 126 | if ($self->{separator}) { | ||
| 127 | print $fh "[$direction +$length] " . $conn->lastpacket_sec . "." . $conn->lastpacket_usec . "\n"; | ||
| 128 | } | ||
| 129 | print $fh $data; | ||
| 130 | |||
| 131 | return; | ||
| 132 | } | ||
| 133 | |||
| 134 | sub _collector { | ||
| 135 | my ($self, $args) = @_; | ||
| 136 | |||
| 137 | my $connections = $self->{connections}; | ||
| 138 | |||
| 139 | my $key = $args->client_ip . ":" . $args->client_port . "-" . $args->server_ip . ":" . $args->server_port; | ||
| 140 | |||
| 141 | if($args->state == Net::LibNIDS::NIDS_JUST_EST()) { | ||
| 142 | # Set the flags to say we want to collect this traffic | ||
| 143 | $args->server->collect_on(); | ||
| 144 | $args->client->collect_on(); | ||
| 145 | |||
| 146 | # Create an empty buffer | ||
| 147 | $connections->{$key}{'client_buffer'} = ''; | ||
| 148 | $connections->{$key}{'server_buffer'} = ''; | ||
| 149 | $connections->{$key}{'client_bytes'} = 0; # Bytes FROM the client | ||
| 150 | $connections->{$key}{'server_bytes'} = 0; # Bytes FROM the server | ||
| 151 | |||
| 152 | # Create a filehandle that is used subsequently to save data | ||
| 153 | # TODO: We should probably check to see whether the file exists already | ||
| 154 | my $fh = new IO::File $self->_generate_filename($args), O_WRONLY | O_CREAT | O_TRUNC; | ||
| 155 | if (!defined $fh) { | ||
| 156 | die "Could not open output file! $!"; | ||
| 157 | } | ||
| 158 | binmode $fh; | ||
| 159 | $connections->{$key}{'fh'} = $fh; | ||
| 160 | |||
| 161 | } elsif ($args->state == Net::LibNIDS::NIDS_CLOSE()) { | ||
| 162 | $self->_end_connection($key, $args, "was closed"); | ||
| 163 | return; | ||
| 164 | |||
| 165 | } elsif ($args->state == Net::LibNIDS::NIDS_RESET()) { | ||
| 166 | $self->_end_connection($key, $args, "was reset"); | ||
| 167 | return; | ||
| 168 | |||
| 169 | } elsif ($args->state == Net::LibNIDS::NIDS_TIMED_OUT()) { | ||
| 170 | $self->_end_connection($key, $args, "timed out"); | ||
| 171 | return; | ||
| 172 | |||
| 173 | } elsif ($args->state == Net::LibNIDS::NIDS_DATA()) { | ||
| 174 | # Data toward the client FROM the server | ||
| 175 | if ($args->client->count_new) { | ||
| 176 | $connections->{$key}{'server_bytes'} += $args->client->count_new; | ||
| 177 | $self->_save_data($key, $args, 'server'); | ||
| 178 | return; | ||
| 179 | } | ||
| 180 | # Data toward the server FROM the client | ||
| 181 | if ($args->server->count_new) { | ||
| 182 | $connections->{$key}{'client_bytes'} += $args->server->count_new; | ||
| 183 | $self->_save_data($key, $args, 'client'); | ||
| 184 | return; | ||
| 185 | } | ||
| 186 | |||
| 187 | } | ||
| 188 | # UNREACHED, unless Net::LibNIDS changes | ||
| 189 | return; | ||
| 190 | } | ||
| 191 | |||
| 192 | sub _generate_filename { | ||
| 193 | my ($self, $conn) = @_; | ||
| 194 | |||
| 195 | my $directory = time2str('%Y-%m-%d', $conn->lastpacket_sec); | ||
| 196 | unless ( -e $directory ) { mkdir($directory); } | ||
| 197 | |||
| 198 | my $name = $directory . '/' . $conn->client_ip . "." . $conn->client_port . "-" . $conn->server_ip . "." . $conn->server_port; | ||
| 199 | return $name; | ||
| 200 | } | ||
| 201 | |||
| 202 | # Called when libnids finishes processing a file, to expunge old data and | ||
| 203 | # close any file handles | ||
| 204 | sub _cleanup { | ||
| 205 | my $self = shift; | ||
| 206 | |||
| 207 | my $connections = $self->{connections}; | ||
| 208 | foreach my $key (keys %$connections) { | ||
| 209 | # undef automatically closes file with IO::File | ||
| 210 | undef $connections->{$key}{'fh'}; | ||
| 211 | } | ||
| 212 | return; | ||
| 213 | } | ||
| 214 | |||
| 215 | =head1 AUTHOR | ||
| 216 | |||
| 217 | David Cannings <david at edeca.net> | ||
| 218 | |||
| 219 | =head1 BUGS | ||
| 220 | |||
| 221 | Please report any bugs or feature requests to C<bug-tcp-rebuild at rt.cpan.org>, or through | ||
| 222 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=TCP-Rebuild>. I will be notified, and then you'll | ||
| 223 | automatically be notified of progress on your bug as I make changes. | ||
| 224 | |||
| 225 | =head1 TODO | ||
| 226 | |||
| 227 | Things that would be nice to implement | ||
| 228 | |||
| 229 | =over 4 | ||
| 230 | |||
| 231 | =item * Dump packet data to XML format | ||
| 232 | |||
| 233 | =item * Allow caller to supply a filename template | ||
| 234 | |||
| 235 | =item * Allow caller to supply a separator template | ||
| 236 | |||
| 237 | =item * Optional encoding of packet data (e.g. Base64) | ||
| 238 | |||
| 239 | =item * Introduce stream/packet statistics | ||
| 240 | |||
| 241 | =back | ||
| 242 | |||
| 243 | |||
| 244 | =head1 SUPPORT | ||
| 245 | |||
| 246 | You can find documentation for this module with the perldoc command. | ||
| 247 | |||
| 248 | perldoc TCP::Rebuild | ||
| 249 | |||
| 250 | |||
| 251 | You can also look for information at: | ||
| 252 | |||
| 253 | =over 4 | ||
| 254 | |||
| 255 | =item * RT: CPAN's request tracker | ||
| 256 | |||
| 257 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=TCP-Rebuild> | ||
| 258 | |||
| 259 | =item * AnnoCPAN: Annotated CPAN documentation | ||
| 260 | |||
| 261 | L<http://annocpan.org/dist/TCP-Rebuild> | ||
| 262 | |||
| 263 | =item * CPAN Ratings | ||
| 264 | |||
| 265 | L<http://cpanratings.perl.org/d/TCP-Rebuild> | ||
| 266 | |||
| 267 | =item * Search CPAN | ||
| 268 | |||
| 269 | L<http://search.cpan.org/dist/TCP-Rebuild/> | ||
| 270 | |||
| 271 | =back | ||
| 272 | |||
| 273 | |||
| 274 | =head1 ACKNOWLEDGEMENTS | ||
| 275 | |||
| 276 | Many thanks to CPAN::Mini, which provided many ideas as I packaged this code | ||
| 277 | into a module and shell script. | ||
| 278 | |||
| 279 | =head1 LICENSE AND COPYRIGHT | ||
| 280 | |||
| 281 | Copyright 2010 David Cannings. | ||
| 282 | |||
| 283 | This program is free software; you can redistribute it and/or modify it | ||
| 284 | under the terms of either: the GNU General Public License as published | ||
| 285 | by the Free Software Foundation; or the Artistic License. | ||
| 286 | |||
| 287 | See http://dev.perl.org/licenses/ for more information. | ||
| 288 | |||
| 289 | |||
| 290 | =cut | ||
| 291 | |||
| 292 | 1; # End of TCP::Rebuild |