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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
|
/*
Unix SMB/CIFS implementation.
Utility to extract pcap files from samba (log level 10) log files
Copyright (C) Jelmer Vernooij 2003
Thanks to Tim Potter for the genial idea
Portions (from capconvert.c) (C) Andrew Tridgell 1997
Portions (from text2pcap.c) (C) Ashok Narayanan 2001
Example:
Output NBSS(SMB) packets in hex and convert to pcap adding
Eth/IP/TCP headers
log2pcap -h < samba.log | text2pcap -T 139,139 - samba.pcap
Output directly to pcap format without Eth headers or TCP
sequence numbers
log2pcap samba.log samba.pcap
TODO:
- Hex to text2pcap outputs are not properly parsed in Wireshark
the NBSS or SMB level. This is a bug.
- Writing directly to pcap format doesn't include sequence numbers
in the TCP packets
- Check if a packet is a response or request and set IP to/from
addresses accordingly. Currently all packets come from the same
dummy IP and go to the same dummy IP
- Add a message when done parsing about the number of pacekts
processed
- Parse NBSS packet header data from log file
- Have correct IP and TCP checksums.
Warning:
Samba log level 10 outputs a max of 512 bytes from the packet data
section. Packets larger than this will be truncated.
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/>.
*/
#include "includes.h"
#include <popt.h>
/* We don't care about the paranoid malloc checker in this standalone
program */
#undef malloc
#include <assert.h>
int quiet = 0;
int hexformat = 0;
#define itoa(a) ((a) < 0xa?'0'+(a):'A' + (a-0xa))
#include <stdlib.h>
#include <unistd.h>
#include <sys/time.h>
#include <stdio.h>
#include <fcntl.h>
#define TCPDUMP_MAGIC 0xa1b2c3d4
/* tcpdump file format */
struct tcpdump_file_header {
uint32_t magic;
uint16_t major;
uint16_t minor;
int32_t zone;
uint32_t sigfigs;
uint32_t snaplen;
uint32_t linktype;
};
struct tcpdump_packet {
struct timeval ts;
uint32_t caplen;
uint32_t len;
};
typedef struct {
uint8_t ver_hdrlen;
uint8_t dscp;
uint16_t packet_length;
uint16_t identification;
uint8_t flags;
uint8_t fragment;
uint8_t ttl;
uint8_t protocol;
uint16_t hdr_checksum;
uint32_t src_addr;
uint32_t dest_addr;
} hdr_ip_t;
static hdr_ip_t HDR_IP = {0x45, 0, 0, 0x3412, 0, 0, 0xff, 6, 0, 0x01010101, 0x02020202};
typedef struct {
uint16_t source_port;
uint16_t dest_port;
uint32_t seq_num;
uint32_t ack_num;
uint8_t hdr_length;
uint8_t flags;
uint16_t window;
uint16_t checksum;
uint16_t urg;
} hdr_tcp_t;
static hdr_tcp_t HDR_TCP = {139, 139, 0, 0, 0x50, 0, 0, 0, 0};
static void print_pcap_header(FILE *out)
{
struct tcpdump_file_header h;
h.magic = TCPDUMP_MAGIC;
h.major = 2;
h.minor = 4;
h.zone = 0;
h.sigfigs = 0;
h.snaplen = 102400; /* As long packets as possible */
h.linktype = 101; /* Raw IP */
fwrite(&h, sizeof(struct tcpdump_file_header), 1, out);
}
static void print_pcap_packet(FILE *out, unsigned char *data, long length,
long caplen)
{
struct tcpdump_packet p;
p.ts.tv_usec = 0;
p.ts.tv_sec = 0;
p.caplen = caplen;
p.len = length;
fwrite(&p, sizeof(struct tcpdump_packet), 1, out);
fwrite(data, sizeof(unsigned char), caplen, out);
}
static void print_hex_packet(FILE *out, unsigned char *data, long length)
{
long i,cur = 0;
while(cur < length) {
fprintf(out, "%06lX ", cur);
for(i = cur; i < length && i < cur + 16; i++) {
fprintf(out, "%02x ", data[i]);
}
cur = i;
fprintf(out, "\n");
}
}
static void print_netbios_packet(FILE *out, unsigned char *data, long length,
long actual_length)
{
unsigned char *newdata; long offset = 0;
long newlen;
newlen = length+sizeof(HDR_IP)+sizeof(HDR_TCP);
newdata = (unsigned char *)malloc(newlen);
HDR_IP.packet_length = htons(newlen);
HDR_TCP.window = htons(0x2000);
HDR_TCP.source_port = HDR_TCP.dest_port = htons(139);
memcpy(newdata+offset, &HDR_IP, sizeof(HDR_IP));offset+=sizeof(HDR_IP);
memcpy(newdata+offset, &HDR_TCP, sizeof(HDR_TCP));offset+=sizeof(HDR_TCP);
memcpy(newdata+offset,data,length);
print_pcap_packet(out, newdata, newlen, actual_length+offset);
free(newdata);
}
unsigned char *curpacket = NULL;
unsigned short curpacket_len = 0;
long line_num = 0;
/* Read the log message produced by lib/util.c:show_msg() containing the:
* SMB_HEADER
* SMB_PARAMETERS
* SMB_DATA.ByteCount
*
* Example:
* [2007/04/08 20:41:39, 5] lib/util.c:show_msg(516)
* size=144
* smb_com=0x73
* smb_rcls=0
* smb_reh=0
* smb_err=0
* smb_flg=136
* smb_flg2=49153
* smb_tid=1
* smb_pid=65279
* smb_uid=0
* smb_mid=64
* smt_wct=3
* smb_vwv[ 0]= 117 (0x75)
* smb_vwv[ 1]= 128 (0x80)
* smb_vwv[ 2]= 1 (0x1)
* smb_bcc=87
*/
static void read_log_msg(FILE *in, unsigned char **_buffer,
unsigned short *buffersize, long *data_offset,
long *data_length)
{
unsigned char *buffer;
int tmp; long i;
assert(fscanf(in, " size=%hu\n", buffersize)); line_num++;
buffer = (unsigned char *)malloc(*buffersize+4); /* +4 for NBSS Header */
memset(buffer, 0, *buffersize+4);
/* NetBIOS Session Service */
buffer[0] = 0x00;
buffer[1] = 0x00;
memcpy(buffer+2, &buffersize, 2); /* TODO: need to copy as little-endian regardless of platform */
/* SMB Packet */
buffer[4] = 0xFF;
buffer[5] = 'S';
buffer[6] = 'M';
buffer[7] = 'B';
assert(fscanf(in, " smb_com=0x%x\n", &tmp)); buffer[smb_com] = tmp; line_num++;
assert(fscanf(in, " smb_rcls=%d\n", &tmp)); buffer[smb_rcls] = tmp; line_num++;
assert(fscanf(in, " smb_reh=%d\n", &tmp)); buffer[smb_reh] = tmp; line_num++;
assert(fscanf(in, " smb_err=%d\n", &tmp)); memcpy(buffer+smb_err, &tmp, 2); line_num++;
assert(fscanf(in, " smb_flg=%d\n", &tmp)); buffer[smb_flg] = tmp; line_num++;
assert(fscanf(in, " smb_flg2=%d\n", &tmp)); memcpy(buffer+smb_flg2, &tmp, 2); line_num++;
assert(fscanf(in, " smb_tid=%d\n", &tmp)); memcpy(buffer+smb_tid, &tmp, 2); line_num++;
assert(fscanf(in, " smb_pid=%d\n", &tmp)); memcpy(buffer+smb_pid, &tmp, 2); line_num++;
assert(fscanf(in, " smb_uid=%d\n", &tmp)); memcpy(buffer+smb_uid, &tmp, 2); line_num++;
assert(fscanf(in, " smb_mid=%d\n", &tmp)); memcpy(buffer+smb_mid, &tmp, 2); line_num++;
assert(fscanf(in, " smt_wct=%d\n", &tmp)); buffer[smb_wct] = tmp; line_num++;
for(i = 0; i < buffer[smb_wct]; i++) {
assert(fscanf(in, " smb_vwv[%*3d]=%*5d (0x%X)\n", &tmp)); line_num++;
memcpy(buffer+smb_vwv+i*2, &tmp, 2);
}
*data_offset = smb_vwv+buffer[smb_wct]*2;
assert(fscanf(in, " smb_bcc=%ld\n", data_length)); buffer[(*data_offset)] = *data_length; line_num++;
(*data_offset)+=2;
*_buffer = buffer;
}
/* Read the log message produced by lib/util.c:dump_data() containing:
* SMB_DATA.Bytes
*
* Example:
* [2007/04/08 20:41:39, 10] lib/util.c:dump_data(2243)
* [000] 00 55 00 6E 00 69 00 78 00 00 00 53 00 61 00 6D .U.n.i.x ...S.a.m
* [010] 00 62 00 61 00 20 00 33 00 2E 00 30 00 2E 00 32 .b.a. .3 ...0...2
* [020] 00 34 00 2D 00 49 00 73 00 69 00 6C 00 6F 00 6E .4.-.I.s .i.l.o.n
* [030] 00 20 00 4F 00 6E 00 65 00 46 00 53 00 20 00 76 . .O.n.e .F.S. .v
* [040] 00 34 00 2E 00 30 00 00 00 49 00 53 00 49 00 4C .4...0.. .I.S.I.L
* [050] 00 4F 00 4E 00 00 00 .O.N...
*/
static long read_log_data(FILE *in, unsigned char *buffer, long data_length)
{
long i, addr; char real[2][16]; int ret;
unsigned int tmp;
for(i = 0; i < data_length; i++) {
if(i % 16 == 0){
if(i != 0) {
/* Read and discard the ascii data after each line. */
assert(fscanf(in, " %8c %8c\n", real[0], real[1]) == 2);
}
ret = fscanf(in, " [%03lX]", &addr); line_num++;
if(!ret) {
if(!quiet)
fprintf(stderr, "%ld: Only first %ld bytes are logged, "
"packet trace will be incomplete\n", line_num, i-1);
return i-1;
}
assert(addr == i);
}
if(!fscanf(in, "%02X", &tmp)) {
if(!quiet)
fprintf(stderr, "%ld: Log message formatted incorrectly. "
"Only first %ld bytes are logged, packet trace will "
"be incomplete\n", line_num, i-1);
while ((tmp = getc(in)) != '\n');
return i-1;
}
buffer[i] = tmp;
}
/* Consume the newline so we don't increment num_lines twice */
while ((tmp = getc(in)) != '\n');
return data_length;
}
int main(int argc, const char **argv)
{
const char *infile, *outfile;
FILE *out, *in;
int opt;
poptContext pc;
char buffer[4096];
long data_offset = 0;
long data_length = 0;
long data_bytes_read = 0;
size_t in_packet = 0;
struct poptOption long_options[] = {
POPT_AUTOHELP
{
.longName = "quiet",
.shortName = 'q',
.argInfo = POPT_ARG_NONE,
.arg = &quiet,
.val = 0,
.descrip = "Be quiet, don't output warnings",
},
{
.longName = "hex",
.shortName = 'h',
.argInfo = POPT_ARG_NONE,
.arg = &hexformat,
.val = 0,
.descrip = "Output format readable by text2pcap",
},
POPT_TABLEEND
};
pc = poptGetContext(NULL, argc, argv, long_options,
POPT_CONTEXT_KEEP_FIRST);
poptSetOtherOptionHelp(pc, "[<infile> [<outfile>]]");
while((opt = poptGetNextOpt(pc)) != -1) {
switch (opt) {
case POPT_ERROR_BADOPT:
fprintf(stderr, "\nInvalid option %s: %s\n\n",
poptBadOption(pc, 0), poptStrerror(opt));
poptPrintUsage(pc, stderr, 0);
exit(1);
}
}
poptGetArg(pc); /* Drop argv[0], the program name */
infile = poptGetArg(pc);
if(infile) {
in = fopen(infile, "r");
if(!in) {
perror("fopen");
poptFreeContext(pc);
return 1;
}
} else in = stdin;
outfile = poptGetArg(pc);
if(outfile) {
out = fopen(outfile, "w+");
if(!out) {
perror("fopen");
fprintf(stderr, "Can't find %s, using stdout...\n", outfile);
poptFreeContext(pc);
return 1;
}
}
if(!outfile) out = stdout;
if(!hexformat)print_pcap_header(out);
while(!feof(in)) {
char *p;
p = fgets(buffer, sizeof(buffer), in);
if (p == NULL) {
fprintf(stderr, "error reading from input file\n");
break;
}
line_num++;
if(buffer[0] == '[') { /* Header */
if(strstr(buffer, "show_msg")) {
in_packet++;
if(in_packet == 1)continue;
read_log_msg(in, &curpacket, &curpacket_len, &data_offset, &data_length);
} else if(in_packet && strstr(buffer, "dump_data")) {
data_bytes_read = read_log_data(in, curpacket+data_offset, data_length);
} else {
if(in_packet){
if(hexformat) print_hex_packet(out, curpacket, curpacket_len);
else print_netbios_packet(out, curpacket, curpacket_len, data_bytes_read+data_offset);
free(curpacket);
}
in_packet = 0;
}
}
}
if (in != stdin) {
fclose(in);
}
if (out != stdout) {
fclose(out);
}
poptFreeContext(pc);
return 0;
}
|