Skip to content

Instantly share code, notes, and snippets.

@ElliotJH
Created January 23, 2014 19:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ElliotJH/8585138 to your computer and use it in GitHub Desktop.
Save ElliotJH/8585138 to your computer and use it in GitHub Desktop.
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <sys/time.h>
#include <stdlib.h>
#include <openssl/sha.h>
#include <string.h>
#include <stdbool.h>
int main(int argc, const char * argv[])
{
char *parent = (char *)argv[1];
char *tree = (char *)argv[2];
char *difficulty = (char *)argv[3];
int counter = 0;
bool lower = false;
char hash_string[SHA_DIGEST_LENGTH*2];
chdir(argv[4]);
int difficulty_length = strlen(difficulty);
struct timeval time;
gettimeofday(&time, NULL);
char *commit = malloc(sizeof(char) * 1024);
char *blob = malloc(sizeof(char) * 1024);
while (lower == false) {
// sprintf(commit, "tree %s\nparent %s\nauthor CTF user <me@example.com> %li +0000\ncommitter CTF user <me@example.com> %li +0000\n\nGive me a Gitcoin! %i", tree, parent, time.tv_sec, time.tv_sec, counter);
commit = "tree 2e26d427ff8d82f764cec06065edd19689f9bdd8\nparent c356dd2aa0e21938d1e2ea0ac78f6acb2c4c6a4a\nauthor Dan Palmer <dan.palmer@me.com> 1390002604 +0000\ncommitter Dan Palmer <dan.palmer@me.com> 1390002604 +0000\n\nBasic hierarchy layout";
//commit2 = "tree f7206ae5eb29fde9ec130a50ac8031b055d6db13\nparent 0000002c6c1bd3bd95cc31c6c419f4954eb3abac\nauthor Stripe CTF <ctf@stripe.com> 1390494623 +0000\ncommitter Stripe CTF <ctf@stripe.com> 1390494623 +0000\n\nMined a Gitcoin!\nnonce 00123bea";
sprintf(blob, "commit %dX%s", strlen(commit), commit);
size_t length = strlen(blob);
blob[10] = '\0';
unsigned char hash[SHA_DIGEST_LENGTH];
SHA1(blob, length, hash);
int i = 0;
for (i=0; i < SHA_DIGEST_LENGTH; i++) {
sprintf((char*)&(hash_string[i*2]), "%02x", hash[i]);
}
lower = strncmp(hash_string, difficulty, difficulty_length) < 0;
lower = true;
if (lower == true) {
char temp[512];
sprintf(temp, "echo \"%s\" > dfile && git hash-object -t commit --no-filters dfile", commit);
printf("Expected Hash:\n");
printf("%s\n", hash_string);
printf("Actual Hash:\n");
int output = system(temp);
printf("\nExit code: %i\n", output);
printf("%s", commit);
}
counter++;
if (counter % 10000 == 0) {
fprintf(stdout, "%i\n", counter);
gettimeofday(&time, NULL);
}
}
//free(commit);
//free(blob);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment