ssl_stats.lua 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. --------------------------------------------------
  2. -- $Header: /cvs/WIP/datcat-import/crawdad/bin/analysis/trace_stats.lua,v 1.1 2007/04/25 15:18:28 emile Exp $
  3. -- extracts various stats (subset of crl_stats)
  4. -- from a trace file, use like:
  5. -- tshark -q <other opts> -Xlua_script:trace_stats.lua <trace>
  6. -- wireshark/tshark needs to be compiled --with-lua
  7. --------------------------------------------------
  8. do
  9. ip_addr_extractor = Field.new("ip.addr")
  10. tcp_src_port_extractor = Field.new("tcp.srcport")
  11. tcp_dst_port_extractor = Field.new("tcp.dstport")
  12. tcp_stream_extractor = Field.new("tcp.stream")
  13. tls_handshake_type_extractor = Field.new("ssl.handshake.type")
  14. tls_content_type_extractor = Field.new("ssl.record.content_type")
  15. tls_ccs_extractor = Field.new("ssl.change_cipher_spec")
  16. icmp_type_extractor = Field.new("icmp.type")
  17. local function init_listener()
  18. local tap = Listener.new("ssl")
  19. local file = assert(io.open("handshake_stats", "w"))
  20. file:write("stream,time\n")
  21. file:close()
  22. ----------------------
  23. ----- stats functions
  24. ----------------------
  25. -- ipv4 counts
  26. local ipv4_src_cache = {}
  27. local ipv4_dst_cache = {}
  28. local ipv4_src_count = 0
  29. local ipv4_dst_count = 0
  30. function stats_ipv4_counts(pinfo,tvb)
  31. local ip_src
  32. local ip_dst
  33. ip_src, ip_dst = ip_addr_extractor()
  34. if ( ip_src ) then
  35. if (not ipv4_src_cache[ tostring(ip_src) ] == true ) then
  36. ipv4_src_cache[ tostring(ip_src) ] = true
  37. ipv4_src_count = ipv4_src_count + 1
  38. else
  39. -- print("src already recorded")
  40. end
  41. else
  42. -- print("NO src")
  43. end
  44. if ( ip_dst ) then
  45. if (not ipv4_dst_cache[ tostring(ip_dst) ] == true ) then
  46. ipv4_dst_cache[ tostring(ip_dst) ] = true
  47. ipv4_dst_count = ipv4_dst_count + 1
  48. else
  49. -- print("dst already recorded")
  50. end
  51. else
  52. -- print("NO dst")
  53. end
  54. end
  55. -- tcp stream counts
  56. local tcp_stream_cache = {}
  57. local tcp_stream_count = 0
  58. function stats_stream_counts(pinfo,tvb)
  59. local stream
  60. local sport, dport, saddr, daddr
  61. stream = tcp_stream_extractor()
  62. saddr, daddr = ip_addr_extractor()
  63. sport = tcp_src_port_extractor()
  64. dport = tcp_dst_port_extractor()
  65. if ( stream ) then
  66. if (not tcp_stream_cache[ tostring(stream) ] == true ) then
  67. tcp_stream_cache[ tostring(stream) ] = true
  68. tcp_stream_count = tcp_stream_count + 1
  69. print("Stream #" .. tostring(tcp_stream_count) .. " | " .. tostring(saddr) .. ":" .. tostring(sport) .. " > " .. tostring(daddr) .. ":" .. tostring(dport) )
  70. else
  71. -- print("stream already recorded")
  72. end
  73. else
  74. -- print("NO stream")
  75. end
  76. end
  77. -- ssl stats
  78. local tls_src_starts = {}
  79. local tls_ccs_cache = {}
  80. function stats_tls_handshake(pinfo, tvb)
  81. local hs_type, rec_type, ccs, stream
  82. hs_type = tls_handshake_type_extractor()
  83. ccs = tls_ccs_extractor()
  84. stream = tcp_stream_extractor()
  85. if(hs_type) then
  86. local type_string
  87. type_string = tostring(hs_type)
  88. if(type_string == "1") then
  89. print("Start time for stream #" .. tostring(stream) .. " is " .. tostring(pinfo.abs_ts))
  90. tls_src_starts[ tostring(stream) ] = pinfo.abs_ts
  91. end
  92. end
  93. if(ccs) then
  94. if (not tls_ccs_cache[ tostring(stream) ] == true ) then
  95. tls_ccs_cache[ tostring(stream) ] = true
  96. print("Received 1st CCS for stream #" .. tostring(stream))
  97. else
  98. if( tls_src_starts[ tostring(stream)] ) then
  99. -- We have received both CCS and Finished messages
  100. local hs_time = pinfo.abs_ts - tls_src_starts[ tostring(stream)]
  101. print("Total handshake time: " .. tostring(hs_time) )
  102. local file = assert(io.open("handshake_stats", "a"))
  103. file:write(tostring(stream) .. "," .. tostring(hs_time) .. "\n")
  104. file:close()
  105. end
  106. end
  107. end
  108. end
  109. -- start/end times
  110. local start_time
  111. local end_time
  112. function stats_start_end_times(pinfo)
  113. if (not start_time) then
  114. start_time = pinfo.abs_ts
  115. end_time = pinfo.abs_ts
  116. else
  117. if ( start_time > pinfo.abs_ts ) then start_time = pinfo.abs_ts end
  118. if ( end_time < pinfo.abs_ts ) then end_time = pinfo.abs_ts end
  119. end
  120. end
  121. -------------------
  122. ----- tap functions
  123. -------------------
  124. function tap.reset()
  125. end
  126. function tap.packet(pinfo,tvb,ip)
  127. stats_ipv4_counts(pinfo,tvb)
  128. stats_stream_counts(pinfo,tvb)
  129. stats_start_end_times(pinfo)
  130. stats_tls_handshake(pinfo, tvb)
  131. end
  132. function tap.draw()
  133. print("=== extra stats ===================================================")
  134. print("start_time: " .. start_time )
  135. print("end_time: " .. end_time )
  136. print("ipv4_src_address_count: " .. ipv4_src_count )
  137. print("ipv4_dst_address_count: " .. ipv4_dst_count )
  138. print("tcp_stream_count: " .. tcp_stream_count )
  139. print("===================================================================")
  140. end
  141. end
  142. init_listener()
  143. end