pxeger
view Revexpy.py @ 1:cf3c32071eb0
Added payloads.py file
| author | martin@fujitsux |
|---|---|
| date | Fri Nov 20 16:12:01 2009 +0100 (8 months ago) |
| parents | |
| children |
line source
1 '''
2 Created on Oct 26, 2009
4 @author: martin
5 '''
6 from compiler.pycodegen import TRY_FINALLY
7 import sys,traceback
8 import sre_parse
9 from sre_constants import *
10 import re
11 import _sre
13 _LITERAL_CODES = set([LITERAL, NOT_LITERAL])
14 _REPEATING_CODES = set([REPEAT, MIN_REPEAT, MAX_REPEAT])
15 _SUCCESS_CODES = set([SUCCESS, FAILURE])
16 _ASSERT_CODES = set([ASSERT, ASSERT_NOT])
18 lookuptable = {
19 CATEGORY_DIGIT : 48, # '0' char
20 CATEGORY_NOT_DIGIT: 65, # 'A' char
21 CATEGORY_SPACE : 32, # space
22 CATEGORY_NOT_SPACE: 65, # 'A' char
23 CATEGORY_WORD: 65, # 'A' char
24 CATEGORY_NOT_WORD: 33, # ! char
25 CATEGORY_LINEBREAK:12, # linebreak
26 CATEGORY_NOT_LINEBREAK: 65, # 'A' char
27 CATEGORY_LOC_WORD: 65, # 'A' char
28 CATEGORY_LOC_NOT_WORD: 33, # ! char
29 CATEGORY_UNI_DIGIT : 48, # '0' char
30 CATEGORY_UNI_NOT_DIGIT: 65, # 'A' char
31 CATEGORY_UNI_SPACE : 32, # space
32 CATEGORY_UNI_NOT_SPACE: 65, # 'A' char
33 CATEGORY_UNI_WORD: 65, # 'A' char
34 CATEGORY_UNI_NOT_WORD: 33, # ! char
35 CATEGORY_UNI_LINEBREAK:12, # linebreak
36 CATEGORY_UNI_NOT_LINEBREAK: 65 # 'A' char
37 }
40 class OutputContainer:
41 def __init__(self):
42 self._repeat = 1
43 self._outputchars = []
45 def getChars(self):
46 return self._outputchars
48 def append(self, _obj):
49 # print "APPEND: %s" % _obj
50 #Are we dealing with another outputcontainer?
51 if hasattr(_obj, "getChars"):
52 # print "Adding char %s %s times" % (_obj, self._repeat)
53 chars = _obj.getChars()
54 while(self._repeat > 0):
55 self._outputchars.extend(chars)
56 self._repeat = self._repeat - 1
57 else:
58 # print "Adding char %s %s times" % (_obj, self._repeat)
59 while(self._repeat > 0):
60 #Otherwise, just a char
61 self._outputchars.append(_obj)
62 self._repeat = self._repeat - 1
64 self._repeat = 1
65 def out(self, arg):
66 pass
67 # print "debug %s" % arg
68 def repeat(self, arg):
69 self._repeat = arg
70 def toPythonDeclaration(self):
71 str = "["
72 for char in self._outputchars:
73 str += "%s," % char
74 # if char > 31 and char < 127:
75 # str += chr(char)
76 # else :
77 # str += "\\x%s" % hex(char)
78 str += "]"
79 return str
81 def __str__(self):
82 str = ""
83 for char in self._outputchars:
84 str += chr(char)
85 return str
87 def dbg(arg):
88 pass
89 #print "debug: %s" % arg
90 def getNot(arg):
91 for char in range(255):
92 for notchar in arg:
93 if char not in arg: return char
95 def _simple(av):
96 # check if av is a "simple" operator
97 lo, hi = av[2].getwidth()
98 if lo == 0 and hi == MAXREPEAT:
99 raise error, "nothing to repeat"
100 return lo == hi == 1 and av[2][0][0] != SUBPATTERN
102 def _reverse_in(inspecifier):
103 out = OutputContainer()
104 #print "IN: %s" % inspecifier
105 invert = None
106 for op, av in inspecifier:
107 if op is NEGATE:
108 #print "NOT"
109 invert = []
110 elif op is LITERAL:
111 if invert is not None:
112 invert.append(av)
113 else:
114 out.append(av)
115 break
116 elif op is RANGE:
117 if invert is not None:
118 invert.append(av[0])
119 else:
120 out.append(av[0])
121 break
122 elif op is CATEGORY:
123 if invert is not None:
124 invert.append(lookuptable[av])
125 else:
126 out.append(lookuptable[av])
127 break
128 else:
129 raise error, "internal: unsupported set operator"
130 if invert is not None:
131 #print "Not: %s" % invert
132 valid = getNot(invert)
133 #print "GetNot: %s" % valid
134 out.append(valid)
136 return out
137 def reverse(pattern):
138 dbg(pattern)
139 LITERAL_CODES = _LITERAL_CODES
140 REPEATING_CODES = _REPEATING_CODES
141 SUCCESS_CODES = _SUCCESS_CODES
142 ASSERT_CODES = _ASSERT_CODES
143 flags = 0
144 out = OutputContainer()
145 emit = out.out
146 for op, av in pattern:
147 if op is LITERAL:
148 out.append(av)
149 elif op is NOT_LITERAL:
150 out.append(getNot([av]))
151 elif op is IN:
152 out.append(_reverse_in(av))
153 elif op is ANY:
154 out.append(lookuptable[CATEGORY_DIGIT])
155 elif op in REPEATING_CODES:
156 dbg("REPEATING_CODES")
157 if flags & SRE_FLAG_TEMPLATE:
158 raise error, "internal: unsupported template operator"
159 elif _simple(av) and op is not REPEAT:
160 out.repeat(av[0])
161 subpattern = reverse(av[2])
162 out.append(subpattern)
163 else:
164 print "NOT IMPL not _simple(av) in REPEATING CODES"
165 elif op is SUBPATTERN:
166 if av[0]:
167 subpattern = reverse(av[1])
168 out.append(subpattern)
169 elif op in SUCCESS_CODES:
170 emit(OPCODES[op])
172 elif op is CALL:
173 print "NOT IMPL CALL"
174 dbg("op is CALL")
175 elif op is AT:
176 pass
177 elif op is BRANCH:
178 dbg("op is BRANCH")
179 subpattern = reverse(av[1][0])
180 out.append(subpattern)
181 elif op is CATEGORY:
182 print "op is CATEGORY"
183 emit(lookuptable[av])
184 elif op is GROUPREF:
185 print "NOT IMPL Groupref"
186 dbg("op is GROUPREF")
187 elif op is GROUPREF_EXISTS:
188 print "NOT IMPL Groupref exists"
189 dbg("op is GROUPREF_EXISTS")
190 else:
191 raise ValueError, ("unsupported operand type", op)
192 return out
195 def doReversing(p):
196 # p = 'ab*de.gh+i{10}'
197 # p = re.compile()
198 dbg("Pattern:" + p)
199 pattern = sre_parse.parse(p, 0)
200 out = reverse(pattern)
201 return out
203 if __name__ == '__main__':
204 # line = "ab*de[^a-Z]...";
205 # result = doReversing(line)
206 # if re.match(line, result):
207 # print "%s\n --OK-> \n%s" % (line,result)
208 # else:
209 # print "%s\n --FAIL!-> \n%s" % (line, result)
211 file = open("payloads.py", 'w')
212 file.write("payloads =[\n")
213 first = True
214 strippedlines = (l.rstrip() for l in sys.stdin.readlines())
215 for line in strippedlines:
216 if not line.startswith("#") and len(line) > 0:
217 try:
218 result = doReversing(line)
219 pattern = re.compile(line);
220 if re.match(line, str(result)):
221 if not first: file.write(",\n")
222 first = False
223 file.write(" ")
224 file.write(result.toPythonDeclaration())
225 else:
226 print "%s\n --FAIL!-> \n%s" % (line, str(result))
227 except :
228 print "%s\n --Exception-> " % line
229 traceback.print_exc(file=sys.stdout)
231 file.write("]")
232 file.close()
