# ------------------------------------------------------------------
# nfquery Print matching netflow records
# ------------------------------------------------------------------
require 'win32ole'
USAGE = "nfquery <capture-filename> <fieldname>=<value>"
EXAMPLE = "nfquery c:\temp\nfcap.usnf SrcAddress=209.209.1.200"
if ARGV.length != 2
puts USAGE
exit 1
end
# Set up input
InputFile = ARGV[0]
UnsniffDB = WIN32OLE.new("Unsniff.Database")
QueryFieldName = ARGV[1].split('=')[0]
QueryFieldValue = ARGV[1].split('=')[1]
# Open capfile and get packet index
UnsniffDB.OpenForRead(InputFile)
PacketIndex = UnsniffDB.PacketIndex
# Scan each packet looking for netflow protocol id
(0..PacketIndex.Count-1).each do |idx|
pkt = PacketIndex.Item(idx)
nf_layer = pkt.FindLayerByGUID("{DF2428E1-4843-48CF-B7DD-CCC9E5AE4BC1}")
next if nf_layer.nil?
pdu_count=nf_layer.FindField("Count").Value.to_i
(0..pdu_count-1).each do |pduidx|
nf_pdu = nf_layer.FindField(">NetflowRecord>pdu[#{pduidx}]")
next if nf_pdu.nil?
target_field=nf_pdu.FindField(QueryFieldName)
if target_field.Value == QueryFieldValue
duration_ms = nf_pdu.FindField("End Time").Value.to_i - nf_pdu.FindField("Start Time").Value.to_i
s_ip = nf_pdu.FindField("SrcAddress").Value
d_ip = nf_pdu.FindField("DstAddress").Value
s_port = nf_pdu.FindField("SrcPort").Value
d_port = nf_pdu.FindField("DstPort").Value
octets = nf_pdu.FindField("Octets").Value
proto = nf_pdu.FindField("Protocol").Value
print pkt.ID.to_s.ljust(6) + " "
[s_ip,d_ip,s_port,d_port,octets,proto,duration_ms].each do |item|
print item.to_s.ljust(16) + " "
end
print "\n"
end
end
end
UnsniffDB.Close()