我正在尝试使用Nokogiri解析两个XML文件,该文件基于存在的属性值。问题在于,即使在xpath
上不存在节点,我的初始有条件始终返回真实。我认为我已经做对了,但是显然我还没有。
这是我的两个XML文件:
dnsrecon_output.xml(典型文件输出)
<?xml version="1.0" ?>
<records>
<record ns_server="192.168.1.1" type="info" zone_transfer="failed"/>
<record ns_server="192.168.1.2" type="info" zone_transfer="failed"/>
<record address="192.168.1.1" mname="ns1.mynetwork.local" type="SOA"/>
<record Version="9.8.2rc1-RedHat-9.8.2-0.37.rc1.el6_7.5" address="192.168.1.1" recursive="True" target="ns1.mynetwork.local" type="NS"/>
<record Version="9.8.2rc1-RedHat-9.8.2-0.37.rc1.el6_7.5" address="192.168.1.2" recursive="True" target="ns2.mynetwork.local" type="NS"/>
<record address="192.168.1.3" name="web.local" type="A"/>
</records>
dnsrecon_brute.xml
<?xml version="1.0" ?>
<records>
<record address="192.168.1.3" name="dev.web.local" type="A"/>
<record name="ftp.web.local" target="dev.web.local" type="CNAME"/>
<record address="192.168.1.4" name="git.web.local" type="A"/>
<record address="192.168.1.5" name="ops.web.local" type="A"/>
<record address="192.168.1.6" name="whm.web.local" type="A"/>
<record address="192.168.1.7" name="www.web.local" type="A"/>
</records>
但是,如果zone_transfer=success
输出看起来像这样...
dnsrecon_output_xfer.xml(只是显示我要匹配的内容)
<?xml version="1.0" ?>
<records>
<record ns_server="192.168.1.2" type="info" zone_transfer="success"/>
<record address="192.168.1.2" mname="ns" type="SOA" zone_server="192.168.1.2"/>
<record address="192.168.1.2" target="ns1.network.local" type="NS" zone_server="192.168.1.2"/>
<record address="192.168.1.100" name="win.network.local" type="A" zone_server="192.168.1.2"/>
<record address="192.168.1.2" name="ns1.network.local" type="A" zone_server="192.168.1.2"/>
<record address="192.168.1.2" mname="ns1.network.local" type="SOA"/>
</records>
这是我的诺科吉里解析。主要是我要检查zone_transfer=success
是否存在,如果它不移动到*_brute.xml
文件:
document = Nokogiri::XML(File.open("dnsrecon_output.xml"))
document.remove_namespaces!
# Strictly for Zone transfers
if document.xpath("//records//record[@zone_transfer='success']")
puts "Zone Transfer Successful ... YIKES!!!"
document.xpath("//records//record[@type='A']").each do |record|
# change the output here to match what we need it to...
puts "#{record.at_xpath("@address")} - #{record.at_xpath("@name")}"
end
else
puts "No zone transfers... moving to brute"
document2 = Nokogiri::XML(File.open("dnsrecon_brute.xml"))
document2.remove_namespaces!
document2.xpath("//records//record[@type='A' or @type='CNAME']").each do |record|
puts "#{record.at_xpath("@address")} - #{record.at_xpath("@name")}"
end
end
您会注意到,我最初正在解析dnsrecon_output.xml
文件,该文件在记录标签中具有zone_transfer="failed"
。但是,我在控制台中的输出是:
Zone Transfer Successful ... YIKES!!!
192.168.1.3 - web.local
,永远不会运行else
部分,应该是。我在哪里出错了属性值检查?
您的支票返回一个空的Nokogiri::XML::NodeSet
,这是真实的。
读取属性并比较其值:
if document.xpath("//records//record/@zone_transfer").first.value == 'success'
...
请注意,仅当XPath匹配至少一个元素时,这才能工作(因此,它对" brute"文件不起作用)。
或添加any?
以获得布尔返回:
if document.xpath("//records//record[@zone_transfer='success']").any?
...
x = document.xpath("//records//record[@zone_transfer='success']")
p x
--output:--
[]
和:
if []
puts %q{It's true!}
end
--output:--
It's true.
在Ruby中评估错误的唯一值是false
本身和nil
。空数组,空哈希,空白字符串,0
等都是True。
相反,您可以做这样的事情:
if document.xpath("//records//record[@zone_transfer='success']").empty?
puts "no matching elements"
#got to next xml file
else
puts "found matches"
#do your other xpath search
end
--output:--
no matching elements