Scheming
Scheming

Reputation: 1

Python reading proccess memory with ctypes

I am attempting to read my players health. I have been on a roll but have run into a problem. I am able to read what type of information is at a certain address but can't read what the actual value is, for example here is the response I receive.

<ctypes.c_char_Array_64 object at 0x0000000002EBF9C8>

I am looking for what information is held in the c_char_Array_64 object but have no idea how I would go about it.

Here is my code:

class User:
    ctypes.wintypes.DWORD = "Entity"
    ctypes.wintypes.c_int = "Team"
    ctypes.wintypes.c_int = "Health"
    ctypes.wintypes.c_int = "Player"

    def getSelfInfo(self):
        adr1 = clientdll + dw_LocalPlayer
        adr2 = ctypes.create_string_buffer(64)
        bytes_read = ctypes.c_size_t()

        (rPM(PROCESS.handle, adr1, adr2, sys.getsizeof(ctypes.wintypes.DWORD), ctypes.byref(bytes_read)))
        print adr2
t = User()
t.getSelfInfo()

Upvotes: 0

Views: 1876

Answers (2)

Padraic Cunningham
Padraic Cunningham

Reputation: 180522

You need to get the value:

print(ar2.value)

From the docs:

If you need mutable memory blocks, ctypes has a create_string_buffer() function which creates these in various ways. The current memory block contents can be accessed (or changed) with the raw property; if you want to access it as NUL terminated string, use the value property:

>>> from ctypes import *
>>> p = create_string_buffer(3)      # create a 3 byte buffer, initialized to NUL bytes
>>> print sizeof(p), repr(p.raw)
3 '\x00\x00\x00'
>>> p = create_string_buffer("Hello")      # create a buffer containing a NUL terminated string
>>> print sizeof(p), repr(p.raw)
6 'Hello\x00'
>>> print repr(p.value)
'Hello'
>>> p = create_string_buffer("Hello", 10)  # create a 10 byte buffer
>>> print sizeof(p), repr(p.raw)
10 'Hello\x00\x00\x00\x00\x00'
>>> p.value = "Hi"
>>> print sizeof(p), repr(p.raw)
10 'Hi\x00lo\x00\x00\x00\x00\x00'
>>>

Upvotes: 1

ShadowRanger
ShadowRanger

Reputation: 155684

The empty slice of most ctypes array types will return the Python equivalent type. So to convert your 64 byte buffer to a str (in Py3 bytes), you can do:

print ar2[:]

That will read the full raw 64 bytes mind you. If you want to read it as a C-style string (so the first NUL byte terminates the Python equivalent str), you'd use .value:

print ar2.value

Upvotes: 0

Related Questions