-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnsdp-config
executable file
·290 lines (268 loc) · 8.98 KB
/
nsdp-config
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
#!/usr/bin/perl -w
#######################################################################
#
# NSDP discovery script v.1.0b
# Copyright 2012 Konstantin S. Vishnivetsky
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# For contacts:
# WEB: http://www.vishnivetsky.ru
# E-mail: [email protected]
# SkyPE: kvishnivetsky
# ICQ: 328-468-511
# Phone: +7 913 774-7588
#
#######################################################################
use strict;
use utf8;
use IO::Select;
use IO::Socket;
use Data::Dumper;
my @passwordSalt = (
0x4e, 0x74, 0x67, 0x72,
0x53, 0x6d, 0x61, 0x72,
0x74, 0x53, 0x77, 0x69,
0x74, 0x63, 0x68, 0x52,
0x6f, 0x63, 0x6b
);
my $sock = IO::Socket::INET->new(
Proto => 'udp',
LocalAddr => '0.0.0.0',
# LocalAddr => '192.168.0.254',
Broadcast => 1,
LocalPort => 63321,
ReuseAddr => 1
) or die "Can't bind : $@\n";
$sock->sockopt(SO_BROADCAST() => 1);
my $select = IO::Select->new($sock);
my $seq = rand(65535);
my $state = 0;
my $portcount = 5;
my $DEBUG = 1;
my $swmac = "\x00\x00\x00\x00\x00\x00"; # Initial value of switch mac address
my $selfmac = ""; # MAC address of network interface, used for communications witch switch
my $DST_ADDR = pack('CCCC', 255,255,255,255); # broadcast address, if your system has multiple interfaces,
# then use local broadcast for selected network interface
getConfig($sock, $seq++);
printf "Waiting for reply from device...\n" if $DEBUG;
my $isAlive = 1;
while($isAlive) {
my @ready = $select->can_read(1);
if (@ready) {
my $data = undef;
my $from = recv($sock, $data, 1500, 0) || die "recv: $!";
my($port, $from_addr) = sockaddr_in($from);
my @hexdata = unpack("C*", $data);
printf "Recv:[%s]\n", join(' ', map(sprintf("0x%.2x", $_), @hexdata)) if $DEBUG;
parseNSDP($data);
my $offset = 32;
my $isDone = 0;
while(!($isDone) && $offset < length($data)) {
my $type = unpack("n", substr($data, $offset)); $offset += 2;
if ($type == 0x0000) {
$isDone = 1;
print "\n";
next;
}
if ($type == 0xffff) {
$isDone = 1;
print "\n";
next;
}
my $len = unpack("n", substr($data, $offset)); $offset += 2;
my $val = substr($data, $offset, $len); $offset += $len;
if ($type == 0x0001) {
$val =~ s/ +//;
printf "Device Model: %s\n", $val;
next;
}
if ($type == 0x0003) {
$val =~ s/ +//;
printf "Device Name: %s\n", $val;
next;
}
if ($type == 0x0004) {
printf "MAC: %s\n", join(':', map(sprintf("%.2x", $_), unpack("C*", $val)));
$swmac = $val;
next;
}
if ($type == 0x0006) {
printf "IP Address: %s\n", join('.', unpack("C*", $val));
next;
}
if ($type == 0x0007) {
printf "Network Mask: %s\n", join('.', unpack("C*", $val));
next;
}
if ($type == 0x0008) {
printf "Gateway: %s\n", join('.', unpack("C*", $val));
next;
}
if ($type == 0x000b) {
my $val = unpack('C', $val);
printf "DHCP Client: %d\n", $val if defined($val);
next;
}
if ($type == 0x000d) {
printf "Firmware version: %s\n", $val;
next;
}
if ($type == 0x0010) {
my $val = unpack('C', $val);
printf "TFTP Server: %d\n", $val if defined($val);
next;
}
if ($type == 0x0c00) {
# my $val = substr($data, $offset, $len); $offset += $len;
my($port, $speed, $duplex) = unpack('CCC', $val);
printf "Port status: %d, Speed: %s, Duplex: %d\n", $port, portSpeed($speed), $duplex;
next;
}
if ($type == 0x1000) {
my @stat = unpack('CNNNNNNNNNNNN', $val);
printf "Packet statistics: %d,%d,%d,%d,%d,%d,%d\n", $stat[0], $stat[1], $stat[3], $stat[5], $stat[7], $stat[9], $stat[11];
next;
}
if ($type == 0x1c00) {
printf "Port diagbostics %s\n", join(' ', map(sprintf('%.2x', $_), unpack('C'.$len, $val)));
next;
}
if ($type == 0x2000) {
my $val = unpack('C', $val);
printf "VLAN mode: %d\n", $val if defined($val);
next;
}
if ($type == 0x2800) {
my($vlan, $membership) = unpack('na2', $val);
next if !(defined($vlan) && defined($membership));
my($mports, $tags) = unpack('CC', $membership);
my $pvids = $mports ^ $tags;
printf "VLAN: %.4d m:%s t:%s p:%s\n",
$vlan,
substr(unpack('B8', pack('C', $mports)), 0, $portcount),
substr(unpack('B8', pack('C', $tags)), 0, $portcount),
substr(unpack('B8', pack('C', $pvids)), 0, $portcount);
next;
}
if ($type == 0x5c00) {
printf "Port miroring mode: %s\n", join(' ', map(sprintf('%.2x', $_), unpack('C*', $val)));
next;
}
if ($len == 0x01) {
printf "%.4x(%.4x)=%.2x\n", $type, $len, unpack('C', $val) if $DEBUG;
next;
}
if ($len == 0x02) {
printf "%.4x(%.4x)=%.4x\n", $type, $len, unpack('n', $val) if $DEBUG;
next;
}
printf "%.4x(%.4x)\n", $type, $len if $DEBUG;
}
} else {
$isAlive = 0;
}
}
if (defined($ARGV[0])) {
if ($ARGV[0] eq '-w') {
setConfig($sock, $seq++, $swmac, 'password');
my @ready = $select->can_read(30);
if (@ready) {
my $data = undef;
my $from = recv($sock, $data, 1500, 0) || die "recv: $!";
my($port, $from_addr) = sockaddr_in($from);
my @hexdata = unpack("C*", $data);
printf "Recv:[%s]\n", join(' ', map(sprintf("0x%.2x", $_), @hexdata)) if $DEBUG;
parseNSDP($data);
}
}
}
$sock->close();
exit;
sub getConfig {
my $sock = shift;
my $seq = shift;
my $msg = pack('na6a6a6Sna4N', 0x0101, "\x00\x00\x00\x00\x00\x00", $selfmac, "\x00\x00\x00\x00\x00\x00", 0x0000, $seq, "NSDP", 0x00000000);
$msg .= "\x00\x01\x00\x00";
$msg .= "\x00\x02\x00\x00";
$msg .= "\x00\x03\x00\x00";
$msg .= "\x00\x04\x00\x00";
$msg .= "\x00\x05\x00\x00";
$msg .= "\x00\x06\x00\x00";
$msg .= "\x00\x07\x00\x00";
$msg .= "\x00\x08\x00\x00";
$msg .= "\x00\x0b\x00\x00";
$msg .= "\x00\x0c\x00\x00";
$msg .= "\x00\x0d\x00\x00";
$msg .= "\x00\x0f\x00\x00";
$msg .= "\x0c\x00\x00\x00";
# $msg .= "\x10\x00\x00\x00";
# $msg .= "\x1c\x00\x00\x00";
$msg .= "\x20\x00\x00\x00";
$msg .= "\x28\x00\x00\x00";
# $msg .= "\x5c\x00\x00\x00";
$msg .= "\xff\xff\x00\x00";
my @hexdata = unpack("C*", $msg);
printf "%s\n", join(' ', map(sprintf("0x%.2x", $_), @hexdata)) if $DEBUG;
my $dst = sockaddr_in(63322, $DST_ADDR);
$sock->send($msg, 0, $dst) or die "send: $!";
};
# For Firmware version 1.02.02 and newer
sub saltPassword {
my @passwordOrigin = unpack("C*", shift);
my @passwordSalted;
for (my $i=0; $i < (@passwordOrigin + 0); $i++) {
$passwordSalted[$i] = $passwordSalt[$i % (@passwordSalt + 0)] ^ $passwordOrigin[$i];
}
return pack("C*", @passwordSalted);
};
sub setConfig {
my $sock = shift;
my $seq = shift;
my $swmac = shift;
my $password = shift;
my $msg = pack('na6a6a6Sna4N', 0x0103, "\x00\x00\x00\x00\x00\x00", $selfmac, $swmac, 0x0000, $seq, "NSDP", 0x0000);
$msg .= pack('nna'.length($password), 0x000a, length($password), saltPassword($password)); # TLV with password
$msg .= pack('nnC', 0x000b, 0x0001, 1); # DHCP mode On|Off
$msg .= pack('nnCCCC', 0x0006, 0x0004, 192,168,0,239); # Device IP address
$msg .= pack('nnCCCC', 0x0007, 0x0004, 255,255,255,0); # Device IP address
$msg .= pack('nnCCCC', 0x0008, 0x0004, 192,168,0,254); # GW IP address
$msg .= pack('nnC', 0x2000, 0x0001, 0x04); # Enable advanced 8021q VLANs
$msg .= pack('nnnCC', 0x2800, 0x0004, 0001, unpack('C', pack('B8', '11111000')), unpack('C', pack('B8', '11111000'))); # vlan, memeber then tagged ports
$msg .= pack('nn', 0xffff, 0x0000); # trailer
my @hexdata = unpack("C*", $msg);
printf "%s\n", join(' ', map(sprintf("0x%.2x", $_), @hexdata));
my $dst = sockaddr_in(63322, $DST_ADDR);
$sock->send($msg, 0, $dst) or die "send: $!";
};
sub parseNSDP {
my $data = shift;
my($ctype, $result, $reserved, $selfmac, $dstmac, $reserved1, $seq, $sign, $reserved2) = unpack('nna4a6a6a2na4Nn', $data);
my @selfmac_ = unpack('C6', $selfmac);
my @dstmac_ = unpack('C6', $dstmac);
printf "ctype=%.4x\n", $ctype if $DEBUG;
printf "result=%.4x\n", $result if $DEBUG;
printf "selfmac=%s\n", join(':', map(sprintf("%.2x", $_), @selfmac_)) if $DEBUG;
printf "dstmac=%s\n", join(':', map(sprintf("%.2x", $_), @dstmac_)) if $DEBUG;
printf "seq=%.4x\n", $seq if $DEBUG;
printf "sign=%s\n", $sign if $DEBUG;
};
# --------------------------
sub portSpeed {
my $speed = shift;
return "Down" if 0 == $speed;
return "10M" if 1 == $speed;
return "100M" if 4 == $speed;
return "1000M" if 5 == $speed;
};