Skip to content

Instantly share code, notes, and snippets.

@knowlet
Created July 5, 2015 13:40
Show Gist options
  • Save knowlet/a438ae6ab6a58afccb81 to your computer and use it in GitHub Desktop.
Save knowlet/a438ae6ab6a58afccb81 to your computer and use it in GitHub Desktop.
A sample int overflow crackme.
#include <stdio.h>
#include <stdlib.h>
#define KEYMAXLEN 64
void printKey(int c)
{
char bits[4];
char *buffer;
int i = 0;
while (c != -1) {
bits[i++] = c & 0xFF;
c >>= 8;
}
FILE* stream;
stream = fopen("key.dat", "rb");
if (!stream) {
puts("not here!");
exit(1);
}
buffer = calloc(KEYMAXLEN, sizeof(char));
fread (buffer, sizeof(char), KEYMAXLEN, stream);
while (*buffer) {
if (i == 4) i = 0;
printf("%c", (*buffer - 0xFA) ^ bits[i]);
++buffer;
++i;
}
puts("");
}
int main(int argc, char const *argv[])
{
int a, b, c;
puts("今天有 n 男 m 女 去露營,總共需要幾個睡袋?");
printf("男生有幾人? ");
scanf("%d", &a);
printf("女生有幾人? ");
scanf("%d", &b);
c = a + b;
if (a < 0 || b < 0) {
puts("人數怎麼會有負數呢?");
exit(0);
}
if (c >= 0) printf("有 %d 個男生 %d 個女生,總共需要 %d 個睡袋!\n", a, b, c);
else puts("segmentation fault");
if (c == 0x80000000) {
printf("The key is ");
printKey(c);
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
// intoverflowsoeasy
FILE *stream;
char *a;
char bits[] = {0x00, 0x00, 0x00, 0x80};
int i = 0;
if (argc < 2) {
puts("Usage: keygen <key>");
return 1;
}
stream = fopen("key.dat", "wb");
a = argv[1];
while (*a) {
fputc((*a++ ^ bits[i++]) + 0xFA, stream);
if (i == 4) i = 0;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment