forked from jmpews/Dobby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
440 lines (362 loc) · 14.4 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
cmake_minimum_required(VERSION 3.5)
project(Dobby)
enable_language(ASM)
include(cmake/Util.cmake)
include(cmake/Globals.cmake)
include(cmake/Macros.cmake)
include(cmake/XcodeGenerator.cmake)
include(cmake/AutoFiles.cmake)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_C_STANDARD 11)
AutoFiles("." "CmakeSource" "\\.(cc|cpp|c|h)$")
# :< You Shall Not Pass!
if(0)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Werror")
endif()
# ===== Handle Option =====
option(DOBBY_GENERATE_SHARED "Build shared library" ON)
option(DOBBY_DEBUG "Enable debug logging" OFF)
option(NearBranch "Enable Near Branch Trampoline" ON)
option(DynamicBinaryInstrument "Enable Dynamic Binary Instrument" ON)
option(FullFloatingPointRegisterPack "Save and pack all floating-point registers" OFF)
option(EnableObfuscation "Enable llvm obfuscation" OFF)
option(Plugin.SymbolResolver "Resolve symbol by [DobbySymbolResolver] " ON)
option(Plugin.GlobalOffsetTableHook "Global Offset Table Hook by [DobbyGlobalOffsetTableReplace] " ON)
option(Plugin.LinkerLoadCallback "Register image load callback " OFF)
# frida is better choice
option(Plugin.ApplicationEventMonitor "Auto monitor linker, file, etc." OFF)
option(Plugin.Android.BionicLinkerRestriction "Enable android bionic linker restriction" OFF)
# Use native assembly bridge to replace the runtime codegen
# if(CLOSURE_BRIDGE_TEMPLATE)
# SET(CMAKE_ASM_FLAGS "${CMAKE_C_FLAGS}")
# enable_language(ASM)
# add_definitions(-DENABLE_CLOSURE_BRIDGE_TEMPLATE)
# endif()
# Enable debug will log more information
if ((NOT DEFINED CMAKE_BUILD_TYPE) OR (CMAKE_BUILD_TYPE STREQUAL "Debug"))
message(STATUS "[Dobby] Enabling debug logging because CMAKE_BUILD_TYPE is Debug or undefined")
set(DOBBY_DEBUG ON)
endif()
if(DOBBY_DEBUG)
add_definitions(-DDOBBY_DEBUG)
add_definitions(-DLOGGING_DEBUG)
message(STATUS "[Dobby] Enable debug logging")
endif()
# Enable full floating point register pack
# for arm64, allow access q8 - q31
if(FullFloatingPointRegisterPack)
add_definitions(-DFULL_FLOATING_POINT_REGISTER_PACK)
message(STATUS "[Dobby] Save and pack all floating-point registers")
endif()
if(SYSTEM.Darwin)
# -lstdc++
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -stdlib=libc++")
if (NOT DOBBY_DEBUG)
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,-x -Wl,-S")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,-exported_symbol,_log_internal_impl -Wl,-exported_symbol,_log_set_level")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,-exported_symbol,_CodePatch")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,-exported_symbol,_DobbyBuildVersion -Wl,-exported_symbol,_DobbyHook -Wl,-exported_symbol,_DobbyInstrument -Wl,-exported_symbol,_DobbyDestroy")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,-exported_symbol,_DobbyGlobalOffsetTableReplace")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,-exported_symbol,_DobbySymbolResolver")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,-exported_symbol,_intercept_routing_common_bridge_handler")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,-exported_symbol,_dobby_enable_near_branch_trampoline -Wl,-exported_symbol,_dobby_disable_near_branch_trampoline")
endif()
elseif(SYSTEM.Android)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fomit-frame-pointer")
if(NOT DOBBY_DEBUG)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -ffunction-sections -fdata-sections")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--gc-sections -Wl,--exclude-libs,ALL")
endif()
elseif(SYSTEM.Linux)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC")
elseif(SYSTEM.Windows)
add_definitions(-D_CRT_SECURE_NO_WARNINGS -D_CRT_NONSTDC_NO_DEPRECATE -D_CRT_SECURE_NO_DEPRECATE)
if(NOT DOBBY_DEBUG)
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /export:log_internal_impl")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /export:DobbyHook /export:DobbyInstrument /export:DobbySymbolResolver")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /export:dobby_enable_near_branch_trampoline /export:dobby_disable_near_branch_trampoline")
endif()
endif()
if(COMPILER.Clang)
if(NOT DOBBY_DEBUG)
# set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-rtti -fvisibility=hidden -fvisibility-inlines-hidden")
endif()
if(PROCESSOR.ARM)
set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} -arch armv7 -x assembler-with-cpp")
elseif(PROCESSOR.AARCH64)
set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} -arch arm64 -x assembler-with-cpp")
endif()
endif()
set(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} ${CMAKE_CXX_FLAGS}")
message(STATUS "CMAKE_C_COMPILER: ${CMAKE_C_COMPILER}")
message(STATUS "CMAKE_CXX_COMPILER: ${CMAKE_CXX_COMPILER}")
message(STATUS "CMAKE_C_FLAGS: ${CMAKE_C_FLAGS}")
message(STATUS "CMAKE_CXX_FLAGS: ${CMAKE_CXX_FLAGS}")
message(STATUS "CMAKE_SHARED_LINKER_FLAGS: ${CMAKE_SHARED_LINKER_FLAGS}")
# arch prefix
if(PROCESSOR.ARM)
set(ARCH1 ARM)
set(arch1 arm)
set(core_arch arm)
elseif(PROCESSOR.AARCH64)
set(ARCH1 ARM64)
set(arch1 arm64)
set(core_arch arm64)
elseif(PROCESSOR.X86)
set(ARCH1 X86)
set(arch1 x86)
set(core_arch ia32)
elseif(PROCESSOR.X86_64)
set(ARCH1 X64)
set(arch1 x64)
set(core_arch x64)
else()
endif()
# system prefix
if(SYSTEM.Darwin OR SYSTEM.iOS OR SYSTEM.macOS)
set(platform1 posix)
set(platform2 Darwin)
elseif(SYSTEM.Linux OR SYSTEM.Android)
set(platform1 posix)
set(platform2 Linux)
elseif(SYSTEM.Windows)
set(platform1 windows)
set(platform2 Windows)
else()
endif()
if(CMAKE_GENERATOR STREQUAL Xcode)
endif()
include(cmake/dobby.xcode.source.cmake)
include_directories(
.
./include
./source
./source/include
./source/UserMode
./external
./external/logging
./external/xnucxx
./external/misc-helper
builtin-plugin
)
set(DOBBY_DIR ${CMAKE_CURRENT_SOURCE_DIR})
set(dobby.SOURCE_FILE_LIST ${dobby.SOURCE_FILE_LIST}
# cpu
source/core/arch/CpuFeature.cc
source/core/arch/CpuRegister.cc
# assembler
source/core/modules/assembler/assembler.cc
source/core/modules/assembler/assembler-${core_arch}.cc
# codegen
source/core/modules/codegen/codegen-${core_arch}.cc
# memory kit
source/MemoryAllocator/CodeBuffer/CodeBufferBase.cc
source/MemoryAllocator/CodeBuffer/code-buffer-${arch1}.cc
source/MemoryAllocator/AssemblyCodeBuilder.cc
source/MemoryAllocator/MemoryArena.cc
# instruction relocation
source/InstructionRelocation/${arch1}/${ARCH1}InstructionRelocation.cc
# intercept routing
source/InterceptRouting/InterceptRouting.cpp
# intercept routing trampoline
source/TrampolineBridge/Trampoline/${arch1}/trampoline-${arch1}.cc
# intercept routing plugin (buildin)
source/InterceptRouting/Routing/FunctionInlineReplace/function-inline-replace.cc
source/InterceptRouting/Routing/FunctionInlineReplace/FunctionInlineReplaceExport.cc
# plugin register
source/InterceptRouting/RoutingPlugin/RoutingPlugin.cc
# platform util
source/UserMode/PlatformUtil/${platform2}/ProcessRuntimeUtility.cc
# user mode - platform interface
source/UserMode/UnifiedInterface/platform-${platform1}.cc
# user mode - executable memory
source/UserMode/ExecMemory/code-patch-tool-${platform1}.cc
source/UserMode/ExecMemory/clear-cache-tool-all.c
# main
source/dobby.cpp
source/Interceptor.cpp
)
if (PROCESSOR.X86_64 OR PROCESSOR.X86)
set(NearBranch ON)
if (PROCESSOR.X86_64)
add_definitions(-DDETOURS_X64)
endif()
endif()
if(SYSTEM.Darwin)
include_directories(
source/UserMode/ExecMemory/substrated/include
)
set(dobby.SOURCE_FILE_LIST ${dobby.SOURCE_FILE_LIST}
source/UserMode/ExecMemory/code-patch-tool-darwin.cc
)
endif()
if(SYSTEM.iOS)
add_definitions(-DCODE_PATCH_WITH_SUBSTRATED)
set(dobby.SOURCE_FILE_LIST ${dobby.SOURCE_FILE_LIST}
source/UserMode/ExecMemory/substrated/mach_interface_support/substrated_client.c
)
endif()
if(FunctionWrapper OR DynamicBinaryInstrument)
set(dobby.SOURCE_FILE_LIST ${dobby.SOURCE_FILE_LIST}
# closure trampoline bridge
source/TrampolineBridge/ClosureTrampolineBridge/common-bridge-handler.cc
source/TrampolineBridge/ClosureTrampolineBridge/${arch1}/helper-${arch1}.cc
source/TrampolineBridge/ClosureTrampolineBridge/${arch1}/closure-bridge-${arch1}.cc
source/TrampolineBridge/ClosureTrampolineBridge/${arch1}/${ARCH1}AssemblyClosureTrampoline.cc
# user mode - multi thread support
# source/UserMode/MultiThreadSupport/ThreadSupport.cpp
# source/UserMode/Thread/PlatformThread.cc
# source/UserMode/Thread/platform-thread-${platform1}.cc
)
endif()
if(FunctionWrapper)
message(FATAL_ERROR "[!] FunctionWrapper plugin is not supported")
endif()
if(DynamicBinaryInstrument)
message(STATUS "[Dobby] Enable dynamic binary instrument(hook instruction with register context)")
set(dobby.SOURCE_FILE_LIST ${dobby.SOURCE_FILE_LIST}
source/InterceptRouting/Routing/DynamicBinaryInstrument/dynamic-binary-instrument.cc
source/InterceptRouting/Routing/DynamicBinaryInstrument/DynamicBinaryInstrumentExport.cc
source/InterceptRouting/Routing/DynamicBinaryInstrument/intercept_routing_handler.cc
)
endif()
if(NearBranch)
message(STATUS "[Dobby] Enable near branch trampoline(trampoline within single instruction)")
set(dobby.SOURCE_FILE_LIST ${dobby.SOURCE_FILE_LIST}
source/InterceptRouting/RoutingPlugin/NearBranchTrampoline/NeaBranchTrampoline.cc
source/MemoryAllocator/NearMemoryArena.cc)
endif()
add_subdirectory(external/misc-helper)
get_target_property(misc_helper.SOURCE_FILE_LIST misc_helper SOURCES)
# add logging library
add_subdirectory(external/logging)
get_target_property(logging.SOURCE_FILE_LIST logging SOURCES)
# add xnucxx library
add_subdirectory(external/xnucxx)
get_target_property(xnucxx.SOURCE_FILE_LIST xnucxx SOURCES)
if(Plugin.GlobalOffsetTableHook AND SYSTEM.Darwin)
message(STATUS "[Dobby] Enable global offset table hook")
include_directories(builtin-plugin/GlobalOffsetTableHook)
add_subdirectory(builtin-plugin/GlobalOffsetTableHook)
get_target_property(global_offset_table_hook.SOURCE_FILE_LIST global_offset_table_hook SOURCES)
set(dobby.plugin.SOURCE_FILE_LIST ${dobby.plugin.SOURCE_FILE_LIST}
${global_offset_table_hook.SOURCE_FILE_LIST}
)
endif()
if(Plugin.SymbolResolver)
message(STATUS "[Dobby] Enable symbol resolver")
include_directories(builtin-plugin/SymbolResolver)
add_subdirectory(builtin-plugin/SymbolResolver)
get_target_property(symbol_resolver.SOURCE_FILE_LIST symbol_resolver SOURCES)
set(dobby.plugin.SOURCE_FILE_LIST ${dobby.plugin.SOURCE_FILE_LIST}
${symbol_resolver.SOURCE_FILE_LIST}
)
endif()
if(Plugin.Android.BionicLinkerRestriction)
if(NOT SYSTEM.Android)
message(FATAL_ERROR "[!] Plugin.Android.BionicLinkerRestriction only works on Android.")
endif()
message(STATUS "[Dobby] Enable Plugin.Android.BionicLinkerRestriction")
set(dobby.plugin.SOURCE_FILE_LIST ${dobby.plugin.SOURCE_FILE_LIST}
builtin-plugin/BionicLinkerRestriction/bionic_linker_restriction.cc
)
endif()
if(Plugin.HideSystemCall)
set(dobby.plugin.SOURCE_FILE_LIST ${dobby.plugin.SOURCE_FILE_LIST}
)
endif()
if(Plugin.LinkerLoadCallback)
set(dobby.plugin.SOURCE_FILE_LIST ${dobby.plugin.SOURCE_FILE_LIST}
builtin-plugin/LinkerImageLoadCallback/linker_load_callback.cc
)
endif()
set(dobby.HEADER_FILE_LIST
include/dobby.h
)
# add build version
string(TIMESTAMP TODAY "%Y%m%d")
set(VERSION_REVISION "-${TODAY}")
if (EXISTS "${CMAKE_SOURCE_DIR}/.git")
execute_process(
COMMAND git rev-parse --short --verify HEAD
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE VERSION_COMMIT_HASH
OUTPUT_STRIP_TRAILING_WHITESPACE
)
if(VERSION_COMMIT_HASH)
set(VERSION_REVISION "${VERSION_REVISION}-${VERSION_COMMIT_HASH}")
endif()
endif()
set(DOBBY_BUILD_VERSION "Dobby${VERSION_REVISION}")
add_definitions(-D__DOBBY_BUILD_VERSION__="${DOBBY_BUILD_VERSION}")
message(STATUS "[Dobby] ${DOBBY_BUILD_VERSION}")
if(DOBBY_GENERATE_SHARED)
message(STATUS "[Dobby] Generate shared library")
set(DOBBY_LIBRARY_TYPE SHARED)
else()
message(STATUS "[Dobby] Generate static library")
set(DOBBY_LIBRARY_TYPE STATIC)
endif()
add_library(dobby ${DOBBY_LIBRARY_TYPE} ${dobby.HEADER_FILE_LIST} ${dobby.SOURCE_FILE_LIST} ${logging.SOURCE_FILE_LIST} ${xnucxx.SOURCE_FILE_LIST} ${dobby.plugin.SOURCE_FILE_LIST})
target_include_directories(dobby PUBLIC include)
if(EnableObfuscation)
set(linker_flags "${linker_flags} -Wl,-mllvm -Wl,-obfuscator-conf=all")
endif()
set_target_properties(dobby
PROPERTIES LINK_FLAGS "${linker_flags}"
)
if(SYSTEM.Darwin)
# set(CMAKE_BUILD_WITH_INSTALL_NAME_DIR TRUE)
set(CMAKE_INSTALL_NAME_DIR "@rpath")
set(CMAKE_SHARED_LIBRARY_RUNTIME_C_FLAG "-Wl,-rpath,")
add_library(DobbyX ${DOBBY_LIBRARY_TYPE} ${dobby.HEADER_FILE_LIST} ${dobby.SOURCE_FILE_LIST} ${logging.SOURCE_FILE_LIST} ${xnucxx.SOURCE_FILE_LIST} ${dobby.plugin.SOURCE_FILE_LIST})
set_target_properties(DobbyX
PROPERTIES LINK_FLAGS "${linker_flags}"
)
# set framework property
set_target_properties(DobbyX PROPERTIES
FRAMEWORK TRUE
FRAMEWORK_VERSION A
MACOSX_FRAMEWORK_IDENTIFIER "com.dobby.dobby"
# MACOSX_FRAMEWORK_INFO_PLIST Info.plist
VERSION 1.0.0 # current version
SOVERSION 1.0.0 # compatibility version
PUBLIC_HEADER include/dobby.h
XCODE_ATTRIBUTE_CODE_SIGN_IDENTITY "Apple Development"
)
# set_target_properties(Dobby PROPERTIES
# LINK_FLAGS "${CMAKE_SHARED_LINKER_FLAGS}"
# )
# message(STATUS "[Dobby] Enable Gollum.framework(iOS: 11.0 <= version, version <= 12.2, version == 12.4 )")
# add_custom_command(TARGET Dobby
# POST_BUILD
# COMMAND mkdir -p $<TARGET_FILE_DIR:${dobby_output_name}>/Frameworks
# COMMAND cp -R ${CMAKE_SOURCE_DIR}/buildin-plugin/Gollum_2019.12.31.framework $<TARGET_FILE_DIR:${dobby_output_name}>/Frameworks/Gollum.framework
# )
endif()
if(SYSTEM.Android)
target_link_libraries(dobby log)
endif()
if(SYSTEM.Linux)
target_link_libraries(dobby dl)
endif()
if(SYSTEM.Darwin)
target_link_libraries(DobbyX
"-framework Foundation")
endif()
if(SYSTEM.Darwin)
install(TARGETS DobbyX
FRAMEWORK DESTINATION Framework)
endif()
install(TARGETS dobby
LIBRARY DESTINATION lib)
if(SYSTEM.Darwin)
add_subdirectory(builtin-plugin/Dyld2HideLibrary)
add_subdirectory(builtin-plugin/ObjcRuntimeHook)
if(PROCESSOR.AARCH64)
add_subdirectory(builtin-plugin/SupervisorCallMonitor)
endif()
endif()
add_subdirectory(example)