From cc5b3ce7d280fbc0c9e60b3e51da5dbad4a30ae5 Mon Sep 17 00:00:00 2001 From: Jonathan Date: Sat, 7 Nov 2015 23:54:37 +0000 Subject: [PATCH 1/2] Arguments --- CMakeLists.txt | 2 +- Handle.cpp | 2 +- main.cpp | 65 ++++++++++++++++++++++---------------------------- 3 files changed, 30 insertions(+), 39 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c9b0e65..5a8e4d8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,4 +4,4 @@ project(linux_manual_map) set(SOURCE_FILES main.cpp FileSystem.cpp Handle.cpp Loader.cpp Region.cpp Module.cpp Elf.cpp Elf64.cpp Elf32.cpp) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") -add_executable(linux_manual_map ${SOURCE_FILES}) \ No newline at end of file +add_executable(linux_manual_map ${SOURCE_FILES}) diff --git a/Handle.cpp b/Handle.cpp index 595f436..972f922 100644 --- a/Handle.cpp +++ b/Handle.cpp @@ -193,4 +193,4 @@ bool remote::WriteProcessMemory(Handle hProcess, void* lpBaseAddress, void* lpBu *lpNumberOfBytesWritten = (size_t) result; return (((size_t) result) == nSize); -} \ No newline at end of file +} diff --git a/main.cpp b/main.cpp index ca9fc89..30a3d39 100644 --- a/main.cpp +++ b/main.cpp @@ -1,45 +1,36 @@ #include "main.hpp" -using namespace std; +int main(int argc, char **argv) { -int main() { - - remote::Handle proc = remote::OpenProcess(5388); - - if(proc.IsRunning()) { - remote::Module libc = remote::GetModuleHandle(proc, "libpthread-2.19.so"); - - if(libc.IsValid()) { - // We need to grab symbol list - cout << "libc = " << hex << libc.GetStart() << endl; - } - - /* - std::vector rs = remote::GetRegions(proc.pid); - - for(size_t i = 0; i < rs.size(); i++) { - if(!rs[i].filename.empty()) { - std::cout << "Filename: " << rs[i].filename << std::endl; - } - - if(!rs[i].pathname.empty()) { - std::cout << "Path: " << rs[i].pathname << std::endl; - } - - std::cout << "Start: " << std::hex << rs[i].start << std::endl; - std::cout << "End: " << std::hex << rs[i].end << std::endl; - std::cout << "Permissions: " << rs[i].read << rs[i].write << rs[i].exec << rs[i].shared << std::endl; - std::cout << "Offset: " << std::hex << rs[i].offset << std::endl; - std::cout << "Device: " << std::dec << rs[i].deviceMajor << ":" << rs[i].deviceMinor << std::endl; - std::cout << "INode: " << std::dec << rs[i].inodeFileNumber << std::endl; - std::cout << "-------------------------------------" << std::endl; - } + if(argc != 2) { + std::cout << "Error: please provide a process id!\n"; + return -1; + } - size_t calc_base = proc.GetModuleAddress("gnome-calculator"); + remote::Handle proc = remote::OpenProcess(atoi(argv[1])); + + if(!proc.IsRunning()) { + std::cout << "Error: process is not running!\n"; + return -1; + } - cout << "Calculator Base: " << hex << calc_base << endl; - */ + std::vector rs = remote::GetRegions(proc.pid); + + for(size_t i = 0; i < rs.size(); i++) { + if(!rs[i].filename.empty()) + std::cout << "Filename: " << rs[i].filename << std::endl; + + if(!rs[i].pathname.empty()) + std::cout << "Path: " << rs[i].pathname << std::endl; + + std::cout << "Start: " << std::hex << rs[i].start << std::endl; + std::cout << "End: " << std::hex << rs[i].end << std::endl; + std::cout << "Permissions: " << rs[i].read << rs[i].write << rs[i].exec << rs[i].shared << std::endl; + std::cout << "Offset: " << std::hex << rs[i].offset << std::endl; + std::cout << "Device: " << std::dec << rs[i].deviceMajor << ":" << rs[i].deviceMinor << std::endl; + std::cout << "INode: " << std::dec << rs[i].inodeFileNumber << std::endl; + std::cout << "-------------------------------------" << std::endl; } return 0; -} \ No newline at end of file +} From 38ebba7bcb693774cd712547b989876124f420d8 Mon Sep 17 00:00:00 2001 From: Jonathan Date: Wed, 11 Nov 2015 14:39:54 +0000 Subject: [PATCH 2/2] Add dummy program --- CMakeLists.txt | 28 ++++++++++++- Elf.hpp => include/Elf.hpp | 0 FileSystem.hpp => include/FileSystem.hpp | 0 Handle.hpp => include/Handle.hpp | 0 Loader.hpp => include/Loader.hpp | 0 Module.hpp => include/Module.hpp | 0 Region.hpp => include/Region.hpp | 0 main.hpp => include/main.hpp | 0 Elf.cpp => src/Elf.cpp | 0 Elf32.cpp => src/Elf32.cpp | 0 Elf64.cpp => src/Elf64.cpp | 0 FileSystem.cpp => src/FileSystem.cpp | 0 Handle.cpp => src/Handle.cpp | 0 Loader.cpp => src/Loader.cpp | 0 Module.cpp => src/Module.cpp | 0 Region.cpp => src/Region.cpp | 0 main.cpp => src/main.cpp | 0 test/CMakeLists.txt | 6 +++ test/dummy.c | 51 ++++++++++++++++++++++++ 19 files changed, 84 insertions(+), 1 deletion(-) rename Elf.hpp => include/Elf.hpp (100%) rename FileSystem.hpp => include/FileSystem.hpp (100%) rename Handle.hpp => include/Handle.hpp (100%) rename Loader.hpp => include/Loader.hpp (100%) rename Module.hpp => include/Module.hpp (100%) rename Region.hpp => include/Region.hpp (100%) rename main.hpp => include/main.hpp (100%) rename Elf.cpp => src/Elf.cpp (100%) rename Elf32.cpp => src/Elf32.cpp (100%) rename Elf64.cpp => src/Elf64.cpp (100%) rename FileSystem.cpp => src/FileSystem.cpp (100%) rename Handle.cpp => src/Handle.cpp (100%) rename Loader.cpp => src/Loader.cpp (100%) rename Module.cpp => src/Module.cpp (100%) rename Region.cpp => src/Region.cpp (100%) rename main.cpp => src/main.cpp (100%) create mode 100644 test/CMakeLists.txt create mode 100644 test/dummy.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 5a8e4d8..e512ff8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,33 @@ cmake_minimum_required(VERSION 2.8.4) + +if (WIN32 OR APPLE) + message(FATAL_ERROR "Windows or Mac OSX is not supported") +endif (WIN32 OR APPLE) + project(linux_manual_map) -set(SOURCE_FILES main.cpp FileSystem.cpp Handle.cpp Loader.cpp Region.cpp Module.cpp Elf.cpp Elf64.cpp Elf32.cpp) +set(CMAKE_BINARY_DIR ${CMAKE_SOURCE_DIR}/build) + +set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}) + +set(PROJECT_INCLUDE_DIR ${PROJECT_SOURCE_DIR}/include) + +include_directories("${PROJECT_INCLUDE_DIR}") +include_directories("${PROJECT_SOURCE_DIR}") + +set(SOURCE_FILES +${PROJECT_SOURCE_DIR}/src/main.cpp +${PROJECT_SOURCE_DIR}/src/FileSystem.cpp +${PROJECT_SOURCE_DIR}/src/Handle.cpp +${PROJECT_SOURCE_DIR}/src/Loader.cpp +${PROJECT_SOURCE_DIR}/src/Region.cpp +${PROJECT_SOURCE_DIR}/src/Module.cpp +${PROJECT_SOURCE_DIR}/src/Elf.cpp +${PROJECT_SOURCE_DIR}/src/Elf64.cpp +${PROJECT_SOURCE_DIR}/src/Elf32.cpp) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") add_executable(linux_manual_map ${SOURCE_FILES}) + +add_subdirectory(test) \ No newline at end of file diff --git a/Elf.hpp b/include/Elf.hpp similarity index 100% rename from Elf.hpp rename to include/Elf.hpp diff --git a/FileSystem.hpp b/include/FileSystem.hpp similarity index 100% rename from FileSystem.hpp rename to include/FileSystem.hpp diff --git a/Handle.hpp b/include/Handle.hpp similarity index 100% rename from Handle.hpp rename to include/Handle.hpp diff --git a/Loader.hpp b/include/Loader.hpp similarity index 100% rename from Loader.hpp rename to include/Loader.hpp diff --git a/Module.hpp b/include/Module.hpp similarity index 100% rename from Module.hpp rename to include/Module.hpp diff --git a/Region.hpp b/include/Region.hpp similarity index 100% rename from Region.hpp rename to include/Region.hpp diff --git a/main.hpp b/include/main.hpp similarity index 100% rename from main.hpp rename to include/main.hpp diff --git a/Elf.cpp b/src/Elf.cpp similarity index 100% rename from Elf.cpp rename to src/Elf.cpp diff --git a/Elf32.cpp b/src/Elf32.cpp similarity index 100% rename from Elf32.cpp rename to src/Elf32.cpp diff --git a/Elf64.cpp b/src/Elf64.cpp similarity index 100% rename from Elf64.cpp rename to src/Elf64.cpp diff --git a/FileSystem.cpp b/src/FileSystem.cpp similarity index 100% rename from FileSystem.cpp rename to src/FileSystem.cpp diff --git a/Handle.cpp b/src/Handle.cpp similarity index 100% rename from Handle.cpp rename to src/Handle.cpp diff --git a/Loader.cpp b/src/Loader.cpp similarity index 100% rename from Loader.cpp rename to src/Loader.cpp diff --git a/Module.cpp b/src/Module.cpp similarity index 100% rename from Module.cpp rename to src/Module.cpp diff --git a/Region.cpp b/src/Region.cpp similarity index 100% rename from Region.cpp rename to src/Region.cpp diff --git a/main.cpp b/src/main.cpp similarity index 100% rename from main.cpp rename to src/main.cpp diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt new file mode 100644 index 0000000..883fd80 --- /dev/null +++ b/test/CMakeLists.txt @@ -0,0 +1,6 @@ +project(unit_tests) + +include_directories(${CMAKE_CURRENT_SOURCE_DIR}) +include_directories(${CMAKE_CURRENT_BINARY_DIR}) + +add_executable(dummy dummy.c) \ No newline at end of file diff --git a/test/dummy.c b/test/dummy.c new file mode 100644 index 0000000..721fe2f --- /dev/null +++ b/test/dummy.c @@ -0,0 +1,51 @@ +#ifndef _GNU_SOURCE + #define _GNU_SOURCE +#endif +#include +#include +#include +#include +#include +#include +#include +#include +#if __WORDSIZE == 64 + #define LX "%lx" + #define LU "%lu" +#else + #define LX "%x" + #define LU "%u" +#endif + +static int counter = 0; +void myfun() +{ + printf("%d: I am here in %s on %d\n", counter++, + __func__, __LINE__); + if (counter >= INT32_MAX) + counter = 0; +} + +int main() +{ + intptr_t here0 = 0; + intptr_t here1 = 0; + const char *str = "Hello World!"; + size_t len = strlen(str); + here0 = (intptr_t)syscall(SYS_brk, 0); + here1 = (intptr_t)syscall(SYS_brk, here0 + len + 1); + printf("Starting dummy 0x"LX" 0x"LX"\n", here0, here1); + printf("Dummy pid: %i\n", getpid()); + memcpy((void *)here0, str, len + 1); + printf("String: %s\n", (const char *)here0); + syscall(SYS_brk, here0); + while (1) { + struct timeval tv = { 0 }; + sleep(2); + gettimeofday(&tv, NULL); + printf("Working "LU"."LU"\n", (size_t)tv.tv_sec, (size_t)tv.tv_usec); + myfun(); + } + printf("Stopping dummy\n"); + return 0; +}