Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python3 compatibility #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions memorpy/Address.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# You should have received a copy of the GNU General Public License
# along with memorpy. If not, see <http://www.gnu.org/licenses/>.

import utils
import memorpy.utils as utils

class AddressException(Exception):
pass
Expand Down Expand Up @@ -58,7 +58,7 @@ def get_instruction(self):

def dump(self, ftype = 'bytes', size = 512, before = 32):
buf = self.process.read_bytes(self.value - before, size)
print utils.hex_dump(buf, self.value - before, ftype=ftype)
print(utils.hex_dump(buf, self.value - before, ftype=ftype))

def __nonzero__(self):
return self.value is not None and self.value != 0
Expand All @@ -77,7 +77,7 @@ def __repr__(self):
def __str__(self):
if not self.symbolic_name:
self.symbolic_name = self.symbol()
return str('<Addr: %s' % self.symbolic_name + ' : "%s" (%s)>' % (str(self.read()).encode('string_escape'), self.default_type))
return str('<Addr: %s' % self.symbolic_name + ' : "%s" (%s)>' % (str(self.read()).encode('unicode_escape'), self.default_type))

def __int__(self):
return int(self.value)
Expand Down
20 changes: 11 additions & 9 deletions memorpy/BaseProcess.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env python
# -*- coding: UTF8 -*-

import utils
import memorpy.utils as utils
import struct

""" Base class for process not linked to any platform """
Expand Down Expand Up @@ -38,14 +38,16 @@ def get_symbolic_name(self, address):
def read(self, address, type = 'uint', maxlen = 50, errors='raise'):
if type == 's' or type == 'string':
s = self.read_bytes(int(address), bytes=maxlen)
news = ''
for c in s:
if c == '\x00':
return news
news += c
if errors=='ignore':
return news
raise ProcessException('string > maxlen')

try:
idx = s.index(b'\x00')
return s[:idx]
except:
if errors == 'ignore':
return s

raise ProcessException('string > maxlen')

else:
if type == 'bytes' or type == 'b':
return self.read_bytes(int(address), bytes=maxlen)
Expand Down
6 changes: 3 additions & 3 deletions memorpy/LinProcess.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@

import copy
import struct
import utils
import memorpy.utils
import platform
import ctypes, re, sys
from ctypes import create_string_buffer, byref, c_int, c_void_p, c_long, c_size_t, c_ssize_t, POINTER, get_errno
import errno
import os
import signal
from BaseProcess import BaseProcess, ProcessException
from structures import *
from .BaseProcess import BaseProcess, ProcessException
from .structures import *
import logging

logger = logging.getLogger('memorpy')
Expand Down
2 changes: 1 addition & 1 deletion memorpy/Locator.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import copy
import time
from Address import Address
from .Address import Address
import struct

class Locator(object):
Expand Down
14 changes: 7 additions & 7 deletions memorpy/MemWorker.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
import string
import re
import logging
import Process
import utils
import memorpy.Process as Process
import memorpy.utils as utils
import struct
from Address import Address
from BaseProcess import ProcessException
from .Address import Address
from .BaseProcess import ProcessException
import traceback
import binascii
from structures import *
from .structures import *

logger = logging.getLogger('memorpy')

Expand Down Expand Up @@ -187,15 +187,15 @@ def mem_search(self, value, ftype = 'match', protec = PAGE_READWRITE | PAGE_READ
raise ProcessException("Can't read_bytes, process %s is not open" % (self.process.pid))

for offset, chunk_size in self.process.iter_region(start_offset=start_offset, end_offset=end_offset, protec=protec, optimizations=optimizations):
b = ''
b = b''
current_offset = offset
chunk_read = 0
chunk_exc = False
while chunk_read < chunk_size:
try:
b += self.process.read_bytes(current_offset, chunk_size)
except IOError as e:
print traceback.format_exc()
print(traceback.format_exc())
if e.errno == 13:
raise
else:
Expand Down
6 changes: 3 additions & 3 deletions memorpy/OSXProcess.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@

import copy
import struct
import utils
import memorpy.utils
import platform
import ctypes, re, sys
import ctypes.util
import errno
import os
import signal
from BaseProcess import BaseProcess, ProcessException
from structures import *
from .BaseProcess import BaseProcess, ProcessException
from .structures import *
import logging
import subprocess

Expand Down
10 changes: 5 additions & 5 deletions memorpy/Process.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
# -*- coding: UTF8 -*-

import sys
from BaseProcess import *
from .BaseProcess import *
if sys.platform=='win32':
from WinProcess import WinProcess as Process
from .WinProcess import WinProcess as Process
elif sys.platform=='darwin':
from OSXProcess import OSXProcess as Process
from .OSXProcess import OSXProcess as Process
elif 'sunos' in sys.platform:
from SunProcess import SunProcess as Process
from .SunProcess import SunProcess as Process
else:
from LinProcess import LinProcess as Process
from .LinProcess import LinProcess as Process
2 changes: 1 addition & 1 deletion memorpy/SunProcess.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# You should have received a copy of the GNU General Public License
# along with memorpy. If not, see <http://www.gnu.org/licenses/>.

from BaseProcess import BaseProcess, ProcessException
from .BaseProcess import BaseProcess, ProcessException
import struct
import os

Expand Down
8 changes: 4 additions & 4 deletions memorpy/WinProcess.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
# along with memorpy. If not, see <http://www.gnu.org/licenses/>.

from ctypes import pointer, sizeof, windll, create_string_buffer, c_ulong, byref, GetLastError, c_bool, WinError
from structures import *
from .structures import *
import copy
import struct
import utils
import memorpy.utils
import platform
from BaseProcess import BaseProcess, ProcessException
from .BaseProcess import BaseProcess, ProcessException

psapi = windll.psapi
kernel32 = windll.kernel32
Expand Down Expand Up @@ -240,7 +240,7 @@ def read_bytes(self, address, bytes = 4, use_NtWow64ReadVirtualMemory64=False):
address = int(address)
buffer = create_string_buffer(bytes)
bytesread = c_size_t(0)
data = ''
data = b''
length = bytes
while length:
if RpM(self.h_process, address, buffer, bytes, byref(bytesread)) or (use_NtWow64ReadVirtualMemory64 and GetLastError() == 0):
Expand Down
2 changes: 1 addition & 1 deletion memorpy/WinStructures.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class THREADENTRY32(Structure):


class TH32CS_CLASS(object):
INHERIT = 2147483648L
INHERIT = 2147483648
SNAPHEAPLIST = 1
SNAPMODULE = 8
SNAPMODULE32 = 16
Expand Down
10 changes: 5 additions & 5 deletions memorpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
logger.addHandler(ch)

import sys
from MemWorker import *
from Locator import *
from Address import *
from Process import *
from utils import *
from .MemWorker import *
from .Locator import *
from .Address import *
from .Process import *
from .utils import *
#if sys.platform=="win32":
# from wintools import * #not a necessary dependency, just used for debugging
4 changes: 2 additions & 2 deletions memorpy/structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@

import sys
if sys.platform=="win32":
from WinStructures import *
from .WinStructures import *
else:
from LinStructures import *
from .LinStructures import *
6 changes: 3 additions & 3 deletions memorpy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def hex_dump(data, addr = 0, prefix = '', ftype = 'bytes'):
packedval = data[i:i + structlen]
tmpval = struct.unpack(structtype, packedval)[0]
except Exception as e:
print e
print(e)

if tmpval == 'NaN':
dump += '{:<15} '.format(tmpval)
Expand All @@ -105,8 +105,8 @@ def hex_dump(data, addr = 0, prefix = '', ftype = 'bytes'):

dump += '\n%s%08X: ' % (prefix, addr)
slice = ''
dump += '%02X ' % ord(byte)
slice += byte
dump += '%02X ' % byte
slice += chr(byte)
addr += 1

remainder = addr % 16
Expand Down