CTFs and solutions

This commit is contained in:
emln
2025-04-27 19:24:27 +02:00
commit aa0fe54b3b
426 changed files with 2756 additions and 0 deletions

View File

@ -0,0 +1,79 @@
#include <stdio.h>
#include <openssl/evp.h>
#include <openssl/hmac.h>
#include <openssl/err.h>
#include <string.h>
#define MAXBUF 1024
void handle_errors(){
ERR_print_errors_fp(stderr);
abort();
}
int main(int argc, char **argv){
unsigned char key[] = "deadbeefdeadbeef";
unsigned char secret[] = "this_is_my_secret";
if(argc != 2){
fprintf(stderr,"Invalid parameters. Usage: %s filename\n",argv[0]);
exit(1);
}
FILE *f_in;
if((f_in = fopen(argv[1],"r")) == NULL) {
fprintf(stderr,"Couldn't open the input file, try again\n");
exit(1);
}
//EVP_MD_CTX *EVP_MD_CTX_new(void);
//pedantic mode? Check if md == NULL
EVP_MD_CTX *hmac_ctx = EVP_MD_CTX_new();
//int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type);
// int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl);
// Returns 1 for success and 0 for failure.
EVP_PKEY *hkey;
hkey = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, key, 16);
if(!EVP_DigestSignInit(hmac_ctx, NULL, EVP_sha512(), NULL, hkey))
handle_errors();
size_t n;
unsigned char buffer[MAXBUF];
if(!EVP_DigestSignUpdate(hmac_ctx, secret, strlen(secret)))
handle_errors();
while((n = fread(buffer,1,MAXBUF,f_in)) > 0){
// Returns 1 for success and 0 for failure.
if(!EVP_DigestSignUpdate(hmac_ctx, buffer, n))
handle_errors();
}
if(!EVP_DigestSignUpdate(hmac_ctx, secret, strlen(secret)))
handle_errors();
unsigned char hmac_value[EVP_MD_size(EVP_sha512())];
size_t hmac_len = EVP_MD_size(EVP_sha512
());
//int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, unsigned char *md, unsigned size_t *s);
// EVP_DigestSignFinal(hmac_ctx, NULL, &hmac_len);
if(!EVP_DigestSignFinal(hmac_ctx, hmac_value, &hmac_len))
handle_errors();
// void EVP_MD_CTX_free(EVP_MD_CTX *ctx);
EVP_MD_CTX_free(hmac_ctx);
printf("CRYPTO25{");
for(int i = 0; i < hmac_len; i++)
printf("%02x", hmac_value[i]);
printf("}");
return 0;
}

3
openssl-dgst/file.txt Normal file
View File

@ -0,0 +1,3 @@
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

BIN
openssl-dgst/hash3 Executable file

Binary file not shown.

53
openssl-dgst/hash3.c Normal file
View File

@ -0,0 +1,53 @@
#include <stdio.h>
#include <openssl/evp.h>
#include <openssl/err.h>
#include <string.h>
#define MAXBUF 1024
int main(int argc, char **argv){
if(argc != 2){
fprintf(stderr,"Invalid parameters. Usage: %s filename\n",argv[0]);
exit(1);
}
FILE *f_in;
if((f_in = fopen(argv[1],"r")) == NULL) {
fprintf(stderr,"Couldn't open the input file, try again\n");
exit(1);
}
//EVP_MD_CTX *EVP_MD_CTX_new(void);
EVP_MD_CTX *md = EVP_MD_CTX_new();
//int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type);
// int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl);
EVP_DigestInit(md, EVP_sha256());
int n;
unsigned char buffer[MAXBUF];
while((n = fread(buffer,1,MAXBUF,f_in)) > 0){
EVP_DigestUpdate(md, buffer, n);
}
unsigned char md_value[EVP_MD_size(EVP_sha256())];
int md_len;
//int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *s);
EVP_DigestFinal_ex(md, md_value, &md_len);
// void EVP_MD_CTX_free(EVP_MD_CTX *ctx);
EVP_MD_CTX_free(md);
printf("The digest is: ");
for(int i = 0; i < md_len; i++)
printf("%02x", md_value[i]);
printf("\n");
return 0;
}

BIN
openssl-dgst/hex Executable file

Binary file not shown.

55
openssl-dgst/keyedigest.c Normal file
View File

@ -0,0 +1,55 @@
#include <stdio.h>
#include <openssl/evp.h>
#include <openssl/err.h>
#include <string.h>
#define MAXBUF 1024
int main(int argc, char **argv){
unsigned char secret[] = "this_is_my_secret";
if(argc != 2){
fprintf(stderr,"Invalid parameters. Usage: %s filename\n",argv[0]);
exit(1);
}
FILE *f_in;
if((f_in = fopen(argv[1],"r")) == NULL) {
fprintf(stderr,"Couldn't open the input file, try again\n");
exit(1);
}
//EVP_MD_CTX *EVP_MD_CTX_new(void);
EVP_MD_CTX *md = EVP_MD_CTX_new();
//int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type);
// int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl);
EVP_DigestInit(md, EVP_sha512());
int n;
unsigned char buffer[MAXBUF];
EVP_DigestUpdate(md, secret, strlen(secret));
while((n = fread(buffer,1,MAXBUF,f_in)) > 0){
EVP_DigestUpdate(md, buffer, n);
}
EVP_DigestUpdate(md, secret, strlen(secret));
unsigned char md_value[EVP_MD_size(EVP_sha512())];
int md_len;
//int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *s);
EVP_DigestFinal_ex(md, md_value, &md_len);
// void EVP_MD_CTX_free(EVP_MD_CTX *ctx);
EVP_MD_CTX_free(md);
printf("The digest is: ");
for(int i = 0; i < md_len; i++)
printf("%02x", md_value[i]);
printf("\n");
return 0;
}