Skip to content

Instantly share code, notes, and snippets.

@byt3bl33d3r
Forked from binkybear/duckhunter.py
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save byt3bl33d3r/922955b78ecd4e68627b to your computer and use it in GitHub Desktop.
Save byt3bl33d3r/922955b78ecd4e68627b to your computer and use it in GitHub Desktop.
#! /usr/bin/env python
import sys
import re
import os
from decimal import Decimal #for conversion milliseconds -> seconds
if len(sys.argv) < 2:
print 'Usage: duck-hunter.py <duckyscript> output.txt'
#print 'Usage: duck-hunter.py <duckyscript> <language> output.txt'
sys.exit()
# Input file is argument / output file is output.txt
infile = open(sys.argv[1])
tmpfile = open("tmp.txt", "w")
#locale = sys.argv[2]
def duckyRules (source):
tmpfile = source
for (k,v) in rules.items():
regex = re.compile(k)
tmpfile = regex.sub(v, tmpfile)
return tmpfile
if __name__ == "__main__":
rules = {
r'ALT' : u'alt',
r'GUI' : 'left-meta',
r'WINDOWS' : 'left-meta',
r'ALT' : 'alt',
r'CONTROL' : 'left-ctrl',
r'CTRL' : 'left-ctrl',
r'SHIFT' : 'left-shift',
r'MENU' : 'left-shift f10',
r'APP' : 'escape',
r'ESCAPE' : 'escape',
r'ESC' : 'esc',
r'END' : 'end',
r'SPACE' : 'space',
r'TAB' : 'tab',
r'PRINTSCREEN' : 'print',
r'ENTER' : 'enter',
r'UPARROW' : 'up',
r'UP' : 'up',
r'DOWNARROW' : 'down',
r'DOWN' : 'down',
r'LEFTARROW' : 'left',
r'LEFT' : 'left',
r'RIGHTARROW' : 'right',
r'RIGHT' : 'right',
r'CAPSLOCK' : 'capslock',
r'F1' : 'f1',
r'F2' : 'f2',
r'F3' : 'f3',
r'F4' : 'f4',
r'F5' : 'f5',
r'F6' : 'f6',
r'F7' : 'f7',
r'F8' : 'f8',
r'F9' : 'f9',
r'F10' : 'f10',
r'DELETE' : 'delete',
r'INSERT' : 'insert',
r'DELAY' : 'sleep',
r'DEFAULT_DELAY' : '"',
r'REPEAT' : '"'}
# For general keyboard commands
prefix = "print '''"
suffix = " | hid-keyboard /dev/hidg0 keyboard'''"
# Process input text
prefixinput = "print '''echo -ne "
prefixoutput = " > /dev/hidg0 '''"
with infile as text:
new_text = duckyRules(text.read())
infile.close()
# Write regex to tmp file
with tmpfile as result:
result.write(new_text)
tmpfile.close()
src = open('tmp.txt', 'r')
dest = open('output.txt', 'w')
for line in src:
if line.startswith('sleep'):
line = line.split()
seconds = (Decimal(line[1]) / Decimal(1000)) % 60
line[1] = str(seconds)
line = ' '.join(line)
dest.write('%s%s%s\n' % (prefix, line.rstrip('\n').strip(), suffix))
elif line.startswith('REM'):
line = '#' + line.rstrip('\n').strip('REM')
dest.write('%s\n' % line.rstrip('\n').strip())
elif line.startswith('STRING'):
line = line.strip('STRING ')
for char in line:
line = '\\' + str(hex(ord(char)))[1:]
dest.write('%s%s%s\n' % (prefixinput, line.rstrip('\n').strip(), prefixoutput))
else:
dest.write('%s%s%s\n' % (prefix, line.rstrip('\n').strip(), suffix))
print "File saved to output.txt"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment