segment_error
segment_error

Reputation: 13

VBScript: Update/Modify XML File with Duplicate Node Names - Input: TXT and XML - Output: XML

I am attempting to use VBscript. (I am not good with VBScript, haven't used it for a few years, actually, but it is one of the few tools that will work in the environment I'm currently working in, and I have at least used it before. Powershell is the other tool that could be utilized, but I am even less experienced in Powershell, and have only used it for a couple point tasks.)

On to the issue: I have two input files: TEXT and XML There will be one new output file: XML Data from the text file will be used to update the XML file.

File 1: Text

    ! uninteresting text
    ! uninteresting text
    ! uninteresting text 
    ! V-1: This text discusses this vulnerability number.
    All of this text is important, but it only applies to #1.
    It does not apply to #2, or #3.
    !
    ! V-2: This text discusses vulnerability #2.
    It should be noted that it is multiple lines.
    ! V-3: This text discusses the 3rd vulnerability.
    !
    !
    ! __ ???
    ! V-4: This text discusses the 4th vulnerability
    There is other text here, that goes on and on,
    talking about this 4th vulnerability.
    It may or may not be important to you.
    EOF

File 2: The XMl file

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <LIST>
        <ASSET>
            <ASSET_TYPE></ASSET_TYPE>
            <HOST_NAME></HOST_NAME>
            <HOST_IP></HOST_IP>
        </ASSET>
        <AUDIT>
            <iAUDIT>
                <AUDIT_INFO></AUDIT_INFO>
                <VULN>
                    <AUDIT_DATA>
                        <VULN_ATTRIBUTE>Vuln_Num</VULN_ATTRIBUTE>
                        <ATTRIBUTE_DATA>V-1</ATTRIBUTE_DATA>
                    </AUDIT_DATA>
                    <AUDIT_DATA>
                        <VULN_ATTRIBUTE>Severity</VULN_ATTRIBUTE>
                        <ATTRIBUTE_DATA></ATTRIBUTE_DATA>
                    </AUDIT_DATA>
                    <AUDIT_DATA>
                        <VULN_ATTRIBUTE>Vuln_Explanation</VULN_ATTRIBUTE>
                        <ATTRIBUTE_DATA>This is a vulnerability.</ATTRIBUTE_DATA>
                    </AUDIT_DATA>
                    <AUDIT_DATA>
                        <VULN_ATTRIBUTE>Documentable</VULN_ATTRIBUTE>
                        <ATTRIBUTE_DATA>false</ATTRIBUTE_DATA>
                    </AUDIT_DATA>
                    <FINDING_DETAILS />
                    <COMMENT />
                </VULN>
                <VULN>
                    <AUDIT_DATA>
                        <VULN_ATTRIBUTE>Vuln_Num</VULN_ATTRIBUTE>
                        <ATTRIBUTE_DATA>V-2</ATTRIBUTE_DATA>
                    </AUDIT_DATA>
                    <AUDIT_DATA>
                        <VULN_ATTRIBUTE>Severity</VULN_ATTRIBUTE>
                        <ATTRIBUTE_DATA></ATTRIBUTE_DATA>
                    </AUDIT_DATA>
                    <AUDIT_DATA>
                        <VULN_ATTRIBUTE>Vuln_Explanation</VULN_ATTRIBUTE>
                        <ATTRIBUTE_DATA>This is a vulnerability.</ATTRIBUTE_DATA>
                    </AUDIT_DATA>
                    <AUDIT_DATA>
                        <VULN_ATTRIBUTE>Documentable</VULN_ATTRIBUTE>
                        <ATTRIBUTE_DATA>false</ATTRIBUTE_DATA>
                    </AUDIT_DATA>
                    <FINDING_DETAILS />
                    <COMMENT />
                </VULN>
                <VULN>
                    <AUDIT_DATA>
                        <VULN_ATTRIBUTE>Vuln_Num</VULN_ATTRIBUTE>
                        <ATTRIBUTE_DATA>V-3</ATTRIBUTE_DATA>
                    </AUDIT_DATA>
                    <AUDIT_DATA>
                        <VULN_ATTRIBUTE>Severity</VULN_ATTRIBUTE>
                        <ATTRIBUTE_DATA></ATTRIBUTE_DATA>
                    </AUDIT_DATA>
                    <AUDIT_DATA>
                        <VULN_ATTRIBUTE>Vuln_Explanation</VULN_ATTRIBUTE>
                        <ATTRIBUTE_DATA>This is a vulnerability.</ATTRIBUTE_DATA>
                    </AUDIT_DATA>
                    <AUDIT_DATA>
                        <VULN_ATTRIBUTE>Documentable</VULN_ATTRIBUTE>
                        <ATTRIBUTE_DATA>false</ATTRIBUTE_DATA>
                    </AUDIT_DATA>
                    <FINDING_DETAILS />
                    <COMMENT />
                </VULN>
                <VULN>
                    <AUDIT_DATA>
                        <VULN_ATTRIBUTE>Vuln_Num</VULN_ATTRIBUTE>
                        <ATTRIBUTE_DATA>V-4</ATTRIBUTE_DATA>
                    </AUDIT_DATA>
                    <AUDIT_DATA>
                        <VULN_ATTRIBUTE>Severity</VULN_ATTRIBUTE>
                        <ATTRIBUTE_DATA></ATTRIBUTE_DATA>
                    </AUDIT_DATA>
                    <AUDIT_DATA>
                        <VULN_ATTRIBUTE>Vuln_Explanation</VULN_ATTRIBUTE>
                        <ATTRIBUTE_DATA>This is a vulnerability.</ATTRIBUTE_DATA>
                    </AUDIT_DATA>
                    <AUDIT_DATA>
                        <VULN_ATTRIBUTE>Documentable</VULN_ATTRIBUTE>
                        <ATTRIBUTE_DATA>false</ATTRIBUTE_DATA>
                    </AUDIT_DATA>
                    <FINDING_DETAILS />
                    <COMMENT />
                </VULN>     
            </iAUDIT>
        </AUDIT>
    </LIST>

I want to make this output file:

file3: XML

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <LIST>
        <ASSET>
            <ASSET_TYPE></ASSET_TYPE>
            <HOST_NAME></HOST_NAME>
            <HOST_IP></HOST_IP>
        </ASSET>
        <AUDIT>
            <iAUDIT>
                <AUDIT_INFO></AUDIT_INFO>
                <VULN>
                    <AUDIT_DATA>
                        <VULN_ATTRIBUTE>Vuln_Num</VULN_ATTRIBUTE>
                        <ATTRIBUTE_DATA>V-1</ATTRIBUTE_DATA>
                    </AUDIT_DATA>
                    <AUDIT_DATA>
                        <VULN_ATTRIBUTE>Severity</VULN_ATTRIBUTE>
                        <ATTRIBUTE_DATA></ATTRIBUTE_DATA>
                    </AUDIT_DATA>
                    <AUDIT_DATA>
                        <VULN_ATTRIBUTE>Vuln_Explanation</VULN_ATTRIBUTE>
                        <ATTRIBUTE_DATA>This is a vulnerability.</ATTRIBUTE_DATA>
                    </AUDIT_DATA>
                    <AUDIT_DATA>
                        <VULN_ATTRIBUTE>Documentable</VULN_ATTRIBUTE>
                        <ATTRIBUTE_DATA>false</ATTRIBUTE_DATA>
                    </AUDIT_DATA>
                    <FINDING_DETAILS>: This text discusses this vulnerability number.
                    All of this text is important, but it only applies to #1.
                    It does not apply to #2, or #3.
                    !
                    ! </FINDING_DETAILS>
                    <COMMENT />
                </VULN>
                <VULN>
                    <AUDIT_DATA>
                        <VULN_ATTRIBUTE>Vuln_Num</VULN_ATTRIBUTE>
                        <ATTRIBUTE_DATA>V-2</ATTRIBUTE_DATA>
                    </AUDIT_DATA>
                    <AUDIT_DATA>
                        <VULN_ATTRIBUTE>Severity</VULN_ATTRIBUTE>
                        <ATTRIBUTE_DATA></ATTRIBUTE_DATA>
                    </AUDIT_DATA>
                    <AUDIT_DATA>
                        <VULN_ATTRIBUTE>Vuln_Explanation</VULN_ATTRIBUTE>
                        <ATTRIBUTE_DATA>This is a vulnerability.</ATTRIBUTE_DATA>
                    </AUDIT_DATA>
                    <AUDIT_DATA>
                        <VULN_ATTRIBUTE>Documentable</VULN_ATTRIBUTE>
                        <ATTRIBUTE_DATA>false</ATTRIBUTE_DATA>
                    </AUDIT_DATA>
                    <FINDING_DETAILS>: This text discusses vulnerability #2.
                    It should be noted that it is multiple lines.
                    ! </FINDING_DETAILS>
                    <COMMENT />
                </VULN>
                <VULN>
                    <AUDIT_DATA>
                        <VULN_ATTRIBUTE>Vuln_Num</VULN_ATTRIBUTE>
                        <ATTRIBUTE_DATA>V-3</ATTRIBUTE_DATA>
                    </AUDIT_DATA>
                    <AUDIT_DATA>
                        <VULN_ATTRIBUTE>Severity</VULN_ATTRIBUTE>
                        <ATTRIBUTE_DATA></ATTRIBUTE_DATA>
                    </AUDIT_DATA>
                    <AUDIT_DATA>
                        <VULN_ATTRIBUTE>Vuln_Explanation</VULN_ATTRIBUTE>
                        <ATTRIBUTE_DATA>This is a vulnerability.</ATTRIBUTE_DATA>
                    </AUDIT_DATA>
                    <AUDIT_DATA>
                        <VULN_ATTRIBUTE>Documentable</VULN_ATTRIBUTE>
                        <ATTRIBUTE_DATA>false</ATTRIBUTE_DATA>
                    </AUDIT_DATA>
                    <FINDING_DETAILS>: This text discusses the 3rd vulnerability.
                    !
                    !
                    ! __ ???
                    ! </FINDING_DETAILS>
                    <COMMENT />
                </VULN>
                <VULN>
                    <AUDIT_DATA>
                        <VULN_ATTRIBUTE>Vuln_Num</VULN_ATTRIBUTE>
                        <ATTRIBUTE_DATA>V-4</ATTRIBUTE_DATA>
                    </AUDIT_DATA>
                    <AUDIT_DATA>
                        <VULN_ATTRIBUTE>Severity</VULN_ATTRIBUTE>
                        <ATTRIBUTE_DATA></ATTRIBUTE_DATA>
                    </AUDIT_DATA>
                    <AUDIT_DATA>
                        <VULN_ATTRIBUTE>Vuln_Explanation</VULN_ATTRIBUTE>
                        <ATTRIBUTE_DATA>This is a vulnerability.</ATTRIBUTE_DATA>
                    </AUDIT_DATA>
                    <AUDIT_DATA>
                        <VULN_ATTRIBUTE>Documentable</VULN_ATTRIBUTE>
                        <ATTRIBUTE_DATA>false</ATTRIBUTE_DATA>
                    </AUDIT_DATA>
                    <FINDING_DETAILS>: This text discusses the 4th vulnerability
                    There is other text here, that goes on and on,
                    talking about this 4th vulnerability.
                    It may or may not be important to you.</FINDING_DETAILS>
                    <COMMENT />
                </VULN>     
            </iAUDIT>
        </AUDIT>
    </LIST>

How can this be done in VBScript?

I have found a couple references, but how do I grab V-1, V-2, V-3, V-4 in the XML. AUDIT_DATA gets used multiple times, and I cannot figure out how to grab the one that I want. Without grabbing the correct one, I won't be able to update FINDING_DETAIL.

I don't want to grab the 1st or nth occurence of the Vulnerability number, I want to be able to specifically locate the V- numbers. In this particular case, it is the very FIRST instance of the AUDIT_DATA in the file, and the V- numbers are in order in the XML file, if it helps any.

I'm hoping that someone else has had a similar issue before, and can assist.

I have located these particular reference links, but am not experienced enough with VBScript to assimilate it into a working solution.

The Microsoft documentation likes to provide Visual Basic examples for some reason, but I'm not sure I can run that without Visual Studio (which I don't have.)

These are similar, but are not the same problem that I have:

VBScript to modify an Element (of many, with same Element names but with different Attributes)?

Locate XML node with specific attribute and change it

reading xml file with vbscript

This is very similar, but is VB.net, not VBScript:

Parsing XML file with nodes of same name with VB.NET

This is my current code, but it's nowhere near a solution, so I'm not sure it does anything other than show that I'm miles away from the solution.

    Set xmlDoc = CreateObject("Microsoft.XMLDOM")
    xmlDoc.Async = "False"
    xmlDoc.Load("C:\scripts\audit\original.xml")


    WScript.echo "ShowAllVULN_ATTRIBUTENodes"

    set colVuln_Attribute = xmlDoc.selectNodes ("/LIST/AUDIT/iAUDIT/VULN/AUDIT_DATA/VULN_ATTRIBUTE")

    For Each objNode in colVuln_Attribute
        WScript.Echo objNode.Text
    Next

    WScript.echo "Show All Attribute Data"

    set colAttribute_Data= xmlDoc.selectNodes ("/LIST/AUDIT/iAUDIT/VULN/AUDIT_DATA/ATTRIBUTE_DATA")

    For Each objNode in colAttribute_Data
        WScript.Echo objNode.Text
    Next

'And that's the extent of what I can do with the XML.

' Maybe it'll help to go over my original thought of how I wanted to tackle this problem, when I thought it was trivial, but I guess trivial depends on your experience and skill level. ' the end result that I want is this: ' For each V-number in the XML, replace the FINDING_DETAILS with the multi-line text of matching V-number. ' ' I want to reference this XML data:

    ' V-1, FINDING_DETAILS
    ' V-2, FINDING_DETAILS
    ' V-3, FINDING_DETAILS
    ' V-4, FINDING_DETAILS

' I wanted to build a table like this, to represent the text file input: ' (I have not even begun on this part.)

    ' V-1, multi-line text
    ' V-2, multi-line text
    ' V-3, multi-line text
    ' V-4, multi-line text

Then, the idea would be to use the V- number as a primary key, and update FINDING_DETAILS.

Please note: I fixed two typos in the XML that caused it to fail to load: standalone=yes

XML VBScript Duplicate Node Name XPath

Upvotes: 1

Views: 1141

Answers (1)

Rich
Rich

Reputation: 4170

I would suggest just handling both the text document and XML documents as text files rather than utilizing the XML node break down that vbscript provides.

Here's a script which splits your text file, searches for the V-* and collects the string, then collects the next strings until your End Of Statement trigger get's handled. This trigger would be at the end of every vulnerability description.

Dim a, text, Vulnerabilities(), eos: a = "C:\testenv\text.txt" : eos = false
Redim Vulnerabilities(0)
text = split(CreateObject("Scripting.FileSystemObject").OpenTextFile(a, 1).ReadAll, vbcrlf)
for i=0 to ubound(text) 
    if instr(text(i), "V-") then
       eos = false
       Vulnerabilities(ubound(Vulnerabilities)) = text(i)
       redim preserve Vulnerabilities(ubound(Vulnerabilities) + 1)
    elseif not eos then
       if instr(text(i), "END OF STATEMENT CHECK" then
            eos = true
            Vulnerabilities(ubound(Vulnerabilities)) = Vulnerabilities(ubound(Vulnerabilities)) & "ENDSTATEMENT"
       else
            Vulnerabilities(ubound(Vulnerabilities)) = text(i)
            redim preserve Vulnerabilities(ubound(Vulnerabilities) + 1)
       end if
    end if
next
redim preserve Vulnerabilities(ubound(Vulnerabilities) - 1) 'trim off extra dynamic entry

Now you've got all the Vulnerabilities data stashed, You can swap out specific XML segments a line at a time and iterate through your Vulnerability data in parallel, populating the XML File one line at a time. Kind of like looking at two documents and cutting out data from the second document then pasting it over the marker on the first document.

Dim b, xml, counter : b = "C:\testenv\xml.xml" : counter = 0
xml = split(CreateObject("Scripting.FileSystemObject").OpenTextFile(a, 1).ReadAll, vbcrlf)
for i=0 to ubound(xml) 
     if instr(xml(i), "Vuln_Num") then
          for j=counter to ubound(Vulnerabilities)
               if instr(Vulnerabilities(j), "V-") then
                    replace(xml(i), "VulnNum", Vulnerabilities(j)
                    exit for
               end if

               'additional conditional statements can be placed here. 
               'such as detecting when the "ENDSTATEMENT" is in 
               'the vulnerability string and ending the string replacements. 
          next
     end if
next

I have handled XML in vbscript with both the XML methods and the raw data file readout methods, and i just prefer to programmatically read it as textual data rather than use the XML node methods. It's just simpler. Best of luck in your endeavors.

Upvotes: 0

Related Questions