I have this file with 2 passords that i need extract them and input in a XML file:
root@nirvana:~# cat old/pwdump.txt
# Metasploit PWDump Export 2.0
# Generated: 2020-01-12 18:43:53 UTC
# Project: default
#
#########################################################
# Plaintext Passwords (3 passwords, 2 services)
# 192.168.0.112:22/tcp (ssh)
paulo secret1
# 192.168.0.112:22/tcp (ssh)
paulosgf secret2
For this i created this code, that first verify if "ssh_login" field exists. If so, it update the username and creates a password field with your respective value. If not, it creates both fields with this values. But i think this approach isn't the right way:
target='192.168.0.112'
File.open("/root/old/pwdump.txt", "r") do |fd|
while(credentials = fd.gets) != nil
if (credentials =~ /[Ss][Ss][Hh]/)
while(credentials = fd.gets) != nil
ssharray = "#{credentials}".to_s.split(/ /)
sshu=ssharray.slice(0).to_s
sshp=ssharray.slice(1).to_s.chomp
break if credentials.include?("\n")
end
if `xmlstarlet sel -t -v "//MetasploitV5/hosts/host/vulns/vuln/vuln_attempts/vuln_attempt[module='auxiliary/scanner/ssh/ssh_login']/module" "/root/#{target}.xml"`
`xmlstarlet ed -L -i "//MetasploitV5/hosts/host/vulns/vuln/vuln_attempts/vuln_attempt[module='auxiliary/scanner/ssh/ssh_login']/module" -t elem -n password -v "#{sshp}" "/root/#{target}.xml"`
`xmlstarlet ed -L -u "//MetasploitV5/hosts/host/vulns/vuln/vuln_attempts/vuln_attempt[module='auxiliary/scanner/ssh/ssh_login']/username" -v "#{sshu}" "/root/#{target}.xml"`
else
`xmlstarlet ed -L -s "//MetasploitV5/hosts/host/vulns/vuln/vuln_attempts" -t elem -n "vuln_attempt" -v "" -s "//MetasploitV5/hosts/host/vulns/vuln/vuln_attempts/vuln_attempt[last()]" -t elem -n "module" -v "auxiliary/scanner/ssh/ssh_login" "/root/#{target}.xml"`
`xmlstarlet ed -L -s "//MetasploitV5/hosts/host/vulns/vuln/vuln_attempts/vuln_attempt[module='auxiliary/scanner/ssh/ssh_login']" -t elem -n username -v "#{sshu}" "/root/#{target}.xml"`
`xmlstarlet ed -L -s "//MetasploitV5/hosts/host/vulns/vuln/vuln_attempts/vuln_attempt[module='auxiliary/scanner/ssh/ssh_login']" -t elem -n password -v "#{sshp}" "/root/#{target}.xml"`
end
end
end
end
After run the code the XML, the values are overridden, with the last credential, and the password field is inserted 2 times:
<vuln_attempt>
<id>101</id>
<vuln-id>72</vuln-id>
<attempted-at>2020-01-18 16:25:22 UTC</attempted-at>
<exploited>true</exploited>
<fail-reason/>
<username>paulosgf</username>
<password>secret2</password>
<password>secret2</password>
<module>auxiliary/scanner/ssh/ssh_login</module>
<session-id>100</session-id>
<loot-id/>
<fail-detail/>
</vuln_attempt>
<vuln_attempt>
<id>102</id>
<vuln-id>72</vuln-id>
<attempted-at>2020-01-18 16:26:18 UTC</attempted-at>
<exploited>true</exploited>
<fail-reason/>
<username>paulosgf</username>
<password>secret2</password>
<password>secret2</password>
<module>auxiliary/scanner/ssh/ssh_login</module>
<session-id>101</session-id>
<loot-id/>
<fail-detail/>
</vuln_attempt>
if i close the statement while(credentials = fd.gets) != nil
in the righ way, after the commands, the code don't change the XML:
File.open("/root/old/pwdump.txt", "r") do |fd|
while(credentials = fd.gets) != nil
if (credentials =~ /[Ss][Ss][Hh]/)
while(credentials = fd.gets) != nil
ssharray = "#{credentials}".to_s.split(/ /)
sshu=ssharray.slice(0).to_s
sshp=ssharray.slice(1).to_s.chomp
break if credentials.include?("\n")
if `xmlstarlet sel -t -v "//MetasploitV5/hosts/host/vulns/vuln/vuln_attempts/vuln_attempt[module='auxiliary/scanner/ssh/ssh_login']/module" "/root/#{target}.xml"`
`xmlstarlet ed -L -i "//MetasploitV5/hosts/host/vulns/vuln/vuln_attempts/vuln_attempt[module='auxiliary/scanner/ssh/ssh_login']/module" -t elem -n password -v "#{sshp}" "/root/#{target}.xml"`
`xmlstarlet ed -L -u "//MetasploitV5/hosts/host/vulns/vuln/vuln_attempts/vuln_attempt[module='auxiliary/scanner/ssh/ssh_login']/username" -v "#{sshu}" "/root/#{target}.xml"`
else
`xmlstarlet ed -L -s "//MetasploitV5/hosts/host/vulns/vuln/vuln_attempts" -t elem -n "vuln_attempt" -v "" -s "//MetasploitV5/hosts/host/vulns/vuln/vuln_attempts/vuln_attempt[last()]" -t elem -n "module" -v "auxiliary/scanner/ssh/ssh_login" "/root/#{target}.xml"`
`xmlstarlet ed -L -s "//MetasploitV5/hosts/host/vulns/vuln/vuln_attempts/vuln_attempt[module='auxiliary/scanner/ssh/ssh_login']" -t elem -n username -v "#{sshu}" "/root/#{target}.xml"`
`xmlstarlet ed -L -s "//MetasploitV5/hosts/host/vulns/vuln/vuln_attempts/vuln_attempt[module='auxiliary/scanner/ssh/ssh_login']" -t elem -n password -v "#{sshp}" "/root/#{target}.xml"`
end
end
end
end
end
What's wrong?