Skip to content

Instantly share code, notes, and snippets.

@nullkal
Created Jan 1, 2015
Embed
What would you like to do?
年賀CTF
#include <stdio.h>
#include <stdlib.h>
unsigned int decode_shl(unsigned int var, unsigned int bit, unsigned int key);
unsigned int decode_shr(unsigned int var, unsigned int bit, unsigned int key);
int main(int argc, char *argv[])
{
unsigned int var;
FILE *src_fp, *dest_fp;
if (argc <= 2) {
fprintf(stderr, "Usage: %s <InputFile(crypt)> <OutputFile(plain)>\n", argv[0]);
exit(1);
}
if ((src_fp = fopen(argv[1], "r")) == NULL) {
fprintf(stderr, "File open error: %s", argv[1]);
exit(1);
}
if ((dest_fp = fopen(argv[2], "w")) == NULL) {
fprintf(stderr, "File open error: %s", argv[2]);
exit(1);
}
while (fread(&var, sizeof(unsigned int), 1, src_fp)) {
var = decode_shl(var, 0x03, 0xcafebabe);
var = decode_shr(var, 0x0b, 0xffffffff);
var = decode_shr(var, 0x13, 0x725c0d30);
var = decode_shl(var, 0x05, 0xffffffff);
var = decode_shl(var, 0x0d, 0x63ae69a0);
var = decode_shr(var, 0x07, 0xdeadbeef);
fwrite(&var, sizeof(unsigned int), 1, dest_fp);
}
fclose(src_fp);
fclose(dest_fp);
return 0;
}
unsigned int decode_shl(unsigned int var, unsigned int bit, unsigned int key)
{
unsigned int i, data;
unsigned int decoded = 0;
unsigned int mask = 0;
for (i = 0; i < bit; ++i) {
mask = (mask << 1) | 0x1;
}
decoded = var & mask;
for (; i < sizeof(unsigned int) * 8; i += bit) {
data = decoded & mask;
data = (data << bit) & key;
decoded |= (var ^ data) & (mask <<= bit);
}
return decoded;
}
unsigned int decode_shr(unsigned int var, unsigned int bit, unsigned int key)
{
unsigned int i, data;
unsigned int decoded = 0;
unsigned int mask = 0;
for (i = 0; i < bit; ++i) {
mask = (mask >> 1) | 0x80000000;
}
decoded = var & mask;
for (; i < sizeof(unsigned int) * 8; i += bit) {
data = decoded & mask;
data = (data >> bit) & key;
decoded |= (var ^ data) & (mask >>= bit);
}
return decoded;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment