-
Notifications
You must be signed in to change notification settings - Fork 4
/
asn1.py
executable file
·173 lines (129 loc) · 5 KB
/
asn1.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
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
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import os
import sys
from Peach.analyzer import *
from Peach.Engine.dom import *
from Peach.Engine.common import *
try:
from pyasn1.type import univ
import pyasn1.codec.ber.decoder
import pyasn1.codec.cer.decoder
import pyasn1.codec.der.decoder
import pyasn1.codec.ber.encoder
import pyasn1.codec.cer.encoder
import pyasn1.codec.der.encoder
except:
#raise PeachException("Error loading pyasn1 library. This library\ncan be installed from the dependencies folder.\n\n")
pass
class Asn1Analyzer(Analyzer):
"""
Produces data models or peach pits from XML documents.
"""
#: Does analyzer support asDataElement()
supportDataElement = True
#: Does analyzer support asCommandLine()
supportCommandLine = False
#: Does analyzer support asTopLevel()
supportTopLevel = True
def __init__(self):
pass
def analyzeAsn1(self, codec, data):
decoder = eval("pyasn1.codec.%s.decoder" % codec)
asn1Obj = decoder.decode(data)[0]
return self.Asn12Peach(codec, asn1Obj)
def Asn12Peach(self, codec, asn1Obj):
obj = Asn1Type(None, None)
obj.asn1Type = asn1Obj.__class__.__name__
obj.encodeType = codec
obj.asnTagSet = None #asn1Obj._tagSet
obj.asn1Spec = None # asn1Obj._asn1Spec
if hasattr(asn1Obj, "_value"):
value = asn1Obj._value
obj.objType = type(value)
if type(value) == long or type(value) == int:
n = Number(None, None)
n.defaultValue = str(value)
n.size = 32
obj.append(n)
elif type(value) == str:
# Could be blob or string...hmmm
# Sometimes we have ASN.1 inside of ASN.1
# most common for OctetString type
if asn1Obj.__class__.__name__ == 'OctetString':
try:
decoder = eval("pyasn1.codec.%s.decoder" % codec)
subAsn1 = decoder.decode(asn1Obj._value)[0]
child = self.Asn12Peach(codec, subAsn1)
b = Block(None, None)
b.append(child)
except:
b = Blob(None, None)
b.defaultValue = value
else:
b = Blob(None, None)
b.defaultValue = value
obj.append(b)
elif type(value) == tuple:
# Probably and ObjectIdentifier!
if asn1Obj.__class__.__name__ == 'ObjectIdentifier':
oid = []
for i in value:
oid.append(str(i))
b = String(None, None)
b.defaultValue = ".".join(oid)
obj.append(b)
elif asn1Obj.__class__.__name__ == 'BitString':
# Make this a blob
b = Blob(None, None)
encoder = eval("pyasn1.codec.%s.encoder" % codec)
b.defaultValue = encoder.encode(asn1Obj)[4:]
obj.append(b)
else:
print("UNKNOWN TUPLE TYPE")
print(asn1Obj.__class__.__name__)
print(value)
raise Exception("foo")
if hasattr(asn1Obj, "_componentValues"):
for c in asn1Obj._componentValues:
child = self.Asn12Peach(codec, c)
obj.append(child)
return obj
def asDataElement(self, parent, args, dataBuffer):
"""
Called when Analyzer is used in a data model.
Should return a DataElement such as Block, Number or String.
"""
dom = self.analyzeAsn1("der", dataBuffer)
# Replace parent with new dom
dom.name = parent.name
parentOfParent = parent.parent
indx = parentOfParent.index(parent)
del parentOfParent[parent.name]
parentOfParent.insert(indx, dom)
# now just cross our fingers :)
def asCommandLine(self, args):
"""
Called when Analyzer is used from command line. Analyzer
should produce Peach PIT XML as output.
"""
raise Exception("asCommandLine not supported (yet)")
#try:
# inFile = args["xmlfile"]
# outFile = args["out"]
#except:
# raise PeachException("XmlAnalyzer requires two parameters, xmlfile and out.")
#
#xml = _Xml2Peach().xml2Peach("file:"+inFile)
#
#fd = open(outfile, "wb+")
#fd.write(xml)
#fd.close()
def asTopLevel(self, peach, args):
"""
Called when Analyzer is used from top level.
From the top level producing zero or more data models and
state models is possible.
"""
raise Exception("asTopLevel not supported")