4

Ok, so I'm not super familiar with using For /F. I can modify it if the file is static and has a set ammount of lines that i can skip and then pull data from. I'm currently trying to modify an .XML file. The file will have varying ammounts of lines, but will always have the following

</SyncWindow> </AutoSyncWindows> <SyncServiceConnections /> <DaysToRetainRecordedData>90</DaysToRetainRecordedData> <SyncRestartRequired>false</SyncRestartRequired> - <LastGroupsSynced>

The value for <DaysToRetainRecordedData>90</DaysToRetainRecordedData> may differ, for example <DaysToRetainRecordedData>30</DaysToRetainRecordedData>

Using tokens, what would be the most efficient way to search that .XML file for that line and overwrite it with the following <DaysToRetainRecordedData>0</DaysToRetainRecordedData>

I can not overwrite the entire .XML file as they have unique server keys that will vary from machine to machine.So I need to be able to find that line and edit the value to 0. Any thoughts? If For /F is not the most efficient way to go, I can move to VBS if need be. But it would have to be called from pure shellcode and would make things a bit more complex.

1 Answer 1

4

The most elegant, flexible and safe way to do this would be to download msxsl.exe and then use a tiny XSLT stylesheet to modify only the value you want in the XML:

<!-- DaysToRetainRecordedData.xsl -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:param name="newValue" select="0" />

  <!-- this template copies your input XML unchanged -->
  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*" />
    </xsl:copy>
  </xsl:template>

  <!-- this template changes one single value -->
  <xsl:template match="DaysToRetainRecordedData/text()">
    <xsl:value-of select="$newValue" />
  </xsl:template>
</xsl:stylesheet>

Call that on the command line with:

msxsl.exe input.xml DaysToRetainRecordedData.xsl –o output.xml newValue=0

The command line parameter newValue will show up in the XSL program.

Sign up to request clarification or add additional context in comments.

7 Comments

So for example, msxsl.exe C:\path\to\xmltobechanged.xml DaysToRetainRecordedData.xsl -o C:\path\to\xmltobechanged.xml newValue=0 ? thanks for the input btw
@Zer0mod: Yes, like that. If it does not work right away for you, come back with the error you get and I'll try to help.
@Tomalak Well, I've basically done the following. I've taken the code that you've posted and saved it as "DaysToRetain.xsl" then I've placed that file in the root of my thumbdrive that is running the batch file. Msxsl.exe is called as follows > E:\msxsl.exe C:\Documents and Settings\User\Local Settings\Application Data\Folder\Existing.Xml DaysToRetain.xsl -o C:\Documents and Settings\User\Local Settings\Application Data\Folder\Existing.Xml newValue=0 I'm getting what looks like a parse error "Parameter name 'Settings\User\Local' must be followed by an '=' character. Thoughts?
@Zer0mod: You must enclose paths in quotes as soon as they contain spaces. That's not to do with msxsl, but with how the command line works. ;-) Also: It may help if you put msxsl.exe into a directory that's in the PATH (C:\Windows, for starters), and then cd into the directory where the XML file is and work with relative paths.
@Tomalak: I just spent hours enclosing paths quotes for Xcopy lol. Can't believe I did that. Thanks for the slap in the head. So my problem now is that I'm getting the following End tag xsl:template` does not match the start tag xsl:value-of
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.