-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsb.c
84 lines (61 loc) · 1.47 KB
/
sb.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
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <getopt.h>
#define SB_OFFSET 1024
#define SB_SIZE 1020
#define M_OFFSET 0x38
int main(int argc, char **argv)
{
char corrupt[2] = {0x13, 0x37};
char *dev = NULL;
int c;
while((c = getopt(argc, argv, "d:")) != -1)
{
switch(c) {
case 'd':
dev = optarg;
break;
case '?':
if(optopt == 'c')
fprintf(stderr, "-c requires an argument ..\n");
break;
}
}
if(dev == NULL) {
fprintf(stderr, "You must specify a device to open (sb -d /dev/device) ..\n");
return -1;
}
char sb_bytes[SB_SIZE];
int fd = open(dev, O_RDWR);
if(fd < 0) {
fprintf(stderr, "Error opening device %s\n", dev);
return -1;
}
lseek(fd, SB_OFFSET, SEEK_SET);
read(fd, &sb_bytes, (SB_SIZE));
int x;
if(sb_bytes[M_OFFSET] == 0x53 && sb_bytes[M_OFFSET+1] == 0xEF) {
fprintf(stdout, "Found magic number at offset: %u-%u\n", M_OFFSET, (M_OFFSET+1));
} else {
fprintf(stderr, "%s is corrupt (bad magic number)\n", dev);
goto clean;
}
fprintf(stdout, "Do you want to corrupt the superblock of %s? (Y/n)\n", dev);
char option[8];
scanf("%s", option);
if(strstr(option, "Y") != NULL || strstr(option, "y") != NULL) {
lseek(fd, (SB_OFFSET + M_OFFSET), SEEK_SET);
int ret = write(fd, &corrupt, 2);
if(ret < 0) {
fprintf(stderr, "Failed to write to %s: %s\n", dev, strerror(errno));
goto clean;
}
}
clean:
close(fd);
fsync(fd);
return 0;
}