質問編集履歴
3
不正な編集対応
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,7 +1,349 @@
|
|
1
|
+
##Pythonに噛まれました
|
2
|
+
|
3
|
+
|
4
|
+
|
5
|
+
Windows APIを利用し,デバッガを作成しています.
|
6
|
+
|
7
|
+
言語はPython(2.7)です.
|
8
|
+
|
9
|
+
|
10
|
+
|
11
|
+
現在,手動で電卓を開き,プロセスIDを取得し,
|
12
|
+
|
13
|
+
電卓のプロセスを停止するプログラムを作成しています.
|
14
|
+
|
15
|
+
プログラムのコンパイルは無事に通るのですが,
|
16
|
+
|
17
|
+
アタッチに失敗します.
|
18
|
+
|
19
|
+
なぜなのかと解決策を質問したいです.
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
また以前,
|
24
|
+
|
25
|
+
PROCESS_ALL_ACCESSが定義されていないと
|
26
|
+
|
27
|
+
コンパイルが通らなかったことがあります.
|
28
|
+
|
1
|
-
|
29
|
+
定義を行って修正しましたが
|
2
|
-
|
3
|
-
|
30
|
+
|
4
|
-
|
5
|
-
大変申し訳ございませんが、諸事情により本質問内容を削除致します。
|
6
|
-
|
7
|
-
|
31
|
+
問題がないかもお願いいたします.
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
###実行例
|
36
|
+
|
37
|
+
実行例(PID:1620):
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
Enter the PID of the process to attach to:1620
|
42
|
+
|
43
|
+
[*] Unable to attach to the process.
|
44
|
+
|
45
|
+
There was an error
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
##my_debugger_defines
|
52
|
+
|
53
|
+
```ここに言語を入力
|
54
|
+
|
55
|
+
from ctypes import *
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
WORD = c_ushort
|
60
|
+
|
61
|
+
DWORD = c_ulong
|
62
|
+
|
63
|
+
LPBYTE = POINTER(c_ubyte)
|
64
|
+
|
65
|
+
LPTSTR = POINTER(c_char)
|
66
|
+
|
67
|
+
HANDLE = c_void_p
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
DEBUG_PROCESS = 0x00000001
|
72
|
+
|
73
|
+
CREATE_NEW_CONSOLE = 0x00000010
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
class STARTUPINFO(Structure):
|
78
|
+
|
79
|
+
_fields_ = [
|
80
|
+
|
81
|
+
("cb", DWORD),
|
82
|
+
|
83
|
+
("lpReserved", LPTSTR),
|
84
|
+
|
85
|
+
("lpDesktop", LPTSTR),
|
86
|
+
|
87
|
+
("lpTitle", LPTSTR),
|
88
|
+
|
89
|
+
("dwX", DWORD),
|
90
|
+
|
91
|
+
("dwY", DWORD),
|
92
|
+
|
93
|
+
("dwXSize", DWORD),
|
94
|
+
|
95
|
+
("dwYSize", DWORD),
|
96
|
+
|
97
|
+
("dwXCountChars", DWORD),
|
98
|
+
|
99
|
+
("dwYCountChars", DWORD),
|
100
|
+
|
101
|
+
("dwFillAttribute", DWORD),
|
102
|
+
|
103
|
+
("dwFlags", DWORD),
|
104
|
+
|
105
|
+
("wShowWindow", WORD),
|
106
|
+
|
107
|
+
("cbReserved2", WORD),
|
108
|
+
|
109
|
+
("lpReserved2", LPBYTE),
|
110
|
+
|
111
|
+
("hStdInput", HANDLE),
|
112
|
+
|
113
|
+
("hStdOutput", HANDLE),
|
114
|
+
|
115
|
+
("hStdError", HANDLE),
|
116
|
+
|
117
|
+
]
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
class PROCESS_INFORMATION(Structure):
|
122
|
+
|
123
|
+
_fields_ = [
|
124
|
+
|
125
|
+
("hProcess", HANDLE),
|
126
|
+
|
127
|
+
("hThread", HANDLE),
|
128
|
+
|
129
|
+
("dwProcessId", DWORD),
|
130
|
+
|
131
|
+
("dwThreadId", DWORD),
|
132
|
+
|
133
|
+
]
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
```
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
##my_debuggera
|
142
|
+
|
143
|
+
```ここに言語を入力
|
144
|
+
|
145
|
+
from ctypes import *
|
146
|
+
|
147
|
+
from my_debugger_defines import *
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
kernel32 = windll.kernel32
|
152
|
+
|
153
|
+
#PROCESS_ALL_ACCESS = 2035711
|
154
|
+
|
155
|
+
PROCESS_ALL_ACCESS = (0x000F0000L | 0x00100000L | 0xFFF)
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
class debugger():
|
160
|
+
|
161
|
+
def __init__(self):
|
162
|
+
|
163
|
+
self.h_process = None
|
164
|
+
|
165
|
+
self.pid = None
|
166
|
+
|
167
|
+
self.debugger_active = False
|
168
|
+
|
169
|
+
|
170
|
+
|
171
|
+
def load(self,path_to_exe):
|
172
|
+
|
173
|
+
creation_flags = DEBUG_PROCESS
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
startupinfo = STARTUPINFO()
|
178
|
+
|
179
|
+
process_information = PROCESS_INFORMATION()
|
180
|
+
|
181
|
+
|
182
|
+
|
183
|
+
startupinfo.dwFlags = 0x1
|
184
|
+
|
185
|
+
startupinfo.wShowWindow = 0x0
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
startupinfo.cb = sizeof(startupinfo)
|
190
|
+
|
191
|
+
|
192
|
+
|
193
|
+
if kernel32.CreateProcessA(path_to_exe,
|
194
|
+
|
195
|
+
None,
|
196
|
+
|
197
|
+
None,
|
198
|
+
|
199
|
+
None,
|
200
|
+
|
201
|
+
None,
|
202
|
+
|
203
|
+
creation_flags,
|
204
|
+
|
205
|
+
None,
|
206
|
+
|
207
|
+
None,
|
208
|
+
|
209
|
+
byref(startupinfo),
|
210
|
+
|
211
|
+
byref(process_information)):
|
212
|
+
|
213
|
+
print "[*] We have successfully lauched the process!"
|
214
|
+
|
215
|
+
print "[*] PID: %d" % process_information.dwProcessId
|
216
|
+
|
217
|
+
self.h_process = self.open_process(process_information.dwProcessId)
|
218
|
+
|
219
|
+
|
220
|
+
|
221
|
+
else:
|
222
|
+
|
223
|
+
print "Error: 0x%08x." % kernel32.GetLastError()
|
224
|
+
|
225
|
+
|
226
|
+
|
227
|
+
def open_process(self,pid):
|
228
|
+
|
229
|
+
|
230
|
+
|
231
|
+
h_process = kernel32.OpenProcess(PROCESS_ALL_ACCESS,False,pid)
|
232
|
+
|
233
|
+
return h_process
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
def attach(self,pid):
|
238
|
+
|
239
|
+
|
240
|
+
|
241
|
+
self.h_process = self.open_process(pid)
|
242
|
+
|
243
|
+
|
244
|
+
|
245
|
+
if kernel32.DebugActiveProcess(pid):
|
246
|
+
|
247
|
+
self.debugger_active = True
|
248
|
+
|
249
|
+
self.pid = int(pid)
|
250
|
+
|
251
|
+
else:
|
252
|
+
|
253
|
+
print "[*] Unable to attach to the process."
|
254
|
+
|
255
|
+
|
256
|
+
|
257
|
+
def run(self):
|
258
|
+
|
259
|
+
while self.debugger_active == True:
|
260
|
+
|
261
|
+
self.get_debug_event()
|
262
|
+
|
263
|
+
|
264
|
+
|
265
|
+
def get_debug_event(self):
|
266
|
+
|
267
|
+
|
268
|
+
|
269
|
+
debug_event = DEBUG_EVENT()
|
270
|
+
|
271
|
+
continue_status = DBG_CONTINUE
|
272
|
+
|
273
|
+
|
274
|
+
|
275
|
+
if kernel32.WaitForDebugEvent(byref(debug_event),INFINITE):
|
276
|
+
|
277
|
+
|
278
|
+
|
279
|
+
raw_input("Press a key to continue...")
|
280
|
+
|
281
|
+
self.debugger_active = False
|
282
|
+
|
283
|
+
kernel32.ContinueDebugEvent(
|
284
|
+
|
285
|
+
debug_event.dwProcessId,
|
286
|
+
|
287
|
+
debug_event.dwThreadId,
|
288
|
+
|
289
|
+
continue_status )
|
290
|
+
|
291
|
+
|
292
|
+
|
293
|
+
def datach(self):
|
294
|
+
|
295
|
+
|
296
|
+
|
297
|
+
if kernel32.DebugActiveProcessStop(self.pid):
|
298
|
+
|
299
|
+
print "[*] Finished debugging. Exiting..."
|
300
|
+
|
301
|
+
return True
|
302
|
+
|
303
|
+
else:
|
304
|
+
|
305
|
+
print "There was an error"
|
306
|
+
|
307
|
+
return False
|
308
|
+
|
309
|
+
```
|
310
|
+
|
311
|
+
|
312
|
+
|
313
|
+
##実行ファイル
|
314
|
+
|
315
|
+
```ここに言語を入力
|
316
|
+
|
317
|
+
import my_debuggera
|
318
|
+
|
319
|
+
|
320
|
+
|
321
|
+
debugger = my_debuggera.debugger()
|
322
|
+
|
323
|
+
|
324
|
+
|
325
|
+
pid = raw_input("Enter the PID of the process to attach to:")
|
326
|
+
|
327
|
+
|
328
|
+
|
329
|
+
debugger.attach(int(pid))
|
330
|
+
|
331
|
+
debugger.run()
|
332
|
+
|
333
|
+
debugger.datach()
|
334
|
+
|
335
|
+
```
|
336
|
+
|
337
|
+
|
338
|
+
|
339
|
+
###再定義
|
340
|
+
|
341
|
+
PROCESS_ALL_ACCESS
|
342
|
+
|
343
|
+
|
344
|
+
|
345
|
+
###補足情報
|
346
|
+
|
347
|
+
Python2.7
|
348
|
+
|
349
|
+
Windows7
|
2
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,349 +1,7 @@
|
|
1
|
-
|
1
|
+
解決致しました。
|
2
2
|
|
3
|
+
皆さまご協力ありがとうございました。
|
3
4
|
|
5
|
+
大変申し訳ございませんが、諸事情により本質問内容を削除致します。
|
4
6
|
|
5
|
-
Windows APIを利用し,デバッガを作成しています.
|
6
|
-
|
7
|
-
言語はPython(2.7)です.
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
現在,手動で電卓を開き,プロセスIDを取得し,
|
12
|
-
|
13
|
-
電卓のプロセスを停止するプログラムを作成しています.
|
14
|
-
|
15
|
-
プログラムのコンパイルは無事に通るのですが,
|
16
|
-
|
17
|
-
アタッチに失敗します.
|
18
|
-
|
19
|
-
なぜなのかと解決策を質問したいです.
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
また以前,
|
24
|
-
|
25
|
-
PROCESS_ALL_ACCESSが定義されていないと
|
26
|
-
|
27
|
-
コンパイルが通らなかったことがあります.
|
28
|
-
|
29
|
-
|
7
|
+
本当にありがとうございました。
|
30
|
-
|
31
|
-
問題がないかもお願いいたします.
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
###実行例
|
36
|
-
|
37
|
-
実行例(PID:1620):
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
Enter the PID of the process to attach to:1620
|
42
|
-
|
43
|
-
[*] Unable to attach to the process.
|
44
|
-
|
45
|
-
There was an error
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
##my_debugger_defines
|
52
|
-
|
53
|
-
```ここに言語を入力
|
54
|
-
|
55
|
-
from ctypes import *
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
WORD = c_ushort
|
60
|
-
|
61
|
-
DWORD = c_ulong
|
62
|
-
|
63
|
-
LPBYTE = POINTER(c_ubyte)
|
64
|
-
|
65
|
-
LPTSTR = POINTER(c_char)
|
66
|
-
|
67
|
-
HANDLE = c_void_p
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
DEBUG_PROCESS = 0x00000001
|
72
|
-
|
73
|
-
CREATE_NEW_CONSOLE = 0x00000010
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
class STARTUPINFO(Structure):
|
78
|
-
|
79
|
-
_fields_ = [
|
80
|
-
|
81
|
-
("cb", DWORD),
|
82
|
-
|
83
|
-
("lpReserved", LPTSTR),
|
84
|
-
|
85
|
-
("lpDesktop", LPTSTR),
|
86
|
-
|
87
|
-
("lpTitle", LPTSTR),
|
88
|
-
|
89
|
-
("dwX", DWORD),
|
90
|
-
|
91
|
-
("dwY", DWORD),
|
92
|
-
|
93
|
-
("dwXSize", DWORD),
|
94
|
-
|
95
|
-
("dwYSize", DWORD),
|
96
|
-
|
97
|
-
("dwXCountChars", DWORD),
|
98
|
-
|
99
|
-
("dwYCountChars", DWORD),
|
100
|
-
|
101
|
-
("dwFillAttribute", DWORD),
|
102
|
-
|
103
|
-
("dwFlags", DWORD),
|
104
|
-
|
105
|
-
("wShowWindow", WORD),
|
106
|
-
|
107
|
-
("cbReserved2", WORD),
|
108
|
-
|
109
|
-
("lpReserved2", LPBYTE),
|
110
|
-
|
111
|
-
("hStdInput", HANDLE),
|
112
|
-
|
113
|
-
("hStdOutput", HANDLE),
|
114
|
-
|
115
|
-
("hStdError", HANDLE),
|
116
|
-
|
117
|
-
]
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
class PROCESS_INFORMATION(Structure):
|
122
|
-
|
123
|
-
_fields_ = [
|
124
|
-
|
125
|
-
("hProcess", HANDLE),
|
126
|
-
|
127
|
-
("hThread", HANDLE),
|
128
|
-
|
129
|
-
("dwProcessId", DWORD),
|
130
|
-
|
131
|
-
("dwThreadId", DWORD),
|
132
|
-
|
133
|
-
]
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
```
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
##my_debuggera
|
142
|
-
|
143
|
-
```ここに言語を入力
|
144
|
-
|
145
|
-
from ctypes import *
|
146
|
-
|
147
|
-
from my_debugger_defines import *
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
kernel32 = windll.kernel32
|
152
|
-
|
153
|
-
#PROCESS_ALL_ACCESS = 2035711
|
154
|
-
|
155
|
-
PROCESS_ALL_ACCESS = (0x000F0000L | 0x00100000L | 0xFFF)
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
class debugger():
|
160
|
-
|
161
|
-
def __init__(self):
|
162
|
-
|
163
|
-
self.h_process = None
|
164
|
-
|
165
|
-
self.pid = None
|
166
|
-
|
167
|
-
self.debugger_active = False
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
def load(self,path_to_exe):
|
172
|
-
|
173
|
-
creation_flags = DEBUG_PROCESS
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
startupinfo = STARTUPINFO()
|
178
|
-
|
179
|
-
process_information = PROCESS_INFORMATION()
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
startupinfo.dwFlags = 0x1
|
184
|
-
|
185
|
-
startupinfo.wShowWindow = 0x0
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
startupinfo.cb = sizeof(startupinfo)
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
if kernel32.CreateProcessA(path_to_exe,
|
194
|
-
|
195
|
-
None,
|
196
|
-
|
197
|
-
None,
|
198
|
-
|
199
|
-
None,
|
200
|
-
|
201
|
-
None,
|
202
|
-
|
203
|
-
creation_flags,
|
204
|
-
|
205
|
-
None,
|
206
|
-
|
207
|
-
None,
|
208
|
-
|
209
|
-
byref(startupinfo),
|
210
|
-
|
211
|
-
byref(process_information)):
|
212
|
-
|
213
|
-
print "[*] We have successfully lauched the process!"
|
214
|
-
|
215
|
-
print "[*] PID: %d" % process_information.dwProcessId
|
216
|
-
|
217
|
-
self.h_process = self.open_process(process_information.dwProcessId)
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
else:
|
222
|
-
|
223
|
-
print "Error: 0x%08x." % kernel32.GetLastError()
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
def open_process(self,pid):
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
h_process = kernel32.OpenProcess(PROCESS_ALL_ACCESS,False,pid)
|
232
|
-
|
233
|
-
return h_process
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
def attach(self,pid):
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
self.h_process = self.open_process(pid)
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
if kernel32.DebugActiveProcess(pid):
|
246
|
-
|
247
|
-
self.debugger_active = True
|
248
|
-
|
249
|
-
self.pid = int(pid)
|
250
|
-
|
251
|
-
else:
|
252
|
-
|
253
|
-
print "[*] Unable to attach to the process."
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
def run(self):
|
258
|
-
|
259
|
-
while self.debugger_active == True:
|
260
|
-
|
261
|
-
self.get_debug_event()
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
def get_debug_event(self):
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
debug_event = DEBUG_EVENT()
|
270
|
-
|
271
|
-
continue_status = DBG_CONTINUE
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
if kernel32.WaitForDebugEvent(byref(debug_event),INFINITE):
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
raw_input("Press a key to continue...")
|
280
|
-
|
281
|
-
self.debugger_active = False
|
282
|
-
|
283
|
-
kernel32.ContinueDebugEvent(
|
284
|
-
|
285
|
-
debug_event.dwProcessId,
|
286
|
-
|
287
|
-
debug_event.dwThreadId,
|
288
|
-
|
289
|
-
continue_status )
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
def datach(self):
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
if kernel32.DebugActiveProcessStop(self.pid):
|
298
|
-
|
299
|
-
print "[*] Finished debugging. Exiting..."
|
300
|
-
|
301
|
-
return True
|
302
|
-
|
303
|
-
else:
|
304
|
-
|
305
|
-
print "There was an error"
|
306
|
-
|
307
|
-
return False
|
308
|
-
|
309
|
-
```
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
##実行ファイル
|
314
|
-
|
315
|
-
```ここに言語を入力
|
316
|
-
|
317
|
-
import my_debuggera
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
debugger = my_debuggera.debugger()
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
pid = raw_input("Enter the PID of the process to attach to:")
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
debugger.attach(int(pid))
|
330
|
-
|
331
|
-
debugger.run()
|
332
|
-
|
333
|
-
debugger.datach()
|
334
|
-
|
335
|
-
```
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
###再定義
|
340
|
-
|
341
|
-
PROCESS_ALL_ACCESS
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
###補足情報
|
346
|
-
|
347
|
-
Python2.7
|
348
|
-
|
349
|
-
Windows7
|
1
test
CHANGED
File without changes
|
test
CHANGED
File without changes
|