CTFs and solutions
This commit is contained in:
2
openssl-enc/COLORFGBG=15;0
Normal file
2
openssl-enc/COLORFGBG=15;0
Normal file
@ -0,0 +1,2 @@
|
||||
<09><><1C><><11>Z<EFBFBD>
|
||||
<13><>5X<35><58>1<>s<EFBFBD><73>b7b<>\k<>HP&<15>p<EFBFBD><70>ܫ8
|
||||
0
openssl-enc/aes128.txt
Normal file
0
openssl-enc/aes128.txt
Normal file
118
openssl-enc/code.c
Normal file
118
openssl-enc/code.c
Normal file
@ -0,0 +1,118 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <openssl/evp.h>
|
||||
#include <openssl/err.h>
|
||||
|
||||
|
||||
#define ENCRYPT 1
|
||||
#define DECRYPT 0
|
||||
#define MAX_BUFFER 1024
|
||||
|
||||
void handle_errors(){
|
||||
ERR_print_errors_fp(stderr);
|
||||
abort();
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
|
||||
// int EVP_CipherInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type, const unsigned char *key, const unsigned char *iv, int enc);
|
||||
// int EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl, const unsigned char *in, int inl);
|
||||
// int EVP_CipherFinal(EVP_CIPHER_CTX *ctx, unsigned char *outm, int *outl);
|
||||
|
||||
|
||||
|
||||
|
||||
if(argc != 5){
|
||||
fprintf(stderr,"Invalid parameters. Usage: %s file_in key iv file_out\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");
|
||||
abort();
|
||||
}
|
||||
|
||||
if(strlen(argv[2])!=64){
|
||||
fprintf(stderr,"Wrong key length\n");
|
||||
abort();
|
||||
}
|
||||
if(strlen(argv[3])!=32){
|
||||
fprintf(stderr,"Wrong IV length\n");
|
||||
abort();
|
||||
}
|
||||
|
||||
FILE *f_out;
|
||||
if((f_out = fopen(argv[4],"wb")) == NULL) {
|
||||
fprintf(stderr,"Couldn't open the output file, try again\n");
|
||||
abort();
|
||||
}
|
||||
|
||||
unsigned char key[strlen(argv[2])/2];
|
||||
for(int i = 0; i < strlen(argv[2])/2;i++){
|
||||
sscanf(&argv[2][2*i],"%2hhx", &key[i]);
|
||||
}
|
||||
|
||||
unsigned char iv[strlen(argv[3])/2];
|
||||
for(int i = 0; i < strlen(argv[3])/2;i++){
|
||||
sscanf(&argv[3][2*i],"%2hhx", &iv[i]);
|
||||
}
|
||||
|
||||
/* Load the human readable error strings for libcrypto */
|
||||
ERR_load_crypto_strings();
|
||||
/* Load all digest and cipher algorithms */
|
||||
OpenSSL_add_all_algorithms();
|
||||
|
||||
|
||||
|
||||
// pedantic mode: check NULL
|
||||
EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
|
||||
|
||||
if(!EVP_CipherInit(ctx,EVP_chacha20(), key, iv, DECRYPT))
|
||||
handle_errors();
|
||||
|
||||
int length;
|
||||
unsigned char ciphertext[MAX_BUFFER+16];
|
||||
|
||||
int n_read;
|
||||
unsigned char buffer[MAX_BUFFER];
|
||||
|
||||
while((n_read = fread(buffer,1,MAX_BUFFER,f_in)) > 0){
|
||||
printf("n_Read=%d-",n_read);
|
||||
if(!EVP_CipherUpdate(ctx,ciphertext,&length,buffer,n_read))
|
||||
handle_errors();
|
||||
printf("length=%d\n",length);
|
||||
if(fwrite(ciphertext, 1, length,f_out) < length){
|
||||
fprintf(stderr,"Error writing the output file\n");
|
||||
abort();
|
||||
}
|
||||
}
|
||||
|
||||
if(!EVP_CipherFinal_ex(ctx,ciphertext,&length))
|
||||
handle_errors();
|
||||
|
||||
printf("lenght=%d\n",length);
|
||||
|
||||
if(fwrite(ciphertext,1, length, f_out) < length){
|
||||
fprintf(stderr,"Error writing in the output file\n");
|
||||
abort();
|
||||
}
|
||||
|
||||
EVP_CIPHER_CTX_free(ctx);
|
||||
|
||||
fclose(f_in);
|
||||
fclose(f_out);
|
||||
|
||||
printf("File encrypted!\n");
|
||||
|
||||
|
||||
// completely free all the cipher data
|
||||
CRYPTO_cleanup_all_ex_data();
|
||||
/* Remove error strings */
|
||||
ERR_free_strings();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
BIN
openssl-enc/enc
Executable file
BIN
openssl-enc/enc
Executable file
Binary file not shown.
BIN
openssl-enc/enc.exe
Executable file
BIN
openssl-enc/enc.exe
Executable file
Binary file not shown.
44
openssl-enc/enc1.c
Normal file
44
openssl-enc/enc1.c
Normal file
@ -0,0 +1,44 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <openssl/evp.h>
|
||||
|
||||
|
||||
#define ENCRYPT 1
|
||||
#define DECRYPT 0
|
||||
|
||||
int main()
|
||||
{
|
||||
|
||||
|
||||
unsigned char key[] = "0123456789ABCDEF";
|
||||
unsigned char iv[] = "1111111111111111";
|
||||
|
||||
EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
|
||||
EVP_CipherInit(ctx,EVP_aes_128_cbc(), key, iv, ENCRYPT);
|
||||
//This disable the padding
|
||||
EVP_CIPHER_CTX_set_padding(ctx, 0);
|
||||
|
||||
unsigned char plaintext[] = "This is the plaintext to encrypt."; //len 33
|
||||
unsigned char ciphertext[48];
|
||||
|
||||
int update_len, final_len;
|
||||
int ciphertext_len=0;
|
||||
|
||||
EVP_CipherUpdate(ctx,ciphertext,&update_len,plaintext,strlen(plaintext));
|
||||
ciphertext_len+=update_len;
|
||||
printf("update size: %d\n",ciphertext_len);
|
||||
|
||||
EVP_CipherFinal_ex(ctx,ciphertext+ciphertext_len,&final_len);
|
||||
ciphertext_len+=final_len;
|
||||
|
||||
EVP_CIPHER_CTX_free(ctx);
|
||||
|
||||
printf("Ciphertext lenght = %d\n", ciphertext_len);
|
||||
for(int i = 0; i < ciphertext_len; i++)
|
||||
printf("%02x", ciphertext[i]);
|
||||
printf("\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
1
openssl-enc/file.enc
Normal file
1
openssl-enc/file.enc
Normal file
@ -0,0 +1 @@
|
||||
<EFBFBD>$<24>4<EFBFBD>jz|<7C>Z<EFBFBD><5A><EFBFBD>9<12><><EFBFBD><EFBFBD><EFBFBD>mi2+`<60>
|
||||
1
openssl-enc/file.txt
Normal file
1
openssl-enc/file.txt
Normal file
@ -0,0 +1 @@
|
||||
CRYPTO25{MyDecryptedString}
|
||||
BIN
openssl-enc/guess.exe
Executable file
BIN
openssl-enc/guess.exe
Executable file
Binary file not shown.
50
openssl-enc/guess.py
Normal file
50
openssl-enc/guess.py
Normal file
@ -0,0 +1,50 @@
|
||||
import os
|
||||
cipher_names = [
|
||||
"AES-128-CBC",
|
||||
"AES-192-CBC",
|
||||
"AES-256-CBC",
|
||||
"AES-128-CTR",
|
||||
"AES-192-CTR",
|
||||
"AES-256-CTR",
|
||||
"AES-128-GCM",
|
||||
"AES-192-GCM",
|
||||
"AES-256-GCM",
|
||||
"DES-CBC",
|
||||
"DES-EDE3-CBC",
|
||||
"ChaCha20-Poly1305",
|
||||
"Camellia-128-CBC",
|
||||
"Camellia-192-CBC",
|
||||
"Camellia-256-CBC",
|
||||
"Camellia-128-CTR",
|
||||
"Camellia-192-CTR",
|
||||
"Camellia-256-CTR",
|
||||
"BF-CBC", # Blowfish
|
||||
"BF-CFB", # Blowfish
|
||||
"BF-OFB", # Blowfish
|
||||
"BF-ECB", # Blowfish
|
||||
"CAST5-CBC", # CAST5
|
||||
"CAST5-CFB", # CAST5
|
||||
"CAST5-OFB", # CAST5
|
||||
"CAST5-ECB", # CAST5
|
||||
"IDEA-CBC", # IDEA
|
||||
"IDEA-CFB", # IDEA
|
||||
"IDEA-OFB", # IDEA
|
||||
"IDEA-ECB", # IDEA
|
||||
"RC2-CBC", # RC2
|
||||
"RC2-CFB", # RC2
|
||||
"RC2-OFB", # RC2
|
||||
"RC2-ECB", # RC2
|
||||
"RC4", # RC4 (stream cipher)
|
||||
"ARIA-128-CBC", #ARIA
|
||||
"ARIA-192-CBC", #ARIA
|
||||
"ARIA-256-CBC", #ARIA
|
||||
"ARIA-128-CTR", #ARIA
|
||||
"ARIA-192-CTR", #ARIA
|
||||
"ARIA-256-CTR", #ARIA
|
||||
"ARIA-128-GCM", #ARIA
|
||||
"ARIA-192-GCM", #ARIA
|
||||
"ARIA-256-GCM", #ARIA
|
||||
]
|
||||
|
||||
for i in cipher_names:
|
||||
os.system("./guess.exe guessalgo.enc "+i)
|
||||
121
openssl-enc/guessalgo.c
Normal file
121
openssl-enc/guessalgo.c
Normal file
@ -0,0 +1,121 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <openssl/evp.h>
|
||||
#include <openssl/err.h>
|
||||
|
||||
|
||||
#define ENCRYPT 1
|
||||
#define DECRYPT 0
|
||||
#define MAX_BUFFER 1024
|
||||
|
||||
void handle_errors(){
|
||||
ERR_print_errors_fp(stderr);
|
||||
abort();
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
|
||||
// int EVP_CipherInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type, const unsigned char *key, const unsigned char *iv, int enc);
|
||||
// int EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl, const unsigned char *in, int inl);
|
||||
// int EVP_CipherFinal(EVP_CIPHER_CTX *ctx, unsigned char *outm, int *outl);
|
||||
|
||||
|
||||
|
||||
|
||||
if(argc != 3){
|
||||
fprintf(stderr,"Invalid parameters. Usage: %s file_in ALGO-TO-USE\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");
|
||||
abort();
|
||||
}
|
||||
/*
|
||||
if(strlen(argv[2])!=32){
|
||||
fprintf(stderr,"Wrong key length\n");
|
||||
abort();
|
||||
}
|
||||
if(strlen(argv[3])!=32){
|
||||
fprintf(stderr,"Wrong IV length\n");
|
||||
abort();
|
||||
}
|
||||
*/
|
||||
/*FILE *f_out;
|
||||
if((f_out = fopen(argv[2],"wb")) == NULL) {
|
||||
fprintf(stderr,"Couldn't open the output file, try again\n");
|
||||
abort();
|
||||
}*/
|
||||
unsigned char key[] = "0123456789ABCDEF";
|
||||
unsigned char iv[] = "0123456789ABCDEF";
|
||||
/*unsigned char key[strlen(argv[2])/2];
|
||||
for(int i = 0; i < strlen(argv[2])/2;i++){
|
||||
sscanf(&argv[2][2*i],"%2hhx", &key[i]);
|
||||
}
|
||||
|
||||
unsigned char iv[strlen(argv[3])/2];
|
||||
for(int i = 0; i < strlen(argv[3])/2;i++){
|
||||
sscanf(&argv[3][2*i],"%2hhx", &iv[i]);
|
||||
}
|
||||
*/
|
||||
/* Load the human readable error strings for libcrypto */
|
||||
ERR_load_crypto_strings();
|
||||
/* Load all digest and cipher algorithms */
|
||||
OpenSSL_add_all_algorithms(); // deprecated since version 1.1.1
|
||||
|
||||
|
||||
|
||||
// pedantic mode: check NULL
|
||||
EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
|
||||
|
||||
if(!EVP_CipherInit(ctx,EVP_get_cipherbyname(argv[2]), key, iv, DECRYPT))
|
||||
handle_errors();
|
||||
EVP_CIPHER_CTX_set_padding(ctx,0);
|
||||
int length;
|
||||
unsigned char ciphertext[MAX_BUFFER+16];
|
||||
|
||||
int n_read;
|
||||
unsigned char buffer[MAX_BUFFER];
|
||||
|
||||
while((n_read = fread(buffer,1,MAX_BUFFER,f_in)) > 0){
|
||||
//printf("n_Read=%d-",n_read);
|
||||
if(!EVP_CipherUpdate(ctx,ciphertext,&length,buffer,n_read))
|
||||
handle_errors();
|
||||
//printf("length=%d\n",length);
|
||||
|
||||
/*if(fwrite(ciphertext, 1, length,f_out) < length){
|
||||
fprintf(stderr,"Error writing the output file\n");
|
||||
abort();
|
||||
}*/
|
||||
}
|
||||
|
||||
if(!EVP_CipherFinal_ex(ctx,ciphertext,&length))
|
||||
handle_errors();
|
||||
|
||||
//printf("lenght=%d\n",length);
|
||||
printf("ALGORITHM:%s, Output: %s\n", argv[2], ciphertext);
|
||||
/*if(fwrite(ciphertext,1, length, f_out) < length){
|
||||
fprintf(stderr,"Error writing in the output file\n");
|
||||
abort();
|
||||
}
|
||||
*/
|
||||
EVP_CIPHER_CTX_free(ctx);
|
||||
|
||||
fclose(f_in);
|
||||
// fclose(f_out);
|
||||
|
||||
//printf("File decrypted!\n");
|
||||
|
||||
|
||||
// completely free all the cipher data
|
||||
CRYPTO_cleanup_all_ex_data();
|
||||
/* Remove error strings */
|
||||
ERR_free_strings();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
1
openssl-enc/guessalgo.enc
Normal file
1
openssl-enc/guessalgo.enc
Normal file
@ -0,0 +1 @@
|
||||
e<EFBFBD>~<04>Mv<4D><76><EFBFBD>6<EFBFBD><36><EFBFBD>9"<22>h<EFBFBD><68>|<7C>/y0o<03>Q><3E>
|
||||
0
openssl-enc/output.txt
Normal file
0
openssl-enc/output.txt
Normal file
Reference in New Issue
Block a user