Skip to content

Commit b86e2bc

Browse files
committed
Initial commit
0 parents  commit b86e2bc

File tree

11 files changed

+879
-0
lines changed

11 files changed

+879
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
build/
2+
.cache/
3+
.vscode/

CMakeLists.txt

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
cmake_minimum_required(VERSION 3.15)
2+
3+
project(shellcoder C CXX RC)
4+
5+
set(CMAKE_CXX_STANDARD 20)
6+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
7+
set(CMAKE_CXX_FLAGS "/EHsc /MD")
8+
9+
## Sorry all you folks, not making a fancy cmake. for LLVM and capstone
10+
## Go ahead and pass your -DLLVM_CLANG_DIR and those in, or override it here
11+
if (NOT DEFINED ${LLVM_CLANG_DIR})
12+
set(LLVM_CLANG_DIR "Z:\\Libraries\\llvm-project\\build")
13+
endif()
14+
15+
if (NOT DEFINED ${CAPSTONE_INCLUDE_DIR})
16+
set(CAPSTONE_INCLUDE_DIR "Z:\\Libraries\\capstone\\include")
17+
endif()
18+
19+
if (NOT DEFINED ${CAPSTONE_LIB_DIR})
20+
set(CAPSTONE_LIB_DIR "Z:\\Libraries\\capstone\\build")
21+
endif()
22+
23+
set(CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH} ${LLVM_CLANG_DIR})
24+
25+
find_package(LLVM REQUIRED CONFIG)
26+
find_package(CLANG REQUIRED CONFIG)
27+
message(STATUS "Using LLVM ${LLVM_PACKAGE_VERSION}")
28+
message(STATUS "LLVM include directory: ${LLVM_INCLUDE_DIRS}")
29+
message(STATUS "Clang include directory: ${CLANG_INCLUDE_DIRS}")
30+
31+
include(FetchContent)
32+
message(STATUS "Checking if ImGui repo needs to be pulled")
33+
FetchContent_Declare(
34+
imgui
35+
GIT_REPOSITORY "https://github.com/ocornut/imgui"
36+
GIT_TAG "v1.89.9"
37+
)
38+
39+
FetchContent_MakeAvailable(imgui)
40+
41+
message(STATUS "ImGui source directory: ${imgui_SOURCE_DIR}")
42+
43+
include_directories(include resources ${LLVM_INCLUDE_DIRS} ${CLANG_INCLUDE_DIRS} ${CAPSTONE_INCLUDE_DIR}
44+
${imgui_SOURCE_DIR} ${imgui_SOURCE_DIR}/backends ${imgui_SOURCE_DIR}/misc/cpp)
45+
46+
link_directories(${LLVM_CLANG_BUILD_DIR}/lib ${CAPSTONE_LIB_DIR})
47+
48+
add_definitions(${LLVM_DEFINITIONS} -DNOMINMAX)
49+
50+
set(LLVM_LIBS LLVMExecutionEngine LLVMSupport LLVMTarget LLVMBitWriter
51+
LLVMMCJIT LLVMX86CodeGen LLVMPasses LLVMAsmParser LLVMX86AsmParser)
52+
53+
set(CLANG_LIBS clangFrontend clangCodeGen)
54+
55+
set(SHELLCODER_SOURCES
56+
src/main.cpp
57+
src/code_compiler.cpp)
58+
59+
set(IMGUI_SOURCES
60+
${imgui_SOURCE_DIR}/imgui.cpp
61+
${imgui_SOURCE_DIR}/imgui_draw.cpp
62+
${imgui_SOURCE_DIR}/imgui_tables.cpp
63+
${imgui_SOURCE_DIR}/imgui_widgets.cpp
64+
${imgui_SOURCE_DIR}/misc/cpp/imgui_stdlib.cpp
65+
${imgui_SOURCE_DIR}/backends/imgui_impl_dx11.cpp
66+
${imgui_SOURCE_DIR}/backends/imgui_impl_win32.cpp)
67+
68+
add_executable(${PROJECT_NAME} WIN32 ${SHELLCODER_SOURCES} ${IMGUI_SOURCES})
69+
target_link_libraries(${PROJECT_NAME} PRIVATE ${LLVM_LIBS} ${CLANG_LIBS} capstone d3d11)
70+
target_sources(${PROJECT_NAME} PRIVATE shellcoder.rc)

README.md

1.22 KB

shellcoder

C++ Windows application with LLVM & Clang embedded to perform compilation of code as it's typed. Supports C or C++ with common compiler flags (whatever clang normally has).

Currently configured to compile for x86, but other support is possible with the right includes.

Why this?

I was tired of the methods used to produce shellcode that I injected into remote processes for certain things. Because of that I wanted a 1 stop shop where I can type/paste the code that I wanted to inject, and get the byte array immediately. The results are already byte formatted, with comments showing the assembly.

include/code_compiler.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#pragma once
2+
#include <vector>
3+
#include <string>
4+
5+
#include "llvm_precomp.h"
6+
7+
bool generate_shellcode(std::string contents, std::vector<std::string> args = {});

include/directx.hpp

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
#pragma once
2+
#include <d3d11.h>
3+
//#include "DirectX/WICTextureLoader.h"
4+
//#include "resource.h"
5+
6+
class directx {
7+
private:
8+
ID3D11Device* g_pd3dDevice;
9+
ID3D11DeviceContext* g_pd3dDeviceContext;
10+
IDXGISwapChain* g_pSwapChain;
11+
ID3D11RenderTargetView* g_mainRenderTargetView;
12+
bool m_DPIScaleSet;
13+
float m_DPIScale;
14+
// ID3D11ShaderResourceView* m_icon;
15+
/* inline void LoadDXImage(int32_t file) {
16+
auto rc = FindResource(NULL, MAKEINTRESOURCE(file), "PNG");
17+
if (!rc)
18+
return;
19+
auto rcdata = LoadResource(NULL, rc);
20+
auto size = SizeofResource(NULL, rc);
21+
if (!rcdata)
22+
return;
23+
const uint8_t* data = static_cast<const uint8_t*>(LockResource(rcdata));
24+
DirectX::CreateWICTextureFromMemory(g_pd3dDevice, data, size, nullptr, &m_icon);
25+
}*/
26+
public:
27+
directx() :
28+
g_pd3dDevice(nullptr),
29+
g_pd3dDeviceContext(nullptr),
30+
g_pSwapChain(nullptr),
31+
g_mainRenderTargetView(nullptr),
32+
m_DPIScaleSet(false),
33+
m_DPIScale(1.f)
34+
{
35+
}
36+
~directx() {
37+
CleanupDeviceD3D();
38+
}
39+
inline ID3D11Device* GetDevice() {
40+
return g_pd3dDevice;
41+
}
42+
inline IDXGISwapChain* GetSwapChain() {
43+
return g_pSwapChain;
44+
}
45+
inline ID3D11DeviceContext* GetDeviceContext() {
46+
return g_pd3dDeviceContext;
47+
}
48+
inline ID3D11RenderTargetView* GetRenderTargetView() {
49+
return g_mainRenderTargetView;
50+
}
51+
inline float GetDPIScale() {
52+
if (!m_DPIScaleSet) {
53+
auto hDC = GetDC(NULL);
54+
auto dpix = GetDeviceCaps(hDC, LOGPIXELSX);
55+
ReleaseDC(NULL, hDC);
56+
auto DPI = MulDiv(100, dpix, 96);
57+
m_DPIScale = DPI / 100.f;
58+
m_DPIScaleSet = true;
59+
}
60+
return m_DPIScale;
61+
}
62+
inline bool CreateDeviceD3D(HWND hWnd) {
63+
DXGI_SWAP_CHAIN_DESC sd;
64+
ZeroMemory(&sd, sizeof(sd));
65+
sd.BufferCount = 2;
66+
sd.BufferDesc.Width = 0;
67+
sd.BufferDesc.Height = 0;
68+
sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
69+
sd.BufferDesc.RefreshRate.Numerator = 60;
70+
sd.BufferDesc.RefreshRate.Denominator = 1;
71+
sd.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;
72+
sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
73+
sd.OutputWindow = hWnd;
74+
sd.SampleDesc.Count = 1;
75+
sd.SampleDesc.Quality = 0;
76+
sd.Windowed = TRUE;
77+
sd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
78+
79+
UINT createDeviceFlags = 0;
80+
D3D_FEATURE_LEVEL featureLevel;
81+
const D3D_FEATURE_LEVEL featureLevelArray[3] = { D3D_FEATURE_LEVEL_10_0, D3D_FEATURE_LEVEL_11_0, D3D_FEATURE_LEVEL_10_0 };
82+
if (D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, createDeviceFlags, featureLevelArray, 3, D3D11_SDK_VERSION, &sd, &g_pSwapChain, &g_pd3dDevice, &featureLevel, &g_pd3dDeviceContext) != S_OK)
83+
return false;
84+
85+
CreateRenderTarget();
86+
return true;
87+
}
88+
inline void CleanupDeviceD3D() {
89+
CleanupRenderTarget();
90+
if (g_pSwapChain) {
91+
g_pSwapChain->Release();
92+
g_pSwapChain = NULL;
93+
}
94+
if (g_pd3dDeviceContext) {
95+
g_pd3dDeviceContext->Release();
96+
g_pd3dDeviceContext = NULL;
97+
}
98+
if (g_pd3dDevice) {
99+
g_pd3dDevice->Release();
100+
g_pd3dDevice = NULL;
101+
}
102+
}
103+
inline void CreateRenderTarget() {
104+
ID3D11Texture2D* pBackBuffer;
105+
g_pSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), reinterpret_cast<LPVOID*>(&pBackBuffer));
106+
if (pBackBuffer) {
107+
g_pd3dDevice->CreateRenderTargetView(pBackBuffer, NULL, &g_mainRenderTargetView);
108+
pBackBuffer->Release();
109+
}
110+
}
111+
inline void CleanupRenderTarget() {
112+
if (g_mainRenderTargetView) {
113+
g_mainRenderTargetView->Release();
114+
g_mainRenderTargetView = NULL;
115+
}
116+
}
117+
};

include/llvm_precomp.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#pragma once
2+
#pragma warning(push)
3+
#pragma warning(disable: 4244)
4+
#pragma warning(disable: 4624)
5+
#pragma warning(disable: 4141)
6+
#pragma warning(disable: 4291)
7+
#include <llvm/InitializePasses.h>
8+
#include <llvm/ExecutionEngine/ExecutionEngine.h>
9+
#include <llvm/ExecutionEngine/MCJIT.h>
10+
#include <llvm/ExecutionEngine/JITEventListener.h>
11+
#include <llvm/ExecutionEngine/SectionMemoryManager.h>
12+
#include <llvm/Passes/PassBuilder.h>
13+
#include <llvm/Support/TargetSelect.h>
14+
#include <llvm/Support/Registry.h>
15+
#include <llvm/Support/Host.h>
16+
#include <llvm/IR/ValueSymbolTable.h>
17+
18+
#include <clang/Basic/DiagnosticOptions.h>
19+
#include <clang/Basic/Diagnostic.h>
20+
#include <clang/CodeGen/CodeGenAction.h>
21+
#include <clang/Frontend/CompilerInstance.h>
22+
#include <clang/Frontend/CompilerInvocation.h>
23+
#include <clang/Frontend/TextDiagnosticPrinter.h>
24+
#pragma warning(pop)

include/resource.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#pragma once
2+
3+
#define IDI_ICON1 101
4+
5+
#ifdef APSTUDIO_INVOKED
6+
#ifndef APSTUDIO_READONLY_SYMBOLS
7+
#define _APS_NEXT_RESOURCE_VALUE 104
8+
#define _APS_NEXT_COMMAND_VALUE 40001
9+
#define _APS_NEXT_CONTROL_VALUE 1001
10+
#define _APS_NEXT_SYMED_VALUE 101
11+
#endif
12+
#endif

resources/Err.ico

66.1 KB
Binary file not shown.

shellcoder.rc

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include "resource.h"
2+
3+
#define APSTUDIO_READONLY_SYMBOLS
4+
#include "winres.h"
5+
#undef APSTUDIO_READONLY_SYMBOLS
6+
7+
#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)
8+
LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US
9+
#pragma code_page(1252)
10+
11+
#ifdef APSTUDIO_INVOKED
12+
13+
1 TEXTINCLUDE
14+
BEGIN
15+
"resource.h\0"
16+
END
17+
18+
2 TEXTINCLUDE
19+
BEGIN
20+
"#include ""winres.h""\r\n"
21+
"\0"
22+
END
23+
24+
3 TEXTINCLUDE
25+
BEGIN
26+
"\r\n"
27+
"\0"
28+
END
29+
30+
#endif
31+
32+
IDI_ICON1 ICON "Err.ico"
33+
34+
#endif
35+
36+
#ifndef APSTUDIO_INVOKED
37+
#endif

0 commit comments

Comments
 (0)