-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.c
109 lines (104 loc) · 2.92 KB
/
main.c
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
#include <time.h> /* time */
#include <stdio.h> /* FILE, fopen, fwrite, fclose, fprintf */
#include <stdint.h> /* uint*_t int*_t */
#include <string.h> /* strlen */
#include <stdlib.h> /* srand, rand */
#include <sys/stat.h> /* stat */
#include <sys/types.h> /* off_t */
static uint8_t
CorruptStep(const char *filename, const off_t filesize, const char *pattern)
{
uint8_t ret = 0;
FILE* fp = fopen(filename, "w");
if (fp != NULL)
{
off_t i;
uint8_t length = (uint8_t) strlen(pattern);
if (length > 0)
{
off_t times = (filesize / length) + (filesize % length);
for (i = 0; i < times; i++)
{
fwrite(pattern, sizeof(char), length, fp);
}
}
else
{
srand((unsigned int) time(NULL));
for (i = 0; i < filesize; i++)
{
int n = rand();
fwrite(&n, sizeof(char), 1, fp);
}
}
fclose(fp);
}
else
{
ret = 1;
}
return ret;
}
static uint8_t
CorruptFile(const char *filename)
{
uint8_t ret = 0;
struct stat st;
if(stat(filename, &st) == 0)
{
if (S_ISREG(st.st_mode) != 0)
{
off_t filesize = st.st_size;
const char* steps[35] = {"", "", "", "", "\x55", "\xAA",
"\x92\x49\x24", "\x49\x24\x92",
"\x24\x92\x49", "\x00", "\x11",
"\x22", "\x33", "\x44", "\x55",
"\x66", "\x77", "\x88", "\x99",
"\xAA", "\xBB", "\xCC", "\xDD",
"\xEE", "\xFF", "\x92\x49\x24",
"\x49\x24\x92", "\x24\x92\x49",
"\x6D\xB6\xDB", "\xB6\xDB\x6D",
"\xDB\x6D\xB6", "", "", "", ""};
uint8_t i;
for (i = 0; i < 35; i++)
{
if (CorruptStep(filename, filesize, steps[i]) != 0)
{
fprintf(stderr, "corrupt: shredding of '%s' ", filename);
fprintf(stderr, "failed on step %d\n", i);
ret = 1;
break;
}
}
}
else
{
fprintf(stderr, "corrupt: '%s' is not a regular file\n", filename);
ret = 1;
}
}
else
{
fprintf(stderr, "corrupt: '%s' not found\n", filename);
ret = 1;
}
return ret;
}
int
main(int argc, char* argv[])
{
if (argc > 1)
{
int i;
for (i = 1; i < argc; i++)
{
CorruptFile(argv[i]);
}
}
else
{
fprintf(stderr, "Usage: corrupt FILES...\n");
fprintf(stderr, "Shreds files using the Gutmann method\n");
}
return 0;
}