forked from hellman/xortool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
args.py
80 lines (66 loc) · 2.52 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
#!/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,
"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"
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:x",
["key-length=", "char=", "spread=", "max-keylen=",
"hex", "help", "usage"])
#TODO: add param "brute all possible freq. chars"
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 ("-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()
except ValueError as err:
raise ArgError(str(err))
if len(arguments) == 1:
PARAMETERS["filename"] = arguments[0]
return