使用Python解码Dnstap时出现问题

时间:2019-01-03 15:33:55

标签: python protocol-buffers

我有任务使用Python解码Dnstap(据我了解,这是Google协议缓冲区类型)。所以,我有这样的东西:

\x00c\n\x04pdns\x12\rdnsst 1.3.3rJ\x08\x05\x10\x01\x18\x01"\x04dOC\xa7*\x04\xc1)<\x080\x8c\x9f\x0185@\xcb\xf1\xd9\xdf\x05M\xc6\x10\x04\x00R%?\xa2\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00\x04play\ngoogleapis\x03com\x00\x00\x01\x00\x01x\x01\x00b\n\x04pdns\x12\rdnsdist 1.3.3rI\x08\x05\x10\x01\x18\x01"\x04\x86\xf9\x97\xb8*\x04\xc1)<\x080\x90\xae\x0385@\xcb\xf1\xd9\xdf\x05M\xd6$\x04\x00R$\x8a\xc0\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00\x03crl\nglobaign\x03net\x00\x00\x01\x00\x01x\x01

我想要得到这样的东西:

type: MESSAGE
identity: "pdns"
version: "dnsdist 1.3.3"
message:
   type: CLIENT_QUERY
   query_time: !!timestamp 2018-01-22 14:56:12.000838207
   socket_family: INET
   socket_protocol: UDP
   query_address: 134.249.139.145
   response_address: 193.41.00.8
   query_port: 32344
   response_port: 33
   query_message: |
       ;; opcode: QUERY, status: NOERROR, id: 3418
       ;; flags: rd; QUERY: 1, ANSWER: 0, AUTHORITY: 0, ADDITIONAL: 0

       ;; QUESTION SECTION:
       ;www.nwsvr3.com. IN   A
   --- 

通过解码,我得到:

identity: "pdns"
version: "dnist 1.3.3"
message {
  type: CLIENT_QUERY
  socket_family: INET
  socket_protocol: UDP
  query_address: "\206\371\227\270"
  response_address: "\301)<\010"
  query_port: 55056
  response_port: 53
  query_time_sec: 1542879435
  query_time_nsec: 271574
  query_message:               "\212\300\001\000\000\001\000\000\000\000\000\000\003crl\nglobsign\003net\000\000\001\000\001"
}
type: MESSAGE 

您可以看到哪些标签:query_address,response_address,query_message dint解码。有人能帮我吗? 我花了很多时间,但我不知道该怎么做。 我的Python代码:

from proto_example.generated.message_pb2 import Dnstap

    test = "\x00c\n\x04pdns\x12\rdnsst 1.3.3rJ\x08\x05\x10\x01\x18\x01"\x04dOC\xa7*\x04\xc1)<\x080\x8c\x9f\x0185@\xcb\xf1\xd9\xdf\x05M\xc6\x10\x04\x00R%?\xa2\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00\x04play\ngoogleapis\x03com\x00\x00\x01\x00\x01x\x01\x00b\n\x04pdns\x12\rdnsdist 1.3.3rI\x08\x05\x10\x01\x18\x01"\x04\x86\xf9\x97\xb8*\x04\xc1)<\x080\x90\xae\x0385@\xcb\xf1\xd9\xdf\x05M\xd6$\x04\x00R$\x8a\xc0\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00\x03crl\nglobaign\x03net\x00\x00\x01\x00\x01x\x01"
    m = Dnstap()
    m.ParseFromString(test)
    print(m)

当在终端中执行以下命令时,会得到proto_example.genic.message_pb2其.py格式文件的位置:

protoc -I . --python_out=. dnstap.proto

非常感谢您。

0 个答案:

没有答案