spacepaste

  1.  
  2. import sys, os
  3. sys.path.append(sys.prefix)
  4. os.environ['PATH'] += ';' + sys.prefix
  5. from win32con import PAGE_READWRITE, MEM_COMMIT, MEM_RESERVE, MEM_RELEASE,\
  6. PROCESS_ALL_ACCESS, PROCESS_VM_OPERATION
  7. from commctrl import LVM_GETITEMTEXT, LVM_GETITEMCOUNT, LVM_SETITEMTEXTA
  8. import struct
  9. import ctypes
  10. import win32api
  11. import win32gui
  12. GetWindowThreadProcessId = ctypes.windll.user32.GetWindowThreadProcessId
  13. VirtualAllocEx = ctypes.windll.kernel32.VirtualAllocEx
  14. VirtualFreeEx = ctypes.windll.kernel32.VirtualFreeEx
  15. OpenProcess = ctypes.windll.kernel32.OpenProcess
  16. WriteProcessMemory = ctypes.windll.kernel32.WriteProcessMemory
  17. ReadProcessMemory = ctypes.windll.kernel32.ReadProcessMemory
  18. memcpy = ctypes.cdll.msvcrt.memcpy
  19. def readListViewItems(hwnd, item=0, subitem=0):
  20. # Allocate virtual memory inside target process
  21. pid = ctypes.create_string_buffer(4)
  22. p_pid = ctypes.addressof(pid)
  23. GetWindowThreadProcessId(hwnd, p_pid) # process owning the given hwnd
  24. hProcHnd = OpenProcess(PROCESS_ALL_ACCESS, False, struct.unpack("i",pid)[0])
  25. pLVI = VirtualAllocEx(hProcHnd, 0, 4096, MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE)
  26. pBuffer = VirtualAllocEx(hProcHnd, 0, 4096, MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE)
  27. # Prepare an LVITEM record and write it to target process memory
  28. lvitem_str = struct.pack('iiiiiiiii', *[0,item,subitem,0,0,pBuffer,4096,0,0])
  29. lvitem_buffer = ctypes.create_string_buffer(lvitem_str)
  30. copied = ctypes.create_string_buffer(4)
  31. p_copied = ctypes.addressof(copied)
  32. WriteProcessMemory(hProcHnd, pLVI, ctypes.addressof(lvitem_buffer), ctypes.sizeof(lvitem_buffer), p_copied)
  33. # iterate items in the SysListView32 control
  34. num_items = win32gui.SendMessage(hwnd, LVM_GETITEMCOUNT)
  35. #item_texts = []
  36. item_text = ''
  37. #for item_index in range(num_items):
  38. # win32gui.SendMessage(hwnd, LVM_GETITEMTEXT, item_index, pLVI)
  39. # target_buff = ctypes.create_string_buffer(4096)
  40. # ReadProcessMemory(hProcHnd, pBuffer, ctypes.addressof(target_buff), 4096, p_copied)
  41. # item_texts.append(target_buff.value)
  42. win32gui.SendMessage(hwnd, LVM_GETITEMTEXT, item, pLVI)
  43. target_buff = ctypes.create_string_buffer(4096)
  44. ReadProcessMemory(hProcHnd, pBuffer, ctypes.addressof(target_buff), 4096, p_copied)
  45. item_text = target_buff.value
  46. VirtualFreeEx(hProcHnd, pBuffer, 0, MEM_RELEASE)
  47. VirtualFreeEx(hProcHnd, pLVI, 0, MEM_RELEASE)
  48. win32api.CloseHandle(hProcHnd)
  49. return item_text
  50. def SetItemText(Handle, pStr, Index , SubIndex = 0):
  51. # Allocate virtual memory inside target process
  52. pid = ctypes.create_string_buffer(4)
  53. p_pid = ctypes.addressof(pid)
  54. GetWindowThreadProcessId(Handle, p_pid) # process owning the given hwnd
  55. hProcHnd = OpenProcess(PROCESS_ALL_ACCESS, False, struct.unpack("i",pid)[0])
  56. pLVI = VirtualAllocEx(hProcHnd, 0, 4096, MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE)
  57. #c_string = pStr.encode('ascii') + '\x00'
  58. c_string = ctypes.c_wchar_p(pStr)
  59. strSize = ctypes.sizeof(c_string)+4
  60. #alloc some shared memory for our string
  61. SharedProcMemString = VirtualAllocEx(hProcHnd, 0, strSize, MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE)
  62. # Prepare an LVITEM record and write it to target process memory
  63. lvitem_str = struct.pack('iiiiiiiii', *[0,Index,SubIndex,0,0,SharedProcMemString,strSize,0,0])
  64. lvitem_buffer = ctypes.create_string_buffer(lvitem_str)
  65. copied = ctypes.create_string_buffer(4)
  66. p_copied = ctypes.addressof(copied)
  67. print ctypes.sizeof(lvitem_buffer)
  68. print strSize
  69. print WriteProcessMemory(hProcHnd, SharedProcMemString, c_string, strSize , p_copied)
  70. print WriteProcessMemory(hProcHnd, pLVI, ctypes.addressof(lvitem_buffer), ctypes.sizeof(lvitem_buffer), p_copied)
  71. #set the text
  72. win32gui.SendMessage(Handle, LVM_SETITEMTEXTA, Index, pLVI)
  73. #'clean up
  74. #FreeMemSharedNT hProcess, SharedProcMem, LVISize
  75. #FreeMemSharedNT hProcess, SharedProcMemString, strSize
  76. VirtualFreeEx(hProcHnd, SharedProcMemString, 0, MEM_RELEASE)
  77. VirtualFreeEx(hProcHnd, pLVI, 0, MEM_RELEASE)
  78. win32api.CloseHandle(hProcHnd)
  79. import win32gui
  80. if __name__ == '__main__':
  81. print "starting main"
  82. RetVal0 = win32gui.FindWindow('#32770', 'Windows Task Manager')
  83. RetVal1 = win32gui.FindWindowEx(RetVal0, 0, '#32770', None)
  84. RetVal = win32gui.FindWindowEx(RetVal1, 0, 'SysListView32', 'Processes')
  85. pSource = 'pythonw.exe'
  86. pDest = 'myGui.exe'
  87. if RetVal:
  88. ii=0
  89. i=0
  90. while ii < 26:
  91. RetStr = readListViewItems(RetVal, i, ii)
  92. #print readListViewItems1(RetVal, i)
  93. #print RetStr
  94. if RetStr == '': # Then ' we've come to the end of the columns
  95. if i == 0: #'was the first loop thru
  96. i = 1 #'could be the correct column, but .exe not found so add +1
  97. ii = -1 #'start the column count over
  98. else:
  99. break
  100. elif ".exe" in RetStr.lower(): #'we found the Process column
  101. #tCount = win32gui.GetItemCount(RetVal)
  102. # iterate items in the SysListView32 control
  103. tCount = win32gui.SendMessage(RetVal, LVM_GETITEMCOUNT)
  104. for i in range(i, tCount - 1):
  105. RetStr = readListViewItems(RetVal, i, ii)
  106. if RetStr.lower() == pSource.lower():
  107. #If Delete Then
  108. # Call DeleteItem(RetVal, i) #'doesnt work as well
  109. #Else
  110. print 'calling SetItemText'
  111. print RetStr
  112. print pSource
  113. SetItemText(RetVal, pDest, i, ii)
  114. #End If
  115. #ModifyExe = True
  116. #'[EXIT DO] can be taken out if the app runs multiple instances
  117. #'the reason why i put it here is because i am trying to limit
  118. #' the amount of unneeded sendmessage calls to taskmanager
  119. #Exit Do #'should only find 1 instance of itself.
  120. #break
  121. ii = ii + 1
  122.