Skip to content

Commit 8bcb3bb

Browse files
committed
driver code so far
1 parent 6d7d87e commit 8bcb3bb

29 files changed

+2510
-0
lines changed

CMakeLists.txt

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
cmake_minimum_required(VERSION 3.23.0)
2+
project(kptnhook VERSION 0.1.0)
3+
4+
#include(CTest)
5+
#enable_testing()
6+
7+
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/deps/findwdk/cmake")
8+
find_package(WDK REQUIRED)
9+
10+
set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS src/shellcode/shellcode32.asm)
11+
set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS src/shellcode/shellcode64.asm)
12+
set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS src/shellcode/structs.asm)
13+
14+
exec_program(powershell "${CMAKE_CURRENT_LIST_DIR}"
15+
ARGS -ExecutionPolicy Bypass -File ${CMAKE_CURRENT_LIST_DIR}\\compile-shellcode.ps1 ${CMAKE_CURRENT_LIST_DIR}\\src\\shellcode\\shellcode32.asm
16+
OUTPUT_VARIABLE SHELLCODE_BYTES32)
17+
18+
message("compiled 32bit shellcode: ${SHELLCODE_BYTES32}")
19+
20+
exec_program(powershell "${CMAKE_CURRENT_LIST_DIR}"
21+
ARGS -ExecutionPolicy Bypass -File ${CMAKE_CURRENT_LIST_DIR}\\compile-shellcode.ps1 ${CMAKE_CURRENT_LIST_DIR}\\src\\shellcode\\shellcode64.asm
22+
OUTPUT_VARIABLE SHELLCODE_BYTES64)
23+
24+
message("compiled 64bit shellcode: ${SHELLCODE_BYTES64}")
25+
26+
wdk_add_driver(kptnhook src/main.cpp)
27+
28+
target_compile_definitions(kptnhook PUBLIC ARR_SHELLCODE32=${SHELLCODE_BYTES32})
29+
target_compile_definitions(kptnhook PUBLIC ARR_SHELLCODE64=${SHELLCODE_BYTES64})
30+
add_custom_command(TARGET kptnhook POST_BUILD
31+
COMMAND ${WDK_ROOT}/bin/${WDK_VERSION}/x64/signtool.exe sign /v /n kptnhook $<TARGET_FILE:kptnhook>
32+
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
33+
COMMENT "signing kernel driver"
34+
)
35+
36+
add_subdirectory(src)
37+
add_subdirectory(include/kptnhook)
38+
#add_subdirectory(deps)
39+
40+
set(CPACK_PROJECT_NAME ${PROJECT_NAME})
41+
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
42+
include(CPack)
43+
44+
message(${WDK_ROOT}/bin/${WDK_VERSION}/x64/signtool.exe )

compile-shellcode.ps1

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<#
2+
compiles an asm file into a c-style byte array
3+
#>
4+
param (
5+
# path to asm file
6+
[parameter(mandatory=$true)]
7+
[string]$path
8+
)
9+
10+
$nasm = "$env:LOCALAPPDATA\bin\NASM\nasm.exe"
11+
12+
if (-not (test-path $nasm)) {
13+
write-error "nasm.exe not found in appdata. install nasm for local user to continue."
14+
exit 1
15+
}
16+
17+
$tempfile = "$env:TEMP\shellcode.bin"
18+
& $nasm $path -f bin -o $tempfile
19+
'{ ' + ((format-hex $tempfile | select -expand bytes | % { '0x{0:x2}' -f $_ }) -join ', ') + ' }'

create_cert.ps1

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
New-SelfSignedCertificate -Subject "kptnhook" -Type CodeSigningCert -CertStoreLocation cert:\CurrentUser\My -NotAfter (Get-Date).AddYears(99)

deps/CMakeLists.txt

Whitespace-only changes.

include/kptnhook/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
target_include_directories(kptnhook PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})

include/kptnhook/arch.h

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#pragma once
2+
#include <ntifs.h>
3+
4+
enum bit { x64, x32 };
5+
enum compat { native, wow };
6+
struct arch {
7+
bit b;
8+
compat com;
9+
};
10+
11+
/// figure out the architecture of a peprocess
12+
arch proc_arch(PEPROCESS p);

include/kptnhook/drvglobal.h

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#pragma once
2+
3+
#include <ntifs.h>
4+
5+
struct driverctx {
6+
PDRIVER_OBJECT obj;
7+
PUNICODE_STRING registry_path;
8+
};
9+
10+
extern driverctx GLOBAL;

include/kptnhook/handler.h

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#pragma once
2+
#include <ntifs.h>
3+
4+
constexpr UNICODE_STRING KNOWN_DLLS[] = {
5+
RTL_CONSTANT_STRING(L"gi_agent.dll")
6+
};
7+
8+
void on_image_load(PUNICODE_STRING img_name, HANDLE proc, PIMAGE_INFO info);
9+
void on_create_proc(HANDLE parent_pid, HANDLE pid, BOOLEAN create);

include/kptnhook/hook.h

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#pragma once
2+
#include <ntifs.h>
3+
4+
constexpr UINT8 stub64[] = { 0xff, 0x25, 0x00, 0x00, 0x00, 0x00 };
5+
constexpr auto STUB_SIZE64 = sizeof(stub64) + sizeof(UINT64);
6+
7+
constexpr UINT8 stub32[] = { 0xe9 };
8+
constexpr auto STUB_SIZE32 = sizeof(stub32) + sizeof(UINT32);
9+
10+
#ifdef ARR_SHELLCODE32
11+
#else
12+
#define ARR_SHELLCODE32 { 0 }
13+
#error shellcode not defined, use the cmake build as it defined this
14+
#endif
15+
16+
#ifdef ARR_SHELLCODE64
17+
#else
18+
#define ARR_SHELLCODE64 { 0 }
19+
#error shellcode not defined, use the cmake build as it defined this
20+
#endif
21+
22+
void hook64(void* func, void* target);
23+
void hook32(void* func, void* target);

include/kptnhook/known_dlls.h

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#pragma once
2+
#include <ntifs.h>
3+
#include <arch.h>
4+
5+
NTSTATUS remove_known_dll(const UNICODE_STRING* filename, bool native_arch);
6+
NTSTATUS add_known_dll(const UNICODE_STRING* filename, arch a);

include/kptnhook/log.h

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#pragma once
2+
3+
#include <ntifs.h>
4+
5+
constexpr auto LOG_LEVEL_DBG = 4;
6+
constexpr auto LOG_LEVEL_TRACE = 3;
7+
constexpr auto LOG_LEVEL_INFO = 2;
8+
constexpr auto LOG_LEVEL_WARN = 1;
9+
constexpr auto LOG_LEVEL_ERR = 0;
10+
11+
#define log_debug(s, ...) DbgPrintEx(DPFLTR_DEFAULT_ID, LOG_LEVEL_DBG, "[kptnhook2][debug] " s "\n", __VA_ARGS__)
12+
#define log_trace(s, ...) DbgPrintEx(DPFLTR_DEFAULT_ID, LOG_LEVEL_TRACE, "[kptnhook2][trace] " s "\n", __VA_ARGS__)
13+
#define log_info(s, ...) DbgPrintEx(DPFLTR_DEFAULT_ID, LOG_LEVEL_INFO, "[kptnhook2][info] " s "\n", __VA_ARGS__)
14+
#define log_warn(s, ...) DbgPrintEx(DPFLTR_DEFAULT_ID, LOG_LEVEL_WARN, "[kptnhook2][warn] " s "\n", __VA_ARGS__)
15+
#define log_error(s, ...) DbgPrintEx(DPFLTR_DEFAULT_ID, LOG_LEVEL_ERR, "[kptnhook2][error] " s "\n", __VA_ARGS__)
16+
17+
#define guard_log(cond, code, msg, ...) if(cond) { log_error(msg, __VA_ARGS__); return code; }
18+
#define guard_nts(status, msg, ...) guard_log(!NT_SUCCESS(status), status, msg, __VA_ARGS__);

include/kptnhook/main.h

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#pragma once
2+
#include <ntifs.h>
3+
4+
extern "C" NTSTATUS NTAPI DriverEntry(PDRIVER_OBJECT drv, PUNICODE_STRING reg_path);
5+
void NTAPI unload(PDRIVER_OBJECT drv);

include/kptnhook/path.h

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#pragma once
2+
#include <ntifs.h>
3+
4+
bool match_filename(PUNICODE_STRING path, PUNICODE_STRING filename);
5+
bool match_filename_ascii(char* path, char* filename);

include/kptnhook/pointers.h

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#pragma once
2+
3+
#define addroffset(type, addr, off) reinterpret_cast<type*>(reinterpret_cast<uintptr_t>(addr) + off)
4+
#define addr_relative_to(a1, a2) reinterpret_cast<size_t>(a1) - reinterpret_cast<size_t>(a2)
5+
#define towow64(ptr) static_cast<UINT32>(reinterpret_cast<UINT_PTR>(ptr))

include/kptnhook/raii.hpp

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#pragma once
2+
#include <ntifs.h>
3+
4+
#define _CONCAT(x,y) x ## y
5+
#define CONCAT(x,y) _CONCAT(x,y)
6+
7+
template<class T>
8+
class raiiwrap {
9+
public:
10+
raiiwrap(T inner) : m_inner(inner) {}
11+
protected:
12+
T m_inner;
13+
};
14+
15+
#define bind_peprocess(p) auto CONCAT(anonymous, __LINE__) = peprocess_res(p);
16+
class peprocess_res : public raiiwrap<PEPROCESS> {
17+
public:
18+
peprocess_res(PEPROCESS p) : raiiwrap(p) {};
19+
~peprocess_res() { ObDereferenceObject(m_inner); }
20+
};
21+
22+
#define bind_handle(h) auto CONCAT(anonymous, __LINE__) = handle_res(h);
23+
class handle_res : public raiiwrap<HANDLE> {
24+
public:
25+
handle_res(HANDLE p) : raiiwrap(p) {};
26+
~handle_res() { ZwClose(m_inner); }
27+
};
28+
29+
#define bind_kapc_state(p) auto CONCAT(anonymous, __LINE__) = kapc_state_res(p);
30+
class kapc_state_res : public raiiwrap<PKAPC_STATE> {
31+
public:
32+
kapc_state_res(PKAPC_STATE p) : raiiwrap(p) {};
33+
~kapc_state_res() { KeUnstackDetachProcess(m_inner); }
34+
};
35+
36+
#define bind_alloc(p) auto CONCAT(anonymous, __LINE__) = alloc_res(p);
37+
class alloc_res : public raiiwrap<PVOID> {
38+
public:
39+
alloc_res(PVOID p) : raiiwrap(p) {};
40+
~alloc_res() { if(m_inner) ExFreePool(m_inner); }
41+
};

include/kptnhook/tibpeb.h

Whitespace-only changes.

0 commit comments

Comments
 (0)