improved base and added PP defines KERNEL_UPSTREAM TESTING DEBUG

This commit is contained in:
adeliktas
2023-10-28 03:55:19 +02:00
parent a6a285b82c
commit fb5d501d88
26 changed files with 813 additions and 476 deletions

19
um/Makefile Normal file
View File

@@ -0,0 +1,19 @@
APP_NAME = main
APP_SRCS = main.cpp memory.cpp
APP_OBJS = $(APP_SRCS:.cpp=.o)
CXX = g++
CXXFLAGS = -Wall -Werror
all: $(APP_NAME)
$(APP_NAME): $(APP_OBJS)
$(CXX) $(CXXFLAGS) -o $@ $(APP_OBJS)
clean:
rm -f $(APP_NAME) $(APP_OBJS)
$(APP_OBJS): %.o: %.cpp
$(CXX) $(CXXFLAGS) -c $< -o $@
.PHONY: all

41
um/communication_struct.h Normal file
View File

@@ -0,0 +1,41 @@
#pragma once
#ifndef DRIVER
#include <stdint.h>
#include <stdio.h>
#endif
#define IOCTL_OPENPROC _IOW('k', 1, int)
#define IOCTL_GETMODULE _IOW('k', 2, const char*)
#define IOCTL_RPM _IOW('k', 3, t_RPM)
#define IOCTL_WPM _IOW('k', 4, t_WPM)
#if KERNEL_UPSTREAM == 0
#define IOCTL_GETPIDMODULE _IOWR('k', 5, t_PM)
#if TESTING == 1
#define IOCTL_VIRT_TO_PHYS _IOWR('k', 6, unsigned long)
#define IOCTL_PHYS_TO_VIRT _IOWR('k', 7, unsigned long)
#endif
#endif
typedef struct s_RPM
{
uintptr_t addr;
ssize_t size;
uintptr_t out;
uintptr_t *out_addr;
} t_RPM;
typedef struct s_WPM
{
uintptr_t addr;
ssize_t size;
uintptr_t value;
} t_WPM;
#if KERNEL_UPSTREAM == 0
typedef struct s_PM {
int pid;
const char *mod;
} t_PM;
#endif

67
um/main.cpp Normal file
View File

@@ -0,0 +1,67 @@
#include "memory.hpp"
std::string target_proc_name{"test_app"};
std::string target_mod_name{"libmath_module.so"};
int main() {
//run_overlay();
if (!open_device())
return -1;
std::cout << "[Main]device opened" << std::endl;
int pid = get_pid(target_proc_name.c_str());
std::cout << "pid of " << target_proc_name << "=" << std::dec << pid << std::endl;
if (!open_process(pid))
return -1;
uint64_t addr;
std::ifstream inFile("test/addr.txt");
if (!inFile.is_open()) {
std::cerr << "Error: Could not open the file for reading." << std::endl;
return 1;
}
inFile >> std::hex;
if (inFile >> addr) {
std::cout << "Read from file var_virtaddr=" << std::hex << addr << std::endl;
} else {
std::cerr << "Error: Failed to read value from file." << std::endl;
return 1;
}
inFile.close();
#if KERNEL_UPSTREAM==1
uint64_t mod_primary_addr = get_module(target_proc_name.c_str());
#else
uint64_t mod_primary_addr = get_pid_module(pid, target_proc_name.c_str());
#endif
std::cout << "module of " << target_proc_name << "=" << std::hex << mod_primary_addr << std::endl;
#if KERNEL_UPSTREAM==1
uint64_t modaddr_libmath_addr = get_module(target_mod_name.c_str());
#else
uint64_t modaddr_libmath_addr = get_pid_module(pid, target_mod_name.c_str());
#endif
std::cout << "module of " << target_mod_name << "=" << std::hex << modaddr_libmath_addr << std::endl;
//mod_primary_addr = 0; // primary_module not required
int value_read = RPM<int>(0 + addr);
std::cout << "Value before write=" << std::dec << value_read << std::endl;
WPM<int>(0 + addr, 1337);
value_read = RPM<int>(0 + addr);
std::cout << "Value after write=" << std::dec << value_read << std::endl;
#if KERNEL_UPSTREAM==0 && TESTING==1
uint64_t physaddr = VIRT_TO_PHYS(0x7fff2a2cb7a4);
std::cout << "phys addr=" << std::hex << physaddr << std::endl;
uint64_t phys2virtaddr = PHYS_TO_VIRT(0x7fff2a2cb7a4);
std::cout << "phys2virtaddr=" << std::hex << phys2virtaddr << std::endl;
//attempt to read relative to proc primary module
int value_read_primarymod = RPM<int>(mod_primary_addr + 0x2A917D6757A4);
std::cout << "value_read_primarymod=" << std::dec << value_read_primarymod << std::endl;
#endif
close_device();
return 0;
}
//pmap -x $(pidof test_app)

116
um/memory.cpp Normal file
View File

@@ -0,0 +1,116 @@
#include "memory.hpp"
int file_desc = 0;
int open_device(void)
{
file_desc = open(DEVICE_FILE, O_RDWR);
if (file_desc < 0) {
perror("Revird: Failed to open the device.");
return -1;
}
return 1;
}
void close_device(void)
{
close(file_desc);
}
int get_pid(const char *program_name) {
FILE *fp;
char command[128];
char buffer[128];
int pid = -1;
// Create a command to run 'pidof' for the specified program
snprintf(command, sizeof(command), "pidof %s", program_name);
// Open a pipe to execute the command and read the output
fp = popen(command, "r");
if (!fp) {
perror("popen");
return -1;
}
// Read the output (should be a space-separated list of PIDs)
if (fgets(buffer, sizeof(buffer), fp) != NULL) {
// Extract the first PID from the list
if (sscanf(buffer, "%d", &pid) == 1) {
}
}
// Close the pipe and check for errors
if (pclose(fp) == -1) {
perror("pclose");
return -1;
}
return pid;
}
int open_process(int pid)
{
uint64_t ret = ioctl(file_desc, IOCTL_OPENPROC, &pid);
if (ret < 0) {
perror("Revird: openprocess failed.");
close(file_desc);
return -1;
}
return 1;
}
uintptr_t get_module(const char *mod)
{
uint64_t ret = ioctl(file_desc, IOCTL_GETMODULE, mod);
if (ret < 0) {
perror("Revird: getmodule failed.");
close(file_desc);
return -1;
}
uintptr_t addr = RPM<uintptr_t>(0x69420);
return addr;
}
#if KERNEL_UPSTREAM == 0
uintptr_t get_pid_module(int pid, const char *mod) {
struct s_PM pma;
pma.pid = pid;
pma.mod = mod;
uint64_t ret = ioctl(file_desc, IOCTL_GETPIDMODULE, &pma);
if (ret < 0) {
perror("Revird: get_pid_module failed.");
close(file_desc);
return -1;
}
uintptr_t addr = RPM<uintptr_t>(0x69420);
return addr;
}
#endif
#if KERNEL_UPSTREAM == 0 && TESTING==1
uintptr_t VIRT_TO_PHYS(uintptr_t vaddr) {
uint64_t ret = ioctl(file_desc, IOCTL_VIRT_TO_PHYS, vaddr);
if (ret < 0) {
perror("VIRT_TO_PHYS failed.");
return 0;
}
// At this point, 'vaddr' contains the physical address.
return vaddr;
}
#endif
#if KERNEL_UPSTREAM == 0 && TESTING==1
uintptr_t PHYS_TO_VIRT(uintptr_t paddr) {
uint64_t ret = ioctl(file_desc, IOCTL_PHYS_TO_VIRT, &paddr);
if (ret < 0) {
perror("PHYS_TO_VIRT failed.");
return 0;
}
// At this point, 'paddr' contains the virtual address.
return paddr;
}
#endif

62
um/memory.hpp Normal file
View File

@@ -0,0 +1,62 @@
#pragma once
#include <stdio.h>
#include <sys/ioctl.h>
#include <stdint.h>
#include <fcntl.h>
#include <unistd.h>
//#include "overlay.hpp"
#include <iostream>
#include <fstream>
#include <cstdint>
#include "communication_struct.h"
#define DEVICE_FILE "/dev/TaxiDriver"
extern int file_desc;
int open_device(void);
void close_device(void);
int get_pid(const char *program_name);
template <typename T>
T RPM(uintptr_t address)
{
struct s_RPM args;
args.addr = address;
args.size = sizeof(T);
args.out = 0;
args.out_addr = &args.out;
uint64_t ret = ioctl(file_desc, IOCTL_RPM, &args);
if (ret < 0) {
perror("Revird: RPM failed.");
close(file_desc);
return 0;
}
return (T)args.out;
}
template <typename T>
void WPM(uintptr_t address, T value)
{
struct s_WPM args_wpm;
args_wpm.addr = address;
args_wpm.size = sizeof(T);
args_wpm.value = value;
uint64_t ret = ioctl(file_desc, IOCTL_WPM, &args_wpm);
if (ret < 0) {
perror("Revird: WPM failed.");
close(file_desc);
return;
}
return;
}
void WPM(uintptr_t addr, uintptr_t value, ssize_t size);
int open_process(int pid);
uintptr_t get_module(const char *mod);
#if KERNEL_UPSTREAM == 0
uintptr_t get_pid_module(int pid, const char *mod);
#if TESTING==1
uintptr_t VIRT_TO_PHYS(uintptr_t vaddr);
uintptr_t PHYS_TO_VIRT(uintptr_t paddr);
#endif
#endif

35
um/test/Makefile Normal file
View File

@@ -0,0 +1,35 @@
# Define the name of your application
APP_NAME = test_app
# Source files for your application
APP_SRCS = main.cpp
APP_OBJS = $(APP_SRCS:.cpp=.o)
# Compiler and compiler flags
CXX = g++
CXXFLAGS = -Wall -Werror -fexceptions
# Library name and source files
LIB_NAME = libmath_module.so
LIB_SRCS = math_module.cpp
LIB_OBJS = $(LIB_SRCS:.cpp=.o)
# Additional flags for creating PIC code
PICFLAGS = -fPIC
# The default target is 'all', which builds your application
all: $(APP_NAME) $(LIB_NAME)
$(APP_NAME): $(APP_OBJS) $(LIB_NAME)
$(CXX) $(CXXFLAGS) -o $@ $(APP_OBJS) -L. -lmath_module -Wl,-rpath,'$$ORIGIN'
$(LIB_NAME): $(LIB_OBJS)
$(CXX) -shared -o $@ $(LIB_OBJS) $(CXXFLAGS)
# Compile source files to object files
%.o: %.cpp
$(CXX) $(CXXFLAGS) $(PICFLAGS) -c $< -o $@
# Clean rule to remove the executable, object files, and library
clean:
rm -f $(APP_NAME) $(APP_OBJS) $(LIB_NAME) $(LIB_OBJS)

1
um/test/addr.txt Normal file
View File

@@ -0,0 +1 @@
0x7fff7203ed94

44
um/test/main.cpp Normal file
View File

@@ -0,0 +1,44 @@
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <cstdint>
#include "math_module.h"
int main() {
MathModule math;
int result = math.add(10, 5);
std::cout << "10 + 5 = " << result << std::endl;
result = math.subtract(10, 5);
std::cout << "10 - 5 = " << result << std::endl;
result = math.multiply(10, 5);
std::cout << "10 * 5 = " << result << std::endl;
double divisionResult = math.divide(10.0, 5.0);
std::cout << "10 / 5 = " << divisionResult << std::endl;
int var = 0;
uint64_t* var_virtaddr = reinterpret_cast<uint64_t*>(&var);
std::ofstream outFile("addr.txt");
if (!outFile.is_open()) {
std::cerr << "Error: Could not open the file for writing." << std::endl;
return 1;
}
outFile << var_virtaddr;
outFile.close();
while (true) {
int random_increment = std::rand() % 100; // Generate a random number between 0 and 99
std::cout << "before inc value=" << std::dec << var << "@" << std::hex << var_virtaddr << std::endl;
var += random_increment;
std::cout << "after inc value=" << std::dec << var << "@" << std::hex << var_virtaddr << std::endl;
std::cin.get(); // Wait for Enter key press
}
return 0;
}
//export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:.

24
um/test/math_module.cpp Normal file
View File

@@ -0,0 +1,24 @@
#include "math_module.h"
MathModule::MathModule() {
// Constructor, if any initialization is needed
}
int MathModule::add(int a, int b) {
return a + b;
}
int MathModule::subtract(int a, int b) {
return a - b;
}
int MathModule::multiply(int a, int b) {
return a * b;
}
double MathModule::divide(double a, double b) {
if (b == 0) {
throw "Division by zero is not allowed.";
}
return a / b;
}

14
um/test/math_module.h Normal file
View File

@@ -0,0 +1,14 @@
#ifndef MATH_MODULE_H
#define MATH_MODULE_H
class MathModule {
public:
MathModule(); // Constructor
int add(int a, int b);
int subtract(int a, int b);
int multiply(int a, int b);
double divide(double a, double b);
};
#endif