Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Almost ported
  • Loading branch information
tarekwiz committed Mar 6, 2019
commit 3377b2dffb4f1a3fdfabba8274e881828f6191f8
Binary file modified .DS_Store
Binary file not shown.
Binary file modified NativeCore/.DS_Store
Binary file not shown.
Binary file added NativeCore/Dependencies/.DS_Store
Binary file not shown.
Binary file added NativeCore/Dependencies/distorm/.DS_Store
Binary file not shown.
Binary file modified NativeCore/Unix/.DS_Store
Binary file not shown.
13 changes: 6 additions & 7 deletions NativeCore/Unix/ControlRemoteProcess.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
//#include <sys/types.h>
#include <csignal>

#if __APPLE__
#include <sys/proc_info.h>
#include <libproc.h>
#include <mach/mach_init.h>
#include <mach/mach_vm.h>
#endif
#include "NativeCore.hpp"

extern "C" void RC_CallConv ControlRemoteProcess(RC_Pointer handle, ControlRemoteProcessAction action)
Expand All @@ -14,13 +20,6 @@ extern "C" void RC_CallConv ControlRemoteProcess(RC_Pointer handle, ControlRemot
{
signal = SIGCONT;
}
#ifdef __linux__
kill(static_cast<pid_t>(reinterpret_cast<intptr_t>(handle)), signal);
#elif __APPLE__
task_t task;

task_for_pid(current_task(), (int)id, &task);
return (RC_Pointer)task;
#endif

}
29 changes: 23 additions & 6 deletions NativeCore/Unix/Debugger.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
#include <sys/ptrace.h>

#include <sys/types.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <sys/user.h>
#ifdef __linux__
#include <sys/ptrace.h>
#include <experimental/filesystem>
#include <cstddef>
#endif


#include "NativeCore.hpp"

#ifdef __linux__
namespace fs = std::experimental::filesystem;
#endif

int ualarm(unsigned int milliseconds)
{
Expand All @@ -24,6 +29,7 @@ int ualarm(unsigned int milliseconds)

pid_t waitpid_timeout(pid_t pid, int* status, int options, int timeoutInMilliseconds, bool& timedOut)
{
#ifdef __linux__
struct sigaction sig = {};
sig.sa_flags = 0;
sig.sa_handler = [](int) {};
Expand All @@ -44,6 +50,9 @@ pid_t waitpid_timeout(pid_t pid, int* status, int options, int timeoutInMillisec
timedOut = false;
}
return res;
#else
return 0;
#endif
}

pid_t waitpid_timeout(int* status, int timeoutInMilliseconds, bool& timedOut)
Expand All @@ -54,25 +63,27 @@ pid_t waitpid_timeout(int* status, int timeoutInMilliseconds, bool& timedOut)
extern "C" bool RC_CallConv AttachDebuggerToProcess(RC_Pointer id)
{
//TODO: Attach to all threads.

#ifdef __linux__
ptrace(PTRACE_ATTACH, static_cast<pid_t>(reinterpret_cast<intptr_t>(id)), nullptr, nullptr);

waitpid(-1, nullptr, 0);

ptrace(PTRACE_CONT, static_cast<pid_t>(reinterpret_cast<intptr_t>(id)), nullptr, nullptr);

#endif
return false;
}

extern "C" void RC_CallConv DetachDebuggerFromProcess(RC_Pointer id)
{
//TODO: Detach to all threads.

#ifdef __linux__
ptrace(PTRACE_DETACH, static_cast<pid_t>(reinterpret_cast<intptr_t>(id)), nullptr, nullptr);
#endif
}

extern "C" bool RC_CallConv AwaitDebugEvent(DebugEvent* evt, int timeoutInMilliseconds)
{
#ifdef __linux__
int status;
bool timedOut;

Expand Down Expand Up @@ -167,10 +178,14 @@ extern "C" bool RC_CallConv AwaitDebugEvent(DebugEvent* evt, int timeoutInMillis
}

return result;
#else
return false;
#endif
}

extern "C" void RC_CallConv HandleDebugEvent(DebugEvent* evt)
{
#ifdef __linux__
auto tid = static_cast<pid_t>(reinterpret_cast<intptr_t>(evt->ThreadId));

siginfo_t si;
Expand All @@ -194,10 +209,12 @@ extern "C" void RC_CallConv HandleDebugEvent(DebugEvent* evt)

ptrace(PTRACE_CONT, tid, nullptr, signal);
}
#endif
}

extern "C" bool RC_CallConv SetHardwareBreakpoint(RC_Pointer id, RC_Pointer address, HardwareBreakpointRegister reg, HardwareBreakpointTrigger type, HardwareBreakpointSize size, bool set)
{
#ifdef __linux__
if (reg == HardwareBreakpointRegister::InvalidRegister)
{
return false;
Expand Down Expand Up @@ -295,6 +312,6 @@ extern "C" bool RC_CallConv SetHardwareBreakpoint(RC_Pointer id, RC_Pointer addr
}
}
}

#endif
return true;
}
126 changes: 91 additions & 35 deletions NativeCore/Unix/EnumerateProcesses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,57 @@
#include <string>
#include <sstream>
#include <fstream>
#ifdef __linux__
#include <experimental/filesystem>

#elif __APPLE__
#include <sys/proc_info.h>
#include <libproc.h>
#endif
#include "NativeCore.hpp"

#ifdef __linux__
namespace fs = std::experimental::filesystem;
#endif

// std::filesystem library doesn't work @Ubuntu 16.10, read_symlink() always fails.
#define USE_CUSTOM_READ_SYMLINK

#ifdef USE_CUSTOM_READ_SYMLINK
#include <unistd.h>

fs::path my_read_symlink(const fs::path& p, std::error_code& ec)
{
fs::path symlink_path;

std::string temp(64, '\0');
for (;; temp.resize(temp.size() * 2))
{
ssize_t result;
if ((result = ::readlink(p.c_str(), /*temp.data()*/ &temp[0], temp.size())) == -1)
{
ec.assign(errno, std::system_category());
break;
}
else
{
if (result != static_cast<ssize_t>(temp.size()))
{
symlink_path = fs::path(std::string(temp.begin(), temp.begin() + result));

ec.clear();

break;
}
}
}

return symlink_path;
}

// std::filesystem library doesn't work @Ubuntu 16.10, read_symlink() always fails.
#ifdef __linux__
#define USE_CUSTOM_READ_SYMLINK

#ifdef USE_CUSTOM_READ_SYMLINK
#include <unistd.h>

fs::path my_read_symlink(const fs::path& p, std::error_code& ec)
{
fs::path symlink_path;

std::string temp(64, '\0');
for (;; temp.resize(temp.size() * 2))
{
ssize_t result;
if ((result = ::readlink(p.c_str(), /*temp.data()*/ &temp[0], temp.size())) == -1)
{
ec.assign(errno, std::system_category());
break;
}
else
{
if (result != static_cast<ssize_t>(temp.size()))
{
symlink_path = fs::path(std::string(temp.begin(), temp.begin() + result));

ec.clear();

break;
}
}
}

return symlink_path;
}

#endif
#endif

enum class Platform
Expand Down Expand Up @@ -85,7 +95,7 @@ extern "C" void RC_CallConv EnumerateProcesses(EnumerateProcessCallback callback
{
return;
}

#ifdef __linux__
fs::path procPath("/proc");
if (fs::is_directory(procPath))
{
Expand Down Expand Up @@ -134,4 +144,50 @@ extern "C" void RC_CallConv EnumerateProcesses(EnumerateProcessCallback callback
}
}
}
#elif __APPLE__
int procCnt = proc_listpids(PROC_ALL_PIDS, 0, NULL, 0);
pid_t pids[1024];
memset(pids, 0, sizeof pids);
proc_listpids(PROC_ALL_PIDS, 0, pids, sizeof(pids));

for (int i = 0; i < procCnt; i++)
{
if (!pids[i]) continue;
char curPath[PROC_PIDPATHINFO_MAXSIZE];
char curName[PROC_PIDPATHINFO_MAXSIZE];
memset(curPath, 0, sizeof curPath);
proc_pidpath(pids[i], curPath, sizeof curPath);
int len = strlen(curPath);
if (len)
{
int pos = len;
while (pos && curPath[pos] != '/') --pos;
strcpy(curName, curPath + pos + 1);

struct proc_bsdinfo bsd_info;
int error = proc_pidinfo (pids[i], PROC_PIDTBSDINFO, (uint64_t) 0, &bsd_info, PROC_PIDTBSDINFO_SIZE);
if (error == 0)
continue;

auto platform = Platform::X86;

if (bsd_info.pbi_flags & PROC_FLAG_LP64)
platform = Platform::X64;

#ifdef RECLASSNET64
if (platform == Platform::X64)
#else
if (platform == Platform::X86)
#endif
{
EnumerateProcessData data = {};
data.Id = (size_t)pids[i];
MultiByteToUnicode(curPath, data.Path, PATH_MAXIMUM_LENGTH);
MultiByteToUnicode(curName, data.Name, PATH_MAXIMUM_LENGTH);
callbackProcess(&data);
}

}
}
#endif
}
4 changes: 4 additions & 0 deletions NativeCore/Unix/EnumerateRemoteSectionsAndModules.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ std::istream& operator >> (std::istream& s, SectionProtection& protection)

extern "C" void RC_CallConv EnumerateRemoteSectionsAndModules(RC_Pointer handle, EnumerateRemoteSectionsCallback callbackSection, EnumerateRemoteModulesCallback callbackModule)
{
#ifdef __APPLE__
return;
#endif

if (callbackSection == nullptr && callbackModule == nullptr)
{
return;
Expand Down
40 changes: 20 additions & 20 deletions NativeCore/Unix/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ INC = -I../Dependencies/distorm/include
CFLAGS = -Wall -fPIC -DRECLASSNET64=1
RESINC =
LIBDIR =
LIB = -lstdc++fs -lstdc++ -lc++experimental
LDFLAGS = -shared -Wl,--no-undefined
LIB = -lstdc++
LDFLAGS = --shared -Wl

INC_DEBUG = $(INC)
CFLAGS_DEBUG = $(CFLAGS) -g
Expand Down Expand Up @@ -91,31 +91,31 @@ $(OBJDIR_DEBUG)/CloseRemoteProcess.o: CloseRemoteProcess.cpp
$(CXX) $(CFLAGS_DEBUG) $(INC_DEBUG) -c CloseRemoteProcess.cpp -o $(OBJDIR_DEBUG)/CloseRemoteProcess.o

$(OBJDIR_DEBUG)/decoder.o: ../Dependencies/distorm/src/decoder.c
$(CXX) $(CFLAGS_DEBUG) $(INC_DEBUG) -c ../Dependencies/distorm/src/decoder.c -o $(OBJDIR_DEBUG)/decoder.o
$(CC) $(CFLAGS_DEBUG) $(INC_DEBUG) -c ../Dependencies/distorm/src/decoder.c -o $(OBJDIR_DEBUG)/decoder.o

$(OBJDIR_DEBUG)/distorm.o: ../Dependencies/distorm/src/distorm.c
$(CXX) $(CFLAGS_DEBUG) $(INC_DEBUG) -c ../Dependencies/distorm/src/distorm.c -o $(OBJDIR_DEBUG)/distorm.o
$(CC) $(CFLAGS_DEBUG) $(INC_DEBUG) -c ../Dependencies/distorm/src/distorm.c -o $(OBJDIR_DEBUG)/distorm.o

$(OBJDIR_DEBUG)/instructions.o: ../Dependencies/distorm/src/instructions.c
$(CXX) $(CFLAGS_DEBUG) $(INC_DEBUG) -c ../Dependencies/distorm/src/instructions.c -o $(OBJDIR_DEBUG)/instructions.o
$(CC) $(CFLAGS_DEBUG) $(INC_DEBUG) -c ../Dependencies/distorm/src/instructions.c -o $(OBJDIR_DEBUG)/instructions.o

$(OBJDIR_DEBUG)/insts.o: ../Dependencies/distorm/src/insts.c
$(CXX) $(CFLAGS_DEBUG) $(INC_DEBUG) -c ../Dependencies/distorm/src/insts.c -o $(OBJDIR_DEBUG)/insts.o
$(CC) $(CFLAGS_DEBUG) $(INC_DEBUG) -c ../Dependencies/distorm/src/insts.c -o $(OBJDIR_DEBUG)/insts.o

$(OBJDIR_DEBUG)/mnemonics.o: ../Dependencies/distorm/src/mnemonics.c
$(CXX) $(CFLAGS_DEBUG) $(INC_DEBUG) -c ../Dependencies/distorm/src/mnemonics.c -o $(OBJDIR_DEBUG)/mnemonics.o
$(CC) $(CFLAGS_DEBUG) $(INC_DEBUG) -c ../Dependencies/distorm/src/mnemonics.c -o $(OBJDIR_DEBUG)/mnemonics.o

$(OBJDIR_DEBUG)/operands.o: ../Dependencies/distorm/src/operands.c
$(CXX) $(CFLAGS_DEBUG) $(INC_DEBUG) -c ../Dependencies/distorm/src/operands.c -o $(OBJDIR_DEBUG)/operands.o
$(CC) $(CFLAGS_DEBUG) $(INC_DEBUG) -c ../Dependencies/distorm/src/operands.c -o $(OBJDIR_DEBUG)/operands.o

$(OBJDIR_DEBUG)/prefix.o: ../Dependencies/distorm/src/prefix.c
$(CXX) $(CFLAGS_DEBUG) $(INC_DEBUG) -c ../Dependencies/distorm/src/prefix.c -o $(OBJDIR_DEBUG)/prefix.o
$(CC) $(CFLAGS_DEBUG) $(INC_DEBUG) -c ../Dependencies/distorm/src/prefix.c -o $(OBJDIR_DEBUG)/prefix.o

$(OBJDIR_DEBUG)/textdefs.o: ../Dependencies/distorm/src/textdefs.c
$(CXX) $(CFLAGS_DEBUG) $(INC_DEBUG) -c ../Dependencies/distorm/src/textdefs.c -o $(OBJDIR_DEBUG)/textdefs.o
$(CC) $(CFLAGS_DEBUG) $(INC_DEBUG) -c ../Dependencies/distorm/src/textdefs.c -o $(OBJDIR_DEBUG)/textdefs.o

$(OBJDIR_DEBUG)/wstring.o: ../Dependencies/distorm/src/wstring.c
$(CXX) $(CFLAGS_DEBUG) $(INC_DEBUG) -c ../Dependencies/distorm/src/wstring.c -o $(OBJDIR_DEBUG)/wstring.o
$(CC) $(CFLAGS_DEBUG) $(INC_DEBUG) -c ../Dependencies/distorm/src/wstring.c -o $(OBJDIR_DEBUG)/wstring.o

clean_debug:
rm -f $(OBJ_DEBUG) $(OUT_DEBUG)
Expand Down Expand Up @@ -170,31 +170,31 @@ $(OBJDIR_RELEASE)/CloseRemoteProcess.o: CloseRemoteProcess.cpp
$(CXX) $(CFLAGS_RELEASE) $(INC_RELEASE) -c CloseRemoteProcess.cpp -o $(OBJDIR_RELEASE)/CloseRemoteProcess.o

$(OBJDIR_RELEASE)/decoder.o: ../Dependencies/distorm/src/decoder.c
$(CXX) $(CFLAGS_RELEASE) $(INC_RELEASE) -c ../Dependencies/distorm/src/decoder.c -o $(OBJDIR_RELEASE)/decoder.o
$(CC) $(CFLAGS_RELEASE) $(INC_RELEASE) -c ../Dependencies/distorm/src/decoder.c -o $(OBJDIR_RELEASE)/decoder.o

$(OBJDIR_RELEASE)/distorm.o: ../Dependencies/distorm/src/distorm.c
$(CXX) $(CFLAGS_RELEASE) $(INC_RELEASE) -c ../Dependencies/distorm/src/distorm.c -o $(OBJDIR_RELEASE)/distorm.o
$(CC) $(CFLAGS_RELEASE) $(INC_RELEASE) -c ../Dependencies/distorm/src/distorm.c -o $(OBJDIR_RELEASE)/distorm.o

$(OBJDIR_RELEASE)/instructions.o: ../Dependencies/distorm/src/instructions.c
$(CXX) $(CFLAGS_RELEASE) $(INC_RELEASE) -c ../Dependencies/distorm/src/instructions.c -o $(OBJDIR_RELEASE)/instructions.o
$(CC) $(CFLAGS_RELEASE) $(INC_RELEASE) -c ../Dependencies/distorm/src/instructions.c -o $(OBJDIR_RELEASE)/instructions.o

$(OBJDIR_RELEASE)/insts.o: ../Dependencies/distorm/src/insts.c
$(CXX) $(CFLAGS_RELEASE) $(INC_RELEASE) -c ../Dependencies/distorm/src/insts.c -o $(OBJDIR_RELEASE)/insts.o
$(CC) $(CFLAGS_RELEASE) $(INC_RELEASE) -c ../Dependencies/distorm/src/insts.c -o $(OBJDIR_RELEASE)/insts.o

$(OBJDIR_RELEASE)/mnemonics.o: ../Dependencies/distorm/src/mnemonics.c
$(CXX) $(CFLAGS_RELEASE) $(INC_RELEASE) -c ../Dependencies/distorm/src/mnemonics.c -o $(OBJDIR_RELEASE)/mnemonics.o
$(CC) $(CFLAGS_RELEASE) $(INC_RELEASE) -c ../Dependencies/distorm/src/mnemonics.c -o $(OBJDIR_RELEASE)/mnemonics.o

$(OBJDIR_RELEASE)/operands.o: ../Dependencies/distorm/src/operands.c
$(CXX) $(CFLAGS_RELEASE) $(INC_RELEASE) -c ../Dependencies/distorm/src/operands.c -o $(OBJDIR_RELEASE)/operands.o
$(CC) $(CFLAGS_RELEASE) $(INC_RELEASE) -c ../Dependencies/distorm/src/operands.c -o $(OBJDIR_RELEASE)/operands.o

$(OBJDIR_RELEASE)/prefix.o: ../Dependencies/distorm/src/prefix.c
$(CXX) $(CFLAGS_RELEASE) $(INC_RELEASE) -c ../Dependencies/distorm/src/prefix.c -o $(OBJDIR_RELEASE)/prefix.o
$(CC) $(CFLAGS_RELEASE) $(INC_RELEASE) -c ../Dependencies/distorm/src/prefix.c -o $(OBJDIR_RELEASE)/prefix.o

$(OBJDIR_RELEASE)/textdefs.o: ../Dependencies/distorm/src/textdefs.c
$(CXX) $(CFLAGS_RELEASE) $(INC_RELEASE) -c ../Dependencies/distorm/src/textdefs.c -o $(OBJDIR_RELEASE)/textdefs.o
$(CC) $(CFLAGS_RELEASE) $(INC_RELEASE) -c ../Dependencies/distorm/src/textdefs.c -o $(OBJDIR_RELEASE)/textdefs.o

$(OBJDIR_RELEASE)/wstring.o: ../Dependencies/distorm/src/wstring.c
$(CXX) $(CFLAGS_RELEASE) $(INC_RELEASE) -c ../Dependencies/distorm/src/wstring.c -o $(OBJDIR_RELEASE)/wstring.o
$(CC) $(CFLAGS_RELEASE) $(INC_RELEASE) -c ../Dependencies/distorm/src/wstring.c -o $(OBJDIR_RELEASE)/wstring.o

clean_release:
rm -f $(OBJ_RELEASE) $(OUT_RELEASE)
Expand Down
2 changes: 1 addition & 1 deletion NativeCore/Unix/NativeCore.Unix.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
buildPhases = (
);
buildToolPath = /usr/bin/make;
buildWorkingDirectory = /Users/h3xc0r3/Documents/GitHub/ReClass.NET/NativeCore/Unix;
buildWorkingDirectory = /Users/tarek/ReClass.NET/NativeCore/Unix;
dependencies = (
);
name = NativeCore.Unix;
Expand Down
Binary file not shown.
Loading