Skip to content

Commit 3b8fd64

Browse files
authored
Merge pull request rapid7#20035 from bcoles/rubocop-modules-encoders
modules/encoders: Resolve RuboCop violations
2 parents ec5b21f + d85ccb2 commit 3b8fd64

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+1605
-1629
lines changed

modules/encoders/cmd/base64.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ def initialize
2626
register_advanced_options(
2727
[
2828
OptString.new('Base64Decoder', [ false, 'The binary to use for base64 decoding', '', %w[base64 base64-long base64-short openssl] ])
29-
],
30-
self.class
29+
]
3130
)
3231
end
3332

modules/encoders/cmd/brace.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ class MetasploitModule < Msf::Encoder
1010

1111
def initialize
1212
super(
13-
'Name' => 'Bash Brace Expansion Command Encoder',
13+
'Name' => 'Bash Brace Expansion Command Encoder',
1414
'Description' => %q{
1515
This encoder uses brace expansion in Bash and other shells
1616
to avoid whitespace without being overly fancy.
1717
},
18-
'Author' => ['wvu', 'egypt'],
19-
'Platform' => %w[ linux unix ],
20-
'Arch' => ARCH_CMD,
18+
'Author' => ['wvu', 'egypt'],
19+
'Platform' => %w[linux unix],
20+
'Arch' => ARCH_CMD,
2121
'EncoderType' => Msf::Encoder::Type::CmdPosixBrace
2222
)
2323
end

modules/encoders/cmd/echo.rb

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,61 +8,50 @@ class MetasploitModule < Msf::Encoder
88

99
def initialize
1010
super(
11-
'Name' => 'Echo Command Encoder',
12-
'Description' => %q{
11+
'Name' => 'Echo Command Encoder',
12+
'Description' => %q{
1313
This encoder uses echo and backlash escapes to avoid commonly restricted characters.
1414
},
15-
'Author' => 'hdm',
16-
'Arch' => ARCH_CMD,
17-
'Platform' => %w[ linux unix ],
18-
'EncoderType' => Msf::Encoder::Type::CmdPosixEcho)
15+
'Author' => 'hdm',
16+
'Arch' => ARCH_CMD,
17+
'Platform' => %w[linux unix],
18+
'EncoderType' => Msf::Encoder::Type::CmdPosixEcho)
1919
end
2020

21-
2221
#
2322
# Encodes the payload
2423
#
2524
def encode_block(state, buf)
2625
# Skip encoding for empty badchars
27-
if state.badchars.length == 0
28-
return buf
29-
end
26+
return buf if state.badchars.empty?
3027

31-
if state.badchars.include?("-")
32-
raise EncodingError
33-
else
34-
# Without an escape character we can't escape anything, so echo
35-
# won't work.
36-
if state.badchars.include?("\\")
37-
raise EncodingError
38-
else
39-
buf = encode_block_bash_echo(state,buf)
40-
end
41-
end
28+
raise EncodingError if state.badchars.include?('-')
4229

43-
return buf
30+
# echo won't work without an escape character
31+
raise EncodingError if state.badchars.include?('\\')
32+
33+
encode_block_bash_echo(state, buf)
4434
end
4535

4636
#
4737
# Uses bash's echo -ne command to hex encode the command string
4838
#
4939
def encode_block_bash_echo(state, buf)
50-
5140
hex = ''
5241

5342
# Can we use single quotes to enclose the echo arguments?
5443
if state.badchars.include?("'")
55-
hex = buf.unpack('C*').collect { |c| "\\\\\\x%.2x" % c }.join
44+
hex = buf.unpack('C*').collect { |c| '\\\\\\x%.2x' % c }.join
5645
else
57-
hex = "'" + buf.unpack('C*').collect { |c| "\\x%.2x" % c }.join + "'"
46+
hex = "'" + buf.unpack('C*').collect { |c| '\\x%.2x' % c }.join + "'"
5847
end
5948

6049
# Are pipe characters restricted?
61-
if state.badchars.include?("|")
50+
if state.badchars.include?('|')
6251
# How about backticks?
63-
if state.badchars.include?("`")
52+
if state.badchars.include?('`')
6453
# Last ditch effort, dollar paren
65-
if state.badchars.include?("$") or state.badchars.include?("(")
54+
if state.badchars.include?('$') || state.badchars.include?('(')
6655
raise EncodingError
6756
else
6857
buf = "$(/bin/echo -ne #{hex})"
@@ -75,7 +64,7 @@ def encode_block_bash_echo(state, buf)
7564
end
7665

7766
# Remove spaces from the command string
78-
if state.badchars.include?(" ")
67+
if state.badchars.include?(' ')
7968
buf.gsub!(/\s/, '${IFS}')
8069
end
8170

modules/encoders/cmd/generic_sh.rb

Lines changed: 32 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -10,42 +10,38 @@ class MetasploitModule < Msf::Encoder
1010

1111
def initialize
1212
super(
13-
'Name' => 'Generic Shell Variable Substitution Command Encoder',
14-
'Description' => %q{
13+
'Name' => 'Generic Shell Variable Substitution Command Encoder',
14+
'Description' => %q{
1515
This encoder uses standard Bourne shell variable substitution
1616
tricks to avoid commonly restricted characters.
1717
},
18-
'Author' => 'hdm',
19-
'Arch' => ARCH_CMD,
20-
'Platform' => 'unix')
18+
'Author' => 'hdm',
19+
'Arch' => ARCH_CMD,
20+
'Platform' => 'unix')
2121
end
2222

23-
2423
#
2524
# Encodes the payload
2625
#
2726
def encode_block(state, buf)
28-
2927
# Skip encoding for empty badchars
30-
if(state.badchars.length == 0)
28+
if state.badchars.empty?
3129
return buf
3230
end
3331

34-
if (state.badchars.include?("-"))
32+
if state.badchars.include?('-')
3533
# Then neither of the others will work. Get rid of spaces and hope
3634
# for the best. This obviously won't work if the command already
3735
# has other badchars in it, in which case we're basically screwed.
38-
if (state.badchars.include?(" "))
36+
if state.badchars.include?(' ')
3937
buf.gsub!(/\s/, '${IFS}')
4038
end
41-
else
39+
elsif state.badchars.include?('\\')
4240
# Without an escape character we can't escape anything, so echo
4341
# won't work. Try perl.
44-
if (state.badchars.include?("\\"))
45-
buf = encode_block_perl(state,buf)
46-
else
47-
buf = encode_block_bash_echo(state,buf)
48-
end
42+
buf = encode_block_perl(state, buf)
43+
else
44+
buf = encode_block_bash_echo(state, buf)
4945
end
5046

5147
return buf
@@ -55,46 +51,43 @@ def encode_block(state, buf)
5551
# Uses the perl command to hex encode the command string
5652
#
5753
def encode_block_perl(state, buf)
58-
59-
hex = buf.unpack("H*")
54+
hex = buf.unpack('H*')
6055
cmd = 'perl -e '
6156
qot = ',-:.=+!@#$%^&'
6257

6358
# Find a quoting character to use
6459
state.badchars.unpack('C*') { |c| qot.delete(c.chr) }
6560

6661
# Throw an error if we ran out of quotes
67-
raise EncodingError if qot.length == 0
62+
raise EncodingError if qot.empty?
6863

6964
sep = qot[0].chr
7065

7166
# Convert spaces to IFS...
72-
if (state.badchars.include?(" "))
67+
if state.badchars.include?(' ')
7368
cmd.gsub!(/\s/, '${IFS}')
7469
end
7570

7671
# Can we use single quotes to enclose the command string?
77-
if (state.badchars.include?("'"))
72+
if state.badchars.include?("'")
7873

79-
if (state.badchars.match(/\(|\)/))
74+
if state.badchars.match(/\(|\)/)
8075

8176
# No parenthesis...
8277
raise EncodingError
8378
end
8479

8580
cmd << "system\\(pack\\(qq#{sep}H\\*#{sep},qq#{sep}#{hex}#{sep}\\)\\)"
8681

87-
else
88-
if (state.badchars.match(/\(|\)/))
89-
if (state.badchars.include?(" "))
90-
# No spaces allowed, no parenthesis, give up...
91-
raise EncodingError
92-
end
93-
94-
cmd << "'system pack qq#{sep}H*#{sep},qq#{sep}#{hex}#{sep}'"
95-
else
96-
cmd << "'system(pack(qq#{sep}H*#{sep},qq#{sep}#{hex}#{sep}))'"
82+
elsif state.badchars.match(/\(|\)/)
83+
if state.badchars.include?(' ')
84+
# No spaces allowed, no parenthesis, give up...
85+
raise EncodingError
9786
end
87+
88+
cmd << "'system pack qq#{sep}H*#{sep},qq#{sep}#{hex}#{sep}'"
89+
else
90+
cmd << "'system(pack(qq#{sep}H*#{sep},qq#{sep}#{hex}#{sep}))'"
9891
end
9992

10093
return cmd
@@ -104,22 +97,21 @@ def encode_block_perl(state, buf)
10497
# Uses bash's echo -ne command to hex encode the command string
10598
#
10699
def encode_block_bash_echo(state, buf)
107-
108100
hex = ''
109101

110102
# Can we use single quotes to enclose the echo arguments?
111-
if (state.badchars.include?("'"))
112-
hex = buf.unpack('C*').collect { |c| "\\\\\\x%.2x" % c }.join
103+
if state.badchars.include?("'")
104+
hex = buf.unpack('C*').collect { |c| '\\\\\\x%.2x' % c }.join
113105
else
114-
hex = "'" + buf.unpack('C*').collect { |c| "\\x%.2x" % c }.join + "'"
106+
hex = "'" + buf.unpack('C*').collect { |c| '\\x%.2x' % c }.join + "'"
115107
end
116108

117109
# Are pipe characters restricted?
118-
if (state.badchars.include?("|"))
110+
if state.badchars.include?('|')
119111
# How about backticks?
120-
if (state.badchars.include?("`"))
112+
if state.badchars.include?('`')
121113
# Last ditch effort, dollar paren
122-
if (state.badchars.include?("$") or state.badchars.include?("("))
114+
if state.badchars.include?('$') || state.badchars.include?('(')
123115
raise EncodingError
124116
else
125117
buf = "$(/bin/echo -ne #{hex})"
@@ -132,7 +124,7 @@ def encode_block_bash_echo(state, buf)
132124
end
133125

134126
# Remove spaces from the command string
135-
if (state.badchars.include?(" "))
127+
if state.badchars.include?(' ')
136128
buf.gsub!(/\s/, '${IFS}')
137129
end
138130

modules/encoders/cmd/ifs.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ class MetasploitModule < Msf::Encoder
1010

1111
def initialize
1212
super(
13-
'Name' => 'Bourne ${IFS} Substitution Command Encoder',
13+
'Name' => 'Bourne ${IFS} Substitution Command Encoder',
1414
'Description' => %q{
1515
This encoder uses Bourne ${IFS} substitution to avoid whitespace
1616
without being overly fancy.
1717
},
18-
'Author' => ['egypt', 'wvu'],
19-
'Platform' => %w[ linux unix ],
20-
'Arch' => ARCH_CMD,
18+
'Author' => ['egypt', 'wvu'],
19+
'Platform' => %w[linux unix],
20+
'Arch' => ARCH_CMD,
2121
'EncoderType' => Msf::Encoder::Type::CmdPosixIFS
2222
)
2323
end

0 commit comments

Comments
 (0)