From d34dcbf4eabc080a9a175286a9ebe9d3ef0dd88f Mon Sep 17 00:00:00 2001 From: leonnicolas Date: Sun, 1 Mar 2020 13:34:14 +0100 Subject: [PATCH] initial commit --- Makefile | 7 +++ README.md | 5 ++ createRainbow.c | 58 +++++++++++++++++++++++ cryptwrapper.h | 26 +++++++++++ crypwrapper.c | 31 +++++++++++++ main.c | 119 ++++++++++++++++++++++++++++++++++++++++++++++++ rainbowvalue.h | 9 ++++ 7 files changed, 255 insertions(+) create mode 100644 Makefile create mode 100644 README.md create mode 100644 createRainbow.c create mode 100644 cryptwrapper.h create mode 100644 crypwrapper.c create mode 100644 main.c create mode 100644 rainbowvalue.h diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..fb6306f --- /dev/null +++ b/Makefile @@ -0,0 +1,7 @@ +all: compile + +compile: + gcc -std=c11 -o brutef main.c crypwrapper.c `libgcrypt-config --cflags --libs` + +rainbow: + gcc -std=c11 -o rainb createRainbow.c crypwrapper.c `libgcrypt-config --cflags --libs` diff --git a/README.md b/README.md new file mode 100644 index 0000000..ce73810 --- /dev/null +++ b/README.md @@ -0,0 +1,5 @@ +type make rainbow to compile rainbow table generator +./rainb file.txt will create hash values from password file file.txt and safe them in file.txt.sha256 + +make will compile only the brute force application +./brutef file.txt.sha256 somepassword will decrypt some summy data with somepassword's hash value and then try to decript it using hashes in file.txt.sha256 diff --git a/createRainbow.c b/createRainbow.c new file mode 100644 index 0000000..2627179 --- /dev/null +++ b/createRainbow.c @@ -0,0 +1,58 @@ +#include +#include +#include +#include "rainbowvalue.h" +#include "cryptwrapper.h" + + + +int main(int argc, char const *argv[]) { + if (argc != 2){ + printf("one argument expected ,argc =%i\n",argc); + return 1; + } + FILE *fptr; + fptr = fopen(argv[1],"r"); + if(fptr == NULL) + { + printf("Could not open file %s\n", argv[1]); + return 1; + } + FILE *tfptr; + char str[strlen(argv[1])+7]; + strcpy(str,argv[1]); + strcat(str,".sha256"); + tfptr = fopen(str,"wb"); + if(tfptr == NULL) + { + printf("Could not create file %s\n", str); + return 1; + } + mycryptwrapper_init(); + char line[256]; + + gcry_error_t err; + size_t mens = gcry_md_get_algo_dlen(algo); + void * digest=malloc(mens); + + //printdata(buf,5); + //printf("\n"); + struct s_rainbowvalue256 r; + while (fgets(line, sizeof(line), fptr)) { + gcry_md_hash_buffer(algo,digest,line,strlen(line)-1);// -1 so trailing \n is not used for calculation + //mycryptwrapper_print(digest, strlen(digest)); + //printf("%s", line); + strcpy(r.pw , line); + strcpy(r.hash,digest); + + fwrite(&r, sizeof(struct s_rainbowvalue256),1,tfptr); + } + + + + + + fclose(tfptr); + fclose(fptr); + return 0; +} \ No newline at end of file diff --git a/cryptwrapper.h b/cryptwrapper.h new file mode 100644 index 0000000..fdc6350 --- /dev/null +++ b/cryptwrapper.h @@ -0,0 +1,26 @@ +#include +#include +#include +#include "gcrypt.h" +#include + + + +#ifndef mycryptwrapper_ +#define mycryptwrapper_ + + +#define NEED_LIBGCRYPT_VERSION "1.6.5" //should be 1.8.5 but my libgcrypt as mismatched versions :( +#define algo GCRY_MD_SHA256 +#define cipher GCRY_CIPHER_DES + + +void mycryptwrapper_init(); + +void mycryptwrapper_print(); + + + + + +#endif \ No newline at end of file diff --git a/crypwrapper.c b/crypwrapper.c new file mode 100644 index 0000000..a05f174 --- /dev/null +++ b/crypwrapper.c @@ -0,0 +1,31 @@ +#include "cryptwrapper.h" + +void mycryptwrapper_init(){ + + /* Version check should be the very first call because it + makes sure that important subsystems are initialized. + #define NEED_LIBGCRYPT_VERSION to the minimum required version. */ + if (!gcry_check_version (NEED_LIBGCRYPT_VERSION)) + { + fprintf (stderr, "libgcrypt is too old (need %s, have %s)\n", + NEED_LIBGCRYPT_VERSION, gcry_check_version (NULL)); + exit (2); + } + + /* Disable secure memory. */ + gcry_control (GCRYCTL_DISABLE_SECMEM, 0); + + /* ... If required, other initialization goes here. */ + + /* Tell Libgcrypt that initialization has completed. */ + gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0); + +} + +void mycryptwrapper_print(const void * buf, size_t len){ + for (size_t i = 0; i< len; i++){ + printf("%02x ", ((uint8_t*)buf)[i]); + } + printf("\n"); +} + diff --git a/main.c b/main.c new file mode 100644 index 0000000..4325303 --- /dev/null +++ b/main.c @@ -0,0 +1,119 @@ +#include +#include "gcrypt.h" +#include +#include +#include +#include + + +#include +#include "cryptwrapper.h" +#include "rainbowvalue.h" + +#define NEED_LIBGCRYPT_VERSION "1.6.5" //should be 1.8.5 but my libgcrypt as mismatched versions :( +#define algo GCRY_MD_SHA256 +#define cipher GCRY_CIPHER_DES + +void printdata(const void * buf, size_t len){ + for (size_t i = 0; i< len; i++){ + printf("%x ", ((uint8_t*)buf)[i]); + } +} + +int main(int argc, char const *argv[]) { + if (argc < 2){ + printf("please select rainbow table\n"); + return 1; + } + FILE * fptr = fopen(argv[1],"rb"); + if (fptr==NULL){ + printf("could not open file %s\n",argv[1]); + return 1; + } + char* pw_clear = malloc(256); + memset(pw_clear,0,256); + if (argc==3){ + strcpy(pw_clear,argv[2]); + } + else{ + strcpy(pw_clear,"ELEONORY"); + } + const char * dummydata = "0123456789"; + mycryptwrapper_init(); + + gcry_cipher_hd_t hd; + gcry_error_t err= gcry_cipher_open(&hd,cipher,GCRY_CIPHER_MODE_CFB,0); + + size_t mens = gcry_md_get_algo_dlen(algo); + void * digest=malloc(mens); + + gcry_md_hash_buffer(algo,digest,pw_clear,strlen(pw_clear)); + printf("hash Key:"); + mycryptwrapper_print(digest,mens); + //printf("\n"); + if(gcry_cipher_setkey(hd,digest,8)){ + printf("could not set key for encryption"); + } + size_t len = 8; + void * iv = malloc(len); + err = gcry_cipher_setiv(hd, iv , len); + if (err){ + printf("could not init init vector"); + } + unsigned char * encrypted_secret = malloc(32); + + err = gcry_cipher_encrypt(hd,encrypted_secret,32,dummydata,10); + if (err){ + printf("could not encrypt"); + } + printf("encypted data:"); + mycryptwrapper_print(encrypted_secret,32); + //printf("\n"); + void * out = malloc(256);//allocate mem for decrypted buffer + struct s_rainbowvalue256 r; + while (fread(&r, sizeof(struct s_rainbowvalue256), 1,fptr)) {//reading hash values from rainbowtable + gcry_cipher_hd_t dhd; + gcry_error_t err= gcry_cipher_open(&dhd,cipher,GCRY_CIPHER_MODE_CFB,0); + if (err){ + printf("could not open handle\n"); + } + //printf("hash key: "); + //mycryptwrapper_print(r.hash,32); + err = gcry_cipher_setkey(dhd,r.hash,8); + if (err) { + printf("could not set key \n"); + } + memset(iv,0,len); + err = gcry_cipher_setiv(dhd, iv , len); + if (err){ + printf("could not init init vector"); + } + + + memset(out,0,256); + err = gcry_cipher_decrypt(dhd,out,256,encrypted_secret,strlen(encrypted_secret)); + if (err){ + printf("could not decrypt\n"); + } + //printf("encrpyted secret:"); + //mycryptwrapper_print(encrypted_secret,strlen(encrypted_secret)); + + //printf("\ndecrypted"); + //mycryptwrapper_print(out,256); + if (strcmp(out,dummydata)==0){ + printf("pw: %sfor data: %s\npwhash: ", r.pw, (char*)out); + mycryptwrapper_print(r.hash, strlen(r.hash)); + return 0; + } + gcry_cipher_close(dhd);//close cipher + } + printf("\nnothing found\n"); + + //printdata(digest,mens); + + + + return 0; +} + + diff --git a/rainbowvalue.h b/rainbowvalue.h new file mode 100644 index 0000000..1ff0573 --- /dev/null +++ b/rainbowvalue.h @@ -0,0 +1,9 @@ + +#ifndef rainbowvalue +#define rainbowvalue + struct s_rainbowvalue256 { + char pw[256]; + char hash[256]; +}; + +#endif \ No newline at end of file