-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathticklbits.py
More file actions
executable file
·196 lines (168 loc) · 6.54 KB
/
ticklbits.py
File metadata and controls
executable file
·196 lines (168 loc) · 6.54 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#! /usr/bin/python2.7
__author__ = 'fuzzynop'
import argparse
import actions
import binascii
import sys
#TODO write this function
def output(args,data):
#do output based on what output type was selected
pass
def get_hex_or_ascii(s):
#-------------------------------------------------------
# get_hex_or_ascii parses a provided string checking if
# the string starts with 'hex'. If it does then the
# following values are treated as a hex string.
# Otherwise it is just the ascii string entered
#-------------------------------------------------------
if s[:3] == 'hex':
try:
value = binascii.a2b_hex(s[3:])
except TypeError as e:
print '*** Not a valid Hex String'
print '*** The value was parsed as hex because the first 3 characters were \'hex\''
print '*** Error Reason:', e.message
sys.exit(1)
else:
value = s
return value
def get_input(args):
#-------------------------------------------------------
# get_input will check the args provided to the program
# and do 1 of 3 things:
# 1. Read the data from a file if -f was used
# 2. Take a string if -s was used
# 3. Read from stdin if neither was provided.
#--------------------------------------------------------
data = ''
if args.file is not None:
data = args.file.read()
elif args.str is not None:
data = get_hex_or_ascii(args.str)
else:
data = sys.stdin.read()
return data
def launch(args):
#--------------|
# - xor -|
#--------------|
if args.action == 'xor':
key = get_hex_or_ascii(args.key)
data = get_input(args)
result = actions.xor_against(key, data)
#TODO make this output different formats using output flags
print repr(result)[1:-1]
return result
#--------------|
# - b64 -|
#--------------|
elif args.action == 'b64':
data = get_input(args)
if args.encode is True:
#do b64encode
result = actions.b64_enc(data)
elif args.decode is True:
#do b64decode (try)
result = actions.b64_dec(data)
else:
result = actions.b64_dec(data)
#if none are true default action is going to be to decode
#wrtiting seperately incase i want to change this later.
print result
return result
#--------------|
# - add -|
#--------------|
elif args.action == 'add':
data = get_input(args)
result = actions.adjust_bytes(data, args.amount)
print result
#--------------|
# - csr -|
#--------------|
elif args.action == 'csr':
data = get_input(args)
result = actions.caeser_cipher(data, args.amount)
print result
#--------------|
# - hex -|
#--------------|
elif args.action == 'hex':
data = get_input(args)
print args
if args.asciitohex is True:
#take ascii and make it hex
result = actions.hex_to_hexstring(data)
elif args.hextoascii is True:
#take hex and form ascii from it
result = actions.hexstring_to_hex(data)
else:
result = actions.hex_to_hexstring(data)
print repr(result)
return result
#--------------|
# - mfb -|
#--------------|
elif args.action == 'mfb':
data = get_input(args)
result = actions.most_frequent_byte(data)
for i in range(0, 5):
print repr(result[i][0]), ':', result[i][1]
def start_parser():
#This function is responsible for parsing arguments
parser = argparse.ArgumentParser()
#----------------------------------
# Input Group for possible ways
# to input data to script
#----------------------------------
input_group = parser.add_mutually_exclusive_group()
#TODO: if file specified use file, is string specified use string, if none specified read from STDIN want to do things like cat * | ticklbits.py xor "aaa"
input_group.add_argument("-f", "--file", help="File as input", type=file)
input_group.add_argument("-s", "--str", help="String as input", type=str)
#---------------------------------
# Output group for possible ways
# to output the data
#---------------------------------
#output_group = parser.add_mutually_exclusive_group()
#-----------------------------------
# Sub Parsers for each action set
#-----------------------------------
subparsers = parser.add_subparsers(dest='action', help='test')
#------------------|
# xor parser |
#------------------|
pars_xor = subparsers.add_parser('xor', help='xor (XOR Operation) --help,-h')
pars_xor.add_argument('key', help="Key is ascii, if you want hex prepend 'hex', ie. hexFF00FA11")
pars_xor.add_argument('--offset', type=int, help="Offset to start XOR from")
#------------------|
# b64 parser |
#------------------|
pars_b64 = subparsers.add_parser('b64', help='b64 (Base 64 Encode or Decode) --help,-h')
b64_group = pars_b64.add_mutually_exclusive_group()
b64_group.add_argument("-e", "--encode", action='store_true', help="Base64 Encode, (Default)")
b64_group.add_argument("-d", "--decode", action='store_true', help="Base64 Decode, must have Valid Base64 String")
#------------------|
# add parser |
#------------------|
pars_add = subparsers.add_parser('add', help='add (Add or Subtract from Each Byte) --help,-h')
pars_add.add_argument('amount', type=int, help="Amount to change each byte by, can be negative")
#-------------------|
#(csr) caeser parser|
#-------------------|
pars_csr = subparsers.add_parser('csr', help='csr (Caeser Cipher) --help,-h')
pars_csr.add_argument('amount', type=int, help='Amount to rotate alphabet, can be negative')
pars_csr.add_argument('-k', '--key', type=str, help='Key to use in keyed Caeser Cipher, changes alphabet used')
#-------------------|
# hex parser |
#-------------------|
pars_hex = subparsers.add_parser('hex', help='hex (Ascii <-> Hex) --help,-h')
pars_hex.add_argument('-a2h', '--asciitohex', action='store_true', help='Convert ascii to hex')
pars_hex.add_argument('-h2a', '--hextoascii', action='store_true', help='Convert hex to ascii')
#-------------------|
# mfb parser |
# most frequent byte|
#-------------------|
pars_mfb = subparsers.add_parser('mfb', help='mfb (Most Frequent Byte) --help,-h')
return parser.parse_args()
args = start_parser()
launch(args)