import ctypes, re
from ctypes import wintypes

kernel32 = ctypes.windll.kernel32

pid = 32572
hProc = kernel32.OpenProcess(0x0010 | 0x0020 | 0x0400, False, pid)

class MEMORY_BASIC_INFORMATION(ctypes.Structure):
    _fields_ = [
        ("BaseAddress", ctypes.c_void_p),
        ("AllocationBase", ctypes.c_void_p),
        ("AllocationProtect", wintypes.DWORD),
        ("RegionSize", ctypes.c_size_t),
        ("State", wintypes.DWORD),
        ("Protect", wintypes.DWORD),
        ("Type", wintypes.DWORD)
    ]

MEM_COMMIT = 0x1000
PAGE_READWRITE = 0x04
PAGE_READONLY = 0x02

mbi = MEMORY_BASIC_INFORMATION()
addr = 0
found_ips = set()
ip_pattern = re.compile(rb'\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b')

while kernel32.VirtualQueryEx(hProc, ctypes.c_void_p(addr), ctypes.byref(mbi), ctypes.sizeof(mbi)):
    if mbi.State == MEM_COMMIT and (mbi.Protect & (PAGE_READWRITE | PAGE_READONLY)):
        buf = ctypes.create_string_buffer(mbi.RegionSize)
        bytesRead = ctypes.c_size_t()
        if kernel32.ReadProcessMemory(hProc, ctypes.c_void_p(addr), buf, mbi.RegionSize, ctypes.byref(bytesRead)):
            matches = ip_pattern.findall(buf.raw[:bytesRead.value])
            for ip in matches:
                if not ip.startswith(b'127.') and not ip.startswith(b'0.') and not ip.startswith(b'255.'):
                    found_ips.add(ip.decode('ascii'))
    addr += mbi.RegionSize
    if addr >= 0x7FFF0000:
        break

kernel32.CloseHandle(hProc)
with open(r'c:\Projects\JX\logs\game_memory_ips.txt', 'w', encoding='utf-8') as f:
    for ip in sorted(found_ips):
        f.write(f"{ip}\n")
