-
Notifications
You must be signed in to change notification settings - Fork 89
/
h2b
executable file
·167 lines (145 loc) · 3.58 KB
/
h2b
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
#!/usr/bin/perl
#
# convert hex to binary, including output of tools like `hexdump`
# while ignoring (and validating) offsets
#
# -samy kamkar, 7/18/2018
use strict;
$|++;
die "usage: $0 [-vv] <hex string | - | file>\n" unless @ARGV || !-t STDIN;
my $verbose = $ARGV[0] =~ /^-v/ ? shift : 0;
my $data = shift;
my $piped;
my ($lastrow, $lastoff, $lastlen, $newdata, $repeat, $rownum, $offsetlen);
if (-e $data)
{
open(F, "<$data") || die "Can't read $data: $!";
$data = join "", <F>;
close F;
}
elsif ($data eq "-")
{
$data = join "", <STDIN>;
}
elsif (!-t STDIN)
{
$piped = 1;
#$data = join "", <>;
while (<>)
{
print "got: $_\n" if $verbose eq "-vv";
print pack("H*", parse($_));
}
}
$data = cleanup($data);
print $data;
sub cleanup
{
$data =~ s/^\r?\n|\r?\n$//gm;
my @data = split "\n", $data;
# ony if 2 or more lines
if (@data >= 2)
{
foreach my $row (@data)
{
my $add = parse($row);
defined($add) ? $newdata .= $add : return;
}
}
else
{
$newdata = $data;
}
$newdata =~ s/\s//g;
if ($newdata =~ /[^\da-f]/i)
{
die "$newdata\n\nData isn't proper hex!\n";
}
$newdata = pack "H*", $newdata;
return $newdata;
}
sub parse
{
my $row = shift;
#chomp($row);
#print STDERR "$row\n";
# tcpdump, reset offset, ignore line
if ($row =~ /^\d+:\d+:\d+\.\d+ /)
{
print STDERR "$row\n" if $verbose;
$lastoff = 0;
}
# ngrep, reset offset, ignore line
elsif ($row =~ /^[A-Z] \d+\.\d+\.\d+\.\d+/)
{
print STDERR "$row\n" if $verbose;
$lastoff = 0;
}
# just pure hex
elsif ($row =~ /^([a-f\d]+)$/i)
{
return $1;
}
# parse offset + data in various tool formats
elsif (
$row =~ /^([a-f\d]{8}) ((?: [a-f\d]{2}){1,16}) /i ||
$row =~ /^([a-f\d]{8,}) \s+ ( (?:[a-f\d]{2} (?:\s? [a-f\d]{2})? \s+ )+ ) \s* \|/ix ||
$row =~ /^\s*0x([a-f\d]+):\s+((?:[a-f\d]{2}[a-f\d]{0,2}\s?){1,8})/i ||
$row =~ /^\s*()((?:[a-f\d]{2}\s*){1,16})/i || # XXX need to be more explicit
$row =~ /^\s*(?:0x)?([a-f\d]+):?(?:\s+([a-f\d][a-f\d\s]+))?(?:\|.*\|\s*)?$/i)
{
# TODO once a format is found, only use that from then on
my ($offset, $rest) = ($1, $2);
$rest =~ s/\s//g;
$row = $rest;
#$newdata .= $rest;
# if this is first row
if (!$rownum)
{
$offsetlen = length($offset);
}
# handle case where last line has offset but no data
if (length($offset) == 0 && $offsetlen && length($row) == $offsetlen)
{
$offset = $row;
$row = "";
}
print STDERR "offset=$offset hex=$rest lastoff=$lastoff lastlen=$lastlen r=$repeat\n" if $verbose eq "-vv";
# if we have an offset, verify future ones are correct
if ($rownum && defined $lastoff)
{
# are we repeating?
if ($repeat)
{
my $times = ((hex($offset) - $lastoff) / $lastlen) - 1;
print STDERR "repeat $times times till " . hex($offset) . " row=$row\n" if $verbose eq "-vv";
$row = $lastrow x $times . $row;
$repeat = 0;
}
# is our new offset the correct increase of bytes?
elsif (hex($offset) - $lastoff != $lastlen)
{
print STDERR "bad offset: (0x$offset)" . hex($offset) . " - $lastoff != $lastlen ($row)\n";
return undef;
}
}
$lastoff = hex($offset);
$lastlen = length($rest) / 2;
$lastrow = $rest;
}
# handle '*' lines which mean repeat until next offset
elsif ($row =~ /^\*\r?$/)
{
$repeat = 1;
$row = "";
print STDERR "repeat=1\n" if $verbose eq "-vv";
}
# no offset
else
{
return "" if $row eq "\r";
return undef;
}
$rownum++;
return $row;
}