Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <cstdio>
- #include <string>
- #include <sstream>
- #include <iostream>
- #include <vector>
- #include <windows.h>
- using namespace std;
- size_t GetSize(string p_sFilename)
- {
- size_t size;
- FILE* f = fopen(p_sFilename.c_str(), "rb");
- // Get file size
- fseek(f, 0, SEEK_END);
- size = ftell(f);
- rewind(f);
- fclose(f);
- return size;
- }
- bool FileExists(string p_sPath)
- {
- DWORD dwAttrib = GetFileAttributesA(p_sPath.c_str());
- return (dwAttrib != INVALID_FILE_ATTRIBUTES);
- }
- unsigned char* ReadBinaryFile(string p_sFilename, size_t *p_rSize)
- {
- unsigned char *p = NULL;
- FILE* f = NULL;
- size_t res = 0;
- if (!FileExists(p_sFilename))
- {
- cout << "Binary file does not exists!" << endl;
- return NULL;
- }
- // Get size and allocate space
- *p_rSize = GetSize(p_sFilename);
- if (*p_rSize == 0) return NULL;
- f = fopen(p_sFilename.c_str(), "rb");
- p = new unsigned char[*p_rSize];
- // Read file
- rewind(f);
- res = fread(p, sizeof(unsigned char), *p_rSize, f);
- fclose(f);
- if (res == 0)
- {
- delete[] p;
- return NULL;
- }
- return p;
- }
- void TestShellcode(string p_sFilename)
- {
- unsigned char *p = NULL;
- size_t size = 0;
- p = ReadBinaryFile(p_sFilename, &size);
- // Check if successful read
- if (size == 0 || p == NULL)
- {
- cout << "Error: Cannot read shellcode file!" << endl;
- return;
- }
- // Get space for shellcode
- void *sc = VirtualAlloc(0, size, MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE);
- if (sc == NULL)
- {
- cout << "Error: Cannot allocate space for shellcode!" << endl;
- return;
- }
- // Copy shellcode and execute it
- memcpy(sc, p, size);
- (*(int(*)()) sc)();
- }
- int main(int argc, char *argv[])
- {
- TestShellcode(argv[1]);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement