質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Q&A

解決済

1回答

5090閲覧

対処方法:a bytes-like object is required, not 'NoneType'

YusukeKanai

総合スコア12

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

0グッド

1クリップ

投稿2017/05/10 04:31

###前提・実現したいこと
ボットネット間の通信をAESで暗号化したいのですが
a bytes-like object is required, not 'NoneType'
のエラーメッセージが出て困っています。

###発生している問題・エラーメッセージ

a bytes-like object is required, not 'NoneType'

###該当のソースコード

Python

1import struct 2 3from Crypto import Random 4from Crypto.Cipher import AES 5from Crypto.Hash import HMAC, SHA256 6from lib.padding import pad, unpad 7 8 9from dh import create_dh_key, calculate_dh_secret 10 11class StealthConn(object): 12 13 14 def __init__(self, conn, client=False, server=False, verbose=False): 15 self.conn = conn 16 self.cipher = None 17 self.client = client 18 self.server = server 19 self.verbose = verbose 20 self.initiate_session() 21 self.secret_key = None 22 self.block_size = 32 23 24 def initiate_session(self): 25 # Perform the initial connection handshake for agreeing on a shared secret 26 ### TODO: Your code here! 27 # This can be broken into code run just on the server or just on the client 28 if self.server or self.client: 29 my_public_key, my_private_key = create_dh_key() 30 # Send them our public key 31 self.send(bytes(str(my_public_key), "ascii")) 32 # Receive their public key 33 their_public_key = int(self.recv()) 34 # Obtain our shared secret 35 shared_hash = calculate_dh_secret(their_public_key, my_private_key) 36 print("Shared hash: {}".format(shared_hash)) 37 38 39 40 self.secret_key = bytes(str(shared_hash[:16]), "ascii") 41 iv = Random.new().read(AES.block_size) 42 self.cipher = AES.new(self.secret_key, AES.MODE_CBC, iv) 43 print('secret key : ', self.secret_key) 44 45 46 def send(self, data): 47 print('send') 48 49 if self.cipher is not None: 50 iv = Random.new().read(AES.block_size) 51 cipher = AES.new(self.secret_key, AES.MODE_CBC, iv) 52 53 #上のself.secret_keyがnoneになっているみたいです 54 pad_data = pad(data, self.block_size, style='pkcs7') 55 56 h = HMAC.new(bytes(str(self.secret_key), "ascii"), digestmod=SHA256) 57 h.update(pad_data) 58 hmac = bytes(h.hexdigest(), "ascii") 59 60 data_pad_hmac = pad_data + hmac 61 62 encrypted_data = self.cipher.encrypt(data_pad_hmac) 63 64 if self.verbose: 65 print("Original data: {}".format(data)) 66 print("Encrypted data: {}".format(repr(encrypted_data))) 67 print("Sending packet of length {}".format(len(encrypted_data))) 68 else: 69 encrypted_data = data 70 71 # Encode the data's length into an unsigned two byte int ('H') 72 pkt_len = struct.pack('H', len(encrypted_data)) 73 self.conn.sendall(pkt_len) 74 self.conn.sendall(encrypted_data) 75 76 def recv(self): 77 print('recv') 78 # Decode the data's length from an unsigned two byte int ('H') 79 pkt_len_packed = self.conn.recv(struct.calcsize('H')) 80 unpacked_contents = struct.unpack('H', pkt_len_packed) 81 pkt_len = unpacked_contents[0] 82 83 encrypted_data = self.conn.recv(pkt_len) 84 85 if self.cipher is not None: 86 iv = Random.new().read(AES.block_size) 87 cipher = AES.new(self.secret_key, AES.MODE_CBC, iv) 88 print('secret key : ', self.secret_key) 89 90 data = self.cipher.decrypt(encrypted_data) 91 92 hmac = data[len(encrypted_data)-64:] 93 pad_data = data[:64] 94 h = HMAC.new(bytes(str(self.secret_key), "ascii"), digestmod=SHA256) 95 h.update(pad_data) 96 97 verify = bytes(h.hexdigest(), "ascii") 98 print('verify : ', verify) 99 100 101 #data = unpad(pad_data, self.block_size, style='pkcs7') 102 103 if hmac == verify: 104 if self.verbose: 105 print("Receiving packet of length {}".format(pkt_len)) 106 print("Encrypted data: {}".format(repr(encrypted_data))) 107 print("Original data: {}".format(data)) 108 else: 109 print("Message is modified") 110 111 else: 112 data = encrypted_data 113 114 115 116 return data 117 118 def close(self): 119 self.conn.close() 120

###試したこと
initiate_session()の中でsecret_keyを指定しているのですが
recv()とsend()の中ではsecret_key = Noneになっているようです。

###補足情報(言語/FW/ツール等のバージョンなど)
より詳細な情報

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

ここ逆じゃないですか?

self.initiate_session() self.secret_key = None

投稿2017/05/10 05:21

YouheiSakurai

総合スコア6142

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問