前提・実現したいこと
python3でsocketのsendの引数に、bytes型を指定したいのですが、変換に失敗します。
発生している問題・エラーメッセージ
'utf-8' codec can't decode byte 0x83 in position 1: invalid start byte
該当のソースコード
エラーが起きているソースコードを追記させていただきます。
以下のリンクのコードを参考にしています。
https://github.com/AllGloryToTheHypnotoad/Black-Hat-Python/blob/master/BHP-Code/Chapter2/bhnet.py
一つのファイルの実行時に指定されたオプションで、サーバ側とクライアント側の動作が変わる様になっています。
サーバとして動作させる場合は、python ./bhnet.py -l -p 9999 -c とオプションを指定し、
そのサーバに接続する際は、python ./bhnet.py -t localhost -p 9999 とオプションを指定して起動しています。
・encoding='utf-8'を追加し、再度更新しました
python
1# coding:utf-8 2 3# filename : bhnet.py 4 5import sys 6import socket 7import getopt 8import threading 9import subprocess 10 11import traceback 12 13# global 14listen = False 15command = False 16upload = "" 17execute = "" 18target = "" 19upload_destination = "" 20port = 0 21 22 23# コマンドを実行し出力を返す 24def run_command(command): 25 command = command.rstrip() 26 27 try: 28 output = subprocess.check_output( 29 command, stderr=subprocess.STDOUT, shell=True) 30 output = output.decode(encoding="shift-jis") 31 # output = output.decode(encoding="utf-8") # 'utf-8' codec can't decode byte 0x83 in position 1: invalid start byte 32 except Exception as e: 33 output = str(e) 34 output += "\r\n" 35 output += "Failed to execute command.\r\n" 36 37 return output 38 39 40# クライアントが送ったデータを処理する 41def client_handler(client_socket): 42 global upload 43 global execute 44 global command 45 46 # uploadが指定されたときの動作 47 if len(upload_destination): 48 file_buffer = "" 49 50 while True: 51 data = client_socket.recv(1024).decode(encoding='utf-8') 52 53 if not data: 54 break 55 else: 56 file_buffer += data 57 58 try: 59 file_descriptor = open(upload_destination, "wb") 60 file_descriptor.write(file_buffer) 61 file_descriptor.close() 62 63 client_socket.send( 64 b"Successfully saved file to %s\r\n" % upload_destination) 65 except Exception: 66 client_socket.send( 67 b"Failed saved file to %s\r\n" % upload_destination) 68 69 # コマンドの実行(execute)が指定されたときの動作 70 if len(execute): 71 output = run_command(execute) 72 client_socket.send(output.encode(encoding='utf-8')) 73 74 # commandが指定された場合はシェルとして動作する 75 if command: 76 while True: 77 propmt = "<BHP:#> " 78 client_socket.send(propmt.encode(encoding='utf-8')) 79 80 cmd_buffer = "" 81 while "\n" not in cmd_buffer: 82 cmd_buffer += client_socket.recv(1024).decode(encoding='utf-8') 83 response = run_command(cmd_buffer) 84 85 client_socket.send(response.encode(encoding='utf-8')) 86 87 88# 受信用の関数 89def server_loop(): 90 global target 91 global port 92 93 # ターゲットの指定がなければすべてのアクセスを受け付ける 94 if not len(target): 95 target = "0.0.0.0" 96 97 server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 98 server.bind((target, port)) 99 100 server.listen(5) 101 102 while True: 103 client_socket, addr = server.accept() 104 105 # クライアントの処理を行うスレッドの作成 106 client_thread = threading.Thread( 107 target=client_handler, args=(client_socket,)) 108 client_thread.start() 109 110 111# クライアントとしてデータを送信する関数 112def client_sender(buffer): 113 114 client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 115 116 try: 117 client.connect((target, port)) 118 print("connected!") 119 120 if len(buffer): 121 client.send(buffer.encode(encoding='utf-8')) 122 123 while True: 124 recv_len = 1 125 response = "" 126 127 while recv_len: 128 data = client.recv(4096).decode(encoding='utf-8') 129 recv_len = len(data) 130 response += data 131 132 if recv_len < 4096: 133 break 134 135 print(response) 136 137 buffer = input("") 138 buffer += "\n" 139 140 client.send(buffer.encode(encoding='utf-8')) 141 except Exception as e: 142 print(e) 143 print(traceback.format_exc()) 144 print("[*] Exception! Exiting.") 145 client.close() 146 147 148def main(): 149 global listen 150 global port 151 global execute 152 global command 153 global upload_destination 154 global target 155 156 if not len(sys.argv[1:]): 157 sys.exit(0) 158 159 try: 160 opts, args = getopt.getopt( 161 sys.argv[1:], 162 "hle:t:p:cu", 163 ["help", "listen", "execute=", "target=", 164 "port=", "command", "upload="] 165 ) 166 except getopt.GetoptError as err: 167 print(str(err)) 168 for o, a in opts: 169 if o in ("-l", "--listen"): 170 listen = True 171 elif o in ("-e", "--execute"): 172 execute = a 173 elif o in ("-c", "--commandshell"): 174 command = True 175 elif o in ("-u", "--upload"): 176 upload_destination = a 177 elif o in ("-t", "--target"): 178 target = a 179 elif o in ("-p", "--port"): 180 port = int(a) 181 else: 182 assert False, "Unhandled Option" 183 184 # クライアントとして動作 185 if not listen and len(target) and port > 0: 186 buffer = sys.stdin.read() 187 client_sender(buffer) 188 189 # サーバとして動作 190 if listen: 191 print("...") 192 server_loop() 193 194 195main()
試したこと
プログラムのコードすべてとTraceback全文を追記しました。
長くなってしまいますが、よろしくお願いいたします。
再度更新いたしました
重ねてお願いとなってしまいますが、よろしくお願いいたします。
補足情報(FW/ツールのバージョンなど)
Python 3.8.1
回答2件
あなたの回答
tips
プレビュー