Skip to content

Commit 8ab4244

Browse files
initial commit
1 parent 9fa388a commit 8ab4244

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

0x03-proc_filesystem/README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# 0x02. Python - /proc filesystem
2+
3+
### System programming & Algorithm ― Linux programming
4+
5+
6+
Task : Hack the VM
7+
8+
Write a script that finds a string in the heap of a running process, and replaces it.
9+
10+
* Usage: read_write_heap.py pid search_string replace_string
11+
* where pid is the pid of the running process
12+
* and strings are ASCII
13+
* The script should look only in the heap of the process
14+
* Output: you can print whatever you think is interesting
15+
* On usage error, print an error message on stdout and exit with status code 1
16+
17+
18+
19+
File: read_write_heap.py
20+
21+
22+
### Usefull Links:
23+
24+
https://www.kernel.org/doc/Documentation/filesystems/proc.txt
25+
26+
https://unix.stackexchange.com/questions/6301/how-do-i-read-from-proc-pid-mem-under-linux/6302#6302
27+
28+
https://stackoverflow.com/questions/12977179/reading-living-process-memory-without-interrupting-it-proc-kcore-is-an-option
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/usr/bin/python3
2+
import sys
3+
import re
4+
5+
6+
def print_usage():
7+
print("Usage: read_write_heap.py pid search_s replace_s")
8+
exit(1)
9+
10+
11+
def read_write_heap(pid, search_s, replace_s, only_writable=True):
12+
mem_perm = 'rw' if only_writable else 'r-'
13+
maps_filename = "/proc/{}/maps".format(pid)
14+
mem_filename = "/proc/{}/mem".format(pid)
15+
try:
16+
with open(maps_filename, 'r') as maps_file:
17+
with open(mem_filename, 'rb+', 0) as mem_file:
18+
for line in maps_file.readlines():
19+
addr_perm = r'([0-9A-Fa-f]+)-([0-9A-Fa-f]+) ([-r][-w])'
20+
m = re.search(addr_perm, line)
21+
h = re.search(r'(\[heap\])', line)
22+
if m.group(3) == mem_perm and h and h.group(0) == "[heap]":
23+
start_addr = int(m.group(1), 16)
24+
end_addr = int(m.group(2), 16)
25+
mem_file.seek(start_addr)
26+
heap = mem_file.read(end_addr - start_addr)
27+
pos = heap.find(bytes(search_s, "ASCII"))
28+
if pos:
29+
mem_file.seek(start_addr + pos)
30+
adjusted_str = replace_s.ljust(len(search_s))
31+
mem_file.write(bytes(adjusted_str, "ASCII"))
32+
else:
33+
print("Couldn't find the %s in the heap", search_s)
34+
except IOError as e:
35+
print("[ERROR] Can not open file {}:".format(maps_filename))
36+
print(" I/O error({}): {}".format(e.errno, e.strerror))
37+
exit(1)
38+
39+
40+
try:
41+
if len(sys.argv) != 4:
42+
print_usage()
43+
pid = int(sys.argv[1])
44+
search_s = sys.argv[2]
45+
replace_s = sys.argv[3]
46+
if (len(search_s) == 0 or len(replace_s) == 0):
47+
print_usage()
48+
read_write_heap(pid, search_s, replace_s)
49+
except Exception as e:
50+
print_usage()

0 commit comments

Comments
 (0)