-
Notifications
You must be signed in to change notification settings - Fork 0
/
state.py
145 lines (121 loc) · 3.72 KB
/
state.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
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
import abc
import string
from autodbg.utils import prefix_iterator
class State(metaclass=abc.ABCMeta):
def next(self):
pass
def back(self):
pass
def forward(self):
pass
class NumState(State):
def __init__(self):
super().__init__()
def back(self):
pass
def forward(self):
pass
class StrState(State):
def __init__(self, length, prefix='', suffix='', charset=None, padding='') -> None:
"""
Args:
length (int): flag 的长度
prefix (str): 初始 flagF
charset (str): 字符集
padding (str): 默认填充
"""
self.raw_length = length-len(prefix)-len(suffix)
self.charset = string.printable if charset == None else charset
self.padding = self.charset[0] if padding == '' else padding
self.prefix = prefix
self.suffix = suffix
self._raw = self.raw_length*self.padding
self.iterator = prefix_iterator(self.raw_length, self.charset)
def pad(self, raw):
return "{}{}{}".format(self.prefix, raw, self.suffix)
@property
def current(self):
return self.pad(self._raw)
def next(self,):
try:
# iterate
self._raw = next(self.iterator)
# join
except StopIteration:
# 没有路时回退
raise Exception("curt_depth < 0, can't trace back anymore!!!")
def forward(self, n):
self.prefix += self._raw[:n]
self.raw_length -= n
self.iterator = prefix_iterator(self.raw_length, self.charset)
def back(self, n):
self.prefix[:len(self.prefix)-n]
self.raw_length += n
self.iterator = prefix_iterator(self.raw_length, self.charset)
class CharState(State):
def __init__(self, length, prefix='', suffix='', charset=None, padding='') -> None:
"""
Args:
length (int): flag 的长度
prefix (str): 初始 flagF
charset (str): 字符集
padding (str): 默认填充
"""
# length
self.raw_length = length - len(prefix) - len(suffix)
# charset
self.charset = string.printable if not charset else charset
# default padding
self.padding = charset[0] if not padding else padding
# prefix & suffix
self.prefix = prefix
self.suffix = suffix
# iterator
self.depth = 0
# vertex
self.vertexes = [iter(self.charset)] * self.raw_length
# brute chars
self.brute_chars = [self.padding] * self.raw_length
self.raw = self.padding*self.raw_length
def pad(self, raw):
return "{}{}{}".format(self.prefix, raw, self.suffix)
@property
def current(self):
return self.pad(self.raw)
def next(self) -> str:
"""返回当前 str + 迭代的 char"""
try:
# iterate
self.brute_chars[self.depth] = next(self.vertexes[self.depth])
# join
self.raw = ''.join(self.brute_chars)
except StopIteration:
# 没有路时回退
self.back(1)
if self.depth < 0:
raise Exception("curt_depth < 0, can't trace back anymore!!!")
else:
return self.next()
def back(self, n):
self.brute_chars[self.depth] = self.padding
# print(self.brute_chars)
self.depth -= n
def forward(self, n):
self.depth += n
self.vertexes[self.depth] = iter(self.charset)
# if __name__ == '__main__':
# # suggest that the desired flag is flag{233}
# fs = StrState(9, "flag{", suffix='}', charset='123', padding=' ')
# condition = iter(["flag{2", "flag{23", "flag{233}"])
# current = next(condition)
# while True:
# fs.next()
# f = fs.current
# print(f)
# if f.startswith(current):
# fs.forward(1)
# try:
# current = next(condition)
# except:
# print("You got the flag")
# break