Skip to content

Instantly share code, notes, and snippets.

@mehaase
Last active September 2, 2017 16:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mehaase/781dfbb5bf7f37a75a526d46f0b3c619 to your computer and use it in GitHub Desktop.
Save mehaase/781dfbb5bf7f37a75a526d46f0b3c619 to your computer and use it in GitHub Desktop.
For writing a file to windows over a command shell.
import sys
if len(sys.argv) < 3:
print('Usage: {} <DEST FILE> <INPUT FILE>'.format(sys.argv[0]))
sys.exit(1)
dest_path = sys.argv[1]
in_path = sys.argv[2]
def escape(line):
escape_chars = ['&', '|', '>', '<', '^']
return ''.join(['^' + c if c in escape_chars else c for c in line])
with open(in_path) as in_file:
file_iter = iter(in_file)
line = next(file_iter)
sys.stdout.write('>{} echo {}'.format(dest_path, escape(line)))
for line in file_iter:
if line.strip() != '':
sys.stdout.write('>>{} echo {}'.format(dest_path, escape(line)))
else:
sys.stdout.write('>>{} echo.\n'.format(dest_path))
@mehaase
Copy link
Author

mehaase commented Sep 2, 2017

Example

Convert an FTP script into a series of echo statements that can be executed in a command shell.

root@kali:~# cat test
ftp foo.com
user myuser
pass mypass
get bar.txt
bye
root@kali:~# python winecho.py ftp.txt test
>ftp.txt echo ftp foo.com
>>ftp.txt echo user myuser
>>ftp.txt echo pass mypass
>>ftp.txt echo get bar.txt
>>ftp.txt echo bye

Copy the output and paste it into a shell. It will create ftp.txt with the contents of the original test file.

It escapes Windows shell characters.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment