This repository has been archived by the owner on Jan 15, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 109
/
break_jsteg.c
281 lines (225 loc) · 6.11 KB
/
break_jsteg.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
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
/*
* Copyright 2001 Niels Provos <provos@citi.umich.edu>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Niels Provos.
* 4. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <err.h>
#include <md5.h>
#include <errno.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <jpeglib.h>
#include <file.h>
#include "config.h"
#include "common.h"
#include "arc4.h"
#include "break_jsteg.h"
#ifndef MIN
#define MIN(a,b) (((a)<(b))?(a):(b))
#endif
#define JSTEGBYTES 8
#define JSTEGHEADER 64
struct jstegobj {
int skip;
u_int8_t coeff[JSTEGBYTES];
u_int8_t header[JSTEGHEADER];
};
int break_jsteg(struct jstegobj *, struct arc4_stream *);
int break_jsteg_filetest(char *filename, struct jstegobj *obj)
{
extern int noprint;
if (file_process(obj->header, sizeof(obj->header)) == 0)
return (0);
fprintf(stdout, "%s : jsteg[", filename);
noprint = 0;
file_process(obj->header, sizeof(obj->header));
noprint = 1;
fprintf(stdout, "]\n");
return (1);
}
void *
break_jsteg_read(char *filename)
{
struct jstegobj *jstegob;
int fd, size;
fd = open(filename, O_RDONLY, 0);
if (fd == -1) {
fprintf(stderr, "%s : error: %s\n",
filename, strerror(errno));
return (NULL);
}
jstegob = malloc(sizeof(struct jstegobj));
if (jstegob == NULL)
err(1, "malloc");
size = read(fd, jstegob, sizeof(*jstegob));
/* Supports old size, too */
if (size != sizeof(*jstegob) && size != sizeof(int) + JSTEGBYTES) {
close(fd);
free(jstegob);
return (NULL);
}
jstegob->skip = ntohl(jstegob->skip);
close(fd);
if (size == sizeof(*jstegob))
break_jsteg_filetest(filename, jstegob);
return (jstegob);
}
int
break_jsteg_write(char *filename, void *arg)
{
struct jstegobj *jstegob = arg;
int fd;
fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0644);
if (fd == -1)
return (-1);
jstegob->skip = htonl(jstegob->skip);
if (write(fd, jstegob, sizeof(*jstegob)) != sizeof(*jstegob)) {
close(fd);
return (-1);
}
close(fd);
return (0);
}
void
break_jsteg_destroy(void *obj)
{
free(obj);
}
void *
break_jsteg_prepare(char *filename, short *dcts, int bits)
{
struct jstegobj *jstegob;
int off, i, j, max, bytes, jsbits;
u_char *p;
short val;
jstegob = malloc(sizeof(struct jstegobj));
if (jstegob == NULL)
err(1, "malloc");
memset(jstegob, 0, sizeof(struct jstegobj));
max = sizeof(jstegob->coeff) * 8;
bytes = jsteg_size(dcts, bits, &off);
jsbits = bytes * 8;
jstegob->skip = bytes - sizeof(jstegob->coeff);
if (jsbits < max || off + jsbits > bits) {
warnx("%s: bad size in bits, %d", __FUNCTION__, bits);
return (NULL);
}
p = jstegob->coeff;
i = off + jsbits - max;
for (j = 0; j < max; j++) {
val = dcts[i++];
if (j != 0 && (j % 8) == 0)
p++;
*p = (*p << 1) | (val & 0x01);
}
/* Also safe the beginning. Can use for later matches */
p = jstegob->header;
i = off;
max = JSTEGHEADER * 8;
if (max > jsbits)
max = jsbits;
for (j = 0; j < max; j++) {
val = dcts[i++];
if (j != 0 && (j % 8) == 0)
p++;
*p = (*p << 1) | (val & 0x01);
}
break_jsteg_filetest(filename, jstegob);
return (jstegob);
}
int
crack_jsteg(char *filename, char *word, void *obj)
{
static u_char oword[57];
static int init;
static struct arc4_stream as;
struct arc4_stream tas;
struct jstegobj *jstegob = obj;
int changed = 0;
if (strcmp(word, oword)) {
strlcpy(oword, word, sizeof(oword));
changed = 1;
init = 0;
}
if (!init || changed) {
arc4_fixedkey(&as, word, strlen(word));
init = 1;
}
tas = as;
if (break_jsteg(jstegob, &tas)) {
extern int noprint;
int i;
u_int8_t header[JSTEGHEADER];
fprintf(stdout, "%s : jsteg(%s)", filename, word);
/* Check if we have a header. Try to file magic it */
for (i = 0; i < JSTEGHEADER; i++)
if (jstegob->header[i])
break;
if (i >= JSTEGHEADER)
goto out;
tas = as;
for (i = 0; i < JSTEGHEADER; i++)
header[i] = jstegob->header[i] ^ arc4_getbyte(&tas);
if (file_process(header, JSTEGHEADER) == 0)
goto out;
fprintf(stdout, "[");
noprint = 0;
file_process(header, JSTEGHEADER);
noprint = 1;
fprintf(stdout, "]");
out:
fprintf(stdout, "\n");
return (1);
}
return (0);
}
int
break_jsteg(struct jstegobj *js, struct arc4_stream *as)
{
u_char plain[JSTEGBYTES];
u_char *p;
int i;
arc4_skipbytes(as, js->skip);
p = js->coeff;
for (i = 0; i < sizeof(js->coeff); i++)
plain[i] = p[i] ^ arc4_getbyte(as);
p = plain + sizeof(plain);
if (memcmp(p - 7, "korejwa", 7) == 0)
return (1);
else if (memcmp(p - 4, "cMk", 3) == 0) {
int n = *(p - 1);
if (n == 4 || n == 5)
return (1);
}
return (0);
}