allan

cp_mho.lua

Dec 25th, 2023
1,348
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.90 KB | Source Code | 0 0
  1. ------------------------------------------------------------------------------
  2. -- Checkpoint Orchestrator Post-Dissector                                   --
  3. ------------------------------------------------------------------------------
  4. --                                                                          --
  5. -- Copyright: Allan Que, 2023                                               --
  6. -- License  : GPL-3.0-or-later                                              --
  7. --            see http://www.gnu.org/licenses/gpl.html                      --
  8. --                                                                          --
  9. ------------------------------------------------------------------------------
  10. --                                                                          --
  11. -- Copy file to your Personal Plugins directory                             --
  12. --  See Help > About > Folders for paths                                    --
  13. --                                                                          --
  14. ------------------------------------------------------------------------------
  15.  
  16. local mho_info = {
  17.     version = "1.1",
  18.     author = "Allan Que",
  19.     description = "Calculates which Check Point Orchestrator handled each"..
  20.         " frame. Capture file must come from $SMODIR/bin/g_tcpdump (Expert"..
  21.         " mode) to have correct values.",
  22. }
  23. set_plugin_info(mho_info)
  24.  
  25.  
  26. -- Access frame.time field (NSTime type)
  27. local frame_t_f = Field.new("frame.time")
  28.  
  29. -- Define new MHO protocol
  30. local mho_p = Proto("mho", "Check Point Orchestrator")
  31. local name_s = mho_p.name:lower()
  32.  
  33. -- Fields under protocol. These appear in the tree.
  34. local mho_ch_field = ProtoField.uint8(name_s..".chassis", "Chassis", base.DEC)
  35. local mho_bl_field = ProtoField.uint8(name_s..".blade", "Blade", base.DEC)
  36. local mho_id_field = ProtoField.string(name_s..".id", "Id", base.ASCII)
  37.  
  38. -- Calculate items and add them to protocol tree
  39. function mho_p.dissector(buffer, pinfo, tree)
  40.     -- Retrieve timestamp from frame
  41.     local frame_time_fld = frame_t_f()
  42.  
  43.     if not mho_p.prefs.enabled or frame_time_fld == nil then
  44.         return
  45.     end
  46.  
  47.     -- Extract frame sub-second timestamp
  48.     local subsec_f = frame_time_fld.value:tonumber() % 1 * 1000000
  49.  
  50.     -- Calculate integer value. Handle rounding if necessary
  51.     local subsec_i = math.floor(subsec_f + 0.5)
  52.  
  53.     -- Chassis and Blade calculations are from
  54.     --   $SMODIR/bin/gtcpdump-helper.sh
  55.     local mho_ch = bit.rshift(bit.band(subsec_i, 16), 4) + 1
  56.     local mho_bl = bit.band(subsec_i, 15)
  57.  
  58.     -- Add items to the tree
  59.     local mho_id = mho_ch..'_'..mho_bl
  60.     local mho_tree = tree:add(mho_p, mho_p.description..", Id: "..mho_id)
  61.     mho_tree:set_generated()
  62.     mho_tree:add(mho_ch_field, mho_ch):set_generated()
  63.     mho_tree:add(mho_bl_field, mho_bl):set_generated()
  64.     mho_tree:add(mho_id_field, mho_id):set_generated()
  65.  
  66.     -- Append extracted blade id to the Info column, if enabled
  67.     if mho_p.prefs.append_info then
  68.         pinfo.cols.info:append(" "..mho_p.name.."="..mho_id)
  69.     end
  70. end
  71.  
  72.  
  73. -- Link fields to protocol
  74. mho_p.fields = { mho_ch_field, mho_bl_field, mho_id_field }
  75.  
  76. -- Set default states to DISABLED - only enable when user requested
  77. mho_p.prefs.enabled = Pref.bool("Extract blade id from g_tcpdump", false,
  78.     "Extract blade id information from g_tcpdump packet captures")
  79. mho_p.prefs.append_info = Pref.bool("Append blade id to Info column", false,
  80.     "Append extracted blade id to the Info output column")
  81. mho_p.prefs.save_prefs = Pref.bool("Save preferences on menu bar click (GUI only)", false,
  82.     "Save preferences when using menu bar to enable/disable")
  83.  
  84. -- Register protocol as a new post-dissector
  85. register_postdissector(mho_p)
  86.  
  87.  
  88. -- Add menu item if running on 3.5 and later GUI
  89. -- set_preference() is only available on 3.5 and later versions
  90. -- Use Preferences > Protocol for older versions
  91. -- tshark uses "-o mho.enabled:true -o mho.append_info:true" parameters
  92. if not (gui_enabled() and set_preference) then return end
  93.  
  94. local function invert_enabled()
  95.     -- Read current enabled preference and store inverted value
  96.     local enabled_b = mho_p.prefs.enabled
  97.     set_preference(name_s..".enabled", not enabled_b)
  98.  
  99.     -- User decides if menu bar triggers a save of preferences to file
  100.     if mho_p.prefs.save_prefs then
  101.         apply_preferences()
  102.     end
  103.  
  104.     -- Rerun post dissection tasks
  105.     --
  106.     -- Note 1: Wireshark has a bug where the displayed protocol tree is not
  107.     --           updated. Briefly select another packet and switch back
  108.     --           to redraw tree.
  109.     --
  110.     -- Note 2: retap_packets() is faster than reload() but does not dissect
  111.     --           packets again. Info column is not updated and could still
  112.     --           contain "MHO=" data. Press Ctrl-R to perform a reload().
  113.     retap_packets()
  114. end
  115.  
  116. register_menu("MHO Blade Id", invert_enabled, MENU_TOOLS_UNSORTED)
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment