-
Notifications
You must be signed in to change notification settings - Fork 30
/
models.py
200 lines (177 loc) · 6.11 KB
/
models.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
''' Creates the models for the phish collector '''
from datetime import datetime
from elasticsearch import Elasticsearch
from urlparse import urlparse
es = Elasticsearch()
class Phish(object):
''' A class representing a possible phishing site '''
_index = 'samples'
_type = 'phish'
def __init__(self, *args, **kwargs):
self.pid = kwargs.get('pid')
self.url = kwargs.get('url')
self.index_url = Phish.clean_url(self.url)
self.ip_address = kwargs.get('ip_address', '0.0.0.0')
self.feed = kwargs.get('feed')
self.indexing_enabled = kwargs.get('indexing_enabled', False)
self.has_kit = kwargs.get('has_kit', False)
self.kits = kwargs.get('kits', [])
self.timestamp = datetime.now()
self.status_code = kwargs.get('status_code')
self.html = kwargs.get('html')
@classmethod
def clean_url(cls, url):
''' Cleans the URL provided to be a basic scheme://host/path format.
This removes any params, trailing slashes, etc. to help us remove duplicate
URLs from our index.
Args:
url {str} - The URL to search
'''
parts = urlparse(url)
path = parts.path
# Strip the trailing slash
if path and path[-1] == '/':
path = path[:-1]
clean_url = '{}://{}{}'.format(parts.scheme, parts.netloc.encode('utf-8'), path.encode('utf-8', 'ignore'))
return clean_url
def to_dict(self):
''' Creates a dict representation of the Phish instance that
is compatible with Elasticsearch '''
return {
'pid': self.pid,
'url': self.url,
'index_url': self.index_url,
'ip_address': self.ip_address,
'feed': self.feed,
'indexing_enabled': self.indexing_enabled,
'has_kit': self.has_kit,
'kits': self.kits,
'timestamp': self.timestamp,
'status_code': self.status_code,
'html': self.html
}
def index(self):
''' Indexes the document into Elasticsearch '''
return es.index(
index=Phish._index,
doc_type=Phish._type,
id=self.pid,
body=self.to_dict())
@classmethod
def exists(cls, url):
''' Checks if a Phish entry with the provided URL already exists in
elasticsearch.
Args:
url {str} - The URL to the phishing page
'''
url = Phish.clean_url(url)
exists = False
result = es.search(
index=cls._index,
doc_type=cls._type,
terminate_after=1,
size=0,
body={'query': {
'term': {
'index_url.raw': url
}
}})
if result['hits']['total']:
exists = True
return exists
@classmethod
def get_most_recent(cls, feed=None):
''' Returns the most recent Phish entry if it exists in Elasticsearch '''
most_recent = None
result = es.search(
index=cls._index,
doc_type=cls._type,
size=1,
body={
"query": {
"term": {
'feed': feed
}
},
"sort": [{
"timestamp": {
"order": "desc"
}
}]
})
hits = result['hits']['hits']
if hits:
sample = hits[0]['_source']
most_recent = Phish(url=sample['url'], pid=sample['pid'])
return most_recent
class PhishKit(object):
''' A class representing phishing kits stored on the filesystem.
Phishkits are stored as child objects in a one-to-many relationship with Phish samples.'''
_index = 'samples'
_type = 'kit'
def __init__(self, **kwargs):
'''
Creates a new instance of the phishkit metadata entry to be stored
in Elasticsearch
'''
self.hash = kwargs.get('hash')
self.filepath = kwargs.get('filepath')
self.filename = kwargs.get('filename')
self.url = kwargs.get('url')
self.emails = kwargs.get('emails')
self.parent = kwargs.get('parent')
def to_dict(self):
''' Creates a dict representation of the Phish instance that
is compatible with Elasticsearch '''
return {
'hash': self.hash,
'filepath': self.filepath,
'filename': self.filename,
'url': self.url,
'emails': self.emails
}
def index(self):
''' Indexes the document into Elasticsearch '''
return es.index(
index=PhishKit._index,
doc_type=PhishKit._type,
id=self.hash,
body=self.to_dict())
@classmethod
def exists(cls, url):
''' Finds a kit with the given URL. Typically, we would search by hash, but
we want to be able to find multiple occurrences of the same kit across different sites.
If a kit is found in Elasticsearch, we return the `models.PhishKit` instance associated
with it.
Args:
url {str} The URL to search for
'''
kit = None
result = es.search(
index=cls._index,
doc_type=cls._type,
terminate_after=1,
size=1,
body={'query': {
'term': {
'url.raw': url
}
}})
if result['hits']['total']:
kit_dict = result['hits']['hits'][0]['_source']
kit = PhishKit.from_dict(kit_dict)
return kit
@classmethod
def from_dict(self, kit_dict):
''' Loads and returns a PhishKit instance from a dict found in a response
from Elasticsearch.
Args:
kit_dict {dict} - The dictionary to load
'''
kit = PhishKit(
hash=kit_dict.get('hash'),
filepath=kit_dict.get('filepath'),
filename=kit_dict.get('filename'),
url=kit_dict.get('url'),
emails=kit_dict.get('emails'))
return kit