Skip to content

Instantly share code, notes, and snippets.

@SakiiR
Last active July 15, 2023 14:08
Show Gist options
  • Save SakiiR/21a9e3538ae97dd5e756565d7498241b to your computer and use it in GitHub Desktop.
Save SakiiR/21a9e3538ae97dd5e756565d7498241b to your computer and use it in GitHub Desktop.
Radare2 utils :)
class R2:
def __init__(self, file_path, args=[], debug=False):
self._r = r2pipe.open(file_path, args)
self._debug = debug
def cmd(self, cmd, debug=True):
if self._debug and debug:
print(f"Executing command: '{cmd}'")
res = self._r.cmd(cmd).rstrip("\n")
if self._debug and debug:
print(f"-> '{res}'")
return res
def print_cmd(self, cmd, debug=True):
res = self.cmd(cmd, debug)
print(res)
return res
def br(self, br):
return self.cmd(f"db 0x{br:016x}")
def dc(self):
return self.cmd("dc")
def step(self):
return self.cmd("ds")
def status(self):
return self.print_cmd("pd 5 @ rip")
def _parse_hex(self, i):
return int(i, 16)
def _parse_json(self, data: str):
return json.loads(data)
def next(self):
return self.cmd("dc")
def reg(self, reg):
return self._parse_hex(self.cmd(f"dr {reg}"))
def gx(self, addr: int) -> int:
return self._parse_json(self.cmd(f"pxqj @ 0x{addr:016x}"))[0]
def gs(self, addr: int) -> int:
return self.cmd(f"ps @ 0x{addr:016x}")
def set_reg(self, reg, value):
return self.cmd(f"dr {reg}=0x{value:016x}")
def write_to_adress(self, addr, value):
return self.cmd(f"wx 0x{value:x} @ 0x{addr:016x}")
def write_to_local_var(self, local, value):
addr = self.local_var_addr(local)
return self.write_to_adress(addr, value)
def local_var_addr(self, local):
return self._parse_hex(self.cmd(f".afvd {local}").split("0x")[1].split(" ")[0])
def local_var(self, local):
return self._parse_hex(self.cmd(f".afvd {local}").split("0x")[2])
def interactive(self):
output = ""
print("Interactive mode enabled, type quit to quit")
while True:
cmd = input("# ")
if cmd == "quit":
break
output += self.print_cmd(cmd, debug=False)
return output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment