質問編集履歴
3
不正な編集対応
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,4 +1,175 @@
|
|
1
|
+
##Pythonに噛まれました
|
2
|
+
|
3
|
+
Windows APIを利用し,デバッガを作成しています.
|
4
|
+
言語はPython(2.7)です.
|
5
|
+
|
6
|
+
現在,手動で電卓を開き,プロセスIDを取得し,
|
7
|
+
電卓のプロセスを停止するプログラムを作成しています.
|
8
|
+
プログラムのコンパイルは無事に通るのですが,
|
9
|
+
アタッチに失敗します.
|
10
|
+
なぜなのかと解決策を質問したいです.
|
11
|
+
|
12
|
+
また以前,
|
13
|
+
PROCESS_ALL_ACCESSが定義されていないと
|
14
|
+
コンパイルが通らなかったことがあります.
|
1
|
-
|
15
|
+
定義を行って修正しましたが
|
2
|
-
皆さまご協力ありがとうございました。
|
3
|
-
大変申し訳ございませんが、諸事情により本質問内容を削除致します。
|
4
|
-
|
16
|
+
問題がないかもお願いいたします.
|
17
|
+
|
18
|
+
###実行例
|
19
|
+
実行例(PID:1620):
|
20
|
+
|
21
|
+
Enter the PID of the process to attach to:1620
|
22
|
+
[*] Unable to attach to the process.
|
23
|
+
There was an error
|
24
|
+
|
25
|
+
|
26
|
+
##my_debugger_defines
|
27
|
+
```ここに言語を入力
|
28
|
+
from ctypes import *
|
29
|
+
|
30
|
+
WORD = c_ushort
|
31
|
+
DWORD = c_ulong
|
32
|
+
LPBYTE = POINTER(c_ubyte)
|
33
|
+
LPTSTR = POINTER(c_char)
|
34
|
+
HANDLE = c_void_p
|
35
|
+
|
36
|
+
DEBUG_PROCESS = 0x00000001
|
37
|
+
CREATE_NEW_CONSOLE = 0x00000010
|
38
|
+
|
39
|
+
class STARTUPINFO(Structure):
|
40
|
+
_fields_ = [
|
41
|
+
("cb", DWORD),
|
42
|
+
("lpReserved", LPTSTR),
|
43
|
+
("lpDesktop", LPTSTR),
|
44
|
+
("lpTitle", LPTSTR),
|
45
|
+
("dwX", DWORD),
|
46
|
+
("dwY", DWORD),
|
47
|
+
("dwXSize", DWORD),
|
48
|
+
("dwYSize", DWORD),
|
49
|
+
("dwXCountChars", DWORD),
|
50
|
+
("dwYCountChars", DWORD),
|
51
|
+
("dwFillAttribute", DWORD),
|
52
|
+
("dwFlags", DWORD),
|
53
|
+
("wShowWindow", WORD),
|
54
|
+
("cbReserved2", WORD),
|
55
|
+
("lpReserved2", LPBYTE),
|
56
|
+
("hStdInput", HANDLE),
|
57
|
+
("hStdOutput", HANDLE),
|
58
|
+
("hStdError", HANDLE),
|
59
|
+
]
|
60
|
+
|
61
|
+
class PROCESS_INFORMATION(Structure):
|
62
|
+
_fields_ = [
|
63
|
+
("hProcess", HANDLE),
|
64
|
+
("hThread", HANDLE),
|
65
|
+
("dwProcessId", DWORD),
|
66
|
+
("dwThreadId", DWORD),
|
67
|
+
]
|
68
|
+
|
69
|
+
```
|
70
|
+
|
71
|
+
##my_debuggera
|
72
|
+
```ここに言語を入力
|
73
|
+
from ctypes import *
|
74
|
+
from my_debugger_defines import *
|
75
|
+
|
76
|
+
kernel32 = windll.kernel32
|
77
|
+
#PROCESS_ALL_ACCESS = 2035711
|
78
|
+
PROCESS_ALL_ACCESS = (0x000F0000L | 0x00100000L | 0xFFF)
|
79
|
+
|
80
|
+
class debugger():
|
81
|
+
def __init__(self):
|
82
|
+
self.h_process = None
|
83
|
+
self.pid = None
|
84
|
+
self.debugger_active = False
|
85
|
+
|
86
|
+
def load(self,path_to_exe):
|
87
|
+
creation_flags = DEBUG_PROCESS
|
88
|
+
|
89
|
+
startupinfo = STARTUPINFO()
|
90
|
+
process_information = PROCESS_INFORMATION()
|
91
|
+
|
92
|
+
startupinfo.dwFlags = 0x1
|
93
|
+
startupinfo.wShowWindow = 0x0
|
94
|
+
|
95
|
+
startupinfo.cb = sizeof(startupinfo)
|
96
|
+
|
97
|
+
if kernel32.CreateProcessA(path_to_exe,
|
98
|
+
None,
|
99
|
+
None,
|
100
|
+
None,
|
101
|
+
None,
|
102
|
+
creation_flags,
|
103
|
+
None,
|
104
|
+
None,
|
105
|
+
byref(startupinfo),
|
106
|
+
byref(process_information)):
|
107
|
+
print "[*] We have successfully lauched the process!"
|
108
|
+
print "[*] PID: %d" % process_information.dwProcessId
|
109
|
+
self.h_process = self.open_process(process_information.dwProcessId)
|
110
|
+
|
111
|
+
else:
|
112
|
+
print "Error: 0x%08x." % kernel32.GetLastError()
|
113
|
+
|
114
|
+
def open_process(self,pid):
|
115
|
+
|
116
|
+
h_process = kernel32.OpenProcess(PROCESS_ALL_ACCESS,False,pid)
|
117
|
+
return h_process
|
118
|
+
|
119
|
+
def attach(self,pid):
|
120
|
+
|
121
|
+
self.h_process = self.open_process(pid)
|
122
|
+
|
123
|
+
if kernel32.DebugActiveProcess(pid):
|
124
|
+
self.debugger_active = True
|
125
|
+
self.pid = int(pid)
|
126
|
+
else:
|
127
|
+
print "[*] Unable to attach to the process."
|
128
|
+
|
129
|
+
def run(self):
|
130
|
+
while self.debugger_active == True:
|
131
|
+
self.get_debug_event()
|
132
|
+
|
133
|
+
def get_debug_event(self):
|
134
|
+
|
135
|
+
debug_event = DEBUG_EVENT()
|
136
|
+
continue_status = DBG_CONTINUE
|
137
|
+
|
138
|
+
if kernel32.WaitForDebugEvent(byref(debug_event),INFINITE):
|
139
|
+
|
140
|
+
raw_input("Press a key to continue...")
|
141
|
+
self.debugger_active = False
|
142
|
+
kernel32.ContinueDebugEvent(
|
143
|
+
debug_event.dwProcessId,
|
144
|
+
debug_event.dwThreadId,
|
145
|
+
continue_status )
|
146
|
+
|
147
|
+
def datach(self):
|
148
|
+
|
149
|
+
if kernel32.DebugActiveProcessStop(self.pid):
|
150
|
+
print "[*] Finished debugging. Exiting..."
|
151
|
+
return True
|
152
|
+
else:
|
153
|
+
print "There was an error"
|
154
|
+
return False
|
155
|
+
```
|
156
|
+
|
157
|
+
##実行ファイル
|
158
|
+
```ここに言語を入力
|
159
|
+
import my_debuggera
|
160
|
+
|
161
|
+
debugger = my_debuggera.debugger()
|
162
|
+
|
163
|
+
pid = raw_input("Enter the PID of the process to attach to:")
|
164
|
+
|
165
|
+
debugger.attach(int(pid))
|
166
|
+
debugger.run()
|
167
|
+
debugger.datach()
|
168
|
+
```
|
169
|
+
|
170
|
+
###再定義
|
171
|
+
PROCESS_ALL_ACCESS
|
172
|
+
|
173
|
+
###補足情報
|
174
|
+
Python2.7
|
175
|
+
Windows7
|
2
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,175 +1,4 @@
|
|
1
|
-
##Pythonに噛まれました
|
2
|
-
|
3
|
-
Windows APIを利用し,デバッガを作成しています.
|
4
|
-
言語はPython(2.7)です.
|
5
|
-
|
6
|
-
現在,手動で電卓を開き,プロセスIDを取得し,
|
7
|
-
電卓のプロセスを停止するプログラムを作成しています.
|
8
|
-
プログラムのコンパイルは無事に通るのですが,
|
9
|
-
アタッチに失敗します.
|
10
|
-
なぜなのかと解決策を質問したいです.
|
11
|
-
|
12
|
-
また以前,
|
13
|
-
PROCESS_ALL_ACCESSが定義されていないと
|
14
|
-
コンパイルが通らなかったことがあります.
|
15
|
-
|
1
|
+
解決致しました。
|
2
|
+
皆さまご協力ありがとうございました。
|
3
|
+
大変申し訳ございませんが、諸事情により本質問内容を削除致します。
|
16
|
-
|
4
|
+
本当にありがとうございました。
|
17
|
-
|
18
|
-
###実行例
|
19
|
-
実行例(PID:1620):
|
20
|
-
|
21
|
-
Enter the PID of the process to attach to:1620
|
22
|
-
[*] Unable to attach to the process.
|
23
|
-
There was an error
|
24
|
-
|
25
|
-
|
26
|
-
##my_debugger_defines
|
27
|
-
```ここに言語を入力
|
28
|
-
from ctypes import *
|
29
|
-
|
30
|
-
WORD = c_ushort
|
31
|
-
DWORD = c_ulong
|
32
|
-
LPBYTE = POINTER(c_ubyte)
|
33
|
-
LPTSTR = POINTER(c_char)
|
34
|
-
HANDLE = c_void_p
|
35
|
-
|
36
|
-
DEBUG_PROCESS = 0x00000001
|
37
|
-
CREATE_NEW_CONSOLE = 0x00000010
|
38
|
-
|
39
|
-
class STARTUPINFO(Structure):
|
40
|
-
_fields_ = [
|
41
|
-
("cb", DWORD),
|
42
|
-
("lpReserved", LPTSTR),
|
43
|
-
("lpDesktop", LPTSTR),
|
44
|
-
("lpTitle", LPTSTR),
|
45
|
-
("dwX", DWORD),
|
46
|
-
("dwY", DWORD),
|
47
|
-
("dwXSize", DWORD),
|
48
|
-
("dwYSize", DWORD),
|
49
|
-
("dwXCountChars", DWORD),
|
50
|
-
("dwYCountChars", DWORD),
|
51
|
-
("dwFillAttribute", DWORD),
|
52
|
-
("dwFlags", DWORD),
|
53
|
-
("wShowWindow", WORD),
|
54
|
-
("cbReserved2", WORD),
|
55
|
-
("lpReserved2", LPBYTE),
|
56
|
-
("hStdInput", HANDLE),
|
57
|
-
("hStdOutput", HANDLE),
|
58
|
-
("hStdError", HANDLE),
|
59
|
-
]
|
60
|
-
|
61
|
-
class PROCESS_INFORMATION(Structure):
|
62
|
-
_fields_ = [
|
63
|
-
("hProcess", HANDLE),
|
64
|
-
("hThread", HANDLE),
|
65
|
-
("dwProcessId", DWORD),
|
66
|
-
("dwThreadId", DWORD),
|
67
|
-
]
|
68
|
-
|
69
|
-
```
|
70
|
-
|
71
|
-
##my_debuggera
|
72
|
-
```ここに言語を入力
|
73
|
-
from ctypes import *
|
74
|
-
from my_debugger_defines import *
|
75
|
-
|
76
|
-
kernel32 = windll.kernel32
|
77
|
-
#PROCESS_ALL_ACCESS = 2035711
|
78
|
-
PROCESS_ALL_ACCESS = (0x000F0000L | 0x00100000L | 0xFFF)
|
79
|
-
|
80
|
-
class debugger():
|
81
|
-
def __init__(self):
|
82
|
-
self.h_process = None
|
83
|
-
self.pid = None
|
84
|
-
self.debugger_active = False
|
85
|
-
|
86
|
-
def load(self,path_to_exe):
|
87
|
-
creation_flags = DEBUG_PROCESS
|
88
|
-
|
89
|
-
startupinfo = STARTUPINFO()
|
90
|
-
process_information = PROCESS_INFORMATION()
|
91
|
-
|
92
|
-
startupinfo.dwFlags = 0x1
|
93
|
-
startupinfo.wShowWindow = 0x0
|
94
|
-
|
95
|
-
startupinfo.cb = sizeof(startupinfo)
|
96
|
-
|
97
|
-
if kernel32.CreateProcessA(path_to_exe,
|
98
|
-
None,
|
99
|
-
None,
|
100
|
-
None,
|
101
|
-
None,
|
102
|
-
creation_flags,
|
103
|
-
None,
|
104
|
-
None,
|
105
|
-
byref(startupinfo),
|
106
|
-
byref(process_information)):
|
107
|
-
print "[*] We have successfully lauched the process!"
|
108
|
-
print "[*] PID: %d" % process_information.dwProcessId
|
109
|
-
self.h_process = self.open_process(process_information.dwProcessId)
|
110
|
-
|
111
|
-
else:
|
112
|
-
print "Error: 0x%08x." % kernel32.GetLastError()
|
113
|
-
|
114
|
-
def open_process(self,pid):
|
115
|
-
|
116
|
-
h_process = kernel32.OpenProcess(PROCESS_ALL_ACCESS,False,pid)
|
117
|
-
return h_process
|
118
|
-
|
119
|
-
def attach(self,pid):
|
120
|
-
|
121
|
-
self.h_process = self.open_process(pid)
|
122
|
-
|
123
|
-
if kernel32.DebugActiveProcess(pid):
|
124
|
-
self.debugger_active = True
|
125
|
-
self.pid = int(pid)
|
126
|
-
else:
|
127
|
-
print "[*] Unable to attach to the process."
|
128
|
-
|
129
|
-
def run(self):
|
130
|
-
while self.debugger_active == True:
|
131
|
-
self.get_debug_event()
|
132
|
-
|
133
|
-
def get_debug_event(self):
|
134
|
-
|
135
|
-
debug_event = DEBUG_EVENT()
|
136
|
-
continue_status = DBG_CONTINUE
|
137
|
-
|
138
|
-
if kernel32.WaitForDebugEvent(byref(debug_event),INFINITE):
|
139
|
-
|
140
|
-
raw_input("Press a key to continue...")
|
141
|
-
self.debugger_active = False
|
142
|
-
kernel32.ContinueDebugEvent(
|
143
|
-
debug_event.dwProcessId,
|
144
|
-
debug_event.dwThreadId,
|
145
|
-
continue_status )
|
146
|
-
|
147
|
-
def datach(self):
|
148
|
-
|
149
|
-
if kernel32.DebugActiveProcessStop(self.pid):
|
150
|
-
print "[*] Finished debugging. Exiting..."
|
151
|
-
return True
|
152
|
-
else:
|
153
|
-
print "There was an error"
|
154
|
-
return False
|
155
|
-
```
|
156
|
-
|
157
|
-
##実行ファイル
|
158
|
-
```ここに言語を入力
|
159
|
-
import my_debuggera
|
160
|
-
|
161
|
-
debugger = my_debuggera.debugger()
|
162
|
-
|
163
|
-
pid = raw_input("Enter the PID of the process to attach to:")
|
164
|
-
|
165
|
-
debugger.attach(int(pid))
|
166
|
-
debugger.run()
|
167
|
-
debugger.datach()
|
168
|
-
```
|
169
|
-
|
170
|
-
###再定義
|
171
|
-
PROCESS_ALL_ACCESS
|
172
|
-
|
173
|
-
###補足情報
|
174
|
-
Python2.7
|
175
|
-
Windows7
|
1
title
CHANGED
File without changes
|
body
CHANGED
File without changes
|