Access to filesystem from WinDBG's JavaScript script

Viewed 641

I'm currently playing with WinDBG script written in JavaScript as it's described by Microsoft.

How can I access the filesystem from within the JavaScript code? I'm interested in both reading and writing to files located somewhere on disk. For JavaScript executing on the browsers these features are disabled because of security reasons but for example NodeJS has its own libraries to support filesystem operations.

2 Answers

This works:

"use strict";

function invokeScript() {
    var debugControl = host.namespace.Debugger.Utility.Control;

    var output = debugControl.ExecuteCommand("vertarget");

    writeOutputToFile(output);
}

function writeOutputToFile(output) {

    var logFilePath = "c:\\debugging\\output\\output.log";

    var logFile;

    if (host.namespace.Debugger.Utility.FileSystem.FileExists(logFilePath)) {        
        logFile = host.namespace.Debugger.Utility.FileSystem.CreateFile(logFilePath, "OpenExisting");
    }
    else {        
        logFile = host.namespace.Debugger.Utility.FileSystem.CreateFile(logFilePath);
    }

    var textWriter = host.namespace.Debugger.Utility.FileSystem.CreateTextWriter(logFile, "Utf16");

    try {
        for (var line of output) {
            textWriter.WriteLine(line);
        }
    }
    finally {
        logFile.Close();
    }
}

I tried File, Blob and ActiveXObject as suggested throughout the Internet, but none of them works in WinDbg.

You could try a combination of .dvalloc + .writemem + .dvfree. Below is a starting point, but far from complete:

function saveTextAsFile()
{
    var dbgOut = host.diagnostics.debugLog;
    var exec = host.namespace.Debugger.Utility.Control.ExecuteCommand;   
    var output = exec(".dvalloc 0x10000");  
    for (var line of output)
    {
        dbgOut("Output: "+line+"\n");
        var index = line.indexOf("starting at ");
        var address = line.substring(index+("starting at ".length));
        dbgOut("Allocated memory at "+address+"\n");
        exec(".writemem f:\\debug\\logs\\fromscript.txt "+address+" L10000")
        var output = exec(".dvfree " + address + " 0x10000");
        break;
    }
}
Related