Last active
December 27, 2015 16:19
-
-
Save pamaury/7354330 to your computer and use it in GitHub Desktop.
ATJ first stage bootloader scrambler, mostly
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <time.h> | |
#include <string.h> | |
unsigned char xor_key[512] = | |
{ | |
0x9b, 0x94, 0xf6, 0x9e, 0x2f, 0x0a, 0x40, 0xfd, 0x9a, 0x78, 0x2c, 0xdd, 0x4f, 0x96, 0x5e, 0xdd, | |
0xc9, 0x01, 0xd3, 0x9d, 0x71, 0x8f, 0xda, 0xbb, 0xc1, 0xdf, 0x5f, 0x01, 0xaa, 0x35, 0x6f, 0xdc, | |
0x33, 0x85, 0xf9, 0x10, 0x64, 0xce, 0xd2, 0xab, 0x62, 0xaa, 0xfc, 0x37, 0xdb, 0x22, 0x28, 0x34, | |
0xa4, 0xc6, 0x79, 0xc6, 0xa1, 0xfe, 0x8b, 0xaa, 0xe6, 0x9b, 0xb4, 0x6e, 0x1b, 0xb1, 0x8a, 0xe2, | |
0x25, 0xa9, 0xa5, 0xca, 0x03, 0x92, 0xa6, 0xd5, 0xf5, 0xb8, 0x77, 0xd3, 0xe4, 0x78, 0xd5, 0x22, | |
0xfe, 0x52, 0x0c, 0x69, 0xa1, 0x40, 0x02, 0x89, 0x7a, 0x46, 0x77, 0xd1, 0xee, 0x4b, 0x8b, 0x72, | |
0xb8, 0x27, 0x81, 0x32, 0xd4, 0xfd, 0xc2, 0x64, 0x9c, 0xcb, 0x25, 0x17, 0x32, 0x3f, 0x6d, 0x8e, | |
0x1d, 0xcd, 0x14, 0xf0, 0x37, 0xfe, 0x47, 0x41, 0xc6, 0x0b, 0x31, 0x91, 0xe9, 0xaa, 0x7c, 0x73, | |
0x36, 0x28, 0x15, 0xb1, 0xa1, 0xb8, 0x30, 0x3e, 0x9f, 0x0b, 0x8d, 0x6c, 0x8c, 0x20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 0x6d, 0x69, 0x90, 0xb8, 0x9a, 0x50, 0xc9, 0x7d, 0x32, 0x12, 0x29, | |
}; | |
static unsigned char *read_it(const char *filename, int *length) | |
{ | |
FILE *fp = fopen(filename, "rb"); | |
if(fp == NULL) | |
{ | |
printf("Cannot open file for reading: %m\n"); | |
return NULL; | |
} | |
fseek(fp, 0, SEEK_END); | |
*length = ftell(fp); | |
fseek(fp, 0, SEEK_SET); | |
unsigned char *data = malloc(*length); | |
fread(data, 1, *length, fp); | |
fclose(fp); | |
return data; | |
} | |
static int write_it(const char *filename, unsigned char *data, int length) | |
{ | |
FILE *fp = fopen(filename, "wb"); | |
if(fp == NULL) | |
{ | |
printf("Cannot open file for writing: %m\n"); | |
return 2; | |
} | |
fwrite(data, 1, length, fp); | |
fclose(fp); | |
return 0; | |
} | |
static void perm_it(unsigned char *data, int length) | |
{ | |
for(int i = 0; (i + 12) < length; i += 12) | |
{ | |
unsigned char perm[8]; | |
perm[0] = data[i + 1]; | |
perm[1] = data[i + 7]; | |
perm[2] = data[i + 2]; | |
perm[3] = data[i + 4]; | |
perm[4] = data[i + 0]; | |
perm[5] = data[i + 6]; | |
perm[6] = data[i + 3]; | |
perm[7] = data[i + 5]; | |
for(int j = 0; j < 8; j++) | |
data[i + j] = perm[j]; | |
} | |
} | |
static void unperm_it(unsigned char *data, int length) | |
{ | |
for(int i = 0; (i + 12) < length; i += 12) | |
{ | |
unsigned char perm[8]; | |
perm[0] = data[i + 4]; | |
perm[1] = data[i + 0]; | |
perm[2] = data[i + 2]; | |
perm[3] = data[i + 6]; | |
perm[4] = data[i + 3]; | |
perm[5] = data[i + 7]; | |
perm[6] = data[i + 5]; | |
perm[7] = data[i + 1]; | |
for(int j = 0; j < 8; j++) | |
data[i + j] = perm[j]; | |
} | |
} | |
static void xor_it(unsigned char *data, int length) | |
{ | |
for(int i = 0; i < length; i += 4) | |
{ | |
unsigned char Ekey = xor_key[xor_key[data[i]] + 256]; | |
for(int j = 1; j < 4; j++) | |
data[i + j] ^= Ekey; | |
} | |
} | |
static void unxor_it(unsigned char *data, int length) | |
{ | |
xor_it(data, length); | |
} | |
static int encrypt(const char *in_file, const char *out_file) | |
{ | |
// read file | |
int length; | |
unsigned char *data = read_it(in_file, &length); | |
if(data == NULL) | |
return 1; | |
int rounded_len = ( (length + 511) / 512 ) * 512; | |
data = realloc(data, rounded_len + 10); | |
// random padding | |
for(int i = length; i < rounded_len; i++) | |
data[i] = rand(); | |
// XOR the file | |
xor_it(data, rounded_len); | |
// apply permutation | |
perm_it(data, rounded_len); | |
// write file | |
return write_it(out_file, data, rounded_len); | |
} | |
int decrypt(const char *in_file, const char *out_file) | |
{ | |
// read file | |
int length; | |
unsigned char *data = read_it(in_file, &length); | |
if(data == NULL) | |
return 1; | |
if(length % 512) | |
printf("Warning: file size is not a multiple of 512!"); | |
// apply permutation | |
unperm_it(data, length); | |
// XOR the file | |
unxor_it(data, length); | |
// write file | |
return write_it(out_file, data, length); | |
} | |
int usage(void) | |
{ | |
printf("usage: rb_atj_crypt [-e|-d] in_file out_file\n"); | |
return 1; | |
} | |
int main(int argc, char **argv) | |
{ | |
srand(time(NULL)); | |
if(argc != 4) | |
return usage(); | |
if(strcmp(argv[1], "-e") == 0) | |
return encrypt(argv[2], argv[3]); | |
if(strcmp(argv[1], "-d") == 0) | |
return decrypt(argv[2], argv[3]); | |
printf("Unknown command '%s'\n", argv[1]); | |
return 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment