Created
February 2, 2015 02:45
-
-
Save binkybear/6a16c93c7602f25b33cc to your computer and use it in GitHub Desktop.
Duck Hunter (In progress)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #! /usr/bin/env python | |
| import sys | |
| import re | |
| import os | |
| 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 findinlist(byte, locale): | |
| dict_us = { | |
| # Symbols | |
| "\x20": "\\x00\\x00\\x00\\x2c\\x00\\x00\\x00\\x00", | |
| "\x21": "\\x20\\x00\\x00\\x1e\\x00\\x00\\x00\\x00", | |
| "\x22": "\\x20\\x00\\x00\\x34\\x00\\x00\\x00\\x00", | |
| "\x23": "\\x20\\x00\\x00\\x20\\x00\\x00\\x00\\x00", | |
| "\x24": "\\x20\\x00\\x00\\x21\\x00\\x00\\x00\\x00", | |
| "\x25": "\\x20\\x00\\x00\\x22\\x00\\x00\\x00\\x00", | |
| "\x26": "\\x20\\x00\\x00\\x24\\x00\\x00\\x00\\x00", | |
| "\x27": "\\x00\\x00\\x00\\x34\\x00\\x00\\x00\\x00", | |
| "\x28": "\\x20\\x00\\x00\\x26\\x00\\x00\\x00\\x00", | |
| "\x29": "\\x20\\x00\\x00\\x27\\x00\\x00\\x00\\x00", | |
| "\x2a": "\\x20\\x00\\x00\\x25\\x00\\x00\\x00\\x00", | |
| "\x2b": "\\x20\\x00\\x00\\x2e\\x00\\x00\\x00\\x00", | |
| "\x2c": "\\x00\\x00\\x00\\x36\\x00\\x00\\x00\\x00", | |
| "\x2d": "\\x00\\x00\\x00\\x2d\\x00\\x00\\x00\\x00", | |
| "\x2e": "\\x00\\x00\\x00\\x37\\x00\\x00\\x00\\x00", | |
| "\x2f": "\\x00\\x00\\x00\\x38\\x00\\x00\\x00\\x00", | |
| # Numbers | |
| "\x30": "\\x00\\x00\\x00\\x27\\x00\\x00\\x00\\x00", | |
| "\x31": "\\x00\\x00\\x00\\x1e\\x00\\x00\\x00\\x00", | |
| "\x32": "\\x00\\x00\\x00\\x1f\\x00\\x00\\x00\\x00", | |
| "\x33": "\\x00\\x00\\x00\\x20\\x00\\x00\\x00\\x00", | |
| "\x34": "\\x00\\x00\\x00\\x21\\x00\\x00\\x00\\x00", | |
| "\x35": "\\x00\\x00\\x00\\x22\\x00\\x00\\x00\\x00", | |
| "\x36": "\\x00\\x00\\x00\\x23\\x00\\x00\\x00\\x00", | |
| "\x37": "\\x00\\x00\\x00\\x24\\x00\\x00\\x00\\x00", | |
| "\x38": "\\x00\\x00\\x00\\x25\\x00\\x00\\x00\\x00", | |
| "\x39": "\\x00\\x00\\x00\\x26\\x00\\x00\\x00\\x00", | |
| # Symbols | |
| "\x3a": "\\x20\\x00\\x00\\x33\\x00\\x00\\x00\\x00", | |
| "\x3b": "\\x00\\x00\\x00\\x33\\x00\\x00\\x00\\x00", | |
| "\x3c": "\\x20\\x00\\x00\\x36\\x00\\x00\\x00\\x00", | |
| "\x3d": "\\x00\\x00\\x00\\x2e\\x00\\x00\\x00\\x00", | |
| "\x3e": "\\x20\\x00\\x00\\x37\\x00\\x00\\x00\\x00", | |
| "\x3f": "\\x20\\x00\\x00\\x38\\x00\\x00\\x00\\x00", | |
| "\x40": "\\x20\\x00\\x00\\x1f\\x00\\x00\\x00\\x00", | |
| # Uppercase | |
| "\x41": "\\x20\\x00\\x00\\x04\\x00\\x00\\x00\\x00", | |
| "\x42": "\\x20\\x00\\x00\\x05\\x00\\x00\\x00\\x00", | |
| "\x43": "\\x20\\x00\\x00\\x06\\x00\\x00\\x00\\x00", | |
| "\x44": "\\x20\\x00\\x00\\x07\\x00\\x00\\x00\\x00", | |
| "\x45": "\\x20\\x00\\x00\\x08\\x00\\x00\\x00\\x00", | |
| "\x46": "\\x20\\x00\\x00\\x09\\x00\\x00\\x00\\x00", | |
| "\x47": "\\x20\\x00\\x00\\x0a\\x00\\x00\\x00\\x00", | |
| "\x48": "\\x20\\x00\\x00\\x0b\\x00\\x00\\x00\\x00", | |
| "\x49": "\\x20\\x00\\x00\\x0c\\x00\\x00\\x00\\x00", | |
| "\x4a": "\\x20\\x00\\x00\\x0d\\x00\\x00\\x00\\x00", | |
| "\x4b": "\\x20\\x00\\x00\\x0e\\x00\\x00\\x00\\x00", | |
| "\x4c": "\\x20\\x00\\x00\\x0f\\x00\\x00\\x00\\x00", | |
| "\x4d": "\\x20\\x00\\x00\\x10\\x00\\x00\\x00\\x00", | |
| "\x4e": "\\x20\\x00\\x00\\x11\\x00\\x00\\x00\\x00", | |
| "\x4f": "\\x20\\x00\\x00\\x12\\x00\\x00\\x00\\x00", | |
| "\x50": "\\x20\\x00\\x00\\x13\\x00\\x00\\x00\\x00", | |
| "\x51": "\\x20\\x00\\x00\\x14\\x00\\x00\\x00\\x00", | |
| "\x52": "\\x20\\x00\\x00\\x15\\x00\\x00\\x00\\x00", | |
| "\x53": "\\x20\\x00\\x00\\x16\\x00\\x00\\x00\\x00", | |
| "\x54": "\\x20\\x00\\x00\\x17\\x00\\x00\\x00\\x00", | |
| "\x55": "\\x20\\x00\\x00\\x18\\x00\\x00\\x00\\x00", | |
| "\x56": "\\x20\\x00\\x00\\x19\\x00\\x00\\x00\\x00", | |
| "\x57": "\\x20\\x00\\x00\\x1a\\x00\\x00\\x00\\x00", | |
| "\x58": "\\x20\\x00\\x00\\x1b\\x00\\x00\\x00\\x00", | |
| "\x59": "\\x20\\x00\\x00\\x1c\\x00\\x00\\x00\\x00", | |
| "\x5a": "\\x20\\x00\\x00\\x1d\\x00\\x00\\x00\\x00", | |
| # Symbols | |
| "\x5b": "\\x00\\x00\\x00\\x2f\\x00\\x00\\x00\\x00", | |
| "\x5c": "\\x00\\x00\\x00\\x31\\x00\\x00\\x00\\x00", | |
| "\x5d": "\\x00\\x00\\x00\\x30\\x00\\x00\\x00\\x00", | |
| "\x5e": "\\x20\\x00\\x00\\x23\\x00\\x00\\x00\\x00", | |
| "\x5f": "\\x00\\x00\\x00\\x2d\\x00\\x00\\x00\\x00", | |
| "\x60": "\\x00\\x00\\x00\\x35\\x00\\x00\\x00\\x00", | |
| # Lowercase | |
| "\x61": "\\x00\\x00\\x00\\x04\\x00\\x00\\x00\\x00", | |
| "\x62": "\\x00\\x00\\x00\\x05\\x00\\x00\\x00\\x00", | |
| "\x63": "\\x00\\x00\\x00\\x06\\x00\\x00\\x00\\x00", | |
| "\x64": "\\x00\\x00\\x00\\x07\\x00\\x00\\x00\\x00", | |
| "\x65": "\\x00\\x00\\x00\\x08\\x00\\x00\\x00\\x00", | |
| "\x66": "\\x00\\x00\\x00\\x09\\x00\\x00\\x00\\x00", | |
| "\x67": "\\x00\\x00\\x00\\x0a\\x00\\x00\\x00\\x00", | |
| "\x68": "\\x00\\x00\\x00\\x0b\\x00\\x00\\x00\\x00", | |
| "\x69": "\\x00\\x00\\x00\\x0c\\x00\\x00\\x00\\x00", | |
| "\x6a": "\\x00\\x00\\x00\\x0d\\x00\\x00\\x00\\x00", | |
| "\x6b": "\\x00\\x00\\x00\\x0e\\x00\\x00\\x00\\x00", | |
| "\x6c": "\\x00\\x00\\x00\\x0f\\x00\\x00\\x00\\x00", | |
| "\x6d": "\\x00\\x00\\x00\\x10\\x00\\x00\\x00\\x00", | |
| "\x6e": "\\x00\\x00\\x00\\x11\\x00\\x00\\x00\\x00", | |
| "\x6f": "\\x00\\x00\\x00\\x12\\x00\\x00\\x00\\x00", | |
| "\x70": "\\x00\\x00\\x00\\x13\\x00\\x00\\x00\\x00", | |
| "\x71": "\\x00\\x00\\x00\\x14\\x00\\x00\\x00\\x00", | |
| "\x72": "\\x00\\x00\\x00\\x15\\x00\\x00\\x00\\x00", | |
| "\x73": "\\x00\\x00\\x00\\x16\\x00\\x00\\x00\\x00", | |
| "\x74": "\\x00\\x00\\x00\\x17\\x00\\x00\\x00\\x00", | |
| "\x75": "\\x00\\x00\\x00\\x18\\x00\\x00\\x00\\x00", | |
| "\x76": "\\x00\\x00\\x00\\x19\\x00\\x00\\x00\\x00", | |
| "\x77": "\\x00\\x00\\x00\\x1a\\x00\\x00\\x00\\x00", | |
| "\x78": "\\x00\\x00\\x00\\x1b\\x00\\x00\\x00\\x00", | |
| "\x79": "\\x00\\x00\\x00\\x1c\\x00\\x00\\x00\\x00", | |
| "\x7a": "\\x00\\x00\\x00\\x1d\\x00\\x00\\x00\\x00", | |
| #Shift chars | |
| "\x7b": "\\x20\\x00\\x00\\x2f\\x00\\x00\\x00\\x00", | |
| "\x7c": "\\x20\\x00\\x00\\x31\\x00\\x00\\x00\\x00", | |
| "\x7d": "\\x20\\x00\\x00\\x30\\x00\\x00\\x00\\x00", | |
| "\x7e": "\\x00\\x00\\x00\\x40\\x00\\x00\\x00\\x00", | |
| #SDLK_RETURN,0x28 | |
| "\x0a": "\\x00\\x00\\x00\\x28\\x00\\x00\\x00\\x00", | |
| "\x0d": "\\x00\\x00\\x00\\x28\\x00\\x00\\x00\\x00" | |
| } | |
| if locale=="us" : print '''echo -ne "''' +dict_us[byte]+ '''" > /dev/hidg0''' | |
| elif locale=="fr" : print '''echo -ne "''' +dict_fr[byte]+ '''" > /dev/hidg0''' | |
| elif locale=="de" : print '''echo -ne "''' +dict_de[byte]+ '''" > /dev/hidg0''' | |
| elif locale=="es" : print '''echo -ne "''' +dict_es[byte]+ '''" > /dev/hidg0''' | |
| elif locale=="sv" : print '''echo -ne "''' +dict_sv[byte]+ '''" > /dev/hidg0''' | |
| elif locale=="it" : print '''echo -ne "''' +dict_it[byte]+ '''" > /dev/hidg0''' | |
| elif locale=="uk" : print '''echo -ne "''' +dict_uk[byte]+ '''" > /dev/hidg0''' | |
| elif locale=="ru" : print '''echo -ne "''' +dict_ru[iso_ru[byte]]+ '''" > /dev/hidg0''' | |
| elif locale=="dk" : print '''echo -ne "''' +dict_dk[byte]+ '''" > /dev/hidg0''' | |
| elif locale=="no" : print '''echo -ne "''' +dict_no[byte]+ '''" > /dev/hidg0''' | |
| elif locale=="pt" : print '''echo -ne "''' +dict_pt[byte]+ '''" > /dev/hidg0''' | |
| elif locale=="be" : print '''echo -ne "''' +dict_be[byte]+ '''" > /dev/hidg0''' | |
| #else: print "#crap, couldn't find ["+byte +"]. Perhaps try adding it to the list." | |
| print '''echo -ne "\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00" > /dev/hidg0''' | |
| def duckyRules (source): | |
| tmpfile = source | |
| #StringMatch = re.match("STRING(.+)\n", tmpfile) | |
| #StringCut = StringMatch[6:-1] | |
| #for byte in StringCut | |
| # findinlist(byte, locale) | |
| rules = [ | |
| { r'REM' : u'#' }, | |
| { r'ALT' : u'alt'}, | |
| { r'GUI r' : '\\x08\\x00\\x00\\x00\\x00\\x00\\x00\\x00' }, | |
| { r'WINDOWS' : '\\x08\\x00\\x00\\x00\\x00\\x00\\x00\\x00' }, | |
| { 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'^STRING' : '' }, | |
| { r'DELAY' : 'sleep' }, | |
| { r'DEFAULT_DELAY' : '"' }, | |
| { r'REPEAT' : '"' }, | |
| ] | |
| for rule in rules: | |
| for (k,v) in rule.items(): | |
| regex = re.compile (k) | |
| tmpfile = regex.sub (v, tmpfile) | |
| return tmpfile | |
| if __name__ == "__main__": | |
| # 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() | |
| with open('tmp.txt', 'r') as src: | |
| with open('output.txt', 'w') as dest: | |
| for line in src: | |
| dest.write('%s%s%s\n' % (prefix, line.rstrip('\n'), 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