照例先聚焦工控协议和数量较少的功能码,主要是modbus协议,功能码较少的是写单线圈指令(write single coil),wireshark中过滤条件可以用modbus.func_code==5
。以14060号包为例,顺序在ETH协议的trailer字段发现有类似MHg3ZjB4MzUw
的字符,尝试base64解码,可解出明文。但是每个末尾都会多出一个0。考虑base64可以将3个字符变成4个字符,猜测如果是0x??
后面应该缺少x??
的部分。同样在ETH协议的frame check sequence
部分的字符正好4个长度,14060号包的值为eDNm
,将fcs的字符解码正好印证猜想,即实际需要解码的是trailer
+ frame check sequence
。后面的每个字符ascii数值减少2和字符串反转基本没有难题。
一步到位解题代码:
import pyshark
import base64
import binascii
captures = pyshark.FileCapture("06.pcap")
tmp = ''
for c in captures:
if hasattr(c, 'modbus'):
if c['modbus'].func_code == '5':
if hasattr(c['eth'], 'trailer'):
tmp += (c['eth'].trailer.replace(':', '') + c['eth'].fcs[2:])
tmp = binascii.a2b_hex(tmp)
clear = base64.b64decode(tmp.decode()).decode()
b = clear.split('0x')[1:]
flag = [chr(int(i, 16) - 2) for i in b]
print(''.join(flag[::-1]))