-
Notifications
You must be signed in to change notification settings - Fork 6
/
sonicwall-decrypter.c
80 lines (62 loc) · 2 KB
/
sonicwall-decrypter.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
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#define LUKS_NAME_SIZE 32
#define LUKS_UUID_SIZE 40
#define NUM_KEYSLOTS 8
typedef struct {
char data[48];
} LUKS_KEYSLOT;
typedef struct {
char magic[6]; // offset 0
uint16_t version; // offset 6
char cipherName[LUKS_NAME_SIZE]; // offset 8
char cipherMode[LUKS_NAME_SIZE]; // offset 40
char hashSpec[LUKS_NAME_SIZE]; // offset 72
uint32_t payloadOffset; // offset 104
uint32_t numKeyBytes; // offset 108
char mkDigest[20]; // offset 112
char mkDigestSalt[32]; // offset 132
uint32_t mkDigestIter; // offset 164
char partitionUUID[LUKS_UUID_SIZE]; // offset 168
LUKS_KEYSLOT keyslots[NUM_KEYSLOTS]; // offset 208
} LUKS_HEADER;
typedef struct {
char Value[52];
} LUKS_DECRYPTION_KEY;
LUKS_DECRYPTION_KEY CalculateDecryptionKey(LUKS_HEADER header)
{
LUKS_DECRYPTION_KEY decryptionKey;
char generatedKey[52];
char digestAndDigestSalt[52];
memcpy(&digestAndDigestSalt, header.mkDigest, 52);
for (uint32_t i = 0; i < 52; i++) {
char xoredValue = digestAndDigestSalt[i] ^ digestAndDigestSalt[sizeof(digestAndDigestSalt) - i - 1];
if((uint32_t)xoredValue < 0x20) {
xoredValue = xoredValue | 0x20;
}
decryptionKey.Value[i] = xoredValue;
}
return decryptionKey;
}
int main() {
LUKS_HEADER header;
LUKS_DECRYPTION_KEY generatedKey;
FILE *file = fopen("luks-header.bin", "rb");
if (file == NULL) {
perror("Error opening file");
return 1;
}
size_t header_size = sizeof(LUKS_HEADER);
size_t elements_read = fread(&header, header_size, 1, file);
if (elements_read != 1) {
perror("Error reading file");
fclose(file);
return 2;
}
generatedKey = CalculateDecryptionKey(header);
fclose(file);
FILE *outputFile = stdout;
fwrite(generatedKey.Value, sizeof(unsigned char), sizeof(generatedKey)/sizeof(unsigned char), outputFile);
return 0;
}