Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Organization & Standarization #1

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
build_debug/
build_release/
.vs/*
out/*
CMakeSettings.json
build/*
bin/*
57 changes: 35 additions & 22 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,31 +1,44 @@
cmake_minimum_required(VERSION 3.0)
cmake_minimum_required(VERSION 3.0...3.15)

project(dll-injection-skeleton)
project(GurkaInjector)

include(GenerateExportHeader)
include (CBuildKit)

add_definitions(-D_WIN32_WINNT=0x0502)
option(USE_STATIC_LINKING "Use static linking for the runtime" ON)

# Common compiler flags
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror -std=c++11")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -O0")
if (USE_STATIC_LINKING)
if (MSVC)
# MSVC specific static linking flag
add_compile_options(/MT$<$<CONFIG:Debug>:d>)
else()
# GCC/Clang specific static linking flag
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static-libgcc -static-libstdc++")
endif()
endif()

# dll-injector.exe
set(dll-injector_src
"src/dll-injector/dll_injector.cc"
)
set(CMAKE_CXX_STANDARD 17)
set(LOCAL_PROJECT OFF)

set(dll-injector_lib
"psapi"
)
if(${CMAKE_CURRENT_SOURCE_DIR} STREQUAL ${CMAKE_SOURCE_DIR})
set(LOCAL_PROJECT ON)
endif()

add_executable(dll-injector ${dll-injector_src})
target_link_libraries(dll-injector ${dll-injector_lib})
option(DIS_BUILD_EXAMPLES "Build examples for Dll Injection Skeleton" ${LOCAL_PROJECT})
option(DIS_ENABLE_TESTING "Build examples for Dll Injection Skeleton" ${LOCAL_PROJECT})

# injected.dll
set(injected_src
"src/injected/injected.cc"
"src/injected/console.cc"
)
add_subdirectory(src)

add_library(injected MODULE ${injected_src})
if(DIS_BUILD_EXAMPLES)
add_subdirectory(samples)
endif()

if(DIS_ENABLE_TESTING)
add_subdirectory(tests)
endif()

install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/
DESTINATION include)

install(FILES
${CMAKE_CURRENT_SOURCE_DIR}/cmake/GurkaInjectorConfig.cmake
DESTINATION lib/cmake/gurka)
154 changes: 137 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,137 @@
# DllInjectionSkeleton
Just a very simple dll injection skeleton, including an injector and a dll to inject


# Building
This assumes that you have TDM-GCC (GCC 5.1.0), CMake (>= 3.0) and a healthy Bash environment (TDM-GCC and CMake in $PATH):

$ cd DllInjectorSkeleton
$ ./cmake.sh all

### To build debug
$ cd build_debug
$ mingw32-make.exe

### To build release
$ cd build_release
$ mingw32-make.exe
# Dll-Injection-Skeleton

The DLL Injector library provides a simple and efficient way to inject and eject DLLs into and from running processes on Windows. With support for both command-line interface and direct integration through CMake, this tool is ideal for developers needing to modify or debug application behavior dynamically. The library leverages Windows API functions to perform DLL injection and ejection, making it a versatile addition to any developer's toolkit. The CMake integration ensures seamless inclusion in larger projects, allowing immediate access to its functionality through easy linking.

## Build

### Prerequisites

Before building the project, ensure you have the following installed on your system:

- **CMake**: Version 3.0 or higher.
- **Visual Studio**: Recommended version for Windows development.

### Steps to Build

#### 1. Clone the Repository

Clone the "Dll-Injection-Skeleton" repository from GitHub:
```bash
cd path/to/dll-injection-skeleton
```

#### 2. Configure the Build with CMake

Run CMake to configure the build. Specify the generator appropriate for your installed Visual Studio version (e.g., "Visual Studio 2019"):

```bash
mkdir build
cd build
cmake -G "Visual Studio 16 2019" .. # Replace with your Visual Studio version
```

#### 3. Build the Project

Open the generated solution file (`dll-injection-skeleton.sln`) in Visual Studio and build the project:

- Select `Build` > `Build Solution` from the Visual Studio menu.
- Alternatively, build from the command line:
```bash
cmake --build . --config Release
```
Replace `Release` with `Debug` if you need a debug build.

#### 4. Running Examples (Optional)

If you enabled building examples (`DIS_BUILD_EXAMPLES` option), you can run them after building:

- Navigate to the `build` directory (if not already there).
- Run the built examples by injecting them into any process of your choice using **CLI** (See CLI).


## Integration

To integrate the **Dll-Injection-Skeleton** library into your project, follow these steps:

### Step 1: Clone the Repository

Clone the **Dll-Injection-Skeleton** repository from GitHub into your project's directory:
```bash
git clone https://github.com/gurka/DllInjectionSkeleton.git
```
### Step 2: Include as Subdirectory

Integrate the library into your CMake project by adding it as a subdirectory in your `CMakeLists.txt` file. Assuming your project's root directory is where you want to include **Dll-Injection-Skeleton**:
```cmake
add_subdirectory(path/to/DllInjectionSkeleton)
```

Replace `path/to/DllInjectionSkeleton` with the actual path relative to your project.

### Step 3: Link to the Library

Link your project to the **Dll-Injection-Skeleton** library target. For example, if your project is named `MyProject` and you want to link against `dll-injector` target from **Dll-Injection-Skeleton**, modify your `CMakeLists.txt`:
```cmake
target_link_libraries(MyProject PRIVATE dll-injector)
```

Make sure to replace `MyProject` with the actual name of your project.

### Step 4: Build Your Project

Build your project using CMake and your preferred build tool (e.g., Visual Studio, Ninja, etc.). Ensure that CMake correctly identifies and integrates **Dll-Injection-Skeleton** into your build system.

### Example CMakeLists.txt

Here is a simplified example of how your `CMakeLists.txt` might look after integrating **Dll-Injection-Skeleton**:
```cmake
cmake_minimum_required(VERSION 3.0)
project(MyProject)
# Include Dll-Injection-Skeleton as a subdirectory
add_subdirectory(path/to/DllInjectionSkeleton)
# Define your project's executable or library
add_executable(MyProject main.cpp)
# Link your project to Dll-Injection-Skeleton
target_link_libraries(MyProject PRIVATE dll-injector)
```

Adjust paths and configurations according to your project's structure and requirements.


## Usage & CLI

### Installation:
Follow instructions in the Build section to build using CMake or download the tool.

### CLI Usage

The `dll-injector-cli` tool supports two commands:

- **Inject Command**: Injects a DLL into a specified process.
```bash
dll-injector-cli inject [process name.exe] [dll path]
```
- **Eject Command**: Ejects a DLL from a specified process.
```bash
dll-injector-cli eject [process name.exe] [dll name]
```
### Example Usage

Examples of using `dll-injector-cli`:
```bash
# Inject DLL into a process
dll-injector-cli inject notepad.exe path/to/mydll.dll

# Eject DLL from a process
dll-injector-cli eject notepad.exe mydll.dll
```
Ensure the process is running and accessible to the user. Successful DLL operations depend on correct process and DLL path/name specifications.

### Notes

- Ensure appropriate permissions to perform DLL injection and ejection.
- Integrate the `dll-injector` target into your CMake projects for programmatic access to DLL injection capabilities, for more information check **Integration** Section.

20 changes: 0 additions & 20 deletions cmake.sh

This file was deleted.

1 change: 1 addition & 0 deletions cmake/GurkaInjectorConfig.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include(${CMAKE_CURRENT_LIST_DIR}/gurka-injector-targets.cmake)
14 changes: 14 additions & 0 deletions include/gurka/dll_injector.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <Windows.h>
#include <stdint.h>

namespace gurka {
uint64_t getModuleBaseAddress(DWORD processID, const char* dllName);
size_t findPids(const char* programName, size_t resPidsSz, DWORD* outPids);
bool injectDLL(HANDLE hProc, const char* dllFullPath);
bool injectDLL(DWORD procId, const char* dllFullPath);
bool unloadDLL(HANDLE hProc, const char* dllName);
bool unloadDLL(HANDLE hProc, const char* dllName);
bool loadDLL(const char* procName, const char* dllPath);
bool unloadDLL(DWORD procId, const char* dllName);
bool unloadDLL(const char* procName, const char* dllPath);
}
5 changes: 5 additions & 0 deletions include/gurka/dll_injector_cli.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

namespace gurka {
int dll_injector_main(int argc, const char** argv);
}
1 change: 1 addition & 0 deletions samples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_subdirectory(injected)
5 changes: 5 additions & 0 deletions samples/injected/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
add_library(injected SHARED injected.cc console.cc)
target_include_directories(injected PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../../include)

add_library(injected_macro_path INTERFACE)
target_compile_definitions(injected_macro_path INTERFACE INJECTED_PATH="${CMAKE_CURRENT_BINARY_DIR}")
2 changes: 1 addition & 1 deletion src/injected/console.cc → samples/injected/console.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,6 @@ void Console::write(const char* format, ...)
vsnprintf(message, sizeof(message), format, args);
va_end(args);

WriteConsoleA(consoleHandle, message, strlen(message), nullptr, nullptr);
WriteConsoleA(consoleHandle, message, (DWORD)strlen(message), nullptr, nullptr);
}
}
File renamed without changes.
12 changes: 9 additions & 3 deletions src/injected/injected.cc → samples/injected/injected.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,23 @@

#include "console.h"

static Console console;
HINSTANCE ghInstance;

void injectedMain()
{
static Console console;

console.init("Injected DLL");

console.write("hey its me ur brother");

MessageBoxA(NULL, "Hi", NULL, NULL);
}

extern "C" __declspec(dllexport) BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
ghInstance = hinstDLL;
(void)lpvReserved;

static std::thread injectedMainThread;

switch (fdwReason)
Expand Down
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
add_subdirectory(dll-injector)
add_subdirectory(cli)
11 changes: 11 additions & 0 deletions src/cli/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../../bin)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../../bin)

add_executable(dll-injector-cli dll_injector_cli.cpp cli.cpp)
target_link_libraries(dll-injector-cli gurka::injector)

if(CMAKE_SIZEOF_VOID_P EQUAL 8)
set_target_properties(dll-injector-cli PROPERTIES OUTPUT_NAME dll-injector-cli-64)
else()
set_target_properties(dll-injector-cli PROPERTIES OUTPUT_NAME dll-injector-cli)
endif()
8 changes: 8 additions & 0 deletions src/cli/cli.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <gurka/dll_injector_cli.h>

using namespace gurka;

int main(int argc, const char* argv[])
{
return dll_injector_main(argc, argv);
}
Loading