Skip to content

Commit d14b5c3

Browse files
authored
Merge pull request #20192 from bcoles/rubocop-modules-auxiliary-sniffer
modules/auxiliary/sniffer: Resolve RuboCop violations
2 parents 9c53b32 + da261da commit d14b5c3

File tree

1 file changed

+50
-44
lines changed

1 file changed

+50
-44
lines changed

modules/auxiliary/sniffer/psnuffle.rb

Lines changed: 50 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -17,38 +17,43 @@ class MetasploitModule < Msf::Auxiliary
1717

1818
def initialize
1919
super(
20-
'Name' => 'pSnuffle Packet Sniffer',
21-
'Description' => 'This module sniffs passwords like dsniff did in the past',
22-
'Author' => 'Max Moser <mmo[at]remote-exploit.org>',
23-
'License' => MSF_LICENSE,
24-
'Actions' =>
25-
[
26-
[ 'Sniffer', 'Description' => 'Run sniffer' ],
27-
[ 'List', 'Description' => 'List protocols' ]
28-
],
20+
'Name' => 'pSnuffle Packet Sniffer',
21+
'Description' => 'This module sniffs passwords like dsniff did in the past.',
22+
'Author' => 'Max Moser <mmo[at]remote-exploit.org>',
23+
'License' => MSF_LICENSE,
24+
'Actions' => [
25+
[ 'Sniffer', { 'Description' => 'Run sniffer' } ],
26+
[ 'List', { 'Description' => 'List protocols' } ]
27+
],
2928
'PassiveActions' => [ 'Sniffer' ],
30-
'DefaultAction' => 'Sniffer'
29+
'DefaultAction' => 'Sniffer',
30+
'Notes' => {
31+
'Stability' => [CRASH_SAFE],
32+
'SideEffects' => [],
33+
'Reliability' => []
34+
}
3135
)
36+
3237
register_options [
3338
OptString.new('PROTOCOLS', [true, 'A comma-delimited list of protocols to sniff or "all".', 'all']),
3439
]
3540

3641
register_advanced_options [
37-
OptPath.new('ProtocolBase', [true, 'The base directory containing the protocol decoders',
42+
OptPath.new('ProtocolBase', [
43+
true, 'The base directory containing the protocol decoders',
3844
File.join(Msf::Config.data_directory, 'exploits', 'psnuffle')
3945
]),
4046
]
4147
deregister_options('RHOSTS')
4248
end
4349

44-
4550
def load_protocols
4651
base = datastore['ProtocolBase']
4752
unless File.directory? base
48-
raise RuntimeError, 'The ProtocolBase parameter is set to an invalid directory'
53+
raise 'The ProtocolBase parameter is set to an invalid directory'
4954
end
5055

51-
allowed = datastore['PROTOCOLS'].split(',').map{|x| x.strip.downcase}
56+
allowed = datastore['PROTOCOLS'].split(',').map { |x| x.strip.downcase }
5257
@protos = {}
5358
decoders = Dir.new(base).entries.grep(/\.rb$/).sort
5459
decoders.each do |n|
@@ -57,15 +62,15 @@ def load_protocols
5762
begin
5863
m.module_eval(File.read(f, File.size(f)))
5964
m.constants.grep(/^Sniffer(.*)/) do
60-
proto = $1
65+
proto = ::Regexp.last_match(1)
6166
next unless allowed.include?(proto.downcase) || datastore['PROTOCOLS'] == 'all'
6267

6368
klass = m.const_get("Sniffer#{proto}")
6469
@protos[proto.downcase] = klass.new(framework, self)
6570

6671
print_status("Loaded protocol #{proto} from #{f}...")
6772
end
68-
rescue => e
73+
rescue StandardError => e
6974
print_error("Decoder #{n} failed to load: #{e.class} #{e} #{e.backtrace}")
7075
end
7176
end
@@ -88,6 +93,7 @@ def run
8893
p = PacketFu::Packet.parse(pkt)
8994
next unless p.is_tcp?
9095
next if p.payload.empty?
96+
9197
@protos.each_key do |k|
9298
@protos[k].parse(p)
9399
end
@@ -107,13 +113,13 @@ class BaseProtocolParser
107113

108114
def initialize(framework, mod)
109115
self.framework = framework
110-
self.module = mod
111-
self.sessions = {}
112-
self.dport = 0
116+
self.module = mod
117+
self.sessions = {}
118+
self.dport = 0
113119
register_sigs
114120
end
115121

116-
def parse(pkt)
122+
def parse(_pkt)
117123
nil
118124
end
119125

@@ -166,12 +172,12 @@ def report_cred(opts)
166172
self.module.create_credential_login(login_data)
167173
end
168174

169-
def report_note(*s)
170-
self.module.report_note(*s)
175+
def report_note(*opts)
176+
self.module.report_note(*opts)
171177
end
172178

173-
def report_service(*s)
174-
self.module.report_service(*s)
179+
def report_service(*opts)
180+
self.module.report_service(*opts)
175181
end
176182

177183
def find_session(sessionid)
@@ -184,39 +190,39 @@ def find_session(sessionid)
184190
purge_keys << ses
185191
end
186192
end
187-
purge_keys.each {|ses| sessions.delete(ses) }
193+
purge_keys.each { |ses| sessions.delete(ses) }
188194

189195
# Does this session already exist?
190-
if (sessions[sessionid])
196+
if sessions[sessionid]
191197
# Refresh the timestamp
192198
sessions[sessionid][:mtime] = Time.now
193-
else
199+
elsif (sessionid =~ /^([^:]+):([^-]+)-([^:]+):(\d+)$/s)
194200
# Create a new session entry along with the host/port from the id
195-
if (sessionid =~ /^([^:]+):([^-]+)-([^:]+):(\d+)$/s)
196-
sessions[sessionid] = {
197-
:client_host => $1,
198-
:client_port => $2,
199-
:host => $3,
200-
:port => $4,
201-
:session => sessionid,
202-
:ctime => Time.now,
203-
:mtime => Time.now
204-
}
205-
end
201+
sessions[sessionid] = {
202+
client_host: ::Regexp.last_match(1),
203+
client_port: ::Regexp.last_match(2),
204+
host: ::Regexp.last_match(3),
205+
port: ::Regexp.last_match(4),
206+
session: sessionid,
207+
ctime: Time.now,
208+
mtime: Time.now
209+
}
206210
end
207211

208212
sessions[sessionid]
209213
end
210214

211215
def get_session_src(pkt)
212-
return "%s:%d-%s:%d" % [pkt.ip_daddr,pkt.tcp_dport,pkt.ip_saddr,pkt.tcp_sport] if pkt.is_tcp?
213-
return "%s:%d-%s:%d" % [pkt.ip_daddr,pkt.udp_dport,pkt.ip_saddr,pkt.udp_sport] if pkt.is_udp?
214-
return "%s:%d-%s:%d" % [pkt.ip_daddr,0,pkt.ip_saddr,0]
216+
return "#{pkt.ip_daddr}:#{pkt.tcp_dport}-#{pkt.ip_saddr}-#{pkt.tcp_sport}" if pkt.is_tcp?
217+
return "#{pkt.ip_daddr}:#{pkt.udp_dport}-#{pkt.ip_saddr}-#{pkt.udp_sport}" if pkt.is_udp?
218+
219+
"#{pkt.ip_daddr}:0-#{pkt.ip_saddr}:0"
215220
end
216221

217222
def get_session_dst(pkt)
218-
return "%s:%d-%s:%d" % [pkt.ip_saddr,pkt.tcp_sport,pkt.ip_daddr,pkt.tcp_dport] if pkt.is_tcp?
219-
return "%s:%d-%s:%d" % [pkt.ip_saddr,pkt.udp_sport,pkt.ip_daddr,pkt.udp_dport] if pkt.is_udp?
220-
return "%s:%d-%s:%d" % [pkt.ip_saddr,0,pkt.ip_daddr,0]
223+
return "#{pkt.ip_saddr}:#{pkt.tcp_sport}-#{pkt.ip_daddr}:#{pkt.tcp_dport}" if pkt.is_tcp?
224+
return "#{pkt.ip_saddr}:#{pkt.udp_sport}-#{pkt.ip_daddr}:#{pkt.udp_dport}" if pkt.is_udp?
225+
226+
"#{pkt.ip_saddr}:0-#{pkt.ip_daddr}:0"
221227
end
222228
end

0 commit comments

Comments
 (0)