一. 驗證客戶端合法性 hmac模塊算法
Python內置的hmac模塊實現了標準的Hmac算法,安全
它利用一個key對message計算「雜湊」後的hash,服務器
使用hmac算法比標準hash算法更安全,由於針對相同的message,不一樣的key會產生不一樣的hash。dom
import os aa=os.urandom(32) print(type(aa),aa) print("*******************************************************888") import hmac message = b'Hello world' key = b'secret' h = hmac.new(key,message,digestmod="md5") # 第一個參數是密鑰key,第二個參數是待加密的字符串,第三個參數是hash函數 print(h.hexdigest()) print("*******************************************************888") 檢測 一下客戶端是否合法 不依靠登錄認證 import hmac h=hmac.new() # secret_key 你想進行加密的bytes 密文=h.digest() # 密文 # 返回摘要,做爲二進制數據字符串值 hmac.compare_digest() # 對比 密文 另一密文
1.驗證客戶端合法性socket
server import os import hmac import socket secret_key=b'lover' sk=socket.socket() sk.bind(('127.0.0.1',8600)) sk.listen() def chek(conn): msg=os.urandom(32) conn.send(msg) h=hmac.new(secret_key,msg) dige=h.digest() client_dige=conn.recv(1024) return hmac.compare_digest(dige,client_dige) conn,addr=sk.accept() res=chek(conn) if res: print("合法的客服端") conn.close() else: print("不合法的客服端") conn.close()
client import hmac import socket secret_key=b'lover' sk=socket.socket() sk.connect(('127.0.0.1',8600))
msg=sk.recv(1024) h=hmac.new(secret_key,msg) dige=h.digest() sk.send(dige)
案例tcp
server from socket import * ip_prot=("192.168.59.1",8600) import hmac import os res_key=b"lover" def server_hma(conn): msg=os.urandom(32) conn.sendall(msg) h= hmac.new(res_key,msg) dis=h.digest() req=conn.recv(len(dis)) cc=hmac.compare_digest(req,dis) return cc def server_conn(conn): while True: if not server_hma(conn): print("這是不符合") break else: date = conn.recv(1024) if not date: break conn.sendall(date.upper()) def server_head(ip_prot): server=socket(AF_INET,SOCK_STREAM) server.bind(ip_prot) server.listen(5) conn,addr=server.accept() server_conn(conn) server_head(ip_prot)
client 合法客服端 from socket import * import hmac ip_prot=("192.168.59.1",8600) import hmac import os res_key=b"lover" def client_hm(client): msg=client.recv(32) h=hmac.new(res_key,msg) dis=h.digest() client.sendall(dis) def client_head(ip): client=socket(AF_INET,SOCK_STREAM) client.connect(ip) client_hm(client) while True: aa=input("》》》").strip() if not aa: continue client.sendall(aa.encode("utf-8")) bb=client.recv(1024) print(bb.decode("utf-8")) client_head(ip_prot)
cliinet 非法客服端(沒有驗證) #_*_coding:utf-8_*_ __author__ = 'Linhaifeng' from socket import * def client_handler(ip_port,bufsize=1024): tcp_socket_client=socket(AF_INET,SOCK_STREAM) tcp_socket_client.connect(ip_port) while True: data=input('>>: ').strip() if not data:continue if data == 'quit':break tcp_socket_client.sendall(data.encode('utf-8')) respone=tcp_socket_client.recv(bufsize) print(respone.decode('utf-8')) tcp_socket_client.close() if __name__ == '__main__': ip_port=('127.0.0.1',9999) bufsize=1024
clinet 非法客戶端 不知道key __author__ = 'Linhaifeng' from socket import * import hmac,os secret_key=b'linhaifeng bang bang bang1111' def conn_auth(conn): ''' 驗證客戶端到服務器的連接 :param conn: :return: ''' msg=conn.recv(32) h=hmac.new(secret_key,msg) digest=h.digest() conn.sendall(digest) def client_handler(ip_port,bufsize=1024): tcp_socket_client=socket(AF_INET,SOCK_STREAM) tcp_socket_client.connect(ip_port) conn_auth(tcp_socket_client) while True: data=input('>>: ').strip() if not data:continue if data == 'quit':break tcp_socket_client.sendall(data.encode('utf-8')) respone=tcp_socket_client.recv(bufsize) print(respone.decode('utf-8')) tcp_socket_client.close() if __name__ == '__main__': ip_port=('127.0.0.1',9999) bufsize=1024 client_handler(ip_port,bufsize)