Advertisement
Guest User

Untitled

a guest
Nov 10th, 2016
530
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.76 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <cstdio>
  3. #include <string>
  4. #include <sstream>
  5. #include <iostream>
  6. #include <vector>
  7. #include <windows.h>
  8.  
  9. using namespace std;
  10.  
  11. size_t GetSize(string p_sFilename)
  12. {
  13.     size_t size;
  14.     FILE* f = fopen(p_sFilename.c_str(), "rb");
  15.  
  16.     // Get file size
  17.  
  18.     fseek(f, 0, SEEK_END);
  19.     size = ftell(f);
  20.     rewind(f);
  21.     fclose(f);
  22.  
  23.     return size;
  24. }
  25.  
  26. bool FileExists(string p_sPath)
  27. {
  28.     DWORD dwAttrib = GetFileAttributesA(p_sPath.c_str());
  29.  
  30.     return (dwAttrib != INVALID_FILE_ATTRIBUTES);
  31. }
  32.  
  33. unsigned char* ReadBinaryFile(string p_sFilename, size_t *p_rSize)
  34. {
  35.     unsigned char *p = NULL;
  36.     FILE* f = NULL;
  37.     size_t res = 0;
  38.  
  39.     if (!FileExists(p_sFilename))
  40.     {
  41.         cout << "Binary file does not exists!" << endl;
  42.         return NULL;
  43.     }
  44.  
  45.     // Get size and allocate space
  46.  
  47.     *p_rSize = GetSize(p_sFilename);
  48.     if (*p_rSize == 0) return NULL;
  49.  
  50.     f = fopen(p_sFilename.c_str(), "rb");
  51.  
  52.     p = new unsigned char[*p_rSize];
  53.  
  54.     // Read file
  55.  
  56.     rewind(f);
  57.     res = fread(p, sizeof(unsigned char), *p_rSize, f);
  58.     fclose(f);
  59.  
  60.     if (res == 0)
  61.     {
  62.         delete[] p;
  63.         return NULL;
  64.     }
  65.  
  66.     return p;
  67. }
  68.  
  69. void TestShellcode(string p_sFilename)
  70. {
  71.     unsigned char *p = NULL;
  72.     size_t size = 0;
  73.  
  74.     p = ReadBinaryFile(p_sFilename, &size);
  75.  
  76.     // Check if successful read
  77.  
  78.     if (size == 0 || p == NULL)
  79.     {
  80.         cout << "Error: Cannot read shellcode file!" << endl;
  81.         return;
  82.     }
  83.  
  84.     // Get space for shellcode
  85.  
  86.     void *sc = VirtualAlloc(0, size, MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE);
  87.  
  88.     if (sc == NULL)
  89.     {
  90.         cout << "Error: Cannot allocate space for shellcode!" << endl;
  91.         return;
  92.     }
  93.  
  94.     // Copy shellcode and execute it
  95.  
  96.     memcpy(sc, p, size);
  97.     (*(int(*)()) sc)();
  98. }
  99.  
  100.  
  101. int main(int argc, char *argv[])
  102. {
  103.     TestShellcode(argv[1]);
  104.     return 0;
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement