質問編集履歴

4

教えていただいた箇所の修正後、一つだけエラーが出たので更新

2020/08/01 04:26

投稿

t4gforce
t4gforce

スコア5

test CHANGED
File without changes
test CHANGED
@@ -10,460 +10,436 @@
10
10
 
11
11
 
12
12
 
13
- エラーメッセージを全文記載いたします。
14
-
15
- サーバ側のTracebackは
16
-
17
13
  ```
18
14
 
19
- Exception in thread Thread-1:
20
-
21
- Traceback (most recent call last):
22
-
23
- File "C:\Users\--\AppData\Local\Programs\Python\Python38-32\lib\threading.py", line 932, in _bootstrap_inner
24
-
25
- self.run()
26
-
27
- File "C:\Users\--\AppData\Local\Programs\Python\Python38-32\lib\threading.py", line 870, in run
28
-
29
- self._target(*self._args, **self._kwargs)
30
-
31
- File "./bhnet.py", line 78, in client_handler
15
+ 'utf-8' codec can't decode byte 0x83 in position 1: invalid start byte
32
-
33
- cmd_buffer += client_socket.recv(1024).decode() # サーバ側例外発生箇所
34
-
35
- TypeError: can't concat str to bytes
36
16
 
37
17
  ```
38
18
 
39
19
 
40
20
 
21
+ ### 該当のソースコード
22
+
23
+
24
+
25
+
26
+
27
+ エラーが起きているソースコードを追記させていただきます。
28
+
29
+ 以下のリンクのコードを参考にしています。
30
+
31
+ https://github.com/AllGloryToTheHypnotoad/Black-Hat-Python/blob/master/BHP-Code/Chapter2/bhnet.py
32
+
33
+ 一つのファイルの実行時に指定されたオプションで、サーバ側とクライアント側の動作が変わる様になっています。
34
+
35
+ サーバとして動作させる場合は、python ./bhnet.py -l -p 9999 -c とオプションを指定し、
36
+
37
+ そのサーバに接続する際は、python ./bhnet.py -t localhost -p 9999 とオプションを指定して起動しています。
38
+
39
+
40
+
41
+
42
+
43
+ ・encoding='utf-8'を追加し、再度更新しました
44
+
45
+
46
+
47
+
48
+
49
+ ```python
50
+
51
+ # coding:utf-8
52
+
53
+
54
+
55
+ # filename : bhnet.py
56
+
57
+
58
+
59
+ import sys
60
+
61
+ import socket
62
+
63
+ import getopt
64
+
65
+ import threading
66
+
67
+ import subprocess
68
+
69
+
70
+
41
- クライアント側のTraceback
71
+ import traceback
72
+
73
+
74
+
75
+ # global
76
+
77
+ listen = False
78
+
79
+ command = False
80
+
81
+ upload = ""
82
+
83
+ execute = ""
84
+
85
+ target = ""
86
+
87
+ upload_destination = ""
88
+
89
+ port = 0
90
+
91
+
92
+
93
+
94
+
95
+ # コマンドを実行し出力を返す
96
+
97
+ def run_command(command):
98
+
99
+ command = command.rstrip()
100
+
101
+
102
+
103
+ try:
104
+
105
+ output = subprocess.check_output(
106
+
107
+ command, stderr=subprocess.STDOUT, shell=True)
108
+
109
+ output = output.decode(encoding="shift-jis")
110
+
111
+ # output = output.decode(encoding="utf-8") # 'utf-8' codec can't decode byte 0x83 in position 1: invalid start byte
112
+
113
+ except Exception as e:
114
+
115
+ output = str(e)
116
+
117
+ output += "\r\n"
118
+
119
+ output += "Failed to execute command.\r\n"
120
+
121
+
122
+
123
+ return output
124
+
125
+
126
+
127
+
128
+
129
+ # クライアントが送ったデータを処理する
130
+
131
+ def client_handler(client_socket):
132
+
133
+ global upload
134
+
135
+ global execute
136
+
137
+ global command
138
+
139
+
140
+
141
+ # uploadが指定されたときの動作
142
+
143
+ if len(upload_destination):
144
+
145
+ file_buffer = ""
146
+
147
+
148
+
149
+ while True:
150
+
151
+ data = client_socket.recv(1024).decode(encoding='utf-8')
152
+
153
+
154
+
155
+ if not data:
156
+
157
+ break
158
+
159
+ else:
160
+
161
+ file_buffer += data
162
+
163
+
164
+
165
+ try:
166
+
167
+ file_descriptor = open(upload_destination, "wb")
168
+
169
+ file_descriptor.write(file_buffer)
170
+
171
+ file_descriptor.close()
172
+
173
+
174
+
175
+ client_socket.send(
176
+
177
+ b"Successfully saved file to %s\r\n" % upload_destination)
178
+
179
+ except Exception:
180
+
181
+ client_socket.send(
182
+
183
+ b"Failed saved file to %s\r\n" % upload_destination)
184
+
185
+
186
+
187
+ # コマンドの実行(execute)が指定されたときの動作
188
+
189
+ if len(execute):
190
+
191
+ output = run_command(execute)
192
+
193
+ client_socket.send(output.encode(encoding='utf-8'))
194
+
195
+
196
+
197
+ # commandが指定された場合はシェルとして動作する
198
+
199
+ if command:
200
+
201
+ while True:
202
+
203
+ propmt = "<BHP:#> "
204
+
205
+ client_socket.send(propmt.encode(encoding='utf-8'))
206
+
207
+
208
+
209
+ cmd_buffer = ""
210
+
211
+ while "\n" not in cmd_buffer:
212
+
213
+ cmd_buffer += client_socket.recv(1024).decode(encoding='utf-8')
214
+
215
+ response = run_command(cmd_buffer)
216
+
217
+
218
+
219
+ client_socket.send(response.encode(encoding='utf-8'))
220
+
221
+
222
+
223
+
224
+
225
+ # 受信用の関数
226
+
227
+ def server_loop():
228
+
229
+ global target
230
+
231
+ global port
232
+
233
+
234
+
235
+ # ターゲットの指定がなければすべてのアクセスを受け付ける
236
+
237
+ if not len(target):
238
+
239
+ target = "0.0.0.0"
240
+
241
+
242
+
243
+ server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
244
+
245
+ server.bind((target, port))
246
+
247
+
248
+
249
+ server.listen(5)
250
+
251
+
252
+
253
+ while True:
254
+
255
+ client_socket, addr = server.accept()
256
+
257
+
258
+
259
+ # クライアントの処理を行うスレッドの作成
260
+
261
+ client_thread = threading.Thread(
262
+
263
+ target=client_handler, args=(client_socket,))
264
+
265
+ client_thread.start()
266
+
267
+
268
+
269
+
270
+
271
+ # クライアントとしてデータを送信する関数
272
+
273
+ def client_sender(buffer):
274
+
275
+
276
+
277
+ client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
278
+
279
+
280
+
281
+ try:
282
+
283
+ client.connect((target, port))
284
+
285
+ print("connected!")
286
+
287
+
288
+
289
+ if len(buffer):
290
+
291
+ client.send(buffer.encode(encoding='utf-8'))
292
+
293
+
294
+
295
+ while True:
296
+
297
+ recv_len = 1
298
+
299
+ response = ""
300
+
301
+
302
+
303
+ while recv_len:
304
+
305
+ data = client.recv(4096).decode(encoding='utf-8')
306
+
307
+ recv_len = len(data)
308
+
309
+ response += data
310
+
311
+
312
+
313
+ if recv_len < 4096:
314
+
315
+ break
316
+
317
+
318
+
319
+ print(response)
320
+
321
+
322
+
323
+ buffer = input("")
324
+
325
+ buffer += "\n"
326
+
327
+
328
+
329
+ client.send(buffer.encode(encoding='utf-8'))
330
+
331
+ except Exception as e:
332
+
333
+ print(e)
334
+
335
+ print(traceback.format_exc())
336
+
337
+ print("[*] Exception! Exiting.")
338
+
339
+ client.close()
340
+
341
+
342
+
343
+
344
+
345
+ def main():
346
+
347
+ global listen
348
+
349
+ global port
350
+
351
+ global execute
352
+
353
+ global command
354
+
355
+ global upload_destination
356
+
357
+ global target
358
+
359
+
360
+
361
+ if not len(sys.argv[1:]):
362
+
363
+ sys.exit(0)
364
+
365
+
366
+
367
+ try:
368
+
369
+ opts, args = getopt.getopt(
370
+
371
+ sys.argv[1:],
372
+
373
+ "hle:t:p:cu",
374
+
375
+ ["help", "listen", "execute=", "target=",
376
+
377
+ "port=", "command", "upload="]
378
+
379
+ )
380
+
381
+ except getopt.GetoptError as err:
382
+
383
+ print(str(err))
384
+
385
+ for o, a in opts:
386
+
387
+ if o in ("-l", "--listen"):
388
+
389
+ listen = True
390
+
391
+ elif o in ("-e", "--execute"):
392
+
393
+ execute = a
394
+
395
+ elif o in ("-c", "--commandshell"):
396
+
397
+ command = True
398
+
399
+ elif o in ("-u", "--upload"):
400
+
401
+ upload_destination = a
402
+
403
+ elif o in ("-t", "--target"):
404
+
405
+ target = a
406
+
407
+ elif o in ("-p", "--port"):
408
+
409
+ port = int(a)
410
+
411
+ else:
412
+
413
+ assert False, "Unhandled Option"
414
+
415
+
416
+
417
+ # クライアントとして動作
418
+
419
+ if not listen and len(target) and port > 0:
420
+
421
+ buffer = sys.stdin.read()
422
+
423
+ client_sender(buffer)
424
+
425
+
426
+
427
+ # サーバとして動作
428
+
429
+ if listen:
430
+
431
+ print("...")
432
+
433
+ server_loop()
434
+
435
+
436
+
437
+
438
+
439
+ main()
42
440
 
43
441
  ```
44
442
 
45
- Traceback (most recent call last):
46
-
47
- File "./bhnet.py", line 122, in client_sender
48
-
49
- data = client.recv(4096).decode()
50
-
51
- UnicodeDecodeError: 'utf-8' codec can't decode byte 0x83 in position 1: invalid start byte
52
-
53
- ```
54
-
55
-
56
-
57
-
58
-
59
-
60
-
61
- ### 該当のソースコード
62
-
63
-
64
-
65
-
66
-
67
- エラーが起きているソースコードを追記させていただきます。
68
-
69
- 以下のリンクのコードを参考にしています。
70
-
71
- https://github.com/AllGloryToTheHypnotoad/Black-Hat-Python/blob/master/BHP-Code/Chapter2/bhnet.py
72
-
73
- 一つのファイルの実行時に指定されたオプションで、サーバ側とクライアント側の動作が変わる様になっています。
74
-
75
- サーバとして動作させる場合は、python ./bhnet.py -l -p 9999 -c とオプションを指定し、
76
-
77
- そのサーバに接続する際は、python ./bhnet.py -t localhost -p 9999 とオプションを指定して起動しています。
78
-
79
- ```python
80
-
81
- # coding:utf-8
82
-
83
-
84
-
85
- # filename : bhnet.py
86
-
87
-
88
-
89
- import sys
90
-
91
- import socket
92
-
93
- import getopt
94
-
95
- import threading
96
-
97
- import subprocess
98
-
99
-
100
-
101
- import traceback
102
-
103
-
104
-
105
- # global
106
-
107
- listen = False
108
-
109
- command = False
110
-
111
- upload = ""
112
-
113
- execute = ""
114
-
115
- target = ""
116
-
117
- upload_destination = ""
118
-
119
- port = 0
120
-
121
-
122
-
123
-
124
-
125
- # コマンドを実行し出力を返す
126
-
127
- def run_command(command):
128
-
129
- command = command.rstrip()
130
-
131
-
132
-
133
- try:
134
-
135
- output = subprocess.check_output(
136
-
137
- command, stderr=subprocess.STDOUT, shell=True)
138
-
139
- except Exception:
140
-
141
- output = "Failed to execute command.\r\n"
142
-
143
-
144
-
145
- return output
146
-
147
-
148
-
149
-
150
-
151
- # クライアントが送ったデータを処理する
152
-
153
- def client_handler(client_socket):
154
-
155
- global upload
156
-
157
- global execute
158
-
159
- global command
160
-
161
-
162
-
163
- # uploadが指定されたときの動作
164
-
165
- if len(upload_destination):
166
-
167
- file_buffer = ""
168
-
169
-
170
-
171
- while True:
172
-
173
- data = client_socket.recv(1024).decode()
174
-
175
-
176
-
177
- if not data:
178
-
179
- break
180
-
181
- else:
182
-
183
- file_buffer += data
184
-
185
-
186
-
187
- try:
188
-
189
- file_descriptor = open(upload_destination, "wb")
190
-
191
- file_descriptor.write(file_buffer)
192
-
193
- file_descriptor.close()
194
-
195
-
196
-
197
- client_socket.send(
198
-
199
- b"Successfully saved file to %s\r\n" % upload_destination)
200
-
201
- except Exception:
202
-
203
- client_socket.send(
204
-
205
- b"Failed saved file to %s\r\n" % upload_destination)
206
-
207
-
208
-
209
- # コマンドの実行(execute)が指定されたときの動作
210
-
211
- if len(execute):
212
-
213
- output = run_command(execute)
214
-
215
- client_socket.send(output)
216
-
217
-
218
-
219
- # commandが指定された場合はシェルとして動作する
220
-
221
- if command:
222
-
223
- while True:
224
-
225
- propmt = "<BHP:#> "
226
-
227
- client_socket.send(propmt.encode())
228
-
229
-
230
-
231
- cmd_buffer = b""
232
-
233
- while b"\n" not in cmd_buffer:
234
-
235
- cmd_buffer += client_socket.recv(1024).decode() # サーバ側例外発生箇所
236
-
237
- response = run_command(cmd_buffer)
238
-
239
-
240
-
241
- client_socket.send(response)
242
-
243
-
244
-
245
-
246
-
247
- # 受信用の関数
248
-
249
- def server_loop():
250
-
251
- global target
252
-
253
- global port
254
-
255
-
256
-
257
- # ターゲットの指定がなければすべてのアクセスを受け付ける
258
-
259
- if not len(target):
260
-
261
- target = "0.0.0.0"
262
-
263
-
264
-
265
- server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
266
-
267
- server.bind((target, port))
268
-
269
-
270
-
271
- server.listen(5)
272
-
273
-
274
-
275
- while True:
276
-
277
- client_socket, addr = server.accept()
278
-
279
-
280
-
281
- # クライアントの処理を行うスレッドの作成
282
-
283
- client_thread = threading.Thread(
284
-
285
- target=client_handler, args=(client_socket,))
286
-
287
- client_thread.start()
288
-
289
-
290
-
291
-
292
-
293
- # クライアントとしてデータを送信する関数
294
-
295
- def client_sender(buffer):
296
-
297
-
298
-
299
- client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
300
-
301
-
302
-
303
- try:
304
-
305
- client.connect((target, port))
306
-
307
- print("connected!")
308
-
309
-
310
-
311
- if len(buffer):
312
-
313
- client.send(buffer.encode())
314
-
315
-
316
-
317
- while True:
318
-
319
- recv_len = 1
320
-
321
- response = ""
322
-
323
-
324
-
325
- while recv_len:
326
-
327
- # クライアント側例外発生箇所
328
-
329
- data = client.recv(4096).decode()
330
-
331
- recv_len = len(data)
332
-
333
- response += data
334
-
335
-
336
-
337
- if recv_len < 4096:
338
-
339
- break
340
-
341
-
342
-
343
- print(response)
344
-
345
-
346
-
347
- buffer = input("")
348
-
349
- buffer += "\n"
350
-
351
-
352
-
353
- client.send(buffer.encode(encoding="utf-8"))
354
-
355
- except Exception as e:
356
-
357
- print(e)
358
-
359
- print(traceback.format_exc())
360
-
361
- print("[*] Exception! Exiting.")
362
-
363
- client.close()
364
-
365
-
366
-
367
-
368
-
369
- def main():
370
-
371
- global listen
372
-
373
- global port
374
-
375
- global execute
376
-
377
- global command
378
-
379
- global upload_destination
380
-
381
- global target
382
-
383
-
384
-
385
- if not len(sys.argv[1:]):
386
-
387
- sys.exit(0)
388
-
389
-
390
-
391
- try:
392
-
393
- opts, args = getopt.getopt(
394
-
395
- sys.argv[1:],
396
-
397
- "hle:t:p:cu",
398
-
399
- ["help", "listen", "execute=", "target=",
400
-
401
- "port=", "command", "upload="]
402
-
403
- )
404
-
405
- except getopt.GetoptError as err:
406
-
407
- print(str(err))
408
-
409
- for o, a in opts:
410
-
411
- if o in ("-l", "--listen"):
412
-
413
- listen = True
414
-
415
- elif o in ("-e", "--execute"):
416
-
417
- execute = a
418
-
419
- elif o in ("-c", "--commandshell"):
420
-
421
- command = True
422
-
423
- elif o in ("-u", "--upload"):
424
-
425
- upload_destination = a
426
-
427
- elif o in ("-t", "--target"):
428
-
429
- target = a
430
-
431
- elif o in ("-p", "--port"):
432
-
433
- port = int(a)
434
-
435
- else:
436
-
437
- assert False, "Unhandled Option"
438
-
439
-
440
-
441
- # クライアントとして動作
442
-
443
- if not listen and len(target) and port > 0:
444
-
445
- buffer = sys.stdin.read()
446
-
447
- client_sender(buffer)
448
-
449
-
450
-
451
- # サーバとして動作
452
-
453
- if listen:
454
-
455
- print("...")
456
-
457
- server_loop()
458
-
459
-
460
-
461
-
462
-
463
- main()
464
-
465
- ```
466
-
467
443
 
468
444
 
469
445
 
@@ -478,6 +454,12 @@
478
454
 
479
455
 
480
456
 
457
+ 再度更新いたしました
458
+
459
+ 重ねてお願いとなってしまいますが、よろしくお願いいたします。
460
+
461
+
462
+
481
463
  ### 補足情報(FW/ツールのバージョンなど)
482
464
 
483
465
 

3

プログラムのコードすべてを追記、Traceback全文を追記

2020/08/01 04:25

投稿

t4gforce
t4gforce

スコア5

test CHANGED
File without changes
test CHANGED
@@ -28,11 +28,11 @@
28
28
 
29
29
  self._target(*self._args, **self._kwargs)
30
30
 
31
- File "./bhnet.py", line 74, in client_handler
31
+ File "./bhnet.py", line 78, in client_handler
32
-
32
+
33
- cmd_buffer += client_socket.recv(1024).decode()
33
+ cmd_buffer += client_socket.recv(1024).decode() # サーバ側例外発生箇所
34
-
34
+
35
- ConnectionResetError: [WinError 10054] 既存の接続はリモート ホストに強制的に切断されました。
35
+ TypeError: can't concat str to bytes
36
36
 
37
37
  ```
38
38
 
@@ -228,9 +228,9 @@
228
228
 
229
229
 
230
230
 
231
- cmd_buffer = ""
231
+ cmd_buffer = b""
232
-
232
+
233
- while "\n" not in cmd_buffer:
233
+ while b"\n" not in cmd_buffer:
234
234
 
235
235
  cmd_buffer += client_socket.recv(1024).decode() # サーバ側例外発生箇所
236
236
 

2

プログラムのコードすべてを追記、Traceback全文を追記

2020/07/30 15:24

投稿

t4gforce
t4gforce

スコア5

test CHANGED
File without changes
test CHANGED
@@ -10,79 +10,471 @@
10
10
 
11
11
 
12
12
 
13
- 例外が発生します。
13
+ エラーメッセージを全文記載いたします。
14
+
15
+ サーバ側のTracebackは
14
16
 
15
17
  ```
16
18
 
19
+ Exception in thread Thread-1:
20
+
21
+ Traceback (most recent call last):
22
+
23
+ File "C:\Users\--\AppData\Local\Programs\Python\Python38-32\lib\threading.py", line 932, in _bootstrap_inner
24
+
25
+ self.run()
26
+
27
+ File "C:\Users\--\AppData\Local\Programs\Python\Python38-32\lib\threading.py", line 870, in run
28
+
29
+ self._target(*self._args, **self._kwargs)
30
+
17
- 'utf-8' codec can't decode byte 0x83 in position 1: invalid start byte
31
+ File "./bhnet.py", line 74, in client_handler
32
+
33
+ cmd_buffer += client_socket.recv(1024).decode()
34
+
35
+ ConnectionResetError: [WinError 10054] 既存の接続はリモート ホストに強制的に切断されました。
18
36
 
19
37
  ```
20
38
 
21
39
 
22
40
 
41
+ クライアント側のTracebackは
42
+
43
+ ```
44
+
45
+ Traceback (most recent call last):
46
+
47
+ File "./bhnet.py", line 122, in client_sender
48
+
49
+ data = client.recv(4096).decode()
50
+
51
+ UnicodeDecodeError: 'utf-8' codec can't decode byte 0x83 in position 1: invalid start byte
52
+
53
+ ```
54
+
55
+
56
+
57
+
58
+
59
+
60
+
23
61
  ### 該当のソースコード
24
62
 
25
63
 
26
64
 
65
+
66
+
67
+ エラーが起きているソースコードを追記させていただきます。
68
+
69
+ 以下のリンクのコードを参考にしています。
70
+
71
+ https://github.com/AllGloryToTheHypnotoad/Black-Hat-Python/blob/master/BHP-Code/Chapter2/bhnet.py
72
+
73
+ 一つのファイルの実行時に指定されたオプションで、サーバ側とクライアント側の動作が変わる様になっています。
74
+
75
+ サーバとして動作させる場合は、python ./bhnet.py -l -p 9999 -c とオプションを指定し、
76
+
77
+ そのサーバに接続する際は、python ./bhnet.py -t localhost -p 9999 とオプションを指定して起動しています。
78
+
27
79
  ```python
28
80
 
29
- client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
30
-
31
-
32
-
33
- try:
34
-
35
- client.connect((localhost, 9999)) # 別途、localhost:9999でserver.listen()をしています。
36
-
37
-
38
-
39
- buffer = input("")
40
-
41
- buffer += "\n"
42
-
43
-
44
-
45
- print(type(buffer)) # <class 'str'>
46
-
47
-
48
-
49
- client.send(buffer.encode(encoding="utf-8")) # 例外発生
50
-
51
-
52
-
53
- except Exception as e:
54
-
55
- print(e) # 'utf-8' codec can't decode byte 0x83 in position 1: invalid start byte
56
-
57
- client.close()
58
-
59
-
81
+ # coding:utf-8
82
+
83
+
84
+
85
+ # filename : bhnet.py
86
+
87
+
88
+
89
+ import sys
90
+
91
+ import socket
92
+
93
+ import getopt
94
+
95
+ import threading
96
+
97
+ import subprocess
98
+
99
+
100
+
101
+ import traceback
102
+
103
+
104
+
105
+ # global
106
+
107
+ listen = False
108
+
109
+ command = False
110
+
111
+ upload = ""
112
+
113
+ execute = ""
114
+
115
+ target = ""
116
+
117
+ upload_destination = ""
118
+
119
+ port = 0
120
+
121
+
122
+
123
+
124
+
125
+ # コマンドを実行し出力を返す
126
+
127
+ def run_command(command):
128
+
129
+ command = command.rstrip()
130
+
131
+
132
+
133
+ try:
134
+
135
+ output = subprocess.check_output(
136
+
137
+ command, stderr=subprocess.STDOUT, shell=True)
138
+
139
+ except Exception:
140
+
141
+ output = "Failed to execute command.\r\n"
142
+
143
+
144
+
145
+ return output
146
+
147
+
148
+
149
+
150
+
151
+ # クライアントが送ったデータを処理する
152
+
153
+ def client_handler(client_socket):
154
+
155
+ global upload
156
+
157
+ global execute
158
+
159
+ global command
160
+
161
+
162
+
163
+ # uploadが指定されたときの動作
164
+
165
+ if len(upload_destination):
166
+
167
+ file_buffer = ""
168
+
169
+
170
+
171
+ while True:
172
+
173
+ data = client_socket.recv(1024).decode()
174
+
175
+
176
+
177
+ if not data:
178
+
179
+ break
180
+
181
+ else:
182
+
183
+ file_buffer += data
184
+
185
+
186
+
187
+ try:
188
+
189
+ file_descriptor = open(upload_destination, "wb")
190
+
191
+ file_descriptor.write(file_buffer)
192
+
193
+ file_descriptor.close()
194
+
195
+
196
+
197
+ client_socket.send(
198
+
199
+ b"Successfully saved file to %s\r\n" % upload_destination)
200
+
201
+ except Exception:
202
+
203
+ client_socket.send(
204
+
205
+ b"Failed saved file to %s\r\n" % upload_destination)
206
+
207
+
208
+
209
+ # コマンドの実行(execute)が指定されたときの動作
210
+
211
+ if len(execute):
212
+
213
+ output = run_command(execute)
214
+
215
+ client_socket.send(output)
216
+
217
+
218
+
219
+ # commandが指定された場合はシェルとして動作する
220
+
221
+ if command:
222
+
223
+ while True:
224
+
225
+ propmt = "<BHP:#> "
226
+
227
+ client_socket.send(propmt.encode())
228
+
229
+
230
+
231
+ cmd_buffer = ""
232
+
233
+ while "\n" not in cmd_buffer:
234
+
235
+ cmd_buffer += client_socket.recv(1024).decode() # サーバ側例外発生箇所
236
+
237
+ response = run_command(cmd_buffer)
238
+
239
+
240
+
241
+ client_socket.send(response)
242
+
243
+
244
+
245
+
246
+
247
+ # 受信用の関数
248
+
249
+ def server_loop():
250
+
251
+ global target
252
+
253
+ global port
254
+
255
+
256
+
257
+ # ターゲットの指定がなければすべてのアクセスを受け付ける
258
+
259
+ if not len(target):
260
+
261
+ target = "0.0.0.0"
262
+
263
+
264
+
265
+ server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
266
+
267
+ server.bind((target, port))
268
+
269
+
270
+
271
+ server.listen(5)
272
+
273
+
274
+
275
+ while True:
276
+
277
+ client_socket, addr = server.accept()
278
+
279
+
280
+
281
+ # クライアントの処理を行うスレッドの作成
282
+
283
+ client_thread = threading.Thread(
284
+
285
+ target=client_handler, args=(client_socket,))
286
+
287
+ client_thread.start()
288
+
289
+
290
+
291
+
292
+
293
+ # クライアントとしてデータを送信する関数
294
+
295
+ def client_sender(buffer):
296
+
297
+
298
+
299
+ client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
300
+
301
+
302
+
303
+ try:
304
+
305
+ client.connect((target, port))
306
+
307
+ print("connected!")
308
+
309
+
310
+
311
+ if len(buffer):
312
+
313
+ client.send(buffer.encode())
314
+
315
+
316
+
317
+ while True:
318
+
319
+ recv_len = 1
320
+
321
+ response = ""
322
+
323
+
324
+
325
+ while recv_len:
326
+
327
+ # クライアント側例外発生箇所
328
+
329
+ data = client.recv(4096).decode()
330
+
331
+ recv_len = len(data)
332
+
333
+ response += data
334
+
335
+
336
+
337
+ if recv_len < 4096:
338
+
339
+ break
340
+
341
+
342
+
343
+ print(response)
344
+
345
+
346
+
347
+ buffer = input("")
348
+
349
+ buffer += "\n"
350
+
351
+
352
+
353
+ client.send(buffer.encode(encoding="utf-8"))
354
+
355
+ except Exception as e:
356
+
357
+ print(e)
358
+
359
+ print(traceback.format_exc())
360
+
361
+ print("[*] Exception! Exiting.")
362
+
363
+ client.close()
364
+
365
+
366
+
367
+
368
+
369
+ def main():
370
+
371
+ global listen
372
+
373
+ global port
374
+
375
+ global execute
376
+
377
+ global command
378
+
379
+ global upload_destination
380
+
381
+ global target
382
+
383
+
384
+
385
+ if not len(sys.argv[1:]):
386
+
387
+ sys.exit(0)
388
+
389
+
390
+
391
+ try:
392
+
393
+ opts, args = getopt.getopt(
394
+
395
+ sys.argv[1:],
396
+
397
+ "hle:t:p:cu",
398
+
399
+ ["help", "listen", "execute=", "target=",
400
+
401
+ "port=", "command", "upload="]
402
+
403
+ )
404
+
405
+ except getopt.GetoptError as err:
406
+
407
+ print(str(err))
408
+
409
+ for o, a in opts:
410
+
411
+ if o in ("-l", "--listen"):
412
+
413
+ listen = True
414
+
415
+ elif o in ("-e", "--execute"):
416
+
417
+ execute = a
418
+
419
+ elif o in ("-c", "--commandshell"):
420
+
421
+ command = True
422
+
423
+ elif o in ("-u", "--upload"):
424
+
425
+ upload_destination = a
426
+
427
+ elif o in ("-t", "--target"):
428
+
429
+ target = a
430
+
431
+ elif o in ("-p", "--port"):
432
+
433
+ port = int(a)
434
+
435
+ else:
436
+
437
+ assert False, "Unhandled Option"
438
+
439
+
440
+
441
+ # クライアントとして動作
442
+
443
+ if not listen and len(target) and port > 0:
444
+
445
+ buffer = sys.stdin.read()
446
+
447
+ client_sender(buffer)
448
+
449
+
450
+
451
+ # サーバとして動作
452
+
453
+ if listen:
454
+
455
+ print("...")
456
+
457
+ server_loop()
458
+
459
+
460
+
461
+
462
+
463
+ main()
60
464
 
61
465
  ```
62
466
 
63
467
 
64
468
 
469
+
470
+
65
471
  ### 試したこと
66
472
 
67
473
 
68
474
 
69
- 例外発生箇所のコードを書き換え、
70
-
71
- ```python
72
-
73
- client.send(bytes(buffer))
74
-
75
- ```
76
-
77
- を試しましたが、string argument without an encoding という例外が発生してしまいます。
78
-
79
-
80
-
81
-
82
-
83
- python3でsendする方法教えていだきたいです
475
+ プログラムのコードすべてとTraceback全文追記しました。
84
-
476
+
85
- よろしくお願いします。
477
+ 長くなってしまいますが、よろしくお願いいたします。
86
478
 
87
479
 
88
480
 

1

例外メッセージの追加

2020/07/30 15:06

投稿

t4gforce
t4gforce

スコア5

test CHANGED
File without changes
test CHANGED
@@ -52,7 +52,7 @@
52
52
 
53
53
  except Exception as e:
54
54
 
55
- print(e)
55
+ print(e) # 'utf-8' codec can't decode byte 0x83 in position 1: invalid start byte
56
56
 
57
57
  client.close()
58
58