Method: Rex::Post::Meterpreter::Ui::Console::CommandDispatcher::Sniffer#cmd_sniffer_dump

Defined in:
lib/rex/post/meterpreter/ui/console/command_dispatcher/sniffer.rb

#cmd_sniffer_dump(*args) ⇒ Object

[View source] [View on GitHub]

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