-
Notifications
You must be signed in to change notification settings - Fork 5
/
cfdecrypt.c
93 lines (77 loc) · 1.94 KB
/
cfdecrypt.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
/* CFDECRYPT: Decrypt Cold Fusion templates encrypted with CFCRYPT
Matt Chapman <[email protected]>
Usage: cfdecrypt <encrypted.cfm >decrypted.cfm
Requires a DES encryption library to compile.
*/
#include <stdio.h>
#include "des.h"
int main(void)
{
char *header = "Allaire Cold Fusion Template\012Header Size: ";
char buffer[54];
int debug = 0;
int headsize, outlen, seek_pos;
int skip_header;
int len, i;
/* Note: This IS the encryption key! It is meant to look like an error. */
char *keystr = "Error: cannot open template file--\"%s\". Please, try again!\012\012";
des_cblock key;
des_cblock input;
des_cblock output;
des_key_schedule schedule;
if ((fread(buffer, 1, 54, stdin) < 54) || (memcmp(buffer, header, 42)))
{
if(debug) fprintf(stderr, "File is not an encrypted template\n");
return 1;
}
if (!memcmp(&buffer[42], "New Version", 11))
{
if(debug) fprintf(stderr, "\nEncrypted with 'New Version'\n");
headsize = 69;
skip_header = 1;
}
else
{
if(debug) fprintf(stderr, "\nEncrypted with 'Old Version'\n");
headsize = atoi(&buffer[42]);
skip_header = 0;
}
seek_pos = fseek(stdin, headsize, SEEK_SET);
if ((headsize < 54) || seek_pos < 0)
{
if(debug){
fprintf(stderr, "Error in file format.\n Head size: %i\n",headsize);
if (headsize < 54){ fprintf(stderr, "Error: Head Size < 54");}
if (seek_pos < 0){ fprintf(stderr, "Couldn't set seek position: %i",seek_pos);}
fprintf(stderr, "\n");
}
return 1;
}
des_string_to_key(keystr, &key);
des_set_key(&key, schedule);
outlen = 0;
while ((len = fread(input, 1, 8, stdin)) == 8)
{
des_ecb_encrypt(&input, &output, schedule, 0);
outlen += 8;
i = 0;
if (skip_header)
{
while (i < 8)
{
if (output[i++] == 0x1A)
{
skip_header = 0;
break;
}
}
}
fwrite(output + i, 1, 8 - i, stdout);
}
for (i = 0; i < len; i++)
{
output[i] = input[i] ^ (outlen + i);
}
fwrite(output, 1, len, stdout);
return 0;
}