-
Notifications
You must be signed in to change notification settings - Fork 1
/
txt2pcap.pl
executable file
·281 lines (238 loc) · 6.72 KB
/
txt2pcap.pl
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
279
280
281
#!/usr/bin/perl
use Net::PcapWriter;
use POSIX;
use strict;
use warnings;
my ($FINname, $FOUTname, $npkts);
my ($line,$tv,$proto,$netsrc,$tpsrc, $netdst,$tpdst,$approto, $netlen, $payload, $flags, $data, $datarand);
my ($sec,$mu,$basename,$FOUT2name,$mampid,$comment);
$basename = "test";
$FINname = $ARGV[0] || "$basename.txt"; #Just a string to identify this experiment.
$FOUTname = $ARGV[1] || "$basename.pcap";
$FOUT2name = $ARGV[2] || "$basename.cap";
$npkts = $ARGV[3] || "-1";
$mampid = $ARGV[4] || "convert";
$comment = $ARGV[5] || "Converted via txt2pcap, pcap2cap";
my $writer = Net::PcapWriter->new($FOUTname) or die "Cant open $FOUTname $!.";
open(FIN, "$FINname") or die "Cant open $FINname, $!.";
$datarand=&generate_random_string(67000);
my $pkts=0;
my $dlen;
while($line=<FIN>){
$line =~ s/^\s+//; # remove leading whitespace
$line =~ s/\s+$//; # remove trailing whitespace
# ignore comments and empty lines
my $is_comment = $line =~ /#/;
my $is_blank = length($line) == 0;
if ( $is_comment || $is_blank ) {
next;
}
($tv,$proto,$netsrc,$tpsrc, $netdst,$tpdst, $netlen, $flags, $payload) = split(/\s+/, $line, 9);
if(!($proto=~/udp||tcp/)) {
print "$0: warning: Not tcp or udp, ignored.\n";
next;
}
# fix timestamp, pcapwriter gets it wrong
my ($tsec, $tmsec);
($tsec, $tmsec) = split(/\./, $tv);
$tsec = int($tsec);
$tmsec = int($tmsec);
# if no payload was specifed create an empty one
if ( !defined $payload ){
$payload = '';
}
my @flags = split(/,/, $flags);
my $src_host=(gethostbyname($netsrc))[4];
my $dst_host=(gethostbyname($netdst))[4];
my $hdrlen = tpheader_length($proto);
$dlen=$netlen-length($payload);
my @tmp = ($tsec,$tmsec);
$data=sprintf('%s%s',$payload,substr($datarand,0,$dlen));
my ($packet) = make_iptp_headers($src_host, $tpsrc, $dst_host, $tpdst, $netlen, $proto, \@flags, $data);
$writer->packet($packet, \@tmp);
$pkts++;
}
if ( $pkts == 0 ){
print "No packets, ignored.\n";
unlink($FOUTname);
exit 1
}
my $command = "pcap2cap -qm '$mampid' -c '$comment' -o '$FOUT2name' '$FOUTname'";
print "$command\n";
system($command);
sub tpheader_length {
my ($proto) = @_;
if ( $proto == 6 ){ return 20; }
if ( $proto == 17 ){ return 8; }
return 0; # will bail out later
}
sub generate_random_string
{
my $length_of_randomstring=shift;# the length of
# the random string to generate
my @chars=('a'..'z','A'..'Z','0'..'9','_');
my $random_string;
foreach (1..$length_of_randomstring)
{
# rand @chars will generate a random
# number between 0 and scalar @chars
$random_string.=$chars[rand @chars];
}
return $random_string;
}
sub tcp_flags {
# defaults
my $urg = 0;
my $ack = 0;
my $psh = 0;
my $rst = 0;
my $syn = 0;
my $fin = 0;
my $offset = 0;
foreach ( @_ ){
if ( $_ eq 'DATA' ){ next; }
elsif ( $_ eq 'URG' ){ $urg = 1; }
elsif ( $_ eq 'ACK' ){ $ack = 1; }
elsif ( $_ eq 'PSH' ){ $psh = 1; }
elsif ( $_ eq 'RST' ){ $rst = 1; }
elsif ( $_ eq 'SYN' ){ $syn = 1; }
elsif ( $_ eq 'FIN' ){ $fin = 1; }
elsif ( $_ =~ /OPT_NOP\((\d+)\)/ ){ $offset += $1; }
else { print "unknown TCP flag `$_', ignored\n"; }
}
return ($urg, $ack, $psh, $rst, $syn, $fin, $offset);
}
sub ip_checksum {
my ($ip) = @_;
$ip->{ip_sum} = 0;
return checksum(ip_pack($ip));
}
sub ip_pack {
my ($ip) = @_;
return pack('H2H2n' .
'nB16' .
'C2S' .
'a4a4',
$ip->{ip_v} . $ip->{ip_hl}, $ip->{ip_tos}, $ip->{ip_len} + 4 * $ip->{ip_hl},
$ip->{ip_id}, $ip->{ip_off},
$ip->{ip_ttl}, $ip->{ip_p}, $ip->{ip_sum},
$ip->{ip_src}, $ip->{ip_dst});
}
sub tcp_checksum {
my ($ip, $tcp, $payload) = @_;
my $ip_pseudo = pack('a4a4CCn',
$ip->{ip_src}, $ip->{ip_dst},
0, $ip->{ip_p}, $ip->{ip_len});
my $tcp_pseudo = tcp_pack($tcp, $payload);
return checksum($ip_pseudo . $tcp_pseudo);
}
sub tcp_options {
my ($n) = @_;
my $nop = pack('C', 1);
my $pad = pack('C', 0);
$data = $nop x $n;
while ( length($data) % 4 != 0 ){
$data .= $pad;
}
return $data;
}
sub tcp_pack {
my ($tcp, $payload) = @_;
return pack('nnNNH2B8nSna*a*',
$tcp->{src}, $tcp->{dst}, $tcp->{seq},$tcp->{ack_seq}, sprintf("%x0", $tcp->{doff}),
$tcp->{flags}, $tcp->{window}, $tcp->{check}, $tcp->{urg_ptr}, $tcp->{options}, $payload);
}
sub make_tcp_header {
my ($ip, $src_port, $dst_port, $flagsref, $payload) = @_;
my ($tcp_urg, $tcp_ack, $tcp_psh, $tcp_rst, $tcp_syn, $tcp_fin, $options) = tcp_flags(@$flagsref);
my $offset = POSIX::ceil($options / 4);
my $tcp = {
src => $src_port,
dst => $dst_port,
seq => 13456,
ack_seq => 0,
doff => (5 + $offset),
flags => "00" . $tcp_urg . $tcp_ack . $tcp_psh . $tcp_rst . $tcp_syn . $tcp_fin,
window => 124,
check => 0,
urg_ptr => 0,
options => tcp_options($options),
};
# hack to get right ip-len (size of tcp header changed with options)
if ( $offset > 0 ){
$ip->{ip_len} += $offset * 4;
$ip->{ip_sum} = ip_checksum($ip);
}
$tcp->{check} = tcp_checksum($ip, $tcp, $payload);
return ip_pack($ip) . tcp_pack($tcp, $payload);
}
sub udp_checksum {
my ($ip, $udp, $payload) = @_;
my $pseudo = pack('a4a4' .
'CCn' .
'nn' .
'nna*',
$ip->{ip_src}, $ip->{ip_dst},
0, $ip->{ip_p}, $ip->{ip_len},
$udp->{src}, $udp->{dst},
$udp->{len}, 0, $payload);
return checksum($pseudo);
}
sub udp_pack {
my ($udp, $payload) = @_;
return pack('nnnSa*', $udp->{src}, $udp->{dst}, $udp->{len}, $udp->{sum}, $payload);
}
sub make_udp_header {
my ($ip, $src_port, $dst_port, $flagsref, $payload) = @_;
my $udp = {
src => $src_port,
dst => $dst_port,
len => $ip->{ip_len},
sum => 0,
};
$udp->{sum} = udp_checksum($ip, $udp, $payload);
return ip_pack($ip) . udp_pack($udp, $payload);
}
sub make_tp_header {
my $proto = $_[0]->{ip_p};
if( $proto == 6 ){
return make_tcp_header(@_);
} elsif( $proto == 17 ){
return make_udp_header(@_);
} else {
print "WTF?. not supported protocol \n";
}
}
sub make_iptp_headers {
my ($src_host,$src_port,$dst_host,$dst_port,$leng,$netp, $flagsref, $payload) = @_;
my $ip = {
ip_v => 4,
ip_hl => 5,
ip_tos => 00,
ip_len => $leng + tpheader_length($netp),
ip_id => 19245,
ip_off => "010" . "0000000000000",
ip_ttl => 30,
ip_p => $netp,
ip_sum => 0,
ip_src => $src_host,
ip_dst => $dst_host,
};
$ip->{ip_sum} = ip_checksum($ip);
return make_tp_header($ip, $src_port, $dst_port, $flagsref, $payload);
}
sub checksum {
# This of course is a blatent rip from _the_ GOD,
# W. Richard Stevens.
my ($msg) = @_;
my ($len_msg,$num_short,$short,$chk);
$len_msg = length($msg);
$num_short = $len_msg / 2;
$chk = 0;
foreach $short (unpack("S$num_short", $msg)) {
$chk += $short;
}
$chk += unpack("C", substr($msg, $len_msg - 1, 1)) if $len_msg % 2;
$chk = ($chk >> 16) + ($chk & 0xffff);
return(~(($chk >> 16) + $chk) & 0xffff);
}