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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
|
/***************************************************************************
* utils.cc -- Miscellaneous utils that didn't fit into any of the other *
* source files. *
* *
***********************IMPORTANT NMAP LICENSE TERMS************************
*
* The Nmap Security Scanner is (C) 1996-2023 Nmap Software LLC ("The Nmap
* Project"). Nmap is also a registered trademark of the Nmap Project.
*
* This program is distributed under the terms of the Nmap Public Source
* License (NPSL). The exact license text applying to a particular Nmap
* release or source code control revision is contained in the LICENSE
* file distributed with that version of Nmap or source code control
* revision. More Nmap copyright/legal information is available from
* https://nmap.org/book/man-legal.html, and further information on the
* NPSL license itself can be found at https://nmap.org/npsl/ . This
* header summarizes some key points from the Nmap license, but is no
* substitute for the actual license text.
*
* Nmap is generally free for end users to download and use themselves,
* including commercial use. It is available from https://nmap.org.
*
* The Nmap license generally prohibits companies from using and
* redistributing Nmap in commercial products, but we sell a special Nmap
* OEM Edition with a more permissive license and special features for
* this purpose. See https://nmap.org/oem/
*
* If you have received a written Nmap license agreement or contract
* stating terms other than these (such as an Nmap OEM license), you may
* choose to use and redistribute Nmap under those terms instead.
*
* The official Nmap Windows builds include the Npcap software
* (https://npcap.com) for packet capture and transmission. It is under
* separate license terms which forbid redistribution without special
* permission. So the official Nmap Windows builds may not be redistributed
* without special permission (such as an Nmap OEM license).
*
* Source is provided to this software because we believe users have a
* right to know exactly what a program is going to do before they run it.
* This also allows you to audit the software for security holes.
*
* Source code also allows you to port Nmap to new platforms, fix bugs, and add
* new features. You are highly encouraged to submit your changes as a Github PR
* or by email to the dev@nmap.org mailing list for possible incorporation into
* the main distribution. Unless you specify otherwise, it is understood that
* you are offering us very broad rights to use your submissions as described in
* the Nmap Public Source License Contributor Agreement. This is important
* because we fund the project by selling licenses with various terms, and also
* because the inability to relicense code has caused devastating problems for
* other Free Software projects (such as KDE and NASM).
*
* The free version of Nmap 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. Warranties,
* indemnification and commercial support are all available through the
* Npcap OEM program--see https://nmap.org/oem/
*
***************************************************************************/
#include "nping.h"
#include "utils.h"
#include "NpingOps.h"
#include "global_structures.h"
#include "output.h"
#include "nbase.h"
#include "pcap.h"
#include "dnet.h"
#include <errno.h>
#include <vector>
extern NpingOps o;
/** Returns true if "source" contains at least one instance of "substring" */
bool contains(const char *source, const char *substring){
if(source==NULL || substring==NULL )
nping_fatal(QT_3,"contains(): NULL value received.");
if( strcasestr(source, substring) )
return true;
else
return false;
} /* End of contains() */
/** Returns true if the supplied string matches "rand" or "random" (not case
* sensitive)*/
bool meansRandom(const char *source){
if(source==NULL)
nping_fatal(QT_3,"meansRandom(): NULL value received.");
if( !strcasecmp(source, "rand") || !strcasecmp(source, "random") )
return true;
else
return false;
} /* End of meansRandom() */
/** Returns true if source contains the representation of a number >= min and
* <= max in the given base (with nothing following). */
static bool isNumber_range(const char *source, int base,
unsigned long min, unsigned long max){
unsigned long ul;
char *tail;
errno = 0;
ul = strtoul(source, &tail, base);
if (errno != 0 || tail == source || *tail != '\0')
return false;
return ul >= min && ul <= max;
}
/** Returns true if "source" is a number in the supplied base that can fit
* into a 8-bit var */
bool isNumber_u8(const char *source, int base){
return isNumber_range(source, base, 0UL, 0xFFUL);
}
/** Returns true if "source" is a number in the supplied base that can fit
* into a 16-bit var */
bool isNumber_u16(const char *source, int base){
return isNumber_range(source, base, 0UL, 0xFFFFUL);
}
/** Returns true if "source" is a number in the supplied base that can fit
* into a 32-bit var */
bool isNumber_u32(const char *source, int base){
return isNumber_range(source, base, 0UL, 0xFFFFFFFFUL);
}
/** Returns a buffer that contains the binary equivalent to the supplied
* hex spec or NULL in case of error.
* @warning Returned pointer points to a static buffer that subsequent calls
* will overwrite. */
u8 *parseBufferSpec(char *str, size_t *outlen){
char auxbuff[1024];
static u8 dst[16384];
size_t dstlen=16384;
unsigned int i=0, j=0;
char *start=NULL;
if(str==NULL || outlen==NULL)
return NULL;
/* This catches the empty string possibility "" */
if(strlen(str) == 0)
return NULL;
else
memset(auxbuff,0,1024);
/* String should be treated as a hex number in this format: 0xAABBCCDDEE...
* We process it the way it is specified, we don't perform byte order
* conversions so if the users says 0x00AA we write dst[0]=0x00, dst[1]==0xAA
* no matter the endianness of the host system. */
if( !strncmp("0x", str, 2) ){
/* This catches the case of an empty "0x" */
if(strlen(str) == 2)
return NULL;
start=str+2;
}
/* String should be treated as list of hex char in this format: \x00\xFF\x0A*/
else if( !strncmp("\\x", str, 2) ){
/* This catches the case of an empty "\x" */
if(strlen(str) == 2)
return NULL;
/* Copy all interesting bytes to an aux array, discard "\x" */
for(i=0; i<strlen(str) && j<1023; i++){
if( str[i]!='\\' && str[i]!='x' && str[i]!='X')
auxbuff[j++]=str[i];
}
auxbuff[j]='\0'; /* NULL terminate the string */
start=auxbuff;
}
/* It must be a hex number in this format: AABBCCDDEE (without 0x or \x) */
else{
start=str;
}
/*OK, here we should have "start" pointing to the beginning of a string
* in the format AABBCCDDEE... */
/* Check if all we've got are hex chars */
for(i=0; i<strlen(start); i++){
if( !isxdigit(start[i]) )
return NULL;
}
/* Check if we have an even number of hex chars */
if( strlen(start)%2 != 0 )
return NULL;
/* We are ready to parse this string */
for(i=0, j=0; j<dstlen && i<strlen(start)-1; i+=2){
char twobytes[3];
twobytes[0]=start[i];
twobytes[1]=start[i+1];
twobytes[2]='\0';
dst[j++]=(u8)strtol(twobytes, NULL, 16);
}
/* Store final length */
*outlen=j;
return dst;
} /* End of parseBufferSpec*/
/* Determines how many bits "a" and "b" have in common until they differ. For
* example, if A is 11111111 and B is 11111101, this function will return 6,
* as the first 6 bits of A and B are equal.
* @param len is the length in BYTES of "a" and "b".
*
* TODO: Check if this function is endian-safe.
*/
int bitcmp(u8 *a, u8*b, int len){
int equal=0;
int firstpart=len-1;
if(a==NULL || b==NULL || len<=0)
return -1;
for(int i=0; i<len; i++){
if(a[i]!=b[i]){
firstpart=i;
break;
}
}
/* Do all bits match? */
if(firstpart==len)
return len*8;
else
equal=firstpart*8;
/* Take the first byte that didn't match completely and determine how
* many bits they have in common until they differ */
for(int i=0, j=0x80; i<8; i++, j/=2){
if( (a[firstpart] & j) == (b[firstpart] & j) )
equal++;
else
return equal;
}
return equal;
} /* End of bitcmp() */
/** Removes every instance of the character stored in parameter "c" in the
* supplied string.
* @warning the supplied buffer is modified by this function. */
int removechar(char *string, char c){
size_t i=0, j=0;
if(string==NULL)
return OP_FAILURE;
while(string[j] != '\0') {
if(string[j] != c)
string[i++] = string[j];
j++;
}
string[i] = '\0';
return OP_SUCCESS;
} /* End of removechar() */
/** Removes every instance of ':' in the supplied string.
* @warning the supplied buffer is modified by this function. Whenever a
* colon is found, the rest of the string is moved one position to the left
* so the colon gets overwritten. */
int removecolon(char *string){
return removechar(string, ':');
}/* End of removecolon() */
/* lamont_hdump() has a bug so 3-byte lines are not printed correctly.
* This function is a better version of hdump written by Luis MartinGarcia.
* It uses current locale to determine if a character is printable or
* not. It prints 73char wide lines like these:
0000 e8 60 65 86 d7 86 6d 30 35 97 54 87 ff 67 05 9e .`e...m05.T..g..
0010 07 5a 98 c0 ea ad 50 d2 62 4f 7b ff e1 34 f8 fc .Z....P.bO{..4..
0020 c4 84 0a 6a 39 ad 3c 10 63 b2 22 c4 24 40 f4 b1 ...j9.<.c.".$@..
* The lines look basically like Wireshark hex dump.
* */
void luis_hdump(char *cp, unsigned int length) {
static char asciify[257]; /* Stores character table */
static bool asc_init=false; /* Flag to generate table only once */
unsigned int i=0, hex=0, asc=0; /* Array indexes */
int line_count=0; /* For byte count at line start */
u8 current_char=0; /* Current character to print */
#define LINE_LEN 70 /* Length of printed line */
char line2print[LINE_LEN]; /* Stores current line */
char printbyte[16]; /* For byte conversion */
memset(line2print, ' ', LINE_LEN);
line2print[LINE_LEN-1]='\0';
/* On the first run, generate a list of nice printable characters
* (according to current locale) */
if( asc_init==false){
asc_init=true;
for(int i=0; i<256; i++){
if( isalnum(i) || isdigit(i) || ispunct(i) ){ asciify[i]=i; }
else{ asciify[i]='.'; }
}
}
#define HEX_START 3
#define ASC_START 53
for(i=0, hex=HEX_START, asc=ASC_START; i<length; i++){
current_char=cp[i];
if( hex==HEX_START+24) hex++; /* Insert space every 8 bytes */
/* First print the hex number */
sprintf(printbyte,"%02x", current_char);
line2print[hex++]=printbyte[0];
line2print[hex++]=printbyte[1];
line2print[hex++]=' ';
/* Then print its ascii equivalent */
line2print[asc++]=asciify[ current_char ];
/* Every 16 buffer bytes, print the line. */
if( (((i+1)%16)==0 && i!=0) || i+1==length ){
printf("%04x%s\n", (16*line_count++), line2print);
hex=HEX_START; asc=ASC_START;
memset(line2print, ' ', LINE_LEN);
line2print[LINE_LEN-1]='\0';
}
}
return;
} /* End of luis_hdump() */
/** Takes a string representing a number, converts it to an unsigned
* long, and stores it in *dst.
* @param str is the string to be converted. The number may be in any
* of the following forms:
* - Hexadecimal number: It must start with "0x" and have an even
* number of hex digits after it.
* - Octal number: It must start with "0" and have any number of
* octal digits ([0,7]) after it.
* - Decimal number: Any string that does not start with "0x" or
* "0" will be treated as a decimal number. It may only contain
* decimal digits (no whitespace, no weird symbols, and not even
* a sign character (+ or -).
* - Random number: The number specification may contain the special
* value "rand" or "random". In that case, a random number of the
* requested length will be generated and stored in the supplied
* buffer.
* @param min values less than this cause an error.
* @param max values greater than this cause an error.
* @param dst should be the address of an unsigned long variable.
* @return OP_SUCCESS if conversion was successful or OP_FAILURE in
* case of error. */
static int parse_unsigned_number(const char *str, unsigned long min, unsigned long max, unsigned long *dst){
unsigned long int result;
char *tail=NULL;
if(str==NULL || dst==NULL)
return OP_FAILURE;
/* Check if the spec contains a sign character */
if(strpbrk(str, "-+") != NULL)
return OP_FAILURE;
/* Case 1: User wants a random value */
if(!strcasecmp(str, "rand") || !strcasecmp(str, "random")){
u32 r = get_random_u32();
*dst = min + (unsigned long) ((max - min + 1) * ((double) r / 0xffffffffUL));
return OP_SUCCESS;
}
/* Case 2: User supplied an actual number */
errno=0;
result=strtoul(str, &tail, 0);
if(errno!=0 || tail==str || *tail!='\0')
return OP_FAILURE;
if (result >= min && result <= max) {
*dst = result;
return OP_SUCCESS;
} else {
return OP_FAILURE;
}
} /* End of parse_number() */
/** Takes a string representing an 8-bit number and converts it into an
* actual integer. The result is stored in memory area pointed by
* "dstbuff". Returns OP_SUCCESS if conversion was successful or
* OP_FAILURE in case of error.*/
int parse_u8(const char *str, u8 *dst){
unsigned long ul;
int ret;
ret = parse_unsigned_number(str, 0UL, 0xffUL, &ul);
if (ret == OP_SUCCESS)
*dst = ul;
return ret;
}
/** Takes a string representing a 16-bit number and converts it into an
* actual integer. The result is stored in memory area pointed by
* "dstbuff". Returns OP_SUCCESS if conversion was successful or
* OP_FAILURE in case of error.*/
int parse_u16(const char *str, u16 *dst){
unsigned long ul;
int ret;
ret = parse_unsigned_number(str, 0UL, 0xffffUL, &ul);
if (ret == OP_SUCCESS)
*dst = ul;
return ret;
}
/** Takes a string representing a 32-bit number and converts it into an
* actual integer. The result is stored in memory area pointed by
* "dstbuff". Returns OP_SUCCESS if conversion was successful or
* OP_FAILURE in case of error.*/
int parse_u32(const char *str, u32 *dst){
unsigned long ul;
int ret;
ret = parse_unsigned_number(str, 0UL, 0xffffffffUL, &ul);
if (ret == OP_SUCCESS)
*dst = ul;
return ret;
}
/** Prints the hexadecimal dump of the supplied buffer to standard output */
int print_hexdump(int level, const u8 *cp, u32 length){
char *str = hexdump(cp, length);
if(str==NULL)
return OP_FAILURE;
nping_print(level, "%s", str);
free(str);
return OP_SUCCESS;
} /* End of print_hexdump() */
|