aboutsummaryrefslogtreecommitdiffstats
path: root/bruiser/ramdump.c
blob: d06d3bc0308787c61d21ba5951c5c2a1ab917df1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/***************************************************Project Mutator****************************************************/
/*first line intentionally left blank.*/
/*bruiser's ram dump module*/
/*Copyright (C) 2018 Farzad Sadeghi

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 3
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.*/
/***********************************************************************************************************/
//#include "ramdump.h"
#include <inttypes.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ptrace.h>
#include <sys/wait.h>
#include <unistd.h>
/***********************************************************************************************************/
void dump_memory_region(FILE* pMemFile, uint64_t start_address, uint64_t length, FILE* out_file) {
  uint64_t address;
  int pageLength = 4096;
  fseeko(pMemFile, start_address, SEEK_SET);
  unsigned char page[pageLength];

  for (address=start_address; address < start_address + length; address += pageLength) {
    fread(&page, 1, pageLength, pMemFile);
    fwrite(&page, 1, pageLength, out_file);
  }
}

void dump_ram(unsigned int pid, FILE* out_file) {
  uint64_t ptraceResult = ptrace(PTRACE_ATTACH, pid, NULL, NULL);
  if (ptraceResult < 0) {
    printf("ramdump: unable to attach to the pid specified\n");
    return;
  }
  wait(NULL);

  char mapsFilename[1024];
  char proc_str[6];
  sprintf(proc_str, "%d", pid);
  sprintf(mapsFilename, "/proc/%s/maps", proc_str);
  FILE* pMapsFile = fopen(mapsFilename, "r");
  char memFilename[1024];
  sprintf(memFilename, "/proc/%s/mem", proc_str);
  FILE* pMemFile = fopen(memFilename, "r");
  char line[256];
  while (fgets(line, 256, pMapsFile) != NULL) {
    uint64_t start_address;
    uint64_t end_address;
    sscanf(line, "%08lx-%08lx\n", &start_address, &end_address);
    dump_memory_region(pMemFile, start_address, end_address - start_address, out_file);
  }

  fclose(pMapsFile);
  fclose(pMemFile);

  ptrace(PTRACE_CONT, pid, NULL, NULL);
  ptrace(PTRACE_DETACH, pid, NULL, NULL);
}

#pragma weak main
int main(int argc, char **argv) {
  if (argc != 2) {
    printf("what happened to the pid?\n");
    return 1;
  }
  FILE* out_file = fopen("/tmp/ramdump", "w");
  int pid = atoi(argv[1]);
  dump_ram(pid, out_file);
  fclose(out_file);
  return 0;
}
/***********************************************************************************************************/
/*last line is intentionally left blank*/