Windows – How to execute a batch file in an alternate data stream in Windows 7

alternate-data-streambatchntfswindows 7

Normally, after creating an executable as an alternate data stream, for instance:

type exec_this.bat > C:\blank.txt:exe.bat

one could simply say

start C:\blank.txt:exe.bat

to run the executable. In Windows 7, all I seem to get is "Access is denied." or "The system cannot find the file $WHATEVER_THE_FILE_IS."

How can I execute this batch file?

Best Answer

The accepted answer only runs one line of the batch file at a time. This will interrupt any advanced logic in the batch file. But, if you use a temporary file you can do this in a one-liner:

cat < blank.txt:exe.bat > temp.bat & temp.bat

You can also execute a PowerShell script in an alternate data stream via a similar command: (Though I am sure there is a pure PowerShell way too.)

cat < blank.txt:exe.ps1 > temp.ps1 & powershell .\temp.ps1

Example

Create batch in the alternate data stream: notepad blank.txt:exe.bat

if 6==7 then goto pie 
echo apple 
goto end 
:pie 
echo pie 
:end 

Broken:

E:\broken>for /f "usebackq delims=φ" %i in (blank.txt:exe.bat) do %i

E:\broken>if 6==7 then goto pie
'if' is not recognized as an internal or external command,
operable program or batch file.

E:\broken>echo apple
apple

E:\broken>goto end

E:\broken>
The filename, directory name, or volume label syntax is incorrect.

E:\broken>echo pie
pie

E:\broken>
The filename, directory name, or volume label syntax is incorrect.

Better:

E:\better>cat < blank.txt:exe.bat > temp.bat & temp.bat

E:\better>if 6 == 7 then goto pie

E:\better>echo apple
apple

E:\better>goto end