Writes to the memory of the process. @note: Page permissions may be changed temporarily while writing. @see: L{write} @type lpBaseAddress: int @param lpBaseAddress: Memory address to begin writing. @type lpBuffer: str @param lpBuffer: By
(self, lpBaseAddress, lpBuffer)
| 1992 | return data |
| 1993 | |
| 1994 | def poke(self, lpBaseAddress, lpBuffer): |
| 1995 | """ |
| 1996 | Writes to the memory of the process. |
| 1997 | |
| 1998 | @note: Page permissions may be changed temporarily while writing. |
| 1999 | |
| 2000 | @see: L{write} |
| 2001 | |
| 2002 | @type lpBaseAddress: int |
| 2003 | @param lpBaseAddress: Memory address to begin writing. |
| 2004 | |
| 2005 | @type lpBuffer: str |
| 2006 | @param lpBuffer: Bytes to write. |
| 2007 | |
| 2008 | @rtype: int |
| 2009 | @return: Number of bytes written. |
| 2010 | May be less than the number of bytes to write. |
| 2011 | """ |
| 2012 | assert isinstance(lpBuffer, compat.bytes) |
| 2013 | hProcess = self.get_handle(win32.PROCESS_VM_WRITE | win32.PROCESS_VM_OPERATION | win32.PROCESS_QUERY_INFORMATION) |
| 2014 | mbi = self.mquery(lpBaseAddress) |
| 2015 | if not mbi.has_content(): |
| 2016 | raise ctypes.WinError(win32.ERROR_INVALID_ADDRESS) |
| 2017 | if mbi.is_image() or mbi.is_mapped(): |
| 2018 | prot = win32.PAGE_WRITECOPY |
| 2019 | elif mbi.is_writeable(): |
| 2020 | prot = None |
| 2021 | elif mbi.is_executable(): |
| 2022 | prot = win32.PAGE_EXECUTE_READWRITE |
| 2023 | else: |
| 2024 | prot = win32.PAGE_READWRITE |
| 2025 | if prot is not None: |
| 2026 | try: |
| 2027 | self.mprotect(lpBaseAddress, len(lpBuffer), prot) |
| 2028 | except Exception: |
| 2029 | prot = None |
| 2030 | msg = "Failed to adjust page permissions for process %s at address %s: %s" |
| 2031 | msg = msg % (self.get_pid(), HexDump.address(lpBaseAddress, self.get_bits()), traceback.format_exc()) |
| 2032 | warnings.warn(msg, RuntimeWarning) |
| 2033 | try: |
| 2034 | r = win32.WriteProcessMemory(hProcess, lpBaseAddress, lpBuffer) |
| 2035 | finally: |
| 2036 | if prot is not None: |
| 2037 | self.mprotect(lpBaseAddress, len(lpBuffer), mbi.Protect) |
| 2038 | return r |
| 2039 | |
| 2040 | def peek_char(self, lpBaseAddress): |
| 2041 | """ |
no test coverage detected