-
Notifications
You must be signed in to change notification settings - Fork 2
/
scam-o-matic.c
229 lines (204 loc) · 6.23 KB
/
scam-o-matic.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
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
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <inttypes.h>
#include <errno.h>
//#define MACOSX
/*
http://www.opensource.apple.com/source/xnu/xnu-1456.1.26/bsd/sys/disk.h
*/
#ifdef MACOSX
#include <sys/disk.h>
#define BLKGET64 DKIOCGETBLOCKCOUNT
#define BLKSECSZ DKIOCGETBLOCKSIZE
#define FLUSHCACHE DKIOCSYNCHRONIZECACHE
#else
#include <linux/fs.h>
#define BLKGET64 BLKGETSIZE64
#define BLKSECSZ BLKSSZGET
#define FLUSHCACHE BLKFLSBUF
#endif // #ifdef MACOSX
/* shamelessly ripped from linux kernel */
static int s1, s2, s3;
void prandom_reset()
{
s1 = 100;
s2 = 200;
s3 = 300;
}
uint32_t prandom32()
{
#define TAUSWORTHE(s,a,b,c,d) ((s&c)<<d) ^ (((s <<a) ^ s)>>b)
s1 = TAUSWORTHE(s1, 13, 19, 4294967294UL, 12);
s2 = TAUSWORTHE(s2, 2, 25, 4294967288UL, 4);
s3 = TAUSWORTHE(s3, 3, 11, 4294967280UL, 17);
return (s1 ^ s2 ^ s3);
}
void prand_fill_buffer(uint32_t* buffer, int size)
{
int i=size/4;
while (i--)
{
buffer[i] = prandom32();
}
}
int check_data(uint32_t* a, uint32_t* b, size_t size)
{
size_t i=0;
while (i < size/4)
{
// printf("0x%X vs 0x%X\r", a[i],b[i]); fflush(stdout);
if (a[i] != b[i])
{
printf("\n\nOoops: 0x%X vs 0x%X\n\n", a[i],b[i]);
return i;
}
i++;
}
return -1;
}
int main(int argc, char *argv[])
{
uint64_t bsize;
uint32_t blksize;
int fd;
char buf[64];
int k=4096;
char* dev;
int ret;
if (argc<2)
{
printf("Salvage a scammed device for usable space\n");
printf("Usage: %s device\n", argv[0]);
printf("(c) Necromant 2011-2012\n");
exit(1);
}
dev = argv[1];
fd = open(dev, O_RDWR|O_DSYNC);
if (fd<0)
{
perror("Failed to open device: ");
exit(1);
}
printf("!!!WARNING!!! I will now destroy all data on the device %s\n", dev);
printf("!!!WARNING!!! If you are ok with that - type OK & press enter\n");
fgets(buf, sizeof(buf), stdin);
if (strcmp("OK\n",buf)!=0)
{
printf("Not doing anything\n");
return 1;
}
printf("Rock'n'roll, then!\n");
ret = ioctl(fd, BLKGET64, &bsize);
if (ret < 0) {
printf("Error getting the block device size: %d (%m)\n", errno);
return 1;
}
ret = ioctl(fd, BLKSECSZ, &blksize);
if (ret < 0) {
printf("Error getting the block device size: %d (%m)\n", errno);
return 1;
}
printf("Device reports to be %" PRIu64 " bytes long.\n", bsize);
printf("Sectors are presumably %u bytes each.\n", blksize);
printf("!!!WARNING!!! Last chance to stop. Are you sure you want to go further?\n If so - type YES, anything else or ctrl+c either\n");
fgets(buf, sizeof(buf), stdin);
if (strcmp("YES\n",buf)!=0)
{
printf("Not doing anything\n");
return 1;
}
printf("Starting a destructive surface test\n");
prandom_reset();
int i;
uint64_t pos=0;
int scam = 0;
uint64_t limit;
const uint32_t step_size = k * blksize;
uint32_t* writer_buf = malloc(step_size);
uint32_t* reader_buf = malloc(step_size);
while (pos < bsize)
{
const uint32_t cur_step_size = (bsize - pos >= step_size) ? step_size : bsize - pos;
printf("\rTesting at position: %" PRIu64 " (%" PRIu64 "%%)\t\t", pos, pos*100/bsize);
fflush(stdout);
prand_fill_buffer(writer_buf, cur_step_size);
pwrite(fd, writer_buf, cur_step_size, pos);
//fsync(fd);
ioctl(fd, FLUSHCACHE);
pread(fd, reader_buf, cur_step_size, pos);
i = check_data(reader_buf, writer_buf, cur_step_size);
if (i >= 0)
{
printf("\r\nMismatch at %" PRIu64 " detected\n",pos+i*4);
scam=1;
break;
}
pos += cur_step_size;
//write(fd, writer_buf, blksize);
}
if (scam)
{
printf("Sorry, dude, but it look like you've been scammed.\n");
printf("Or you might just have a old'n'corrupt card.\n");
printf("In case of scam you still have %" PRIu64 " usable bytes\n",pos+(i-1)*4);
printf("That we can salvage. Let me double check the area for overwrites\n");
limit = pos+(i-1)*4;
pos =0;
prandom_reset();
while (pos<limit)
{
printf("\rDouble checking at position: %" PRIu64 "/%" PRIu64 " (%" PRIu64 "%%)\t\t", pos, limit, pos*100/bsize);
fflush(stdout);
prand_fill_buffer(writer_buf, step_size);
pread(fd, reader_buf, step_size, pos);
i = check_data(reader_buf, writer_buf, step_size);
if (i >= 0 && (pos+i*4 < limit))
{
printf("\r\nMismatch at %" PRIu64 " detected\n",pos+i*4);
scam=2;
break;
}
pos += step_size;
}
if (scam==2)
{
printf("Results somewhat unreliable, think for yourself\n");
printf("You may run the whole thing for another loop\n");
printf("See if it gets better.");
}else
{
printf("The region looks fine. That's %d%% of reported capacity.\n", (int)(limit/bsize*100));
}
} else
{
printf("Card looks fine - have fun\n");
}
printf("Clearing first sector...\n");
bzero(writer_buf, step_size);
pwrite(fd, writer_buf, 512, 0);
if (scam==1)
{
printf("Would you like me to run fdisk and\n");
printf("Automagically create a partition, that will use only\n");
printf("the really avaliable space? (YEP/NOPE)");
fgets(buf, sizeof(buf), stdin);
if (strcmp(buf,"YEP\n")==0)
{
int sectcount = limit/blksize-2048;
printf("Creating partition with %d sectors. Command line below\n",sectcount);
char fdiskbuf[1024];
sprintf(fdiskbuf,"(echo n; echo p; echo 1; echo 2048; echo +%d; echo w)|fdisk %s\n",sectcount,dev);
printf(fdiskbuf);
system(fdiskbuf);
printf("I've done all I could. Good Bye.\n");
printf("Some rights reserved. (c) Necromant 2011\n");
}
}
return 0;
}