未绑定的DNS服务器,Python模块:检查​​响应消息元素

时间:2015-04-25 14:33:04

标签: python dns

目标

我想利用Unbound Python模块在调度到客户端之前检查响应。我主要是对;; ANSWER SECTION:感兴趣,即查询解析到的IP地址。

问题

对于logDnsMsg功能的简单修改看起来像是在追求所需{reply_inforrset_refub_packed_rrset_key结构时的重要任务{1}}字节。

原因是logDnsMsg函数无法按预期;; ANSWER SECTION:个查询A运行,但令人惊讶的是;; ANSWER SECTION:查询;; AUTHORITY SECTION:

Lemme演示了Python实现的logDnsMsg函数和本地log_dns_msg函数之间的比较;前者显示胡言乱语,后者表现完全符合预期。这两个函数都在Python模块上下文中调用,如下所示:

AAAA

请注意,我更改了原始logDnsMsg,因为它使用了日志框架,而不是+++ def operate(id, event, qstate, qdata): log_info("pythonmod: operate called, id: %d, event:%s" % (id, strmodulevent(event))) if (qstate.return_msg): logDnsMsg(qstate) log_dns_msg("blackpie KARMMMMMM XXXXXXX", qstate.return_msg.qinfo, qstate.return_msg.rep) +++ print的输出相同,但是在缓冲区的判断下分散在日志文件中。

print输出:

dig

karm@localhost:~$ dig seznam.cz @127.0.0.1 -p53535 ; <<>> DiG 9.9.4-P2-RedHat-9.9.4-18.P2.fc20 <<>> seznam.cz @127.0.0.1 -p53535 ;; global options: +cmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 38630 ;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1 ;; OPT PSEUDOSECTION: ; EDNS: version: 0, flags:; udp: 4096 ;; QUESTION SECTION: ;seznam.cz. IN A ;; ANSWER SECTION: seznam.cz. 300 IN A 77.75.76.3 ;; Query time: 656 msec ;; SERVER: 127.0.0.1#53535(127.0.0.1) ;; WHEN: Sat Apr 25 16:04:32 CEST 2015 ;; MSG SIZE rcvd: 54 查询的输出AAAAlogDnsMsglog_dns_msg中看起来还不错:

;; AUTHORITY SECTION:

相反,对于[1429970672] unbound[14053:0] info: pythonmod: operate called, id: 1, event:module_event_moddone [1429970672] unbound[14053:0] info: ------------------------------------------------------------------------------------------ [1429970672] unbound[14053:0] info: Query: e.root-servers.net., type: AAAA (28), class: IN (1) [1429970672] unbound[14053:0] info: ------------------------------------------------------------------------------------------ [1429970672] unbound[14053:0] info: Return reply :: flags: 8080, QDcount: 1, Security:0, TTL=86400 [1429970672] unbound[14053:0] info: qinfo :: qname: ['e', 'root-servers', 'net', ''] e.root-servers.net., qtype: AAAA, qclass: IN [1429970672] unbound[14053:0] info: Reply: [1429970672] unbound[14053:0] info: 0:['root-servers', 'net', ''] root-servers.net. flags: 0004 [1429970672] unbound[14053:0] info: type:SOA (6) class:IN (1) [1429970672] unbound[14053:0] info: 0:TTL=3600000 [1429970672] unbound[14053:0] info: [1429970672] unbound[14053:0] info: 0x00 | 00 40 01 61 0C 72 6F 6F 74 2D 73 65 72 76 65 72 73 | . @ . a . r o o t - s e r v e r s 0x10 | 73 03 6E 65 74 00 05 6E 73 74 6C 64 0C 76 65 72 69 | s . n e t . . n s t l d . v e r i 0x20 | 69 73 69 67 6E 2D 67 72 73 03 63 6F 6D 00 78 0C E3 | i s i g n - g r s . c o m . x . . 0x30 | E3 24 00 00 38 40 00 00 1C 20 00 12 75 00 00 36 EE | . $ . . 8 @ . . . . . u . . 6 . 0x40 | EE 80 | . . [1429970672] unbound[14053:0] info: ------------------------------------------------------------------------------------------ [1429970672] unbound[14053:0] info: blackpie KARMMMMMM XXXXXXX ;; ->>HEADER<<- opcode: QUERY, rcode: NOERROR, id: 0 ;; flags: qr ra ; QUERY: 1, ANSWER: 0, AUTHORITY: 1, ADDITIONAL: 0 ;; QUESTION SECTION: e.root-servers.net. IN AAAA ;; ANSWER SECTION: ;; AUTHORITY SECTION: root-servers.net. 3600000 IN SOA a.root-servers.net. nstld.verisign-grs.com. 2014110500 14400 7200 1209600 3600000 ;; ADDITIONAL SECTION: ;; MSG SIZE rcvd: 96 [1429970672] unbound[14053:0] debug: mesh_run: python module exit state is module_finished 查询,就logDnsMsg而言,A完全没用:

;; ANSWER SECTION:

请注意错误地解释为ASCII的[1429970672] unbound[14053:0] info: pythonmod: operate called, id: 1, event:module_event_moddone [1429970672] unbound[14053:0] info: ------------------------------------------------------------------------------------------ [1429970672] unbound[14053:0] info: Query: seznam.cz., type: A (1), class: IN (1) [1429970672] unbound[14053:0] info: ------------------------------------------------------------------------------------------ [1429970672] unbound[14053:0] info: Return reply :: flags: 8080, QDcount: 1, Security:0, TTL=300 [1429970672] unbound[14053:0] info: qinfo :: qname: ['seznam', 'cz', ''] seznam.cz., qtype: A, qclass: IN [1429970672] unbound[14053:0] info: Reply: [1429970672] unbound[14053:0] info: 0:['seznam', 'cz', ''] seznam.cz. flags: 0000 [1429970672] unbound[14053:0] info: type:A (1) class:IN (1) [1429970672] unbound[14053:0] info: 0:TTL=300 [1429970672] unbound[14053:0] info: [1429970672] unbound[14053:0] info: 0x00 | 00 04 4D 4B 4C 03 | . . M K L . [1429970672] unbound[14053:0] info: ------------------------------------------------------------------------------------------ [1429970672] unbound[14053:0] info: blackpie KARMMMMMM XXXXXXX ;; ->>HEADER<<- opcode: QUERY, rcode: NOERROR, id: 0 ;; flags: qr ra ; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0 ;; QUESTION SECTION: seznam.cz. IN A ;; ANSWER SECTION: seznam.cz. 300 IN A 77.75.76.3 ;; AUTHORITY SECTION: ;; ADDITIONAL SECTION: ;; MSG SIZE rcvd: 43 [1429970672] unbound[14053:0] debug: mesh_run: python module exit state is module_finished 字节。

问题

00 04 4D 4B 4C 03中的哪个位置找到了所需的struct ub_packed_rrset_key** rrsets;数据? (如果它确实是正确的地方。)

我一直在摆弄它已经很久没有运气了。我检查了内部用于解包;; ANSWER SECTION:二进制数据的sldns_wire2str_pkt_scan函数,但我不是更明智的。

1 个答案:

答案 0 :(得分:1)

logDnsMsg()正在记录每个DNS资源记录的hex dump内容。它输出字节以及它们的ASCII解释(. s用于不可打印的字节)。这意味着如果数据包含ASCII字符串,这些字符串将在输出中显示,而其他类型的数据(如IP地址)将不会转换为有意义的文本。

数据的前两个字节是RDLENGTH字段,表示记录数据的长度。其余字节是RDATA字段。这些字节的解释取决于记录类型。 A条记录由一个32位IP地址组成,因此很容易解析。

以下示例代码打印A记录的内容:

def print_a_record(data):
    rdlength, rdata = data[:2], data[2:]
    assert rdlength == '\x00\x04'
    assert len(rdata) == 4
    addr_bytes = [ord(c) for c in rdata]
    print('{}.{}.{}.{}'.format(*addr_bytes))

更多信息: