forked from rapid7/metasploit-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pcap_log.rb
202 lines (167 loc) · 5.07 KB
/
pcap_log.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
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
##
# $Id$
##
##
# This file is part of the Metasploit Framework and may be subject to
# redistribution and commercial restrictions. Please see the Metasploit
# Framework web site for more information on licensing and terms of use.
# https://metasploit.com/framework/
##
# $Revision$
module Msf
class Plugin::PcapLog < Msf::Plugin
# Only little-endian is supported in this implementation.
PCAP_FILE_HEADER = "\xD4\xC3\xB2\xA1\x02\x00\x04\x00\x00\x00\x00\x00\x00\x00\x00\x00`\x00\x00\x00\x01\x00\x00\x00"
#
# Implements a pcap console command dispatcher.
#
class PcapLogDispatcher
include Msf::Ui::Console::CommandDispatcher
def name
"PcapLog"
end
def commands
{
"pcap_filter" => "Set/Get a BPF-style packet filter",
"pcap_dir" => "Set/Get a directory to log pcaps to",
"pcap_prefix" => "Set/Get a filename prefix to log pcaps to",
"pcap_iface" => "Set/Get an interface to capture from",
"pcap_start" => "Start a capture",
"pcap_stop" => "Stop a running capture",
"pcap_show_config" => "Show the current PcapLog configuration"
}
end
def cmd_pcap_filter(*args)
@filter = args.join(' ') || @filter
print_line "#{self.name} BPF filter: #{@filter}"
end
def cmd_pcap_prefix(*args)
@prefix = args[0] || @prefix || "msf3-session"
print_line "#{self.name} prefix: #{@prefix}"
end
def cmd_pcap_dir(*args)
@dir = args[0] || @dir || "/tmp"
print_line "#{self.name} Directory: #{@dir}"
end
def cmd_pcap_iface(*args)
@iface = args[0] || @iface
print_line "#{self.name} Interface: #{@iface}"
end
def cmd_pcap_start(*args)
unless @pcaprub_loaded
print_error("Pcap module not available")
return false
end
if @capture_thread && @capture_thread.alive?
print_error "Capture already started."
return false
end
gen_fname
print_line "Starting packet capture from #{@iface} to #{@fname}"
okay,msg = validate_options
unless okay
print_error msg
return false
end
dev = (@iface || ::Pcap.lookupdev)
@capture_file.write(PCAP_FILE_HEADER)
@capture_file.flush
@pcap = ::Pcap.open_live(dev, 65535, true, 1)
@pcap.setfilter(@filter) if @filter
@capture_thread = Thread.new {
@pcap.each do |pkt|
@capture_file.write(convert_to_pcap(pkt))
@capture_file.flush
end
}
end
def cmd_pcap_stop(*args)
if @capture_thread && @capture_thread.alive?
print_line "Stopping packet capture from #{@iface} to #{@fname}"
print_line "Capture Stats: #{@pcap.stats.inspect}"
@pcap = nil
@capture_file.close if @capture_file.respond_to? :close
@capture_thread.kill
@capture_thread = nil
else
print_error "No capture running."
end
end
def convert_to_pcap(packet)
t = Time.now
sz = packet.size
[t.to_i, t.usec, sz, sz, packet].pack("V4A*")
end
def gen_fname
t = Time.now
file_part = "%s_%04d-%02d-%02d_%02d-%02d-%02d.pcap" % [
@prefix, t.year, t.month, t.mday, t.hour, t.min, t.sec
]
@fname = File.join(@dir, file_part)
end
# Check for euid 0 and check for a valid place to write files
def validate_options
# Check for root.
unless Process.euid.zero?
msg = "You must run as root in order to capture packets."
return [false, msg]
end
# Check directory suitability.
unless File.directory? @dir
msg = "Invalid pcap directory specified: '#{@dir}'"
return [false, msg]
end
unless File.writable? @dir
msg = "No write permission to directory: '#{@dir}'"
return [false, msg]
end
@capture_file = File.open(@fname, "ab")
unless File.writable? @fname
msg = "Cannot write to file: '#{@fname}'"
return [false, msg]
end
# If you got this far, you're golden.
msg = "We're good!"
return [true, msg]
end
# Need to pretend to have a datastore for Exploit::Capture to
# function.
def datastore
{}
end
def initialize(*args)
super
@dir = File.join(Msf::Config.config_directory, 'logs')
@prefix = "msf3-session"
@filter = nil
@pcaprub_loaded = false
begin
require 'pcaprub'
@pcaprub_loaded = true
@iface = ::Pcap.lookupdev
rescue ::Exception => e
print_error "#{e.class}: #{e}"
@pcaprub_loaded = false
@pcaprub_error = e
end
end
end
def initialize(framework, opts)
super
add_console_dispatcher(PcapLogDispatcher)
print_status "PcapLog plugin loaded."
end
# Kill the background thread
def cleanup
@capture_thread.kill if @capture_thread && @capture_thread.alive?
@capture_file.close if @capture_file.respond_to? :close
remove_console_dispatcher('PcapLog')
end
def name
"pcap_log"
end
def desc
"Logs all socket operations to pcaps (in /tmp by default)"
end
end
end