forked from ejtaal/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcpdump2ascii.pl
executable file
·235 lines (207 loc) · 5.09 KB
/
tcpdump2ascii.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
#!/usr/bin/perl
use strict;
### README ###
#
#tcpdump2ascii v2.11 (07/12/2001 [email protected])
#
#This is a small perl program which converts tcpdump hex values
#(using tcpdump -x option) to readable ascii characters (31> char <123).
#
#Characters outside this range is printed as a dot (".").
#
#Usage:
#
# ./tcpdump2ascii -h
#
#The "snoop" level is actually irrelevant at this point. Anything above 0 will
#produce verbose snoop-like output.
#
#-codex
### BUGS ###
#Known bugs in 2.11
#------------------
#
#Prints previous snoop output after current packet has been captured. The
#reason tcpdump2ascii is doing this is due to the way it parses the output
#from tcpdump.
#
#This was intentional, because I needed a quick and dirty fix. Next major
#version will probably have this fix unless someone fixes it before I do
#(which would be nice, because then I don't have to do it).
#
#Instead of:
#
# loop {
# <wait for input and parse it>
# }
#
#tcpdump2ascii should probably do:
#
# loop {
# <wait for tcpdump info line>
# loop {
# <gobble hex>
# }
# <print snoop details>
# }
#
#
#
### Original script starts here ###
##
## takes tcpdump -x output and turns it into ASCII
##
## usage: tcpdump -l -x | tcpdump2ascii
##
## v1.00 9/9/1999
## + initial release
##
## v1.10 8/12/1999
## + changed code to use Getopt::Long
## + added -d (debug) option
## + added -l (line) option
## + added -s (suppress) option
##
## v2.00 10/3/2000
## + added "snoop-like" functionality
## + see BUGS file for known bugs
##
## v2.10 24/5/2000
## + bug fixed in TCP -snoop code
## + options t,x,S by [email protected]
##
## v2.11 07/12/2001
## + buglet in hex parsing code, now ignore case (some people
## + seem to use this program for more than tcpdump!)
##
## written by [email protected] 1999
##
my $version="v2.11 07/12/2001 codex\@bogus.net";
use Socket;
use Getopt::Long;
my (@packet,$LINE,$SUPPRESS,$DEBUG,$dumpX,$http);
my ($snoop,$opt_s,$opt_h,$opt_v,$opt_d,$opt_l,$opt_x,$opt_t,$opt_S);
GetOptions(
"v" => \$opt_v,
"h" => \$opt_h,
"d" => \$opt_d,
"l" => \$opt_l,
"s" => \$opt_s,
"S" => \$opt_S,
"t:s" => \$opt_t,
"x:s" => \$opt_x,
"snoop:s" => \$snoop,
);
my @protocol;
if($opt_d) {
## currently does nothing
##
$DEBUG=1;
} else {
$DEBUG=0;
}
if($opt_l) {
$LINE=1; # dont print hexa bytes
} else {
$LINE=0;
}
if($opt_s) {
$SUPPRESS=1; # suppress packet header
} else {
$SUPPRESS=0;
}
$dumpX=16; # default 16 bytes
if($opt_x eq "1") {
$dumpX=16; # suppress packet header
}
elsif ($opt_x eq "2"){
$dumpX=32;
}
$http=0; # default 16 bytes
if($opt_t eq "n") {
$http="n"; # suppress packet header
}
elsif ($opt_t eq "t"){
$http="r";
}
if($opt_h) {
print "tcpdump2ascii, version $version\n";
print "usage: tcpdump -l -x ... | tcpdump2ascii\n";
print " tcpdump2ascii [-h | -v | -d | -s | -l | -snoop <level>] [<filename>]\n";
print " -h : this text\n";
print " -v : print version string \n";
print " -d : print debugging info\n";
print " -l : don't print hex dump\n";
print " -s : suppress tcpdump information line\n";
print " -t : n (detection de \\n), t (detection de \\r\\n http,etc)\n";
print " -x : 1 (dump 16 bytes) 2 (dump 32 bytes); default 16 bytes)\n";
print " -S : skip print header packet\n";
print " -snoop : verbose output like snoop (levels 0-9)\n";
exit(0);
}
if($opt_v) {
print "tcpdump2ascii, version $version\n";
exit(0);
}
## read from stdin
##
my $i = 0;
my $ascii = "";
while(<>){
## print the tcpdump information
##
my $dumpline = $_;
my $converted_hex = "";
print "$dumpline";
if ( $dumpline =~ m/^[0-9\s]*\d\d:\d\d:\d\d/) {
$ascii .= $converted_hex;
if ( $ascii ne '') { print "[$ascii]\n"; }
#print "info line\n";
# Looks like an info line:
print $dumpline;
$ascii = "";
}
#if ( $dumpline =~ m/0x[a-f0-9]+:\s*([\sa-f0-9]+)([^\s]{10})$/ig) {
if ( $dumpline =~ m/0x[a-f0-9]+:\s*([\sa-f0-9]+)\s([^\s]{10,})$/ig) {
#print "prefixed line\n";
# Looks like a hex prefixed ascii line
my $hex = $1;
my $append = $2;
#print "hex before: [$hex]\n";
$hex =~ s/\s//g;
#print "hex after: [$hex]\n";
$converted_hex = $hex;
$converted_hex =~ s/([a-fA-F0-9][a-fA-F0-9])/map_chars($1)/eg;
#$ascii .= $hex;
if ( $hex =~ m/0a/) {
#print "0a found at: ";
my $nl = index( $hex,"0a");
#if ( $nl > -1 ) { print "$nl\n"; }
#else { print "NO\n"; }
if ($nl > -1 && $nl % 2 == 0 ) {
#print "ascii is now ". length( $ascii) ." bytes long";
$ascii .= substr( $converted_hex, 0, (($nl / 2)));
#if ( length( $ascii) > 20 ) { $hex =~ s/([a-fA-F0-9][a-fA-F0-9])/map_chars($1)/eg; }
if ( length( $ascii) > 30 ) { $ascii .= "\n"; }
$ascii .= substr( $converted_hex, (($nl / 2) + 1) );
} else {
$ascii .= $converted_hex;
}
} else {
$ascii .= $converted_hex;
}
}
}
if ( $ascii ne '') { print "[$ascii]\n"; }
exit(0);
sub map_chars {
my ($hex_char) = @_;
#$i++;
my $r = "";
my $no = hex( $hex_char);
if ( $no == 10 ) { $r = "\n" }
elsif ( $no < 32 || $no > 123) { $r = "_" }
else { $r = chr($no) }
#print "i = [$i], hex_char = [$hex_char], r = [$r]\n";
return $r;
}