From ad3d03c116d948b739c48936b3aa112083cfeb01 Mon Sep 17 00:00:00 2001 From: bloodstalker Date: Wed, 27 Dec 2017 23:13:14 +0330 Subject: wip --- compile_commands.json | 7 ++++ jni/Android.mk | 6 +++ main.c | 101 ++++++++++++++++++++++++++++++++++++++++++++++++++ makefile | 30 +++++++++++++++ 4 files changed, 144 insertions(+) create mode 100644 compile_commands.json create mode 100644 jni/Android.mk create mode 100644 main.c create mode 100644 makefile diff --git a/compile_commands.json b/compile_commands.json new file mode 100644 index 0000000..e0edd02 --- /dev/null +++ b/compile_commands.json @@ -0,0 +1,7 @@ +[ + { + "command": "cc -c -o main.o main.c", + "directory": "/home/bloodstalker/devi/abbatoir/hole68", + "file": "/home/bloodstalker/devi/abbatoir/hole68/main.c" + } +] \ No newline at end of file diff --git a/jni/Android.mk b/jni/Android.mk new file mode 100644 index 0000000..a479a88 --- /dev/null +++ b/jni/Android.mk @@ -0,0 +1,6 @@ +LOCAL_PATH := $(call my-dir) + +include $(CLEAR_VARS) +LOCAL_MODULE := main +LOCAL_SRC_FILES := ../main.c +include $(BUILD_EXECUTABLE) diff --git a/main.c b/main.c new file mode 100644 index 0000000..b266ad5 --- /dev/null +++ b/main.c @@ -0,0 +1,101 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +void dump_memory_region(FILE* pMemFile, unsigned long start_address, long length, int serverSocket) +{ + unsigned long address; + int pageLength = 4096; + unsigned char page[pageLength]; + fseeko(pMemFile, start_address, SEEK_SET); + + for (address=start_address; address < start_address + length; address += pageLength) + { + fread(&page, 1, pageLength, pMemFile); + if (serverSocket == -1) + { + // write to stdout + fwrite(&page, 1, pageLength, stdout); + } + else + { + send(serverSocket, &page, pageLength, 0); + } + } +} + +int main(int argc, char **argv) { + + if (argc == 2 || argc == 4) + { + int pid = atoi(argv[1]); + long ptraceResult = ptrace(PTRACE_ATTACH, pid, NULL, NULL); + if (ptraceResult < 0) + { + printf("Unable to attach to the pid specified\n"); + return 1; + } + wait(NULL); + + char mapsFilename[1024]; + sprintf(mapsFilename, "/proc/%s/maps", argv[1]); + FILE* pMapsFile = fopen(mapsFilename, "r"); + char memFilename[1024]; + sprintf(memFilename, "/proc/%s/mem", argv[1]); + FILE* pMemFile = fopen(memFilename, "r"); + int serverSocket = -1; + if (argc == 4) + { + unsigned int port; + int count = sscanf(argv[3], "%d", &port); + if (count == 0) + { + printf("Invalid port specified\n"); + return 1; + } + serverSocket = socket(AF_INET, SOCK_STREAM, 0); + if (serverSocket == -1) + { + printf("Could not create socket\n"); + return 1; + } + struct sockaddr_in serverSocketAddress; + serverSocketAddress.sin_addr.s_addr = inet_addr(argv[2]); + serverSocketAddress.sin_family = AF_INET; + serverSocketAddress.sin_port = htons(port); + if (connect(serverSocket, (struct sockaddr *) &serverSocketAddress, sizeof(serverSocketAddress)) < 0) + { + printf("Could not connect to server\n"); + return 1; + } + } + char line[256]; + while (fgets(line, 256, pMapsFile) != NULL) + { + unsigned long start_address; + unsigned long end_address; + sscanf(line, "%08lx-%08lx\n", &start_address, &end_address); + dump_memory_region(pMemFile, start_address, end_address - start_address, serverSocket); + } + fclose(pMapsFile); + fclose(pMemFile); + if (serverSocket != -1) + { + close(serverSocket); + } + + ptrace(PTRACE_CONT, pid, NULL, NULL); + ptrace(PTRACE_DETACH, pid, NULL, NULL); + } + else + { + printf("%s \n", argv[0]); + printf("%s \n", argv[0]); + exit(0); + } +} diff --git a/makefile b/makefile new file mode 100644 index 0000000..b6f6f40 --- /dev/null +++ b/makefile @@ -0,0 +1,30 @@ +TARGET=main +CC=clang +CC?=clang +CC_FLAGS= +CC_EXTRA?= +CC_FLAGS+=$(CC_EXTRA) + +.DEFAULT:all clean + +.PHONY:all clean $(TARGET) help + +all:$(TARGET) + +.c.o: + $(CC) $(CC_FLAGS) -c $< -o $@ + +android: + export NDK_PROJECT_PATH=`pwd` + ndk-build + +$(TARGET): main.o + $(CC) $^ $(LD_FLAGS) -o $@ + +clean: + rm -f *.o *~ $(TARGET) + +help: + @echo "all is the default target" + @echo "there is delete." + @echo "there is clean." -- cgit v1.2.3