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
|
/*++
Copyright (c) 1991 Microsoft Corporation
Module Name:
bwatch.c
Abstract:
Prints out interesting browser related events for a domain
Author:
Dan Hinsley (DanHi) 10-Oct-1992
Environment:
Application mode
Revision History:
--*/
#define INCLUDE_SMB_TRANSACTION
#include <nt.h>
#include <ntrtl.h>
#include <nturtl.h>
#include <windows.h>
#include <lmcons.h>
#include <nb30.h>
#include <smbtypes.h>
#include <smb.h>
#include <smbgtpt.h>
#include <hostannc.h>
#include <stdio.h>
#include <string.h>
#define SPACES " "
#define ClearNcb( PNCB ) { \
RtlZeroMemory( PNCB , sizeof (NCB) ); \
RtlCopyMemory( (PNCB)->ncb_name, SPACES, sizeof(SPACES)-1 );\
RtlCopyMemory( (PNCB)->ncb_callname, SPACES, sizeof(SPACES)-1 );\
}
//
// Globals
//
NCB myncb;
CHAR localName[16];
ULONG lanNumber=0;
CHAR DecodedName[20];
//
// Functions
//
VOID AddName(UCHAR Suffix);
VOID Reset(UCHAR Lsn);
VOID DecodeName(LPSTR DecodedName, LPSTR EncodedName);
BOOL DecodeSmb(PBYTE Smb);
VOID
usage (
LPSTR CommandName
)
{
printf("usage:\n\t%s [-n #] Domain\n", CommandName);
printf("\n\t-n:# supply lana number, default 0\n\n");
}
_cdecl
main (int argc, char *argv[])
{
CHAR Buffer2[512];
int i;
UCHAR name_number;
USHORT length;
if ( argc < 2 || argc > 3) {
usage (argv[0]);
return 1;
}
//
// Clear out the name
//
for (i = 0; i < 16 ;i++ ) {
localName[i] = ' ';
}
//
// parse the switches
//
for (i=1;i<argc ;i++ ) {
if (argv[i][0] == '-') {
switch (argv[i][1]) {
case 'n':
if (!NT_SUCCESS(RtlCharToInteger (&argv[i][3], 10, &lanNumber))) {
usage (argv[0]);
return 1;
}
break;
default:
usage (argv[0]);
return 1;
break;
}
} else {
//
// not a switch must be a name
//
RtlCopyMemory (localName, argv[i], strlen( argv[i] ));
_strupr(localName);
}
}
printf("Starting to watch domain %s on lana %d\n", localName, lanNumber);
//
// Reset the adapter
//
Reset(FALSE); // Really reset the adapter.
// AddName for all possible names for this domain
AddName(0x1e);
AddName(0);
AddName(' ');
// Jam in Domain annoucement name
strcpy(&localName[2], "__MSBROWSE__");
localName[0] = 0x01;
localName[1] = 0x02;
localName[14] = 0x02;
localName[15] = 0x01;
RtlCopyMemory( myncb.ncb_name, localName, 16);
myncb.ncb_lana_num = (UCHAR)lanNumber;
Netbios( &myncb );
if ( myncb.ncb_retcode != NRC_GOODRET ) {
printf( "AddGroupname MSBROWSE returned an error %lx\n", myncb.ncb_retcode );
}
// Receive Datagram
printf( "Listening for Datagrams\n" );
while (1) {
ClearNcb( &myncb );
myncb.ncb_command = NCBDGRECV;
myncb.ncb_lana_num = (UCHAR)lanNumber;
myncb.ncb_num = 0xff;
myncb.ncb_length = sizeof( Buffer2 );
myncb.ncb_buffer = Buffer2;
Netbios( &myncb );
if ( myncb.ncb_retcode != NRC_GOODRET ) {
printf( "ReceiveDatagram returned an error %lx\n", myncb.ncb_retcode );
if (myncb.ncb_retcode != NRC_INCOMP) {
return 1;
}
}
//
// Find first blank
//
for (i = 0; i < 15 ; i++) {
if (myncb.ncb_callname[i] == ' ') {
break;
}
}
DecodeName(DecodedName, myncb.ncb_callname);
DecodeSmb(myncb.ncb_buffer);
}
//
// Reset the adapter on the way out
//
Reset(TRUE);
return 0;
}
VOID
AddName(
UCHAR Suffix
)
{
ClearNcb( &myncb );
myncb.ncb_command = NCBADDGRNAME;
localName[15] = Suffix;
RtlCopyMemory( myncb.ncb_name, localName, 16);
myncb.ncb_lana_num = (UCHAR)lanNumber;
Netbios( &myncb );
if ( myncb.ncb_retcode != NRC_GOODRET ) {
printf( "AddGroupname 0x%x returned an error %lx\n", Suffix,
myncb.ncb_retcode);
return;
}
}
VOID
Reset(
UCHAR Reset
)
{
ClearNcb( &myncb );
myncb.ncb_command = NCBRESET;
myncb.ncb_lsn = Reset;
myncb.ncb_lana_num = lanNumber;
myncb.ncb_callname[0] = myncb.ncb_callname[1] = myncb.ncb_callname[2] =
myncb.ncb_callname[3] = 0;
Netbios( &myncb );
if ( myncb.ncb_retcode != NRC_GOODRET ) {
printf( "Reset returned an error %lx\n", myncb.ncb_retcode);
return;
}
}
VOID
DecodeName(
LPSTR DecodedName,
LPSTR EncodedName
)
{
CHAR TempString[6];
int i;
//
// Find first blank
//
for (i = 0; i < 15 ; i++) {
if (EncodedName[i] == ' ') {
break;
}
}
strncpy(DecodedName, EncodedName, i);
DecodedName[i] = '\0';
sprintf(TempString, "<%x>", EncodedName[15]);
strcat(DecodedName, TempString);
}
BOOL
DecodeSmb(
PBYTE Smb
)
{
LPSTR MailslotName;
PUCHAR pPacketType;
PNT_SMB_HEADER pSmbHeader;
PREQ_TRANSACTION pSmbTransaction;
PBROWSE_ANNOUNCE_PACKET pBrowseAnnouncePacket;
PREQUEST_ELECTION pRequestElection;
PBECOME_BACKUP pBecomeBackup;
PREQUEST_ANNOUNCE_PACKET pRequestAnnouncement;
//
// Decipher the SMB in the packet
//
pSmbHeader = (PNT_SMB_HEADER) Smb;
if (pSmbHeader->Protocol[0] != 0xff &&
pSmbHeader->Protocol[1] != 'S' &&
pSmbHeader->Protocol[2] != 'M' &&
pSmbHeader->Protocol[3] != 'B') {
printf("Not a valid SMB header\n");
return(FALSE);
}
pSmbTransaction = (PREQ_TRANSACTION)
(Smb + sizeof(NT_SMB_HEADER));
MailslotName = (LPSTR) (pSmbTransaction->Buffer +
pSmbTransaction->SetupCount + 5);
if (!strcmp(MailslotName, "\\MAILSLOT\\BROWSE")) {
pPacketType = (PUCHAR) myncb.ncb_buffer +
pSmbTransaction->DataOffset;
switch (*pPacketType) {
case AnnouncementRequest:
printf("Announcement request from %s. ", DecodedName);
pRequestAnnouncement =
(PREQUEST_ANNOUNCE_PACKET) pPacketType;
printf("Reply %s\n",
pRequestAnnouncement->RequestAnnouncement.Reply);
break;
case Election:
printf("Election request: %s ", DecodedName);
pRequestElection = (PREQUEST_ELECTION) pPacketType;
printf("Version(%d) Criteria(0x%x) ",
pRequestElection->ElectionRequest.Version,
pRequestElection->ElectionRequest.Criteria);
printf("TimeUp(%d)\n",
pRequestElection->ElectionRequest.TimeUp);
break;
case BecomeBackupServer:
printf("BecomeBackupServer from %s ", DecodedName);
pBecomeBackup = (PBECOME_BACKUP) pPacketType;
printf("to %s\n", pBecomeBackup->BecomeBackup.BrowserToPromote);
break;
case LocalMasterAnnouncement:
case WkGroupAnnouncement:
pBrowseAnnouncePacket =
(PBROWSE_ANNOUNCE_PACKET) pPacketType;
switch (*pPacketType) {
case LocalMasterAnnouncement:
printf("LocalMasterAnnouncement from %s. ", DecodedName);
break;
case WkGroupAnnouncement:
printf("Workgroup Announcement from %s. ", DecodedName);
break;
}
printf("UpdateCount = %d\n",
pBrowseAnnouncePacket->BrowseAnnouncement.UpdateCount);
break;
default:
printf("\n**** Packet type %d from %s ****\n",
*pPacketType, DecodedName);
}
}
else if (strcmp(MailslotName, "\\MAILSLOT\\NET\\REPL_CLI") &&
strcmp(MailslotName, "\\MAILSLOT\\NET\\NTLOGON") &&
strcmp(MailslotName, "\\MAILSLOT\\NET\\NETLOGON") &&
strcmp(MailslotName, "\\MAILSLOT\\LANMAN")) {
printf("Received an unknown datagram, name = %s\n",
MailslotName);
}
return(TRUE);
}
|