-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy paththeharvester_xml.py
66 lines (57 loc) · 2.93 KB
/
theharvester_xml.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
from recon.core.module import BaseModule
from bs4 import BeautifulSoup
import re
import os
class Module(BaseModule):
meta = {
'name': 'Import theHarvester XML',
'author': 'ScumSec 0x1414',
'description': 'Imports emails, hosts and virtual hosts from theHarvester XML. Updates the \'contacts\' and \'hosts\' tables.',
'options': (
('filename', None, True, 'Path and filename for theHarvester XML input'),
('domain', None, True, 'Filter all data endswith domain name'),
),
}
def get_name(self, email, title):
def up(name_part):
return name_part[0].upper() + name_part[1:]
answer = {}
parts = re.findall('[a-z]+', email.lower().split('@')[0])
if len(parts) == 2:
answer.update({'first_name': up(parts[0]), 'middle_name': None, 'last_name': up(parts[1]), 'email': email, 'title': title})
elif len(parts) == 3:
answer.update({'first_name': up(parts[0]), 'middle_name': up(parts[1]), 'last_name': up(parts[2]), 'email': email, 'title': title})
else:
answer.update({'first_name': None, 'middle_name': None, 'last_name': None, 'email': email, 'title': title})
return answer
def module_run(self):
domain = self.options['domain']
filename = self.options['filename']
if domain:
if os.path.exists(filename):
with open(filename) as xml_file:
xml_data = xml_file.read()
soup = BeautifulSoup(xml_data, 'xml')
for host in soup.find_all('host'):
hostname = host.find('hostname')
ip = host.find('ip')
if hostname and ip:
if hostname.string.endswith(domain):
self.add_hosts(ip_address=ip.string, host=hostname.string)
else:
if host.string.endswith(domain):
self.add_hosts(host.string)
for vhost in soup.find_all('vhost'):
hostname = host.find('hostname')
ip = host.find('ip')
if hostname and ip:
if hostname.string.endswith(domain):
self.add_hosts(ip_address=ip.string, host=hostname.string)
else:
if vhost.string.endswith(domain):
self.add_hosts(vhost.string)
for email in soup.find_all('email'):
if email.string.split('@')[0]:
if email.string.endswith(domain):
user_data = self.get_name(email.string, 'theHarvester import')
self.add_contacts(**user_data)