covert-bandwidth.lua 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. --------------------------------------------------
  2. -- Author: Cecylia Bocovich <cbocovic@uwaterloo.ca>
  3. -- Purpose: Extracts statistics about TLS handshakes
  4. -- Usage: tshark -q <other opts> -Xlua_script:tls_stats.lua -r <trace>
  5. --------------------------------------------------
  6. do
  7. -- Extractor definitions
  8. ip_addr_extractor = Field.new("ip.addr")
  9. tcp_src_port_extractor = Field.new("tcp.srcport")
  10. tcp_dst_port_extractor = Field.new("tcp.dstport")
  11. tcp_len_extractor = Field.new("tcp.len")
  12. tcp_stream_extractor = Field.new("tcp.stream")
  13. local function main()
  14. local tap = Listener.new("tcp")
  15. local count = 1
  16. local total_bytes = 0
  17. local file = assert(io.open("bandwidth"..tostring(count)..".csv", "w"))
  18. file:write("time,bytes\n")
  19. file:close()
  20. --------------------------------
  21. ----- Handshake Statistics -----
  22. --------------------------------
  23. -- Each stream has a table that holds the following data:
  24. -- {state = [SHAKING, SHOOK, APPLICATION],
  25. -- clnt_session_id = [Bytes], srvr_session_id = [Bytes],
  26. -- session_ticket = [Bytes], resumed = [Boolean],
  27. -- ccs_received = [Int],
  28. -- start_time = [Float], end_time = [Float], shake_time = [Float]}
  29. function stats_tls_handshake(pinfo, tvb)
  30. local ip_src, ip_dst = ip_addr_extractor()
  31. local port_src = tcp_src_port_extractor()
  32. local port_dst = tcp_dst_port_extractor()
  33. local tcp_len = tcp_len_extractor()
  34. -- check if stream is already saved
  35. if(tostring(port_src) == "1080") then
  36. --This packet is headed back to the browser
  37. if( not (tostring(tcp_len) == "0")) then
  38. total_bytes = total_bytes + tonumber(tostring(tcp_len))
  39. local file = assert(io.open("bandwidth"..tostring(count)..".csv", "a"))
  40. file:write(tostring(pinfo.abs_ts) .. "," .. tostring(total_bytes).."\n")
  41. file:close()
  42. end
  43. end
  44. if(tostring(port_dst) == "8888") then
  45. --start new file
  46. if(total_bytes > 0) then
  47. count = count + 1
  48. end
  49. total_bytes = 0
  50. local file = assert(io.open("bandwidth"..tostring(count)..".csv", "w"))
  51. file:write("time,bytes\n")
  52. file:close()
  53. end
  54. end
  55. -- start/end times
  56. local start_time
  57. local end_time
  58. function stats_start_end_times(pinfo)
  59. if (not start_time) then
  60. start_time = pinfo.abs_ts
  61. end_time = pinfo.abs_ts
  62. else
  63. if ( start_time > pinfo.abs_ts ) then start_time = pinfo.abs_ts end
  64. if ( end_time < pinfo.abs_ts ) then end_time = pinfo.abs_ts end
  65. end
  66. end
  67. -------------------
  68. ----- tap functions
  69. -------------------
  70. function tap.reset()
  71. end
  72. function tap.packet(pinfo,tvb,ip)
  73. stats_start_end_times(pinfo)
  74. stats_tls_handshake(pinfo, tvb)
  75. end
  76. function tap.draw()
  77. --print("=== Stream Information ===")
  78. --print_stream_info()
  79. print("=== Handshake Statistics ===")
  80. print("Capture Start Time: " .. tostring(start_time) )
  81. print("Capture End Time: " .. tostring(end_time) )
  82. end
  83. end
  84. main()
  85. end