From eeaab5ec442adb3b4ea21cff49dcfd1eab469473 Mon Sep 17 00:00:00 2001 From: blendin Date: Mon, 20 Feb 2017 17:40:56 -0800 Subject: [PATCH] Added commands for xorring regions of memory (#164) xor: xor a region of memory with a key memfrob: frobnicate a memory area (https://linux.die.net/man/3/memfrob) --- pwndbg/commands/xor.py | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/pwndbg/commands/xor.py b/pwndbg/commands/xor.py index 87c931e34..0d587399a 100644 --- a/pwndbg/commands/xor.py +++ b/pwndbg/commands/xor.py @@ -5,15 +5,46 @@ from __future__ import division from __future__ import print_function from __future__ import unicode_literals +import gdb +import six + import pwndbg.commands import pwndbg.memory +def xor_memory(address, key, count): + """ + Helper function for xorring memory in gdb + """ + mem = pwndbg.memory.read(address, count, partial=True) + + for index, byte in enumerate(mem): + key_index = index % len(key) + mem[index] = byte ^ ord(key[key_index]) + + return mem @pwndbg.commands.Command @pwndbg.commands.OnlyWhenRunning -def xor(self, address, key, count): +def xor(address, key, count): '''xor(address, key, count) XOR ``count`` bytes at ``address`` with the key ``key``. ''' - print(address,key,count) + if not isinstance(address, six.integer_types): + address = int(address, 16) + + xorred_memory = xor_memory(address, key, count) + pwndbg.memory.write(address, xorred_memory) + +@pwndbg.commands.Command +@pwndbg.commands.OnlyWhenRunning +def memfrob(address, count): + '''memfrob(address, count) + + Run the memfrob command on a region of memory + ''' + if not isinstance(address, six.integer_types): + address = int(address, 16) + + xorred_memory = xor_memory(address, '*', count) + pwndbg.memory.write(address, xorred_memory)