Skip to content

Commit

Permalink
Linux embedding tools (#15)
Browse files Browse the repository at this point in the history
* linux support for encoder and generator

* bump lz4 to 1.10.0

* fix compilation under MSVC

* fix typos

* add linux build to github actions
  • Loading branch information
dumbasPL authored Dec 27, 2024
1 parent 7f8fe55 commit b6a3e95
Show file tree
Hide file tree
Showing 11 changed files with 375 additions and 279 deletions.
59 changes: 51 additions & 8 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ on:
- v*

jobs:
build:
build-windows:
runs-on: windows-latest
steps:
- name: Checkout
Expand All @@ -24,10 +24,51 @@ jobs:
configurePreset: x64-windows
buildPreset: Release

- name: Upload artifacts
uses: actions/upload-artifact@v4
with:
name: fumo-windows
path: build/bin/Release/*
if-no-files-found: error
retention-days: 1

build-linux:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Install CMake
uses: lukka/get-cmake@latest

- name: Build
uses: lukka/run-cmake@v10
with:
configurePreset: linux-embedding-tools
buildPreset: Linux Embedding Tools

- name: Upload artifacts
uses: actions/upload-artifact@v4
with:
name: fumo-linux
path: build/bin/*
if-no-files-found: error
retention-days: 1

publish:
runs-on: ubuntu-latest
needs: [build-windows, build-linux]
steps:
- name: Download artifacts
uses: actions/download-artifact@v4
with:
pattern: fumo-*
merge-multiple: true

- name: ZIP embedding tools
working-directory: build/bin/Release
run: |
7z a -tzip -mx=9 -r embedding_tools.zip fumo_encoder.exe fumo_generator.exe initial_loader.bin stage1.dll
7z a -tzip -mx=9 -r embedding_tools_linux.zip fumo_encoder fumo_generator initial_loader.bin stage1.dll
- name: Publish release (version tag)
if: startsWith(github.ref, 'refs/tags/v')
Expand All @@ -37,9 +78,10 @@ jobs:
prerelease: false
draft: true
files: |
build/bin/Release/fumo.exe
build/bin/Release/fumo_encoder.exe
build/bin/Release/embedding_tools.zip
fumo.exe
fumo_encoder.exe
embedding_tools.zip
embedding_tools_linux.zip
- name: Publish pre-release (push to master)
if: github.ref == 'refs/heads/master'
Expand All @@ -50,6 +92,7 @@ jobs:
title: Development build
prerelease: true
files: |
build/bin/Release/fumo.exe
build/bin/Release/fumo_encoder.exe
build/bin/Release/embedding_tools.zip
fumo.exe
fumo_encoder.exe
embedding_tools.zip
embedding_tools_linux.zip
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@ include(cmake/CPM.cmake)
set(FUMO_DRIVER_DEBUG OFF CACHE BOOL "Enable driver debug logs/force reload")
set(FUMO_DEBUG OFF CACHE BOOL "Disable creating new executables")

if (FUMO_DEBUG)
add_compile_definitions(FUMO_DEBUG)
endif()

add_subdirectory(src)
18 changes: 18 additions & 0 deletions CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,19 @@
"FUMO_DEBUG": "ON",
"FUMO_DRIVER_DEBUG": "ON"
}
},
{
"name": "linux-embedding-tools",
"binaryDir": "${sourceDir}/build",
"generator": "Ninja",
"architecture": {
"value": "x64",
"strategy": "external"
},
"cacheVariables": {
"FUMO_DEBUG": "OFF",
"FUMO_DRIVER_DEBUG": "OFF"
}
}
],
"buildPresets": [
Expand All @@ -38,6 +51,11 @@
"name": "Debug",
"configurePreset": "x64-windows-debug",
"configuration": "Release"
},
{
"name": "Linux Embedding Tools",
"configurePreset": "linux-embedding-tools",
"configuration": "Release"
}
]
}
35 changes: 13 additions & 22 deletions cmake/CPM.cmake
Original file line number Diff line number Diff line change
@@ -1,33 +1,24 @@
set(CPM_DOWNLOAD_VERSION 0.38.2)
# SPDX-License-Identifier: MIT
#
# SPDX-FileCopyrightText: Copyright (c) 2019-2023 Lars Melchior and contributors

set(CPM_DOWNLOAD_VERSION 0.40.4)
set(CPM_HASH_SUM "67dcc1deb6e12a2f0705647ccc5f7023e3d15746b944e14352b82373e09b8a0a")

if(CPM_SOURCE_CACHE)
set(CPM_DOWNLOAD_LOCATION "${CPM_SOURCE_CACHE}/cpm/CPM_${CPM_DOWNLOAD_VERSION}.cmake")
set(CPM_DOWNLOAD_LOCATION "${CPM_SOURCE_CACHE}/cpm/CPM_${CPM_DOWNLOAD_VERSION}.cmake")
elseif(DEFINED ENV{CPM_SOURCE_CACHE})
set(CPM_DOWNLOAD_LOCATION "$ENV{CPM_SOURCE_CACHE}/cpm/CPM_${CPM_DOWNLOAD_VERSION}.cmake")
set(CPM_DOWNLOAD_LOCATION "$ENV{CPM_SOURCE_CACHE}/cpm/CPM_${CPM_DOWNLOAD_VERSION}.cmake")
else()
set(CPM_DOWNLOAD_LOCATION "${CMAKE_BINARY_DIR}/cmake/CPM_${CPM_DOWNLOAD_VERSION}.cmake")
set(CPM_DOWNLOAD_LOCATION "${CMAKE_BINARY_DIR}/cmake/CPM_${CPM_DOWNLOAD_VERSION}.cmake")
endif()

# Expand relative path. This is important if the provided path contains a tilde (~)
get_filename_component(CPM_DOWNLOAD_LOCATION ${CPM_DOWNLOAD_LOCATION} ABSOLUTE)

function(download_cpm)
message(STATUS "Downloading CPM.cmake to ${CPM_DOWNLOAD_LOCATION}")
file(DOWNLOAD
https://github.com/cpm-cmake/CPM.cmake/releases/download/v${CPM_DOWNLOAD_VERSION}/CPM.cmake
${CPM_DOWNLOAD_LOCATION}
)
endfunction()

if(NOT (EXISTS ${CPM_DOWNLOAD_LOCATION}))
download_cpm()
else()
# resume download if it previously failed
file(READ ${CPM_DOWNLOAD_LOCATION} check)
if("${check}" STREQUAL "")
download_cpm()
endif()
unset(check)
endif()
file(DOWNLOAD
https://github.com/cpm-cmake/CPM.cmake/releases/download/v${CPM_DOWNLOAD_VERSION}/CPM.cmake
${CPM_DOWNLOAD_LOCATION} EXPECTED_HASH SHA256=${CPM_HASH_SUM}
)

include(${CPM_DOWNLOAD_LOCATION})
110 changes: 58 additions & 52 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,73 +1,79 @@
# disable C4711, C5045, C4820 (caused by lz4)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd4711 /wd5045 /wd4820")

CPMAddPackage(
NAME xorstr
GITHUB_REPOSITORY JustasMasiulis/xorstr
GIT_TAG master
DOWNLOAD_ONLY True
)

if (xorstr_ADDED)
add_library(xorstr INTERFACE)
target_include_directories(xorstr INTERFACE ${xorstr_SOURCE_DIR}/include)
endif()

CPMAddPackage(
NAME lazy_importer
GITHUB_REPOSITORY JustasMasiulis/lazy_importer
GIT_TAG master
DOWNLOAD_ONLY True
)

if (lazy_importer_ADDED)
add_library(lazy_importer INTERFACE)
target_include_directories(lazy_importer INTERFACE ${lazy_importer_SOURCE_DIR}/include)
if(MSVC)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd4711 /wd5045 /wd4820")
endif()

CPMAddPackage(
NAME lz4
GITHUB_REPOSITORY lz4/lz4
VERSION 1.9.4
VERSION 1.10.0
SOURCE_SUBDIR build/cmake
OPTIONS
"LZ4_BUILD_CLI OFF"
"LZ4_BUILD_LEGACY_LZ4C OFF"
)
find_package(lz4 REQUIRED)

CPMAddPackage("gh:SergiusTheBest/FindWDK#master")
list(APPEND CMAKE_MODULE_PATH "${FindWDK_SOURCE_DIR}/cmake")
find_package(WDK REQUIRED)
CPMAddPackage("gh:dumbasPL/linux-pe#master")
find_package(linux-pe REQUIRED)

if (CMAKE_SYSTEM_NAME STREQUAL "Windows")
CPMAddPackage(
NAME xorstr
GITHUB_REPOSITORY JustasMasiulis/xorstr
GIT_TAG master
DOWNLOAD_ONLY True
)

if (xorstr_ADDED)
add_library(xorstr INTERFACE)
target_include_directories(xorstr INTERFACE ${xorstr_SOURCE_DIR}/include)
endif()

CPMAddPackage(
NAME lazy_importer
GITHUB_REPOSITORY JustasMasiulis/lazy_importer
GIT_TAG master
DOWNLOAD_ONLY True
)

CPMAddPackage("gh:dumbasPL/[email protected]")
if (lazy_importer_ADDED)
add_library(lazy_importer INTERFACE)
target_include_directories(lazy_importer INTERFACE ${lazy_importer_SOURCE_DIR}/include)
endif()

if (FUMO_DEBUG)
add_compile_definitions(FUMO_DEBUG)
CPMAddPackage("gh:SergiusTheBest/FindWDK#master")
list(APPEND CMAKE_MODULE_PATH "${FindWDK_SOURCE_DIR}/cmake")
find_package(WDK REQUIRED)

CPMAddPackage("gh:dumbasPL/[email protected]")

add_subdirectory(driver)
add_subdirectory(driver_interface)
add_subdirectory(resource_generator)
add_subdirectory(stage1)
add_subdirectory(stage2)
add_subdirectory(initial_loader)
add_subdirectory(shellcode_extractor)
endif()

add_subdirectory(driver)
add_subdirectory(driver_interface)
add_subdirectory(resource_generator)
add_subdirectory(stage1)
add_subdirectory(stage2)
add_subdirectory(initial_loader)
add_subdirectory(shellcode_extractor)
add_subdirectory(fumo_generator)
add_subdirectory(encoder)

add_custom_command(
COMMENT "building final executable"
DEPENDS fumo_generator initial_loader stage1
$<TARGET_PROPERTY:initial_loader,RUNTIME_OUTPUT_DIRECTORY>/initial_loader.bin
$<TARGET_FILE:stage1>
COMMAND fumo_generator ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${CMAKE_CFG_INTDIR}/fumo.exe
$<TARGET_PROPERTY:initial_loader,RUNTIME_OUTPUT_DIRECTORY>/initial_loader.bin
$<TARGET_FILE:stage1>
OUTPUT ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${CMAKE_CFG_INTDIR}/fumo.exe
)
if (CMAKE_SYSTEM_NAME STREQUAL "Windows")
add_custom_command(
COMMENT "building final executable"
DEPENDS fumo_generator initial_loader stage1
$<TARGET_PROPERTY:initial_loader,RUNTIME_OUTPUT_DIRECTORY>/initial_loader.bin
$<TARGET_FILE:stage1>
COMMAND fumo_generator ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${CMAKE_CFG_INTDIR}/fumo.exe
$<TARGET_PROPERTY:initial_loader,RUNTIME_OUTPUT_DIRECTORY>/initial_loader.bin
$<TARGET_FILE:stage1>
OUTPUT ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${CMAKE_CFG_INTDIR}/fumo.exe
)

add_custom_target(
fumo ALL
DEPENDS ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${CMAKE_CFG_INTDIR}/fumo.exe
)
add_custom_target(
fumo ALL
DEPENDS ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${CMAKE_CFG_INTDIR}/fumo.exe
)
endif()
16 changes: 9 additions & 7 deletions src/encoder/fumo_encoder.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#include <Windows.h>
#include <iostream>
#include <fstream>
#include <vector>
Expand All @@ -7,8 +6,8 @@
#include <filesystem>
#include <stdint.h>
#include <ctime>
#include <fomo_common.h>
#include <util.h>
#include <random>
#include <fumo_data_header.h>
#include <lz4.h>

int main(int argc, char** argv) {
Expand Down Expand Up @@ -74,10 +73,13 @@ int main(int argc, char** argv) {
compressed_data.resize(compressed_size);

// generate xor key
std::srand(std::time(nullptr));
uint64_t xor_key = 0;
for (int i = 0; i < 8; i++)
xor_key |= (std::rand() % 256) << (i * 8);
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<unsigned long long> dis(
std::numeric_limits<std::uint64_t>::min(),
std::numeric_limits<std::uint64_t>::max()
);
uint64_t xor_key = dis(gen);

// pad to 8 bytes
int padding = 8 - (compressed_data.size() % 8);
Expand Down
1 change: 1 addition & 0 deletions src/fumo_generator/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ add_executable(fumo_generator fumo_generator.cpp)
target_compile_features(fumo_generator PUBLIC c_std_17 cxx_std_20)
target_compile_definitions(fumo_generator PRIVATE UNICODE _UNICODE)
target_include_directories(fumo_generator PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../include)
target_link_libraries(fumo_generator PRIVATE linux-pe)
Loading

0 comments on commit b6a3e95

Please sign in to comment.