2
# NetPacket::TCP - Decode and encode TCP (Transmission Control
5
# Comments/suggestions to tpot@samba.org
7
# Encode and checksumming part, Stephanie Wehner, atrak@itsx.com
9
# $Id: TCP.pm,v 1.16 2001/08/01 02:31:27 tpot Exp $
12
package NetPacket::TCP;
15
# Copyright (c) 2001 Tim Potter.
17
# This package is free software and is provided "as is" without express
18
# or implied warranty. It may be used, redistributed and/or modified
19
# under the terms of the Perl Artistic License (see
20
# http://www.perl.com/perl/misc/Artistic.html)
22
# Copyright (c) 1995,1996,1997,1998,1999 ANU and CSIRO on behalf of
23
# the participants in the CRC for Advanced Computational Systems
26
# ACSys makes this software and all associated data and documentation
27
# ('Software') available free of charge. You may make copies of the
28
# Software but you must include all of this notice on any copy.
30
# The Software was developed for research purposes and ACSys does not
31
# warrant that it is error free or fit for any purpose. ACSys
32
# disclaims any liability for all claims, expenses, losses, damages
33
# and costs any user may incur as a result of using, copying or
34
# modifying the Software.
36
# Copyright (c) 2001 Stephanie Wehner
40
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
47
use constant FIN => 0x01;
48
use constant SYN => 0x02;
49
use constant RST => 0x04;
50
use constant PSH => 0x08;
51
use constant ACK => 0x10;
52
use constant URG => 0x20;
53
use constant ECE => 0x40;
54
use constant CWR => 0x80;
57
$myclass = __PACKAGE__;
60
sub Version () { "$myclass v$VERSION" }
63
@ISA = qw(Exporter NetPacket);
65
# Items to export into callers namespace by default
66
# (move infrequently used names to @EXPORT_OK below)
68
@EXPORT = qw(FIN SYN RST PSH ACK URG ECE CWR
71
# Other items we are prepared to export if requested
73
@EXPORT_OK = qw(tcp_strip
79
ALL => [@EXPORT, @EXPORT_OK],
80
strip => [qw(tcp_strip)],
86
# Strip header from packet and return the data contained in it
93
my ($pkt, @rest) = @_;
95
my $tcp_obj = NetPacket::TCP->decode($pkt);
96
return $tcp_obj->{data};
105
my($pkt, $parent, @rest) = @_;
110
$self->{_parent} = $parent;
111
$self->{_frame} = $pkt;
118
($self->{src_port}, $self->{dest_port}, $self->{seqnum},
119
$self->{acknum}, $tmp, $self->{winsize}, $self->{cksum},
120
$self->{urg}, $self->{options}) =
121
unpack("nnNNnnnna*", $pkt);
125
$self->{hlen} = ($tmp & 0xf000) >> 12;
126
$self->{reserved} = ($tmp & 0x0f00) >> 8;
127
$self->{flags} = $tmp & 0x00ff;
129
# Decode variable length header and remaining data
131
my $olen = $self->{hlen} - 5;
132
$olen = 0, if ($olen < 0); # Check for bad hlen
134
# Option length is number of 32 bit words
138
($self->{options}, $self->{data}) = unpack("a" . $olen .
139
"a*", $self->{options});
142
# Return a blessed object
144
bless($self, $class);
158
# First of all, fix the checksum
159
$self->checksum($ip);
161
$tmp = $self->{hlen} << 12;
162
$tmp = $tmp | (0x0f00 & ($self->{reserved} << 8));
163
$tmp = $tmp | (0x00ff & $self->{flags});
165
# Put the packet together
166
$packet = pack('n n N N n n n n a* a*',
167
$self->{src_port}, $self->{dest_port}, $self->{seqnum},
168
$self->{acknum}, $tmp, $self->{winsize}, $self->{cksum},
169
$self->{urg}, $self->{options},$self->{data});
183
my ($packet,$zero,$tcplen,$tmp);
184
my ($src_ip, $dest_ip,$proto,$count);
188
$tcplen = ($self->{hlen} * 4)+ length($self->{data});
190
$tmp = $self->{hlen} << 12;
191
$tmp = $tmp | (0x0f00 & ($self->{reserved} << 8));
192
$tmp = $tmp | (0x00ff & $self->{flags});
194
# Pack pseudo-header for tcp checksum
196
$src_ip = gethostbyname($ip->{src_ip});
197
$dest_ip = gethostbyname($ip->{dest_ip});
199
$packet = pack('a4a4nnnnNNnnnna*a*',
200
$src_ip,$dest_ip,$proto,$tcplen,
201
$self->{src_port}, $self->{dest_port}, $self->{seqnum},
202
$self->{acknum}, $tmp, $self->{winsize}, $zero,
203
$self->{urg}, $self->{options},$self->{data});
205
$self->{cksum} = NetPacket::htons(NetPacket::in_cksum($packet));
209
# Module initialisation
214
# autoloaded methods go after the END token (&& pod) below
220
C<NetPacket::TCP> - Assemble and disassemble TCP (Transmission Control
227
$tcp_obj = NetPacket::TCP->decode($raw_pkt);
228
$tcp_pkt = NetPacket::TCP->encode($ip_pkt);
229
$tcp_data = NetPacket::TCP::strip($raw_pkt);
233
C<NetPacket::TCP> provides a set of routines for assembling and
234
disassembling packets using TCP (Transmission Control Protocol).
240
=item C<NetPacket::TCP-E<gt>decode([RAW PACKET])>
242
Decode the raw packet data given and return an object containing
243
instance data. This method will quite happily decode garbage input.
244
It is the responsibility of the programmer to ensure valid packet data
245
is passed to this method.
247
=item C<NetPacket::TCP-E<gt>encode($ip_obj)>
249
Return a TCP packet encoded with the instance data specified.
250
Needs parts of the ip header contained in $ip_obj in order to calculate
259
=item C<NetPacket::TCP::strip([RAW PACKET])>
261
Return the encapsulated data (or payload) contained in the TCP
262
packet. This data is suitable to be used as input for other
263
C<NetPacket::*> modules.
265
This function is equivalent to creating an object using the
266
C<decode()> constructor and returning the C<data> field of that
273
The instance data for the C<NetPacket::TCP> object consists of
274
the following fields.
280
The source TCP port for the packet.
284
The destination TCP port for the packet.
288
The TCP sequence number for this packet.
292
The TCP acknowledgement number for this packet.
296
The header length for this packet.
300
The 6-bit "reserved" space in the TCP header.
304
Contains the urg, ack, psh, rst, syn, fin, ece and cwr flags for this packet.
308
The TCP window size for this packet.
316
The TCP urgent pointer.
320
Any TCP options for this packet in binary form.
324
The encapsulated data (payload) for this packet.
334
FIN SYN RST PSH ACK URG ECE CWR Can be used to set the appropriate flag.
342
The following tags group together related exportable items.
348
Import the strip function C<tcp_strip>.
352
All the above exportable items.
360
The following script is a primitive pop3 sniffer.
366
use NetPacket::Ethernet qw(:strip);
367
use NetPacket::IP qw(:strip);
371
my($arg, $hdr, $pkt) = @_;
373
my $tcp_obj = NetPacket::TCP->decode(ip_strip(eth_strip($pkt)));
375
if (($tcp_obj->{src_port} == 110) or ($tcp_obj->{dest_port} == 110)) {
376
print($tcp_obj->{data});
380
Net::PcapUtils::loop(\&process_pkt, FILTER => 'tcp');
382
The following uses NetPacket together with Net::Divert to add a syn
383
flag to all TCP packets passing through:
388
use NetPacket::IP qw(IP_PROTO_TCP);
392
$divobj = Net::Divert->new('yourhostname',9999);
394
$divobj->getPackets(\&alterPacket);
397
my($packet,$fwtag) = @_;
399
# decode the IP header
400
$ip_obj = NetPacket::IP->decode($packet);
402
# check if this is a TCP packet
403
if($ip_obj->{proto} == IP_PROTO_TCP) {
405
# decode the TCP header
406
$tcp_obj = NetPacket::TCP->decode($ip_obj->{data});
409
$tcp_obj->{flags} |= SYN;
411
# construct the new ip packet
412
$ip_obj->{data} = $tcp_obj->encode($ip_obj);
413
$packet = $ip_obj->encode;
418
$divobj->putPacket($packet,$fwtag);
426
=item Assembly of TCP fragments into a data stream
428
=item Option processing
430
=item Nicer processing of TCP flags
436
Copyright (c) 2001 Tim Potter.
438
This package is free software and is provided "as is" without express
439
or implied warranty. It may be used, redistributed and/or modified
440
under the terms of the Perl Artistic License (see
441
http://www.perl.com/perl/misc/Artistic.html)
443
Copyright (c) 1995,1996,1997,1998,1999 ANU and CSIRO on behalf of
444
the participants in the CRC for Advanced Computational Systems
447
ACSys makes this software and all associated data and documentation
448
('Software') available free of charge. You may make copies of the
449
Software but you must include all of this notice on any copy.
451
The Software was developed for research purposes and ACSys does not
452
warrant that it is error free or fit for any purpose. ACSys
453
disclaims any liability for all claims, expenses, losses, damages
454
and costs any user may incur as a result of using, copying or
455
modifying the Software.
459
Tim Potter E<lt>tpot@samba.orgE<gt>
461
Stephanie Wehner E<lt>atrak@itsx.comE<gt>
465
# any real autoloaded methods go after this line