Skip to content

Commit bf16d88

Browse files
committed
feat: remove code optimization for shellcode generator
1 parent ab18118 commit bf16d88

File tree

8 files changed

+116
-62
lines changed

8 files changed

+116
-62
lines changed

CMakeLists.txt

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,33 +10,35 @@ string(COMPARE EQUAL "${CMAKE_SYSTEM_NAME}" Windows BUILD_PLATFORM_WINDOWS)
1010
message(STATUS "CMAKE_VS_PLATFORM_NAME=" ${CMAKE_VS_PLATFORM_NAME})
1111

1212
# Determin build platform
13-
if ((NOT DEFINED CMAKE_VS_PLATFORM_NAME) OR (CMAKE_VS_PLATFORM_NAME STREQUAL ""))
13+
if((NOT DEFINED CMAKE_VS_PLATFORM_NAME) OR(CMAKE_VS_PLATFORM_NAME STREQUAL ""))
1414
message(STATUS "CMAKE_VS_PLATFORM_NAME is empty, use default: Win32")
1515
set(CMAKE_VS_PLATFORM_NAME Win32)
1616
endif()
17+
1718
message(STATUS "CMAKE_VS_PLATFORM_NAME=" ${CMAKE_VS_PLATFORM_NAME})
1819
string(COMPARE EQUAL "${CMAKE_VS_PLATFORM_NAME}" Win32 BUILD_ARC_X8632)
1920
string(COMPARE EQUAL "${CMAKE_VS_PLATFORM_NAME}" x64 BUILD_ARC_X8664)
2021

2122
# Determin build type
22-
if ((NOT DEFINED CMAKE_BUILD_TYPE) OR (CMAKE_BUILD_TYPE STREQUAL ""))
23+
if((NOT DEFINED CMAKE_BUILD_TYPE) OR(CMAKE_BUILD_TYPE STREQUAL ""))
2324
message(STATUS "CMAKE_BUILD_TYPE is empty, use default: Debug")
2425
set(CMAKE_BUILD_TYPE Debug)
2526
endif()
27+
2628
message(STATUS "CMAKE_BUILD_TYPE=" ${CMAKE_BUILD_TYPE})
2729
string(COMPARE EQUAL "${CMAKE_BUILD_TYPE}" Debug BUILD_TYPE_DEBUG)
2830
string(COMPARE EQUAL "${CMAKE_BUILD_TYPE}" Release BUILD_TYPE_RELEASE)
2931

30-
# Validate the build configuration
31-
if ((NOT BUILD_PLATFORM_WINDOWS)
32-
OR ((NOT BUILD_ARC_X8632) AND (NOT BUILD_ARC_X8664))
33-
OR ((NOT BUILD_TYPE_DEBUG) AND (NOT BUILD_TYPE_RELEASE)))
32+
# Validate the build configuration
33+
if((NOT BUILD_PLATFORM_WINDOWS)
34+
OR((NOT BUILD_ARC_X8632) AND(NOT BUILD_ARC_X8664))
35+
OR((NOT BUILD_TYPE_DEBUG) AND(NOT BUILD_TYPE_RELEASE)))
3436
message(FATAL_ERROR "mmloader supportes only Windows (X86/AMD64) platform.")
3537
endif()
3638

3739
# Flags
3840
# C standard
39-
set(CMAKE_C_STANDARD_REQUIRED ON)
41+
set(CMAKE_C_STANDARD_REQUIRED ON)
4042
set(CMAKE_C_STANDARD 11)
4143

4244
# C++ standard
@@ -46,11 +48,6 @@ set(CMAKE_CXX_STANDARD 11)
4648
# Build type
4749
set(CMAKE_CONFIGURATION_TYPES Debug Release)
4850

49-
# Default compile flags
50-
# Thanks for the PR https://github.com/microsoft/vcpkg/pull/15151/files from https://github.com/BillyONeal
51-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /GS-")
52-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /GS-")
53-
5451
message(STATUS "CMAKE_CXX_FLAGS=" ${CMAKE_CXX_FLAGS})
5552
message(STATUS "CMAKE_CXX_FLAGS_DEBUG=" ${CMAKE_CXX_FLAGS_DEBUG})
5653
message(STATUS "CMAKE_CXX_FLAGS_RELEASE=" ${CMAKE_CXX_FLAGS_RELEASE})
@@ -59,13 +56,14 @@ message(STATUS "CMAKE_C_FLAGS_DEBUG=" ${CMAKE_CXX_FLAGS_DEBUG})
5956
message(STATUS "CMAKE_C_FLAGS_RELEASE=" ${CMAKE_C_FLAGS_RELEASE})
6057

6158
# Set ouput folder
62-
if (BUILD_ARC_X8632)
59+
if(BUILD_ARC_X8632)
6360
set(MMLOADER_OUT ${CMAKE_SOURCE_DIR}/output/mmloader/x86)
6461
elseif(BUILD_ARC_X8664)
6562
set(MMLOADER_OUT ${CMAKE_SOURCE_DIR}/output/mmloader/x64)
6663
else()
6764
message(FATAL_ERROR "Unsupported Architecture")
6865
endif()
66+
6967
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${MMLOADER_OUT}/lib)
7068
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${MMLOADER_OUT}/lib)
7169
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${MMLOADER_OUT}/bin)
@@ -76,6 +74,12 @@ set_property(GLOBAL PROPERTY USE_FOLDERS ON)
7674
# Definitions
7775
add_definitions(-D_WIN32_WINNT=0x0600) # Windows Vista +
7876

77+
# Enable debugging for all builds
78+
add_compile_options(/W3 /Zi /utf-8)
79+
80+
# Enable symbol and map file generation
81+
add_link_options(/DEBUG /MAP)
82+
7983
# Include paths
8084
include_directories(src/mmloader)
8185

@@ -88,13 +92,13 @@ add_library(${PROJECT_NAME} STATIC
8892
# header
8993
set_target_properties(${PROJECT_NAME}
9094
PROPERTIES
91-
PUBLIC_HEADER "src/mmloader/mmloader.h"
95+
PUBLIC_HEADER "src/mmloader/mmloader.h"
9296
)
9397

9498
# Add post build event to copy the header files
9599
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
96-
COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_SOURCE_DIR}/output/mmloader/include
97-
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${CMAKE_SOURCE_DIR}/src/mmloader/mmLoader.h ${CMAKE_SOURCE_DIR}/output/mmloader/include/
100+
COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_SOURCE_DIR}/output/mmloader/include
101+
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${CMAKE_SOURCE_DIR}/src/mmloader/mmLoader.h ${CMAKE_SOURCE_DIR}/output/mmloader/include/
98102
)
99103

100104
# install lib and header files
@@ -104,27 +108,27 @@ install(TARGETS ${PROJECT_NAME}
104108
)
105109

106110
# Shellcode generator project
107-
if ((NOT DEFINED BUILD_SHELLCODE_GEN) OR (BUILD_SHELLCODE_GEN STREQUAL "") OR (${BUILD_SHELLCODE_GEN} MATCHES "(FALSE|false|0|OFF)"))
111+
if((NOT DEFINED BUILD_SHELLCODE_GEN) OR(BUILD_SHELLCODE_GEN STREQUAL "") OR(${BUILD_SHELLCODE_GEN} MATCHES "(FALSE|false|0|OFF)"))
108112
option(BUILD_SHELLCODE_GEN "Generate the shellcode header files" OFF)
109113
elseif(${BUILD_SHELLCODE_GEN} MATCHES "(TRUE|true|1|null|ON)")
110114
option(BUILD_SHELLCODE_GEN "Generate the shellcode header files" ON)
111115
else()
112116
message(FATAL_ERROR "++++++++++ INVALID FLAG BUILD_SHELLCODE_GEN=" ${BUILD_SHELLCODE_GEN})
113117
endif()
114118

115-
if (BUILD_SHELLCODE_GEN)
119+
if(BUILD_SHELLCODE_GEN)
116120
add_subdirectory(tools/shellcode-generator)
117121
endif()
118122

119123
# Demo project
120-
if ((NOT DEFINED BUILD_MMLOADER_DEMO) OR (BUILD_MMLOADER_DEMO STREQUAL "") OR (${BUILD_MMLOADER_DEMO} MATCHES "(FALSE|false|0|OFF)"))
124+
if((NOT DEFINED BUILD_MMLOADER_DEMO) OR(BUILD_MMLOADER_DEMO STREQUAL "") OR(${BUILD_MMLOADER_DEMO} MATCHES "(FALSE|false|0|OFF)"))
121125
option(BUILD_MMLOADER_DEMO "Build the aps demo" OFF)
122126
elseif(${BUILD_MMLOADER_DEMO} MATCHES "(TRUE|true|1|null|ON)")
123127
option(BUILD_MMLOADER_DEMO "Build the aps demo" ON)
124128
else()
125129
message(FATAL_ERROR "++++++++++ INVALID FLAG BUILD_MMLOADER_DEMO=" ${BUILD_MMLOADER_DEMO})
126130
endif()
127131

128-
if (BUILD_MMLOADER_DEMO)
129-
add_subdirectory(demo)
130-
endif()
132+
if(BUILD_MMLOADER_DEMO)
133+
add_subdirectory(demo)
134+
endif()

demo/CMakeLists.txt

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
project(mmloader-demo)
22

3-
if (BUILD_ARC_X8632)
3+
if(BUILD_ARC_X8632)
44
set(MMLOADER_DEMO_OUT ${CMAKE_SOURCE_DIR}/output/demo/x86)
55
elseif(BUILD_ARC_X8664)
66
set(MMLOADER_DEMO_OUT ${CMAKE_SOURCE_DIR}/output/demo/x64)
77
else()
88
message(FATAL_ERROR "Unsupported Architecture")
99
endif()
1010

11-
#################################################
11+
# ################################################
1212
file(GLOB_RECURSE demo_module_SRC_FILES
1313
"./demo-module/*.h"
1414
"./demo-module/*.c"
@@ -18,14 +18,14 @@ add_library(demo-module SHARED
1818
${demo_module_SRC_FILES}
1919
)
2020
target_compile_definitions(demo-module PUBLIC -DDEMOMODULE_EXPORTS)
21-
set_target_properties(demo-module PROPERTIES
21+
set_target_properties(demo-module PROPERTIES
2222
FOLDER demo
2323
ARCHIVE_OUTPUT_DIRECTORY "${MMLOADER_DEMO_OUT}/lib"
2424
LIBRARY_OUTPUT_DIRECTORY "${MMLOADER_DEMO_OUT}/bin"
2525
RUNTIME_OUTPUT_DIRECTORY "${MMLOADER_DEMO_OUT}/bin"
2626
)
2727

28-
#################################################
28+
# ################################################
2929
file(GLOB_RECURSE demo_mmloader_SRC_FILES
3030
"./demo-mmloader/*.h"
3131
"./demo-mmloader/*.c"
@@ -36,19 +36,24 @@ add_executable(demo-mmloader
3636
${demo_mmloader_SRC_FILES}
3737
)
3838

39-
set_target_properties(demo-mmloader PROPERTIES
39+
set_target_properties(demo-mmloader PROPERTIES
4040
FOLDER demo
4141
ARCHIVE_OUTPUT_DIRECTORY "${MMLOADER_DEMO_OUT}/lib"
4242
LIBRARY_OUTPUT_DIRECTORY "${MMLOADER_DEMO_OUT}/bin"
4343
RUNTIME_OUTPUT_DIRECTORY "${MMLOADER_DEMO_OUT}/bin"
4444
)
4545

46-
target_link_libraries(demo-mmloader PUBLIC
46+
target_link_libraries(demo-mmloader
47+
PRIVATE
48+
Shlwapi
49+
4750
mmloader
4851
)
4952

50-
#################################################
51-
if (BUILD_SHELLCODE_GEN)
53+
add_dependencies(demo-mmloader demo-module)
54+
55+
# ################################################
56+
if(BUILD_SHELLCODE_GEN)
5257
file(GLOB_RECURSE demo_mmloader_shellcode_SRC_FILES
5358
"./demo-mmloader-shellcode/*.h"
5459
"./demo-mmloader-shellcode/*.c"
@@ -59,16 +64,22 @@ if (BUILD_SHELLCODE_GEN)
5964
${demo_mmloader_shellcode_SRC_FILES}
6065
)
6166

62-
add_dependencies(demo-mmloader-shellcode
67+
target_link_libraries(demo-mmloader-shellcode
68+
PRIVATE
69+
Shlwapi
70+
)
71+
72+
add_dependencies(demo-mmloader-shellcode
6373
mmloader-shellcode-generator
6474
)
6575

6676
# Include paths
67-
target_include_directories(demo-mmloader-shellcode
77+
target_include_directories(demo-mmloader-shellcode
6878
PRIVATE
69-
${CMAKE_SOURCE_DIR}/output/mmloader/include)
79+
${CMAKE_SOURCE_DIR}/output/mmloader/include
80+
)
7081

71-
set_target_properties(demo-mmloader-shellcode PROPERTIES
82+
set_target_properties(demo-mmloader-shellcode PROPERTIES
7283
FOLDER demo
7384
ARCHIVE_OUTPUT_DIRECTORY "${MMLOADER_DEMO_OUT}/lib"
7485
LIBRARY_OUTPUT_DIRECTORY "${MMLOADER_DEMO_OUT}/bin"

demo/demo-mmloader-shellcode/demo-mmloader-shellcode.cpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <windows.h>
77

88
#include <strsafe.h>
9+
#include <Shlwapi.h>
910

1011
#ifdef _WIN64
1112
#ifdef _DEBUG
@@ -21,6 +22,8 @@
2122
#endif
2223
#endif
2324

25+
const TCHAR *pszDllPath = _T("demo-module.dll");
26+
2427
class AutoReleaseModuleBuffer {
2528
public:
2629
AutoReleaseModuleBuffer(LPCTSTR szDllPath) : m_pBuffer(NULL), m_hFileMapping(NULL), m_hFile(NULL) {
@@ -110,13 +113,15 @@ main() {
110113
Type_MemModuleHelper pfnMemModuleHelper = (Type_MemModuleHelper)lpShellCodeBase;
111114

112115
// Here we just read the module data from disk file
113-
// In your real project you can download the module data from remote without witting it to disk file
114-
#ifdef _DEBUG
115-
TCHAR szDllPath[] = _T("demo-moduled.dll");
116-
#else
117-
TCHAR szDllPath[] = _T("demo-module.dll");
118-
#endif
119-
AutoReleaseModuleBuffer moduleBuffer(szDllPath);
116+
// In your real project you can download the module data from remote without writing to disk file
117+
// Build the module file path
118+
TCHAR szFullModulePath[MAX_PATH] = {0};
119+
DWORD dwLength = _countof(szFullModulePath);
120+
::GetModuleFileName(::GetModuleHandle(nullptr), szFullModulePath, dwLength);
121+
::PathRemoveFileSpec(szFullModulePath);
122+
::PathCombine(szFullModulePath, szFullModulePath, pszDllPath);
123+
// Read module data to memory buffer
124+
AutoReleaseModuleBuffer moduleBuffer(szFullModulePath);
120125

121126
// Load the module from the buffer
122127
hMemModule = (HMEMMODULE)pfnMemModuleHelper(MHM_BOOL_LOAD, moduleBuffer, (LPVOID)TRUE, &dwErrorCode);
@@ -133,7 +138,7 @@ main() {
133138
_tprintf(_T("Get address of demoFunction successfully. Address: 0x%p!\r\n"), lpAddr);
134139

135140
// Function pointer type of demoFunction
136-
typedef BOOL(__stdcall * Type_TargetFunction)(unsigned char *, unsigned int);
141+
typedef BOOL (*Type_TargetFunction)(unsigned char *, unsigned int);
137142

138143
// Call the demoFunction
139144
Type_TargetFunction pfnFunction = (Type_TargetFunction)lpAddr;

demo/demo-mmloader/demo-mmloader.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@
66
#include <windows.h>
77

88
#include <strsafe.h>
9+
#include <Shlwapi.h>
910

1011
#include <mmLoader.h>
1112

13+
const TCHAR *pszDllPath = _T("demo-module.dll");
14+
1215
class AutoReleaseModuleBuffer {
1316
public:
1417
AutoReleaseModuleBuffer(LPCTSTR szDllPath) : m_pBuffer(NULL), m_hFileMapping(NULL), m_hFile(NULL) {
@@ -85,8 +88,14 @@ main() {
8588

8689
// Here we just read the module data from disk file
8790
// In your real project you can download the module data from remote without writing to disk file
88-
TCHAR szDllPath[] = _T("demo-module.dll");
89-
AutoReleaseModuleBuffer moduleBuffer(szDllPath);
91+
// Build the module file path
92+
TCHAR szFullModulePath[MAX_PATH] = {0};
93+
DWORD dwLength = _countof(szFullModulePath);
94+
::GetModuleFileName(::GetModuleHandle(nullptr), szFullModulePath, dwLength);
95+
::PathRemoveFileSpec(szFullModulePath);
96+
::PathCombine(szFullModulePath, szFullModulePath, pszDllPath);
97+
// Read module data to memory buffer
98+
AutoReleaseModuleBuffer moduleBuffer(szFullModulePath);
9099

91100
// Load the module from the buffer
92101
hMemModule = (HMEMMODULE)MemModuleHelper(MHM_BOOL_LOAD, moduleBuffer, (LPVOID)TRUE, &dwErrorCode);
@@ -103,7 +112,7 @@ main() {
103112
_tprintf(_T("Get address of demoFunction successfully. Address: 0x%p!\r\n"), lpAddr);
104113

105114
// Function pointer type of demoFunction
106-
typedef BOOL(_stdcall * Type_TargetFunction)(unsigned char *, unsigned int);
115+
typedef BOOL (*Type_TargetFunction)(unsigned char *, unsigned int);
107116

108117
// Call the demoFunction
109118
Type_TargetFunction pfnFunction = (Type_TargetFunction)lpAddr;

gen-vc-proj.bat renamed to gen-win-x86.bat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
cmake ^
22
-S . ^
3-
-B .build ^
3+
-B .build/win32 ^
44
-A Win32 ^
55
-DCMAKE_INSTALL_PREFIX=./pacakge ^
66
-DBUILD_SHELLCODE_GEN=TRUE ^

gen-win-x86_64.bat

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
cmake ^
2+
-S . ^
3+
-B .build/win64 ^
4+
-A x64 ^
5+
-DCMAKE_INSTALL_PREFIX=./pacakge ^
6+
-DBUILD_SHELLCODE_GEN=TRUE ^
7+
-DBUILD_MMLOADER_DEMO=TRUE

src/mmLoader/mmLoader.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ typedef struct __MEMMODULE_S {
4242
HMODULE hModule;
4343
LPVOID lpBase;
4444
PIMAGE_DOS_HEADER pImageDosHeader;
45-
}; // MemModule base
45+
}; // MemModule base
4646
DWORD dwSizeOfImage; // MemModule size
4747
DWORD dwCrc; // MemModule crc32
4848

0 commit comments

Comments
 (0)