forked from hellman/xortool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
args.py
99 lines (83 loc) · 3.34 KB
/
args.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env python
#-*- coding:utf-8 -*-
import getopt
from routine import *
class ArgError(Exception):
pass
PARAMETERS = {
"input_is_hex": 0,
"max_key_length": 65,
"known_key_length": None,
"most_frequent_char": None,
"brute_chars": None,
"brute_printable": None,
"frequency_spread": 0,
"filename": "-", # stdin by default
}
def show_usage_and_exit():
print "xortool.py"
print " A tool to do some xor analysis:"
print " - guess the key length (based on count of equal chars)"
print " - guess the key (base on knowledge of most probable char)"
print "Usage:"
print " ", os.path.basename(sys.argv[0]), "[-h|--help] [OPTIONS] [<filename>]"
print "Options:"
print " ", "-l,--key-length length of the key (integer)"
print " ", "-c,--char most possible char (one char or hex code)"
print " ", "-m,--max-keylen=32 maximum key length to probe (integer)"
print " ", "-x,--hex input is hex-encoded str"
print " ", "-b,--brute-chars brute force all possible characters"
print " ", "-o,--brute-printable same as -b but will only use printable"
print " ", " characters for keys"
sys.exit(1)
return
def parse_parameters():
"""
Parse arguments and update PARAMETERS if needed
"""
options, arguments = get_options_and_arguments(sys.argv[1:])
update_parameters(options, arguments)
return PARAMETERS
def get_options_and_arguments(program_arguments):
options, arguments = [], []
try:
options, arguments = getopt.gnu_getopt(program_arguments, "l:c:s:m:xbo",
["key-length=", "char=", "spread=", "max-keylen=",
"hex", "help", "usage","brute-chars", "brute-printable"])
except getopt.GetoptError:
show_usage_and_exit()
return options, arguments
def update_parameters(options, arguments):
global PARAMETERS
try:
for option, value in options:
if option in ("-x", "--hex"):
PARAMETERS["input_is_hex"] = 1
elif option in ("-c", "--char"):
PARAMETERS["most_frequent_char"] = parse_char(value)
elif option in ("-l", "--key-length"):
PARAMETERS["known_key_length"] = int(value)
elif option in ("-b", "--brute-chars"):
PARAMETERS["brute_chars"] = True
elif option in ("-o", "--brute-printable"):
PARAMETERS["brute_printable"] = True
elif option in ("-s", "--spread"):
PARAMETERS["frequency_spread"] = int(value)
elif option in ("-m", "--max-keylen"):
PARAMETERS["max_key_length"] = int(value)
elif option in ("-h", "--help", "--usage"):
show_usage_and_exit()
else:
raise ArgError("Unknown argument: {0}".format(option))
except ValueError as err:
raise ArgError(str(err))
if PARAMETERS["most_frequent_char"] and \
PARAMETERS["brute_printable"] or \
PARAMETERS["most_frequent_char"] and \
PARAMETERS["brute_chars"] or \
PARAMETERS["brute_printable"] and \
PARAMETERS["brute_chars"]:
raise ArgError("Only one out of -c, -b or -o should be used")
if len(arguments) == 1:
PARAMETERS["filename"] = arguments[0]
return