aboutsummaryrefslogtreecommitdiffstats
path: root/main.c
diff options
context:
space:
mode:
authorbloodstalker <thabogre@gmail.com>2017-12-27 21:15:44 +0000
committerbloodstalker <thabogre@gmail.com>2017-12-27 21:15:44 +0000
commit56d839f3fa2017cd105a2098bb84f70ee3a125f9 (patch)
treec6cf4c1035a3dcf931ba4ddcc5514678fc2fb24c /main.c
parentwip (diff)
downloadmemdump-56d839f3fa2017cd105a2098bb84f70ee3a125f9.tar.gz
memdump-56d839f3fa2017cd105a2098bb84f70ee3a125f9.zip
wip
Diffstat (limited to '')
-rw-r--r--main.c147
1 files changed, 67 insertions, 80 deletions
diff --git a/main.c b/main.c
index b266ad5..8bdd992 100644
--- a/main.c
+++ b/main.c
@@ -7,95 +7,82 @@
#include <arpa/inet.h>
#include <unistd.h>
-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);
+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);
- }
+ for (address=start_address; address < start_address + length; address += pageLength) {
+ fread(&page, 1, pageLength, pMemFile);
+ if (serverSocket == -1) {
+ 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);
- 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;
+ 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 2;
}
- 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;
- }
+ serverSocket = socket(AF_INET, SOCK_STREAM, 0);
+ if (serverSocket == -1) {
+ printf("Could not create socket\n");
+ return 3;
}
- 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);
+
+ 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 4;
}
+ }
+ 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);
+ }
- ptrace(PTRACE_CONT, pid, NULL, NULL);
- ptrace(PTRACE_DETACH, pid, NULL, NULL);
- }
- else
- {
- printf("%s <pid>\n", argv[0]);
- printf("%s <pid> <ip-address> <port>\n", argv[0]);
- exit(0);
- }
+ fclose(pMapsFile);
+ fclose(pMemFile);
+
+ if (serverSocket != -1) {
+ close(serverSocket);
+ }
+
+ ptrace(PTRACE_CONT, pid, NULL, NULL);
+ ptrace(PTRACE_DETACH, pid, NULL, NULL);
+ } else {
+ printf("%s <pid>\n", argv[0]);
+ printf("%s <pid> <ip-address> <port>\n", argv[0]);
+ exit(0);
+ }
}