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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
|
/**
* NetNuke - Erases all storage media deteced by the system
* Copyright (C) 2009 Joseph Hunkeler <jhunkeler@gmail.com, jhunk@stsci.edu>
*
* This file is part of NetNuke.
*
* NetNuke 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.
*
* NetNuke 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 NetNuke. If not, see <http://www.gnu.org/licenses/>.
**/
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <unistd.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <err.h>
#include <time.h>
#ifdef __FreeBSD__
#include <libutil.h>
#include <sys/disk.h>
#else
#include "human_readable.h"
#include <sys/ioctl.h>
#include <linux/fs.h>
#endif
#include "netnuke.h"
/* Global variables */
static int64_t error;
nukeLevel_t udef_nukelevel = NUKE_PATTERN; /* Static patterns is default */
bool udef_verbose = false;
bool udef_verbose_high = false;
int8_t udef_wmode = 0; /* O_SYNC is default */
int32_t udef_passes = 1;
bool udef_testmode = true; /* Test mode should always be enabled by default. */
int32_t udef_blocksize = 512; /* 1 block = 512 bytes*/
const char sPattern[] = {
0xA0, 0xB0, 0xC0, 0xD0, 0xE0, 0xF0,
0xA1, 0xB1, 0xC1, 0xD1, 0xE1, 0xF1,
0xA2, 0xB2, 0xC2, 0xD2, 0xE2, 0xF2,
0xA3, 0xB3, 0xC3, 0xD3, 0xE3, 0xF3
};
/* List of media types that we can nuke */
#ifdef __FreeBSD__
const char* mediaList[18] = {
"ad", //ATAPI
"da", //SCSI
"sa", //SCSI
"adv", //AdvanSys Narrow
"adw", //AdvanSys Wide
"amd", //AMD 53C974 (Tekram DC390(T))
"ncr", //NCR PCI
"bt", //BusLogic
"aha", //Adaptec 154x/1535
"ahb", //Adaptec 174x
"ahc", //Adaptec 274x/284x/294x
"aic", //Adaptec 152x/AIC-6360/AIC-6260
"isp", //QLogic
"dpt", //DPT RAID
"amr", //AMI MegaRAID
"mlx", //Mylex DAC960 RAID
"wt", //Wangtek and Archive QIC-02/QIC-36
};
#else
const char* mediaList[3] = {
"hd", //ATAPI
"sd", //SCSI
};
#endif
void fillRandom(char buffer[], uint64_t length)
{
uint32_t random = 0, random_count = 0;
int32_t linebreak = 0;
/* Initialize random seed */
srand(time(NULL) * time(NULL) / 3 + 6201985 * 3.14159);
/* Define a single static pattern */
if(udef_nukelevel == NUKE_PATTERN)
{
random = rand() % strlen(sPattern) + 1;
//printf("size = %d STATIC = ### %x ###\n", strlen(sPattern), (int)random);
buffer[random_count] = sPattern[random];
}
/* Fills the write buffer with random garbage */
for(random_count = 0; random_count < length; random_count++)
{
if(udef_nukelevel == NUKE_RANDOM_SLOW || udef_nukelevel == NUKE_RANDOM_FAST)
{
random = rand() % RAND_MAX;
//printf("RANDOM = ### %x ###\n", random);
buffer[random_count] = random;
}
/* This is a debug feature to prove the random generator is functioning */
if(udef_verbose_high)
{
printf("0x%08X ", (char)random);
if(linebreak == 5)
{
printf("\n");
linebreak = -1;
}
linebreak++;
}
}
if(udef_verbose_high)
putchar('\n');
}
int nuke(char* media, uint64_t size)
{
/* test with 1G worth of data */
if(udef_testmode == true)
size = (1024 * 1024) * 100;
errno = 0;
char mediaSize[BUFSIZ];
char writeSize[BUFSIZ];
char writePerSecond[BUFSIZ];
int32_t pass;
uint64_t byteSize = udef_blocksize;
uint64_t bytesWritten = 0L;
uint64_t times, block;
//char *wTable = (char*)malloc(sizeof(char)*byteSize);
char wTable[byteSize];
uint32_t startTime, currentTime, endTime;
int32_t retainer = 0;
if(wTable == NULL)
{
fprintf(stderr, "Could not allocate write table buffer at size %jd\n", byteSize);
return 1;
}
/* Set the IO mode */
int O_UFLAG = udef_wmode ? O_ASYNC : O_SYNC;
//char* testflag = udef_testmode ? "/dev/null" : media;
if(udef_testmode == true)
sprintf(media, "%s", "./testmode.img");
int fd = open(media, O_RDWR | O_TRUNC | O_SYNC | O_DIRECT, 0700 );
if(errno != 0)
{
perror("nuke");
exit(1);
}
/* Generate a size string based on the media size. example: 256M */
humanize_number(mediaSize, 5, (uint64_t)size, "",
HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
printf("Wiping %s: %ju (%s)\n", media, (intmax_t)size, mediaSize);
/* Dump random garbage to the write table */
if(udef_nukelevel == NUKE_RANDOM_SLOW || udef_nukelevel == NUKE_RANDOM_FAST)
fillRandom(wTable, byteSize);
else if(udef_nukelevel == NUKE_ZERO)
memset(wTable, 0, byteSize);
else
staticPattern(wTable, byteSize);
/* Begin write passes */
for( pass = 1; pass <= udef_passes ; pass++ )
{
/* Determine how many writes to perform, and at what byte size */
times = size / byteSize;
startTime = time(NULL);
for( block = 0 ; block <= times; block++)
{
currentTime = time(NULL);
long double bytestmp = 0.0L;
bytestmp += bytesWritten;
long double bytes = (float)(size / times * block);
/* Generate a size string based on bytes written. example: 256M */
humanize_number(writeSize, 5,
bytes, "", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
/* Generate a size string based on writes per second. example: 256M */
humanize_number(writePerSecond, 5,
(intmax_t)((long double)bytes / ((long double)currentTime - (long double)startTime)), "",
HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
/* Save I/O by printing our progress every X iterations with a retainer*/
if(retainer > RETAINER || block == times)
{
if(udef_passes > 1)
printf("pass %d ", pass);
/* Output our progress */
printf("\t%jd of %jd blocks [ %s / %3.1Lf%% / %s/s ]\r",
block,
times,
writeSize,
(bytes / (long double)size) * 100L,
writePerSecond
);
retainer = -1;
/* Recycle the write table with random garbage */
if(udef_nukelevel == NUKE_RANDOM_SLOW)
fillRandom(wTable, byteSize);
}
if(block >= times)
{
break;
}
bytesWritten = write(fd, wTable, byteSize);
//printf("\nWRITE #%jd\n", block);
/*
if(bytesWritten != byteSize)
{
fprintf(stderr, "\nPossible error occurred.\n");
continue;
}
*/
/*
if(errno != 0)
{
perror("write");
exit(1);
}
*/
++retainer;
} /* BLOCK WRITE */
endTime = time(NULL);
} /* PASSES */
putchar('\n');
//free(wTable);
close(fd);
return 0;
}
void echoList()
{
int i = 0;
int mt = 0;
int mediaFound = 0;
do
{
/* Media size */
uint64_t size;
/* Holds the media type, and number. example: ad0 */
char media[BUFSIZ];
char mediaShort[BUFSIZ];
/* Generate a device string based on the current interation*/
#ifdef __FreeBSD__
sprintf(media, "/dev/%s%d", mediaList[mt], i);
#else
sprintf(media, "/dev/%s%c", mediaList[mt], 'a' + i);
#endif
/* Set media size */
size = getSize(media);
#ifdef __FreeBSD__
/* SATA checkpoint */
/*
if((int64_t)size < 1L && (strcmp(media, "/dev/ad0") == 0))
{
printf("SATA checkpoint initialized. IDE Channel 1 *may* be skipped!\n");
sprintf(media, "/dev/%s%d", "/dev/ad", 4);
}
*/
#endif
/* We MUST use the int64_t cast. Unsigned integers cannot have a negative value.
* Otherwise this loop would never end. */
if((int64_t)size > 0L)
{
strncpy(mediaShort, &media[5], strlen(media));
printf("%s: %jd\n", mediaShort , (intmax_t)size);
mediaFound++;
}
else
{
/* To prevent overrunning */
if(mediaList[mt] == NULL || mediaList[mt] == '\0')
break;
/*
else if(i < 5 && (strcmp(mediaList[mt], "ad")) == 0)
break;
*/
/* mediaList iteration */
mt++;
/* Reset iteratior so that the next increment is zero */
i = -1;
}
i++;
} while( 1 );
printf("%d device%s detected\n", mediaFound, mediaFound < 1 || mediaFound > 1 ? "s" : " \b" );
putchar('\n');
}
/* Function: getSize
* Argument 1: Fully qualified path to device
* Return: Media size, or -1 on failure
*/
uint64_t getSize(const char* media)
{
/* Media size */
uint64_t size;
/* Attempt to open the media read-only */
int fd = open(media, O_RDONLY);
if(fd)
{
/* Set media size */
#ifdef __FreeBSD__
error = ioctl(fd, DIOCGMEDIASIZE, &size);
#else
error = ioctl(fd, BLKGETSIZE, &size);
/* For Linux we must adjust the size returned, because we want bytes, not blocks */
size *= 512;
#endif
}
else
exit(1);
if(error != 0)
{
/* Errors will be completely ambiguous due to the nature of this
* program. */
return -1;
}
/* Close the media file descriptor */
close(fd);
/* Return the size of the media */
return size;
}
void usage(const char* cmd)
{
printf("usage: %s [options] ...\n", cmd);
printf("--help -h This message\n");
printf("--write-mode s -w s Valid values:\n\
0: Synchronous (default)\n\
1: Asynchronous\n");
printf("--nuke-level n -nl n Varying levels of destruction:\n\
0: Zero out (quick wipe)\n\
1: Static patterns (0xA, 0xB, ...)\n\
2: Fast random (single random buffer across media)\n\
3: Slow random (regenerate random buffer)\n\
4: Ultra-slow re-writing method\n");
printf("--passes n -p n Number of wipes to perform on a single device\n");
printf("--disable-test Disables test-mode, and allows write operations\n");
printf("--block-size n -b n Blocks at once\n");
printf("--verbose -v\n");
printf("--verbose-high -v Debug level verbosity\n");
printf("--version -V\n");
putchar('\n');
}
void version_short()
{
printf("NetNuke v%d.%d-%s\nCopyright (C) 2009 %s <%s>\n\
This software is licensed under %sv%d\n",
NETNUKE_VERSION_MAJOR, NETNUKE_VERSION_MINOR,
NETNUKE_VERSION_REVISION, NETNUKE_AUTHOR,
NETNUKE_AUTHOR_EMAIL, NETNUKE_LICENSE_TYPE,
NETNUKE_LICENSE_VERSION);
}
void version(const char* cmd)
{
printf("NetNuke v%d.%d-%s\n \
Copyright (C) 2009 %s <%s>\n \
This software is licensed under %sv%d\n\n%s\n\n",
NETNUKE_VERSION_MAJOR, NETNUKE_VERSION_MINOR,
NETNUKE_VERSION_REVISION, NETNUKE_AUTHOR,
NETNUKE_AUTHOR_EMAIL, NETNUKE_LICENSE_TYPE,
NETNUKE_LICENSE_VERSION, NETNUKE_LICENSE_PREAMBLE
);
printf("NOTE: humanize_number was ported from NetBSD to function on Linux.\n");
printf("That source code (from libutil) is licensed under the BSD software license.\n\n");
}
int filterArg(const char* key, char* value, short flags)
{
int test;
int length = strlen(value);
if((flags & NONEGATIVE) > 0)
{
if((atoi(value) < 0))
{
printf("argument %s received a negative number\n", key);
exit(1);
}
}
if((flags & NOZERO) > 0)
{
if((atoi(value) == 0))
{
printf("argument %s received zero. not allowed.\n", key);
exit(1);
}
}
if((flags & NEEDNUM) > 0)
{
for(test = 0; test < length; test++)
{
if(!isdigit(value[test]))
{
printf("argument %s did not receive an integer\n", key);
exit(1);
}
}
}
return 0;
}
int main(int argc, char* argv[])
{
int tok = 0;
/* Static arguments that must happen first */
for(tok = 0; tok < argc; tok++)
{
if(ARGMATCH("--help") || ARGMATCH("-h"))
{
usage(argv[0]);
exit(0);
}
if(ARGMATCH("--version") || ARGMATCH("-V"))
{
version(argv[0]);
exit(0);
}
if(ARGMATCH("--verbose") || ARGMATCH("-v"))
{
udef_verbose = true;
}
if(ARGMATCH("--verbose-high") || ARGMATCH("-vv"))
{
udef_verbose_high = true;
udef_verbose = true;
}
}
/* Dynamic arguments come second */
for(tok = 0; tok < argc; tok++)
{
if(ARGMATCH("--write-mode") || ARGMATCH("-w"))
{
ARGNULL(+1);
if(filterArg(argv[tok], argv[tok+1], NONEGATIVE|NEEDNUM) == 0)
{
ARGVALINT(udef_wmode);
}
}
if(ARGMATCH("--nuke-level") || ARGMATCH("-n"))
{
ARGNULL(+1);
if(filterArg(argv[tok-1], argv[tok+1], NONEGATIVE|NEEDNUM) == 0)
{
ARGVALINT(udef_nukelevel);
if(udef_nukelevel > NUKE_REWRITE)
udef_nukelevel = NUKE_PATTERN;
}
/* TODO: Remove this when it is implemented! */
if(udef_nukelevel == NUKE_REWRITE)
{
printf("*** Rewrite mode is not implemented, using default.\n");
udef_nukelevel = NUKE_PATTERN;
}
}
if(ARGMATCH("--passes") || ARGMATCH("-p"))
{
ARGNULL(+1);
if(filterArg(argv[tok-1], argv[tok+1], NEEDNUM) == 0)
{
ARGVALINT(udef_passes);
if(udef_passes < 1)
udef_passes = 1;
}
}
if(ARGMATCH("--disable-test"))
{
udef_testmode = false;
}
if(ARGMATCH("--block-size") || ARGMATCH("-b"))
{
ARGNULL(+1);
if(filterArg(argv[tok-1], argv[tok+1], NONEGATIVE|NOZERO|NEEDNUM) == 0)
{
ARGVALINT(udef_blocksize);
}
}
if(argv[tok+1] == NULL)
{
//usage(argv[0]);
//exit(1);
//break;
}
}
int i = 0;
int mt = 0;
version_short();
putchar('\n');
if(udef_verbose)
{
char* nlstr = {0};
switch(udef_nukelevel)
{
case NUKE_ZERO:
nlstr = "Zeroing";
break;
case NUKE_PATTERN:
nlstr = "Pattern";
break;
case NUKE_RANDOM_SLOW:
nlstr = "Slow Random";
break;
case NUKE_RANDOM_FAST:
nlstr = "Fast Random";
break;
default:
nlstr = "Unknown";
break;
}
printf("Test mode: %s\n", udef_testmode ? "ENABLED" : "DISABLED");
printf("Block size: %d\n", udef_blocksize);
printf("Wipe method: %s\n", nlstr);
printf("Num. of passes: %u\n", udef_passes);
printf("Write mode: %cSYNC\n", udef_wmode ? 'A' : 0);
}
putchar('\n');
echoList();
do
{
/* Media size */
uint64_t size;
/* Holds the media type, and number. example: ad0 */
char media[BUFSIZ];
/* Generate a device string based on the current interation*/
#ifdef __FreeBSD__
sprintf(media, "/dev/%s%d", mediaList[mt], i);
#else
sprintf(media, "/dev/%s%c", mediaList[mt], 'a' + i);
#endif
/* Set media size */
size = getSize(media);
#ifdef __FreeBSD__
/*
if((int64_t)size < 1L && strcmp(media, "/dev/ad0"));
{
sprintf(media, "/dev/%s%d", "ad", 4);
}
*/
#endif
/* We MUST use the int64_t cast. Unsigned integers cannot have a negative value.
* Otherwise this loop would never end. */
if((int64_t)size > 0L)
{ //printf("%s: %jd (%s)\n", media, (intmax_t)size, buf);
nuke(media, size);
}
else
{
/* To prevent overrunning */
if(mediaList[mt] == NULL || mediaList[mt] == '\0')
break;
/* mediaList iteration */
mt++;
/* Reset iteratior so that the next increment is zero */
i = -1;
}
i++;
} while( 1 );
/* End */
return 0;
}
|