-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLuaMISP.lua
More file actions
executable file
·186 lines (165 loc) · 4.07 KB
/
LuaMISP.lua
File metadata and controls
executable file
·186 lines (165 loc) · 4.07 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
local utils = require 'utils'
----------------------------------------
-- AbstractMISP
AbstractMISP = {}
function AbstractMISP:new(o)
o = o or {}
setmetatable(o, self)
self.__index = self
return o
end
function AbstractMISP:toJson()
return utils.jsonEncode(self)
end
function AbstractMISP:addTag(tagname)
table.insert(self.Tag, {
name = tagname
})
end
function AbstractMISP:addTags(tags)
for i, tag in ipairs(tags)
do
self:addTag(tag)
end
end
----------------------------------------
-- Event
Event = AbstractMISP:new()
function Event:new(o)
o = o or {}
setmetatable(o, self)
self.__index = self
if not o.uuid then
o.uuid = utils.uuid()
end
if not o.Tag then
o.Tag = {}
end
if not o.Attribute then
o.Attribute = {}
end
if not o.Object then
o.Object = {}
end
return o
end
function Event:__tostring()
return string.format('<Event title="%s" Attribute=%s Object=%s>', self.title, #self.Attribute, #self.Object)
end
function Event:fullToString()
local output = tostring(self)
for i, object in ipairs(self.Object) do
output = output .. '\n\t' .. object:fullToString()
end
for i, attribute in ipairs(self.Attribute) do
output = output .. '\n\t' .. tostring(attribute)
end
return output
end
function Event:addAttribute(attribute)
if attribute ~= nil then
table.insert(self.Attribute, attribute)
end
end
function Event:addAttributes(attributes)
for i, attribute in ipairs(attributes)
do
self:addAttribute(attribute)
end
end
function Event:addObject(object)
if object ~= nil then
table.insert(self.Object, object)
end
end
function Event:addObjects(objects)
for i, object in ipairs(objects)
do
self:addObject(object)
end
end
----------------------------------------
-- Attribute
Attribute = AbstractMISP:new()
function Attribute:new(o)
o = o or {}
setmetatable(o, self)
self.__index = self
if not o.uuid then
o.uuid = utils.uuid()
end
if not o.Tag then
o.Tag = {}
end
if o.data then
o.data = utils.base64.encode(o.data)
end
if o.type ~= nil and o.value ~= nil then
return o
else
return nil
end
end
function Attribute:__tostring()
return string.format('<Attribute type="%s" value="%s">', self.type, self.value)
end
function Attribute:fullToString()
return tostring(self)
end
----------------------------------------
-- Object
-- /!\ Current limitation: Do not support object-template. You have to supply the object-template data when creating the object
Object = AbstractMISP:new()
function Object:new(o)
o = o or {}
setmetatable(o, self)
self.__index = self
if not o.uuid then
o.uuid = utils.uuid()
end
if not o.Attribute then
o.Attribute = {}
end
if not o.ObjectReference then
o.ObjectReference = {}
end
return o
end
function Object:__tostring()
return string.format('<Object name="%s" Attribute=%s Reference=%s>', self.name, #self.Attribute, #self.ObjectReference)
end
function Object:fullToString()
local output = tostring(self)
for i, attribute in ipairs(self.Attribute) do
output = output .. '\n\t\t' .. tostring(attribute)
end
return output
end
function Object:addAttribute(attribute)
if attribute ~= nil then
table.insert(self.Attribute, attribute)
end
end
function Object:addAttributes(attributes)
for i, attribute in ipairs(attributes)
do
self:addAttribute(attribute)
end
end
function Object:addReference(attribute, type)
local reference = {
uuid = utils.uuid(),
object_uuid = self.uuid,
referenced_uuid = attribute.uuid,
relationship_type = type,
}
table.insert(self.ObjectReference, reference)
end
function Object:getAttributeByName(attribute_name)
for i, attribute in ipairs(self.Attribute) do
if attribute.object_relation == attribute_name then
return attribute
end
end
return nil
end