Class: Rex::Post::Meterpreter::Ui::Console::CommandDispatcher::Sniffer

Inherits:
Object
  • Object
show all
Includes:
Extensions::Sniffer, Rex::Post::Meterpreter::Ui::Console::CommandDispatcher
Defined in:
lib/rex/post/meterpreter/ui/console/command_dispatcher/sniffer.rb

Overview

Packet sniffer extension user interface.

Constant Summary collapse

Klass =
Console::CommandDispatcher::Sniffer

Constants included from Extensions::Sniffer

Extensions::Sniffer::COMMAND_ID_SNIFFER_CAPTURE_DUMP, Extensions::Sniffer::COMMAND_ID_SNIFFER_CAPTURE_DUMP_READ, Extensions::Sniffer::COMMAND_ID_SNIFFER_CAPTURE_RELEASE, Extensions::Sniffer::COMMAND_ID_SNIFFER_CAPTURE_START, Extensions::Sniffer::COMMAND_ID_SNIFFER_CAPTURE_STATS, Extensions::Sniffer::COMMAND_ID_SNIFFER_CAPTURE_STOP, Extensions::Sniffer::COMMAND_ID_SNIFFER_INTERFACES, Extensions::Sniffer::EXTENSION_ID_SNIFFER, Extensions::Sniffer::TLV_TYPE_EXTENSION_SNIFFER, Extensions::Sniffer::TLV_TYPE_SNIFFER_ADDITIONAL_FILTER, Extensions::Sniffer::TLV_TYPE_SNIFFER_BYTE_COUNT, Extensions::Sniffer::TLV_TYPE_SNIFFER_EXCLUDE_PORTS, Extensions::Sniffer::TLV_TYPE_SNIFFER_INCLUDE_PORTS, Extensions::Sniffer::TLV_TYPE_SNIFFER_INTERFACES, Extensions::Sniffer::TLV_TYPE_SNIFFER_INTERFACE_HANDLE, Extensions::Sniffer::TLV_TYPE_SNIFFER_INTERFACE_ID, Extensions::Sniffer::TLV_TYPE_SNIFFER_PACKET, Extensions::Sniffer::TLV_TYPE_SNIFFER_PACKETS, Extensions::Sniffer::TLV_TYPE_SNIFFER_PACKET_COUNT

Instance Attribute Summary

Attributes included from Ui::Text::DispatcherShell::CommandDispatcher

#shell, #tab_complete_items

Instance Method Summary collapse

Methods included from Rex::Post::Meterpreter::Ui::Console::CommandDispatcher

check_hash, #client, #docs_dir, #filter_commands, #log_error, #msf_loaded?, #session, set_hash, #unknown_command

Methods included from Msf::Ui::Console::CommandDispatcher::Session

#cmd_background, #cmd_background_help, #cmd_exit, #cmd_irb, #cmd_irb_help, #cmd_irb_tabs, #cmd_pry, #cmd_pry_help, #cmd_resource, #cmd_resource_help, #cmd_resource_tabs, #cmd_sessions, #cmd_sessions_help

Methods included from Ui::Text::DispatcherShell::CommandDispatcher

#cmd_help, #cmd_help_help, #cmd_help_tabs, #deprecated_cmd, #deprecated_commands, #deprecated_help, #docs_dir, #help_to_s, included, #print, #print_error, #print_good, #print_line, #print_status, #print_warning, #tab_complete_directory, #tab_complete_filenames, #tab_complete_generic, #tab_complete_source_address, #unknown_command, #update_prompt

Constructor Details

#initialize(shell) ⇒ Sniffer

Initializes an instance of the sniffer command interaction.



25
26
27
# File 'lib/rex/post/meterpreter/ui/console/command_dispatcher/sniffer.rb', line 25

def initialize(shell)
  super
end

Instance Method Details

#cmd_sniffer_dump(*args) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/rex/post/meterpreter/ui/console/command_dispatcher/sniffer.rb', line 138

def cmd_sniffer_dump(*args)
  intf = args[0].to_i
  if (intf == 0 or not args[1])
    print_error("Usage: sniffer_dump [interface-id] [pcap-file]")
    return
  end

  path_cap = args[1]
  path_raw = args[1] + '.raw'

  fd = ::File.new(path_raw, 'wb+')

  print_status("Flushing packet capture buffer for interface #{intf}...")
  res = client.sniffer.capture_dump(intf)
  print_status("Flushed #{res[:packets]} packets (#{res[:bytes]} bytes)")

  bytes_all = res[:bytes] || 0
  bytes_got = 0
  bytes_pct = 0
  linktype = res[:linktype]
  while (bytes_all > 0)
    res = client.sniffer.capture_dump_read(intf,1024*512)

    bytes_got += res[:bytes]

    pct = ((bytes_got.to_f / bytes_all.to_f) * 100).to_i
    if(pct > bytes_pct)
      print_status("Downloaded #{"%.3d" % pct}% (#{bytes_got}/#{bytes_all})...")
      bytes_pct = pct
    end
    break if res[:bytes] == 0
    fd.write(res[:data])
  end

  fd.close

  print_status("Download completed, converting to PCAP...")

  fd = nil
  if(::File.exist?(path_cap))
    fd = ::File.new(path_cap, 'ab+')
  else
    fd = ::File.new(path_cap, 'wb+')
    fd.write([0xa1b2c3d4, 2, 4, 0, 0, 65536, linktype].pack('NnnNNNN'))
  end

  pkts = {}
  od = ::File.new(path_raw, 'rb')


  # TODO: reorder packets based on the ID (only an issue if the buffer wraps)
  while(true)
    buf = od.read(20)
    break unless buf

    idh,idl,thi,tlo,len = buf.unpack('N5')
    break unless len
    if(len > 10000)
      print_error("Corrupted packet data (length:#{len})")
      break
    end

    pkt_id = (idh << 32) +idl
    pkt_ts = Rex::Proto::SMB::Utils.time_smb_to_unix(thi,tlo)
    pkt    = od.read(len)

    fd.write([pkt_ts,0,len,len].pack('NNNN')+pkt)
  end
  od.close
  fd.close

  ::File.unlink(path_raw)
  print_status("PCAP file written to #{path_cap}")
end

#cmd_sniffer_interfaces(*args) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/rex/post/meterpreter/ui/console/command_dispatcher/sniffer.rb', line 55

def cmd_sniffer_interfaces(*args)

  ifaces = client.sniffer.interfaces()

  print_line()

  ifaces.each do |i|
    if i.length == 8
      # Windows
      print_line(sprintf("%d - '%s' ( type:%d mtu:%d usable:%s dhcp:%s wifi:%s )",
        i['idx'], i['description'],
        i['type'], i['mtu'], i['usable'], i['dhcp'], i['wireless'])
      )
    else
      # Mettle
      print_line(sprintf("%d - '%s' ( usable:%s )",
        i['idx'], i['description'], i['usable'])
      )
    end
  end

  print_line()

  return true
end

#cmd_sniffer_release(*args) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/rex/post/meterpreter/ui/console/command_dispatcher/sniffer.rb', line 125

def cmd_sniffer_release(*args)
  intf = args[0].to_i
  if (intf == 0)
    print_error("Usage: sniffer_release [interface-id]")
    return
  end

  res = client.sniffer.capture_release(intf)
  print_status("Flushed #{res[:packets]} packets (#{res[:bytes]} bytes) from interface #{intf}")

  return true
end

#cmd_sniffer_start(*args) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/rex/post/meterpreter/ui/console/command_dispatcher/sniffer.rb', line 81

def cmd_sniffer_start(*args)
  intf = args.shift.to_i
  if (intf == 0)
    print_error("Usage: sniffer_start [interface-id] [packet-buffer (1-200000)] [bpf filter (posix meterpreter only)]")
    return
  end
  maxp = (args.shift || 50000).to_i
  bpf  = args.join(" ")

  client.sniffer.capture_start(intf, maxp, bpf)
  print_status("Capture started on interface #{intf} (#{maxp} packet buffer)")
  return true
end

#cmd_sniffer_stats(*args) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/rex/post/meterpreter/ui/console/command_dispatcher/sniffer.rb', line 109

def cmd_sniffer_stats(*args)
  intf = args[0].to_i
  if (intf == 0)
    print_error("Usage: sniffer_stats [interface-id]")
    return
  end

  stats = client.sniffer.capture_stats(intf)
  print_status("Capture statistics for interface #{intf}")
  stats.each_key do |k|
    print_line("\t#{k}: #{stats[k]}")
  end

  return true
end

#cmd_sniffer_stop(*args) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/rex/post/meterpreter/ui/console/command_dispatcher/sniffer.rb', line 95

def cmd_sniffer_stop(*args)
  intf = args[0].to_i
  if (intf == 0)
    print_error("Usage: sniffer_stop [interface-id]")
    return
  end

  res = client.sniffer.capture_stop(intf)
  print_status("Capture stopped on interface #{intf}")
  print_status("There are #{res[:packets]} packets (#{res[:bytes]} bytes) remaining")
  print_status("Download or release them using 'sniffer_dump' or 'sniffer_release'")
  return true
end

#commandsObject

List of supported commands.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rex/post/meterpreter/ui/console/command_dispatcher/sniffer.rb', line 32

def commands
  {
    'sniffer_interfaces' => 'Enumerate all sniffable network interfaces',
    'sniffer_start'      => 'Start packet capture on a specific interface',
    'sniffer_stop'       => 'Stop packet capture on a specific interface',
    'sniffer_stats'      => 'View statistics of an active capture',
    'sniffer_dump'       => 'Retrieve captured packet data to PCAP file',
    'sniffer_release'    => 'Free captured packets on a specific interface instead of downloading them',
  }

  #reqs = {
  #  'sniffer_interfaces' => [COMMAND_ID_SNIFFER_INTERFACES],
  #  'sniffer_start'      => [COMMAND_ID_SNIFFER_CAPTURE_START],
  #  'sniffer_stop'       => [COMMAND_ID_SNIFFER_CAPTURE_STOP],
  #  'sniffer_stats'      => [COMMAND_ID_SNIFFER_CAPTURE_STATS],
  #  'sniffer_dump'       => [COMMAND_ID_SNIFFER_CAPTURE_DUMP],
  #  'sniffer_release'    => [COMMAND_ID_SNIFFER_CAPTURE_RELEASE],
  #}

  #filter_commands(all, reqs)
end

#nameObject

Name for this dispatcher sni



216
217
218
# File 'lib/rex/post/meterpreter/ui/console/command_dispatcher/sniffer.rb', line 216

def name
  "Sniffer"
end