allan

tcp_analysis_errors.lua

Dec 25th, 2023
1,293
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.60 KB | Source Code | 0 0
  1. ------------------------------------------------------------------------------
  2. -- TCP Error Tracker                                                        --
  3. --                                                                          --
  4. -- Counts the number of TCP errors by TCP stream, and source and            --
  5. --  destination IP addresses. Also supports Check Point Orchestrators when  --
  6. --  used with the cp_mho.lua plugin - but not required.                     --
  7. --                                                                          --
  8. ------------------------------------------------------------------------------
  9. --                                                                          --
  10. -- Copyright: Allan Que, 2023                                               --
  11. -- License  : GPL-3.0-or-later                                              --
  12. --            see http://www.gnu.org/licenses/gpl.html                      --
  13. --                                                                          --
  14. ------------------------------------------------------------------------------
  15. --                                                                          --
  16. -- Copy file to your Personal Plugins directory                             --
  17. --  See Help > About > Folders for paths                                    --
  18. --                                                                          --
  19. ------------------------------------------------------------------------------
  20.  
  21. local function tcp_errors_tap()
  22.     local title_s="TCP Analysis Errors"
  23.     local mho_mode_b=false
  24.  
  25.     local stream_m = {}
  26.     local src_m = {}
  27.     local dst_m = {}
  28.     local mho_m = {}
  29.    
  30.     -- Exclude packets from counting towards TCP errors
  31.     local tap_filter_s=
  32.         "tcp.analysis.flags"..
  33.         "&& !tcp.analysis.keep_alive"..
  34.         "&& !tcp.analysis.keep_alive_ack"
  35.  
  36.     -- Include any filters user has already set in Wireshark
  37.     if get_filter() ~= "" then tap_filter_s=tap_filter_s.."&&"..get_filter(s) end
  38.  
  39.     -- Additional filters when operating with Check Point MHO g_tcpdump packets
  40.     -- Accomodates for traffic thru one MHO and response thru another,
  41.     --   and gets around capture timing issues that show up as out-of-order
  42.     if get_preference("mho.enabled") then
  43.         tap_filter_s=tap_filter_s..
  44.             "&& !tcp.analysis.out_of_order"..
  45.             "&& !tcp.analysis.ack_lost_segment"..
  46.             "&& !tcp.analysis.spurious_retransmission"
  47.         title_s=title_s.." - MHO Mode"
  48.         mho_mode_b=true
  49.     end
  50.  
  51.     -- Create the tcp tap and window
  52.     local tap = Listener.new("tcp", tap_filter_s)
  53.     local win = TextWindow.new(title_s)
  54.  
  55.     -- remove() must be after tap object is instantiated
  56.     win:set_atclose(function ()
  57.         -- Remove tap when not in use
  58.         -- Must be outside tap.packet()
  59.         tap:remove()
  60.     end)
  61.  
  62.     local function increment_counter(field_o, table_m)
  63.         local field_s = tostring(field_o)
  64.         local count_i = table_m[field_s] or 0
  65.         table_m[field_s] = count_i + 1
  66.     end
  67.  
  68.     local function mho_id(abs_t)
  69.         -- Extract subseconds and convert to integer with rounding
  70.         local subsec_f = tonumber(abs_t) % 1 * 1000000
  71.         local subsec_i = math.floor(subsec_f + 0.5)
  72.  
  73.         -- Chassis and Blade calculations are from
  74.         --   $SMODIR/bin/gtcpdump-helper.sh
  75.         local mho_ch = bit.rshift(bit.band(subsec_i, 16), 4) + 1
  76.         local mho_bl = bit.band(subsec_i, 15)
  77.  
  78.         return mho_ch.."_"..mho_bl
  79.     end
  80.  
  81.     -- function called for every single packet
  82.     function tap.packet(pinfo, buffer, tap_data)
  83.         increment_counter(tap_data.th_stream, stream_m)
  84.         increment_counter(pinfo.src, src_m)
  85.         increment_counter(pinfo.dst, dst_m)
  86.         if mho_mode_b then
  87.             increment_counter(mho_id(pinfo.abs_ts), mho_m)
  88.         end
  89.     end
  90.  
  91.     -- Output supplied table data to text window
  92.     local function output_table(title_s, table_m, col1_width_i)
  93.         win:append("** "..title_s.." **\n")
  94.         for key_s, value_i in pairs(table_m) do
  95.             win:append(string.format("%"..col1_width_i.."s - %5d\n",key_s, value_i))
  96.         end
  97.         win:append("\n")
  98.     end
  99.  
  100.     -- Update text window with latest info - run periodically
  101.     function tap.draw()
  102.         win:clear()
  103.         win:append(title_s.."\n")
  104.         output_table("By Stream", stream_m, 5)
  105.         output_table("By Source IP", src_m, 15)
  106.         output_table("By Destination IP", dst_m, 15)
  107.         if mho_mode_b then
  108.             output_table("By MHO Id", mho_m, 4)
  109.         end
  110.     end
  111.  
  112.     -- Called at the end of capture run
  113.     function tap.reset()
  114.         win:clear()
  115.         stream_m = {}
  116.         src_m = {}
  117.         dst_m = {}
  118.     end
  119.  
  120.     -- Sets display filter to supplied string
  121.     local function display_filter(filter_s)
  122.         set_filter(filter_s)
  123.         apply_filter()
  124.     end
  125.  
  126.     -- Dialog boxes for Stream, IP and MHO buttons
  127.     local function stream_dlg()
  128.         new_dialog("Enter a Stream Number",
  129.             function(stream_s)
  130.                 -- Validate input is a number
  131.                 if tonumber(stream_s) == nil then return end
  132.                 display_filter("tcp.stream=="..stream_s)
  133.             end,
  134.         "Stream")
  135.     end
  136.  
  137.     local function ip_dlg()
  138.         new_dialog("Enter an IPv4 Address",
  139.             function(input_ip_s)
  140.                 _, _, valid_ip_s = string.find(input_ip_s, "^%s*(%d+%.%d+%.%d+%.%d+)%s*$")
  141.                 if valid_ip_s == nil then return end
  142.                 display_filter("ip.addr=="..valid_ip_s)
  143.             end,
  144.         "IPv4")
  145.     end
  146.  
  147.     local function mho_dlg()
  148.         new_dialog("Enter MHO Id",
  149.             function(input_mho_s)
  150.                 _, _, valid_mho_s = string.find(input_mho_s, "^%s*([12]_1?%d)%s*$")
  151.                 if valid_mho_s == nil then return end
  152.                 display_filter("mho.id=="..valid_mho_s)
  153.             end,
  154.         "MHO Id")
  155.     end
  156.  
  157.     -- Buttons at the bottom of text window
  158.     -- Order = top-down is left-to-right
  159.     win:add_button("Copy All", function()
  160.         copy_to_clipboard(win:get_text())
  161.     end)
  162.     if mho_mode_b then win:add_button("MHO", mho_dlg) end
  163.     win:add_button("IPv4", ip_dlg)
  164.     win:add_button("Stream", stream_dlg)
  165.     win:add_button("No Filter", function()
  166.         display_filter("")
  167.     end)
  168.  
  169.     -- Start sending packets into tap
  170.     retap_packets()
  171. end
  172.  
  173. -- Require GUI and versions 3.5 and later
  174. if not (gui_enabled() and get_preference) then return end
  175.  
  176. register_menu("TCP/Error Tracker", tcp_errors_tap, MENU_TOOLS_UNSORTED)
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment