当前位置:   article > 正文

python dpkt解析ssl流

dpkt dpkt.sll.sll内容编码

用法:python extract_tls_flow.py -vr  white_pcap/11/2018-01-10_13-05-09_2.pcap  -o pcap_ssl_flow.txt  >log.txt

python dpkt解析ssl流,记录含有client hello到app data的完整ssl 流,同时记录ssl证书:

  1. #!/usr/bin/env python
  2. from __future__ import absolute_import
  3. from __future__ import print_function
  4. import argparse
  5. from binascii import hexlify
  6. import socket
  7. import struct
  8. import json
  9. import sys
  10. import textwrap
  11. import dpkt
  12. from constants import PRETTY_NAMES
  13. from asn1crypto import x509
  14. global streambuffer
  15. streambuffer = {}
  16. global encrypted_streams
  17. encrypted_streams = [] # change_cipher
  18. global ssl_servers_certs
  19. ssl_servers_certs = {}
  20. global ssl_servers_with_client_hello
  21. ssl_servers_with_client_hello = set()
  22. global client_hello_set
  23. client_hello_set = set()
  24. global ssl_flows
  25. ssl_flows = []
  26. global buffer
  27. buffer = {}
  28. need_more_parse = False
  29. class FlowDirection(object):
  30. OUT = 1
  31. IN = 2
  32. UNKNOWN = 3
  33. class Extension(object):
  34. """
  35. Encapsulates TLS extensions.
  36. """
  37. def __init__(self, payload):
  38. self._type_id, payload = unpacker('H', payload)
  39. self._type_name = pretty_name('extension_type', self._type_id)
  40. self._length, payload = unpacker('H', payload)
  41. # Data contains an array with the 'raw' contents
  42. self._data = None
  43. # pretty_data contains an array with the 'beautified' contents
  44. self._pretty_data = None
  45. if self._length > 0:
  46. self._data, self._pretty_data = parse_extension(payload[:self._length],
  47. self._type_name)
  48. def __str__(self):
  49. # Prints out data array in textual format
  50. return '{0}: {1}'.format(self._type_name, self._pretty_data)
  51. def analyze_packet(_timestamp, packet, nth):
  52. """
  53. Main analysis loop for pcap.
  54. """
  55. eth = dpkt.ethernet.Ethernet(packet)
  56. if isinstance(eth.data, dpkt.ip.IP):
  57. #print("timestamp:", _timestamp, "debug")
  58. parse_ip_packet(eth.data, nth, _timestamp)
  59. def parse_arguments():
  60. """
  61. Parses command line arguments.
  62. """
  63. global filename
  64. global verboseprint
  65. global output_file
  66. parser = argparse.ArgumentParser(
  67. formatter_class=argparse.RawDescriptionHelpFormatter,
  68. description=textwrap.dedent('''\
  69. Captures, parses and shows TLS Handshake packets
  70. Copyright (C) 2015 Peter Mosmans [Go Forward]
  71. This program is free software: you can redistribute it and/or modify
  72. it under the terms of the GNU General Public License as published by
  73. the Free Software Foundation, either version 3 of the License, or
  74. (at your option) any later version.'''))
  75. parser.add_argument('-r', '--read', metavar='FILE', action='store',
  76. help='read from file (don\'t capture live packets)')
  77. parser.add_argument('-v', '--verbose', action='store_true',
  78. help='increase output verbosity')
  79. parser.add_argument('-o', '--output', action='store',
  80. help='output file')
  81. args = parser.parse_args()
  82. if args.verbose:
  83. def verboseprint(*args):
  84. print('# ', end="")
  85. for arg in args:
  86. print(arg, end="")
  87. print()
  88. else:
  89. verboseprint = lambda *a: None
  90. filename = None
  91. if args.read:
  92. filename = args.read
  93. output_file = "demo_output.txt"
  94. if args.output:
  95. output_file = args.output
  96. def parse_ip_packet(ip, nth, timestamp):
  97. """
  98. Parses IP packet.
  99. """
  100. sys.stdout.flush()
  101. if isinstance(ip.data, dpkt.tcp.TCP) and len(ip.data.data):
  102. # print("****TCP packet found****", "tcp payload:", list(ip.data.data))
  103. parse_tcp_packet(ip, nth, timestamp)
  104. def parse_tcp_packet(ip, nth, timestamp):
  105. """
  106. Parses TCP packet.
  107. """
  108. stream = ip.data.data
  109. """ refer: The Transport Layer Security (TLS) Protocol URL:https://tools.ietf.org/html/rfc5246
  110. enum {
  111. change_cipher_spec(20), alert(21), handshake(22),
  112. application_data(23), (255)
  113. } ContentType;
  114. """
  115. # ssl flow
  116. if (stream[0]) in {20, 21, 22, 23}:
  117. if (stream[0]) in {20, 21, 22}:
  118. parse_tls_records(ip, stream, nth)
  119. else:
  120. connection = '{0}:{1}-{2}:{3}'.format(socket.inet_ntoa(ip.src),
  121. ip.data.sport,
  122. socket.inet_ntoa(ip.dst),
  123. ip.data.dport)
  124. print("*"*99)
  125. print("23 SSL application data:{} 10 sample:{} nth:{}".format(connection, list(stream[:10]), nth))
  126. # buffer record recent ssl flow from handshake to app data TODO precise description
  127. record_recent_data_flow(ip, stream, nth, timestamp)
  128. def has_application_data(flow_list):
  129. for flow in flow_list:
  130. if flow[0] == 23:
  131. return True
  132. return Fals
声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号