summaryrefslogtreecommitdiff
path: root/hypodermic/process.py
diff options
context:
space:
mode:
authorjakob <jakob@memeware.net>2017-09-09 10:37:51 -0400
committerjakob <jakob@memeware.net>2017-09-09 10:37:51 -0400
commitcd49a830921bbf59aa0ed86a453d63d2fc8a2d1e (patch)
tree1b4b8a7b852c9097cf23def492b52ba5c533421d /hypodermic/process.py
parent1a17f23fce09b8325e237a2a65a2227ecf1310b8 (diff)
Open a file descriptor in the inferior.
Diffstat (limited to 'hypodermic/process.py')
-rw-r--r--hypodermic/process.py29
1 files changed, 28 insertions, 1 deletions
diff --git a/hypodermic/process.py b/hypodermic/process.py
index 1e2c1d4..bfb1013 100644
--- a/hypodermic/process.py
+++ b/hypodermic/process.py
@@ -22,7 +22,7 @@ import os.path
import re
from hypodermic.memory import Region, maps
-from hypodermic.shellcode import assemble
+from hypodermic.shellcode import assemble, open_shellcode
_AMD64_INDICES = {
"r15": 0,
@@ -338,6 +338,33 @@ class Process(object):
after = [self.get_register(reg) for reg in preserve]
return before, after
+ def open(self, path: str) -> int:
+ """Attempts to open a file descriptor within the inferior.
+
+ Args:
+ path (str): The path of the file to open.
+
+ Raises:
+ OSError: If the path cannot be opened.
+
+ Returns:
+ The file descriptor.
+ """
+ if self.arch == "x64":
+ old_rax = self.get_register("rax")
+ self.run_code(open_shellcode(path), preserve=["rax"])
+ fd = self.get_register("rax")
+ self.set_register("rax", old_rax)
+ else:
+ old_eax = self.get_register("eax")
+ self.run_code(open_shellcode(path, arch="i386"), preserve=["eax"])
+ fd = self.get_register("eax")
+ self.set_register("eax", old_eax)
+
+ if fd < 0:
+ raise OSError("Couldn't open {}".format(path))
+ return fd
+
@property
def arch(self) -> str:
"""Returns the architecture of the host processor.