Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
scizzydo committed Sep 7, 2023
0 parents commit b86e2bc
Show file tree
Hide file tree
Showing 11 changed files with 879 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
build/
.cache/
.vscode/
70 changes: 70 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
cmake_minimum_required(VERSION 3.15)

project(shellcoder C CXX RC)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_FLAGS "/EHsc /MD")

## Sorry all you folks, not making a fancy cmake. for LLVM and capstone
## Go ahead and pass your -DLLVM_CLANG_DIR and those in, or override it here
if (NOT DEFINED ${LLVM_CLANG_DIR})
set(LLVM_CLANG_DIR "Z:\\Libraries\\llvm-project\\build")
endif()

if (NOT DEFINED ${CAPSTONE_INCLUDE_DIR})
set(CAPSTONE_INCLUDE_DIR "Z:\\Libraries\\capstone\\include")
endif()

if (NOT DEFINED ${CAPSTONE_LIB_DIR})
set(CAPSTONE_LIB_DIR "Z:\\Libraries\\capstone\\build")
endif()

set(CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH} ${LLVM_CLANG_DIR})

find_package(LLVM REQUIRED CONFIG)
find_package(CLANG REQUIRED CONFIG)
message(STATUS "Using LLVM ${LLVM_PACKAGE_VERSION}")
message(STATUS "LLVM include directory: ${LLVM_INCLUDE_DIRS}")
message(STATUS "Clang include directory: ${CLANG_INCLUDE_DIRS}")

include(FetchContent)
message(STATUS "Checking if ImGui repo needs to be pulled")
FetchContent_Declare(
imgui
GIT_REPOSITORY "https://github.com/ocornut/imgui"
GIT_TAG "v1.89.9"
)

FetchContent_MakeAvailable(imgui)

message(STATUS "ImGui source directory: ${imgui_SOURCE_DIR}")

include_directories(include resources ${LLVM_INCLUDE_DIRS} ${CLANG_INCLUDE_DIRS} ${CAPSTONE_INCLUDE_DIR}
${imgui_SOURCE_DIR} ${imgui_SOURCE_DIR}/backends ${imgui_SOURCE_DIR}/misc/cpp)

link_directories(${LLVM_CLANG_BUILD_DIR}/lib ${CAPSTONE_LIB_DIR})

add_definitions(${LLVM_DEFINITIONS} -DNOMINMAX)

set(LLVM_LIBS LLVMExecutionEngine LLVMSupport LLVMTarget LLVMBitWriter
LLVMMCJIT LLVMX86CodeGen LLVMPasses LLVMAsmParser LLVMX86AsmParser)

set(CLANG_LIBS clangFrontend clangCodeGen)

set(SHELLCODER_SOURCES
src/main.cpp
src/code_compiler.cpp)

set(IMGUI_SOURCES
${imgui_SOURCE_DIR}/imgui.cpp
${imgui_SOURCE_DIR}/imgui_draw.cpp
${imgui_SOURCE_DIR}/imgui_tables.cpp
${imgui_SOURCE_DIR}/imgui_widgets.cpp
${imgui_SOURCE_DIR}/misc/cpp/imgui_stdlib.cpp
${imgui_SOURCE_DIR}/backends/imgui_impl_dx11.cpp
${imgui_SOURCE_DIR}/backends/imgui_impl_win32.cpp)

add_executable(${PROJECT_NAME} WIN32 ${SHELLCODER_SOURCES} ${IMGUI_SOURCES})
target_link_libraries(${PROJECT_NAME} PRIVATE ${LLVM_LIBS} ${CLANG_LIBS} capstone d3d11)
target_sources(${PROJECT_NAME} PRIVATE shellcoder.rc)
Binary file added README.md
Binary file not shown.
7 changes: 7 additions & 0 deletions include/code_compiler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once
#include <vector>
#include <string>

#include "llvm_precomp.h"

bool generate_shellcode(std::string contents, std::vector<std::string> args = {});
117 changes: 117 additions & 0 deletions include/directx.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
#pragma once
#include <d3d11.h>
//#include "DirectX/WICTextureLoader.h"
//#include "resource.h"

class directx {
private:
ID3D11Device* g_pd3dDevice;
ID3D11DeviceContext* g_pd3dDeviceContext;
IDXGISwapChain* g_pSwapChain;
ID3D11RenderTargetView* g_mainRenderTargetView;
bool m_DPIScaleSet;
float m_DPIScale;
// ID3D11ShaderResourceView* m_icon;
/* inline void LoadDXImage(int32_t file) {
auto rc = FindResource(NULL, MAKEINTRESOURCE(file), "PNG");
if (!rc)
return;
auto rcdata = LoadResource(NULL, rc);
auto size = SizeofResource(NULL, rc);
if (!rcdata)
return;
const uint8_t* data = static_cast<const uint8_t*>(LockResource(rcdata));
DirectX::CreateWICTextureFromMemory(g_pd3dDevice, data, size, nullptr, &m_icon);
}*/
public:
directx() :
g_pd3dDevice(nullptr),
g_pd3dDeviceContext(nullptr),
g_pSwapChain(nullptr),
g_mainRenderTargetView(nullptr),
m_DPIScaleSet(false),
m_DPIScale(1.f)
{
}
~directx() {
CleanupDeviceD3D();
}
inline ID3D11Device* GetDevice() {
return g_pd3dDevice;
}
inline IDXGISwapChain* GetSwapChain() {
return g_pSwapChain;
}
inline ID3D11DeviceContext* GetDeviceContext() {
return g_pd3dDeviceContext;
}
inline ID3D11RenderTargetView* GetRenderTargetView() {
return g_mainRenderTargetView;
}
inline float GetDPIScale() {
if (!m_DPIScaleSet) {
auto hDC = GetDC(NULL);
auto dpix = GetDeviceCaps(hDC, LOGPIXELSX);
ReleaseDC(NULL, hDC);
auto DPI = MulDiv(100, dpix, 96);
m_DPIScale = DPI / 100.f;
m_DPIScaleSet = true;
}
return m_DPIScale;
}
inline bool CreateDeviceD3D(HWND hWnd) {
DXGI_SWAP_CHAIN_DESC sd;
ZeroMemory(&sd, sizeof(sd));
sd.BufferCount = 2;
sd.BufferDesc.Width = 0;
sd.BufferDesc.Height = 0;
sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
sd.BufferDesc.RefreshRate.Numerator = 60;
sd.BufferDesc.RefreshRate.Denominator = 1;
sd.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;
sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
sd.OutputWindow = hWnd;
sd.SampleDesc.Count = 1;
sd.SampleDesc.Quality = 0;
sd.Windowed = TRUE;
sd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;

UINT createDeviceFlags = 0;
D3D_FEATURE_LEVEL featureLevel;
const D3D_FEATURE_LEVEL featureLevelArray[3] = { D3D_FEATURE_LEVEL_10_0, D3D_FEATURE_LEVEL_11_0, D3D_FEATURE_LEVEL_10_0 };
if (D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, createDeviceFlags, featureLevelArray, 3, D3D11_SDK_VERSION, &sd, &g_pSwapChain, &g_pd3dDevice, &featureLevel, &g_pd3dDeviceContext) != S_OK)
return false;

CreateRenderTarget();
return true;
}
inline void CleanupDeviceD3D() {
CleanupRenderTarget();
if (g_pSwapChain) {
g_pSwapChain->Release();
g_pSwapChain = NULL;
}
if (g_pd3dDeviceContext) {
g_pd3dDeviceContext->Release();
g_pd3dDeviceContext = NULL;
}
if (g_pd3dDevice) {
g_pd3dDevice->Release();
g_pd3dDevice = NULL;
}
}
inline void CreateRenderTarget() {
ID3D11Texture2D* pBackBuffer;
g_pSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), reinterpret_cast<LPVOID*>(&pBackBuffer));
if (pBackBuffer) {
g_pd3dDevice->CreateRenderTargetView(pBackBuffer, NULL, &g_mainRenderTargetView);
pBackBuffer->Release();
}
}
inline void CleanupRenderTarget() {
if (g_mainRenderTargetView) {
g_mainRenderTargetView->Release();
g_mainRenderTargetView = NULL;
}
}
};
24 changes: 24 additions & 0 deletions include/llvm_precomp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once
#pragma warning(push)
#pragma warning(disable: 4244)
#pragma warning(disable: 4624)
#pragma warning(disable: 4141)
#pragma warning(disable: 4291)
#include <llvm/InitializePasses.h>
#include <llvm/ExecutionEngine/ExecutionEngine.h>
#include <llvm/ExecutionEngine/MCJIT.h>
#include <llvm/ExecutionEngine/JITEventListener.h>
#include <llvm/ExecutionEngine/SectionMemoryManager.h>
#include <llvm/Passes/PassBuilder.h>
#include <llvm/Support/TargetSelect.h>
#include <llvm/Support/Registry.h>
#include <llvm/Support/Host.h>
#include <llvm/IR/ValueSymbolTable.h>

#include <clang/Basic/DiagnosticOptions.h>
#include <clang/Basic/Diagnostic.h>
#include <clang/CodeGen/CodeGenAction.h>
#include <clang/Frontend/CompilerInstance.h>
#include <clang/Frontend/CompilerInvocation.h>
#include <clang/Frontend/TextDiagnosticPrinter.h>
#pragma warning(pop)
12 changes: 12 additions & 0 deletions include/resource.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once

#define IDI_ICON1 101

#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE 104
#define _APS_NEXT_COMMAND_VALUE 40001
#define _APS_NEXT_CONTROL_VALUE 1001
#define _APS_NEXT_SYMED_VALUE 101
#endif
#endif
Binary file added resources/Err.ico
Binary file not shown.
37 changes: 37 additions & 0 deletions shellcoder.rc
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include "resource.h"

#define APSTUDIO_READONLY_SYMBOLS
#include "winres.h"
#undef APSTUDIO_READONLY_SYMBOLS

#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)
LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US
#pragma code_page(1252)

#ifdef APSTUDIO_INVOKED

1 TEXTINCLUDE
BEGIN
"resource.h\0"
END

2 TEXTINCLUDE
BEGIN
"#include ""winres.h""\r\n"
"\0"
END

3 TEXTINCLUDE
BEGIN
"\r\n"
"\0"
END

#endif

IDI_ICON1 ICON "Err.ico"

#endif

#ifndef APSTUDIO_INVOKED
#endif
Loading

0 comments on commit b86e2bc

Please sign in to comment.