当前位置:   article > 正文

关于Python灰帽子中debugger.attach()不成功解决方法_debugger attached package报错

debugger attached package报错
本人第一次写博客,写的不好见谅。


最近在做PythonGreyHat中的实验,发现第三章的debugger.attach()一直不成功,返回信息一直为

[*]unable to attach to the process.

[*]ErrorCode:0x0000005


ErrorCode是kernel32.GetLastError()返回的,查得是拒绝访问。


解决方法是提权。我在my_debugger.py中定义了提权方法:enableDebugPrivilege()


我的my_debugger_defines.py:

  1. from ctypes import *
  2. BYTE = c_ubyte
  3. WORD = c_ushort
  4. DWORD = c_ulong
  5. LPBYTE = POINTER(c_ubyte)
  6. LPTSTR = POINTER(c_char)
  7. HANDLE = c_void_p
  8. PVOID = c_void_p
  9. LPVOID = c_void_p
  10. UINT_PTR= c_ulong
  11. SIZE_T = c_ulong
  12. LONG = c_long
  13. DEBUG_PROCESS = 0x01
  14. CREATE_NEW_CONSOLE = 0X010
  15. PROCESS_ALL_ACCESS = 0x001f0fff
  16. INFINITE = 0xFFFFFFFF
  17. DBG_CONTINUE = 0x00010002
  18. EXCEPTION_DEBUG_EVENT = 0x1
  19. CREATE_THREAD_DEBUG_EVENT = 0x2
  20. CREATE_PROCESS_DEBUG_EVENT = 0x3
  21. EXIT_THREAD_DEBUG_EVENT = 0x4
  22. EXIT_PROCESS_DEBUG_EVENT = 0x5
  23. LOAD_DLL_DEBUG_EVENT = 0x6
  24. UNLOAD_DLL_DEBUG_EVENT = 0x7
  25. OUTPUT_DEBUG_STRING_EVENT = 0x8
  26. RIP_EVENT = 0x9
  27. EXCEPTION_ACCESS_VIOLATION = 0xC0000005
  28. EXCEPTION_BREAKPOINT = 0x80000003
  29. EXCEPTION_GUARD_PAGE = 0x80000001
  30. EXCEPTION_SINGLE_STEP = 0x80000004
  31. TH32CS_SNAPHEAPLIST = 0x00000001
  32. TH32CS_SNAPPROCESS = 0x00000002
  33. TH32CS_SNAPTHREAD = 0x00000004
  34. TH32CS_SNAPMODULE = 0x00000008
  35. TH32CS_INHERIT = 0x80000000
  36. TH32CS_SNAPALL = (TH32CS_SNAPHEAPLIST | TH32CS_SNAPPROCESS | TH32CS_SNAPTHREAD | TH32CS_SNAPMODULE)
  37. THREAD_ALL_ACCESS = 0x001F03FF
  38. CONTEXT_FULL = 0x00010007
  39. CONTEXT_DEBUG_REGISTERS = 0x00010010
  40. PAGE_EXECUTE_READWRITE = 0x00000040
  41. HW_ACCESS = 0x00000003
  42. HW_EXECUTE = 0x00000000
  43. HW_WRITE = 0x00000001
  44. PAGE_NOACCESS = 0x00000001
  45. PAGE_READONLY = 0x00000002
  46. PAGE_READWRITE = 0x00000004
  47. PAGE_WRITECOPY = 0x00000008
  48. PAGE_EXECUTE = 0x00000010
  49. PAGE_EXECUTE_READ = 0x00000020
  50. PAGE_EXECUTE_READWRITE = 0x00000040
  51. PAGE_EXECUTE_WRITECOPY = 0x00000080
  52. PAGE_GUARD = 0x00000100
  53. PAGE_NOCACHE = 0x00000200
  54. PAGE_WRITECOMBINE = 0x00000400
  55. class EXCEPTION_RECORD(Structure):
  56. pass
  57. EXCEPTION_RECORD._fields_ = [
  58. ("ExceptionCode", DWORD),
  59. ("ExceptionFlags", DWORD),
  60. ("ExceptionRecord", POINTER(EXCEPTION_RECORD)),
  61. ("ExceptionAddress", PVOID),
  62. ("NumberParameters", DWORD),
  63. ("ExceptionInformation", UINT_PTR * 15),
  64. ]
  65. class _EXCEPTION_RECORD(Structure):
  66. _fields_ = [
  67. ("ExceptionCode", DWORD),
  68. ("ExceptionFlags", DWORD),
  69. ("ExceptionRecord", POINTER(EXCEPTION_RECORD)),
  70. ("ExceptionAddress", PVOID),
  71. ("NumberParameters", DWORD),
  72. ("ExceptionInformation", UINT_PTR * 15),
  73. ]
  74. # Exceptions
  75. class EXCEPTION_DEBUG_INFO(Structure):
  76. _fields_ = [
  77. ("ExceptionRecord", EXCEPTION_RECORD),
  78. ("dwFirstChance", DWORD),
  79. ]
  80. # it populates this union appropriately
  81. class DEBUG_EVENT_UNION(Union):
  82. _fields_ = [
  83. ("Exception", EXCEPTION_DEBUG_INFO),
  84. # ("CreateThread", CREATE_THREAD_DEBUG_INFO),
  85. # ("CreateProcessInfo", CREATE_PROCESS_DEBUG_INFO),
  86. # ("ExitThread", EXIT_THREAD_DEBUG_INFO),
  87. # ("ExitProcess", EXIT_PROCESS_DEBUG_INFO),
  88. # ("LoadDll", LOAD_DLL_DEBUG_INFO),
  89. # ("UnloadDll", UNLOAD_DLL_DEBUG_INFO),
  90. # ("DebugString", OUTPUT_DEBUG_STRING_INFO),
  91. # ("RipInfo", RIP_INFO),
  92. ]
  93. # DEBUG_EVENT describes a debugging event
  94. # that the debugger has trapped
  95. class DEBUG_EVENT(Structure):
  96. _fields_ = [
  97. ("dwDebugEventCode", DWORD),
  98. ("dwProcessId", DWORD),
  99. ("dwThreadId", DWORD),
  100. ("u", DEBUG_EVENT_UNION),
  101. ]
  102. class STARTUPINFO(Structure):
  103. _fields_=[
  104. ("cb", DWORD),
  105. ("lpReserved", LPTSTR),
  106. ("lpDesktop", LPTSTR),
  107. ("lpTitle", LPTSTR),
  108. ("dwX", DWORD),
  109. ("dwY", DWORD),
  110. ("dwXSize", DWORD),
  111. ("dwYSize", DWORD),
  112. ("dwXCountChars",DWORD),
  113. ("dwYCountChars",DWORD),
  114. ("dwFillAttribute",DWORD),
  115. ("dwFlags", DWORD),
  116. ("wShowWindow", WORD),
  117. ("cbReserved2", WORD),
  118. ("lpReserved2", LPBYTE),
  119. ("hStdInput", HANDLE),
  120. ("hStdOutput", HANDLE),
  121. ("hStdError", HANDLE),
  122. ]
  123. class PROCESS_INFORMATION(Structure):
  124. _fields_=[
  125. ("hProcess", HANDLE),
  126. ("hThread", HANDLE),
  127. ("dwProcessId", DWORD),
  128. ("dwThreadId", DWORD),
  129. ]
  130. class DEBUG_EVENT(Structure):
  131. _fields_=[
  132. ("dwDebugEventCode",DWORD),
  133. ("dwProcessId",DWORD),
  134. ("dwThreadId",DWORD),
  135. ]
  136. class FLOATING_SAVE_AREA(Structure):
  137. _fields_=[
  138. ("ControlWord", c_ulong),
  139. ("StatusWord", c_ulong),
  140. ("TagWord", c_ulong),
  141. ("ErrorOffset", c_ulong),
  142. ("ErrorSelector", c_ulong),
  143. ("DataOffset", c_ulong),
  144. ("DataSelector", c_ulong),
  145. ("RegisterArea", 80*c_ubyte),
  146. ("Cr0NpxState", c_ulong),
  147. ]
  148. class CONTEXT(Structure):
  149. _fields_=[
  150. ("ContextFlags", DWORD),
  151. ("Dr0", DWORD),
  152. ("Dr1", DWORD),
  153. ("Dr2", DWORD),
  154. ("Dr3", DWORD),
  155. ("Dr6", DWORD),
  156. ("Dr7", DWORD),
  157. ("FloatSave", FLOATING_SAVE_AREA),
  158. ("SegGs",DWORD),
  159. ("SegFs",DWORD),
  160. ("SegEs",DWORD),
  161. ("SegDs",DWORD),
  162. ("Edi",DWORD),
  163. ("Esi",DWORD),
  164. ("Ebx",DWORD),
  165. ("Edx",DWORD),
  166. ("Ecx",DWORD),
  167. ("Eax",DWORD),
  168. ("Ebp",DWORD),
  169. ("Eip",DWORD),
  170. ("SegCs",DWORD),
  171. ("EFlags",DWORD),
  172. ("Esp",DWORD),
  173. ("SegSs",DWORD),
  174. ("ExtendedRegisters",512*BYTE),
  175. ]
  176. class LUID(Structure):
  177. _fields_=[
  178. ("LowPart",DWORD),
  179. ("HighPart",LONG),
  180. ]
  181. class LUID_AND_ATTRIBUTES(Structure):
  182. _fields_=[
  183. ("Luid", LUID),
  184. ("Attributes",DWORD),
  185. ]
  186. class TOKEN_PRIVILEGES(Structure):
  187. _fields_=[
  188. ("PrivilegeCount",DWORD),
  189. ("Privileges",LUID_AND_ATTRIBUTES*1),
  190. ]

我的my_debugger.py中:

1.class debugger()增加的内容:

  1. def enableDebugPrivilege(self):
  2. advapi32 = windll.LoadLibrary("Advapi32.dll")
  3. hToken=HANDLE()
  4. if advapi32.OpenProcessToken(kernel32.GetCurrentProcess(),0x20,byref(hToken)):
  5. tp=TOKEN_PRIVILEGES()
  6. tp.PrivilegeCount=1
  7. if not advapi32.LookupPrivilegeValueA(0,"SeDebugPrivilege",byref(tp.Privileges[0].Luid)):
  8. print("[*]can't lookup privilege value.")
  9. print("[*]errorcode:0x%08x."%kernel32.GetLastError())
  10. return False
  11. tp.Privileges[0].Attributes=0X02
  12. if not advapi32.AdjustTokenPrivileges(hToken,0,byref(tp),sizeof(tp),0,0):
  13. print("[*]can't adjust privilege value.")
  14. print("[*]errorcode:0x%08x."%kernel32.GetLastError())
  15. return False
  16. kernel32.CloseHandle(hToken)
  17. return True
  18. else:
  19. print("[*]can't open process token.")
  20. print("[*]error code:0x%08x."%kernel32.GetLastError())
  21. return False

2.将debugger()中attach的定义添加一句:

  1. def attach(self,pid):
  2. if not self.enableDebugPrivilege():
  3. return False
  4. self.h_process=self.open_process(pid)

本人较粗心,上述代码可能有错误,所以请纠正。希望能帮到你。



声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/287949?site
推荐阅读
相关标签
  

闽ICP备14008679号