├── .gitignore ├── RunShellcode.cpp ├── RunShellcode.sln ├── RunShellcode.vcxproj ├── stdafx.cpp ├── stdafx.h └── targetver.h /.gitignore: -------------------------------------------------------------------------------- 1 | RunShellcode.vcxproj.filters 2 | RunShellcode.vcxproj.user 3 | ReadMe.txt 4 | .vs/ 5 | Debug/ 6 | -------------------------------------------------------------------------------- /RunShellcode.cpp: -------------------------------------------------------------------------------- 1 | // InjectShellcode.cpp : Defines the entry point for the console application. 2 | // 3 | 4 | #include "stdafx.h" 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | #define BUFFERSIZE 1024 11 | 12 | void display_error(LPTSTR lpszFunction) 13 | { 14 | LPVOID lp_message_buffer; 15 | LPVOID lp_display_buffer; 16 | DWORD dw = GetLastError(); 17 | 18 | FormatMessage( 19 | FORMAT_MESSAGE_ALLOCATE_BUFFER | 20 | FORMAT_MESSAGE_FROM_SYSTEM | 21 | FORMAT_MESSAGE_IGNORE_INSERTS, 22 | NULL, 23 | dw, 24 | MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), 25 | (LPTSTR)&lp_message_buffer, 26 | 0, 27 | NULL); 28 | 29 | lp_display_buffer = 30 | (LPVOID)LocalAlloc(LMEM_ZEROINIT, 31 | (lstrlen((LPCTSTR)lp_message_buffer) 32 | + lstrlen((LPCTSTR)lpszFunction) 33 | + 40) // account for format string 34 | * sizeof(TCHAR)); 35 | 36 | if (FAILED(StringCchPrintf((LPTSTR)lp_display_buffer, 37 | LocalSize(lp_display_buffer) / sizeof(TCHAR), 38 | TEXT("%s failed with error code %d as follows:\n%s"), 39 | lpszFunction, 40 | dw, 41 | lp_message_buffer))) 42 | { 43 | printf("FATAL ERROR: Unable to output error code.\n"); 44 | } 45 | 46 | _tprintf(TEXT("ERROR: %s\n"), (LPCTSTR)lp_display_buffer); 47 | 48 | LocalFree(lp_message_buffer); 49 | LocalFree(lp_display_buffer); 50 | } 51 | 52 | int wmain(int argc, WCHAR *argv[]) 53 | { 54 | if (argc < 2) 55 | { 56 | printf("Usage: %s ", argv[0]); 57 | exit(0); 58 | } 59 | 60 | printf("Opening %S\n", argv[1]); 61 | HANDLE file_handle = CreateFile( 62 | argv[1], // file to open 63 | GENERIC_READ, // open for reading 64 | FILE_SHARE_READ, // share for reading 65 | NULL, // default security 66 | OPEN_EXISTING, // existing file only 67 | FILE_ATTRIBUTE_NORMAL, // normal file 68 | NULL); // no attr. template 69 | 70 | if (file_handle == INVALID_HANDLE_VALUE) 71 | { 72 | display_error(TEXT("CreateFile")); 73 | _tprintf(TEXT("Terminal failure: unable to open file \"%s\" for read.\n"), argv[1]); 74 | return -1; 75 | } 76 | 77 | LARGE_INTEGER file_size; 78 | GetFileSizeEx(file_handle, &file_size); 79 | 80 | DWORD shellcodeSize = file_size.LowPart; 81 | 82 | BYTE *p_shellcode_buffer = reinterpret_cast(VirtualAlloc(NULL, shellcodeSize, MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE)); 83 | 84 | if (!p_shellcode_buffer) 85 | { 86 | printf("Failed to allocate %d\n", shellcodeSize); 87 | return -1; 88 | } 89 | 90 | printf("Allocated %d bytes at @%p\n", shellcodeSize, p_shellcode_buffer); 91 | 92 | DWORD read_bytes; 93 | DWORD offset = 0; 94 | char ReadBuffer[BUFFERSIZE] = { 0 }; 95 | while (ReadFile(file_handle, ReadBuffer, BUFFERSIZE - 1, &read_bytes, NULL) == TRUE && read_bytes>0) 96 | { 97 | memcpy(p_shellcode_buffer + offset, reinterpret_cast(ReadBuffer), read_bytes); 98 | offset += read_bytes; 99 | } 100 | 101 | printf("Read %d bytes\n", offset); 102 | int(*shellcode_ptr)() = (int(__cdecl *)(void))p_shellcode_buffer; 103 | 104 | printf("Calling function pointer: %p\n", shellcode_ptr); 105 | 106 | _asm { 107 | int 3; 108 | } 109 | shellcode_ptr(); 110 | return 0; 111 | } 112 | -------------------------------------------------------------------------------- /RunShellcode.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 16 4 | VisualStudioVersion = 16.0.29318.209 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "RunShellcode", "RunShellcode.vcxproj", "{23D97E8B-F10D-47F3-9730-53638E25A501}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|x64 = Debug|x64 11 | Debug|x86 = Debug|x86 12 | Release|x64 = Release|x64 13 | Release|x86 = Release|x86 14 | EndGlobalSection 15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 16 | {23D97E8B-F10D-47F3-9730-53638E25A501}.Debug|x64.ActiveCfg = Debug|x64 17 | {23D97E8B-F10D-47F3-9730-53638E25A501}.Debug|x64.Build.0 = Debug|x64 18 | {23D97E8B-F10D-47F3-9730-53638E25A501}.Debug|x86.ActiveCfg = Debug|Win32 19 | {23D97E8B-F10D-47F3-9730-53638E25A501}.Debug|x86.Build.0 = Debug|Win32 20 | {23D97E8B-F10D-47F3-9730-53638E25A501}.Release|x64.ActiveCfg = Release|x64 21 | {23D97E8B-F10D-47F3-9730-53638E25A501}.Release|x64.Build.0 = Release|x64 22 | {23D97E8B-F10D-47F3-9730-53638E25A501}.Release|x86.ActiveCfg = Release|Win32 23 | {23D97E8B-F10D-47F3-9730-53638E25A501}.Release|x86.Build.0 = Release|Win32 24 | EndGlobalSection 25 | GlobalSection(SolutionProperties) = preSolution 26 | HideSolutionNode = FALSE 27 | EndGlobalSection 28 | GlobalSection(ExtensibilityGlobals) = postSolution 29 | SolutionGuid = {052CD4D6-7749-47F0-864C-CF20F1A03208} 30 | EndGlobalSection 31 | EndGlobal 32 | -------------------------------------------------------------------------------- /RunShellcode.vcxproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | Debug 14 | x64 15 | 16 | 17 | Release 18 | x64 19 | 20 | 21 | 22 | {23D97E8B-F10D-47F3-9730-53638E25A501} 23 | Win32Proj 24 | RunShellcode 25 | 10.0 26 | 27 | 28 | 29 | Application 30 | true 31 | v142 32 | Unicode 33 | 34 | 35 | Application 36 | false 37 | v142 38 | true 39 | Unicode 40 | 41 | 42 | Application 43 | true 44 | v142 45 | Unicode 46 | 47 | 48 | Application 49 | false 50 | v142 51 | true 52 | Unicode 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | true 74 | 75 | 76 | true 77 | 78 | 79 | false 80 | 81 | 82 | false 83 | 84 | 85 | 86 | Use 87 | Level3 88 | Disabled 89 | WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) 90 | MultiThreaded 91 | 92 | 93 | Console 94 | true 95 | 96 | 97 | 98 | 99 | Use 100 | Level3 101 | Disabled 102 | _DEBUG;_CONSOLE;%(PreprocessorDefinitions) 103 | 104 | 105 | Console 106 | true 107 | 108 | 109 | 110 | 111 | Level3 112 | Use 113 | MaxSpeed 114 | true 115 | true 116 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 117 | MultiThreaded 118 | 119 | 120 | Console 121 | true 122 | true 123 | true 124 | 125 | 126 | 127 | 128 | Level3 129 | Use 130 | MaxSpeed 131 | true 132 | true 133 | NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 134 | 135 | 136 | Console 137 | true 138 | true 139 | true 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | Create 153 | Create 154 | Create 155 | Create 156 | 157 | 158 | 159 | 160 | 161 | -------------------------------------------------------------------------------- /stdafx.cpp: -------------------------------------------------------------------------------- 1 | // stdafx.cpp : source file that includes just the standard includes 2 | // RunShellcode.pch will be the pre-compiled header 3 | // stdafx.obj will contain the pre-compiled type information 4 | 5 | #include "stdafx.h" 6 | 7 | // TODO: reference any additional headers you need in STDAFX.H 8 | // and not in this file 9 | -------------------------------------------------------------------------------- /stdafx.h: -------------------------------------------------------------------------------- 1 | // stdafx.h : include file for standard system include files, 2 | // or project specific include files that are used frequently, but 3 | // are changed infrequently 4 | // 5 | 6 | #pragma once 7 | 8 | #include "targetver.h" 9 | 10 | #include 11 | #include 12 | 13 | 14 | 15 | // TODO: reference additional headers your program requires here 16 | -------------------------------------------------------------------------------- /targetver.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | // Including SDKDDKVer.h defines the highest available Windows platform. 4 | 5 | // If you wish to build your application for a previous Windows platform, include WinSDKVer.h and 6 | // set the _WIN32_WINNT macro to the platform you wish to support before including SDKDDKVer.h. 7 | 8 | #include 9 | --------------------------------------------------------------------------------