-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
02d5142
commit 0185692
Showing
6 changed files
with
102 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
#!/usr/bin/env python3 | ||
|
||
import os | ||
import tempfile | ||
from pathlib import Path | ||
from collections import defaultdict | ||
|
||
from tinydb import TinyDB | ||
|
||
from das.common import Logger, run_command | ||
|
||
|
||
class DB: | ||
"""Class for utilities that serve for manual DB manipulations.""" | ||
|
||
def __init__(self, db_path): | ||
""" | ||
Constructor. | ||
:param db_path: a TinyDB database file path | ||
:type db_path: str | ||
""" | ||
self.db = TinyDB(db_path) | ||
|
||
def create_generic(self, scan_path, domains_path=None): | ||
"""Create TinyDB from a generic scan output and a list of domain names (projectdiscovery/dnsx must be in PATH). | ||
:param scan_path: an input file path with newline-separated scan output | ||
:type scan_path: pathlib.PosixPath | ||
:param domains_path: an input file path with newline-separated domain names | ||
:type domains_path: pathlib.PosixPath | ||
:return: number of hosts added to DB | ||
:rtype: int | ||
""" | ||
with open(scan_path) as f: | ||
# fmt -> 127.0.0.1:1337 | ||
scan = f.read().splitlines() | ||
|
||
if domains_path: | ||
with open(domains_path) as f: | ||
domains = set(f.read().splitlines()) | ||
|
||
with tempfile.NamedTemporaryFile('w', suffix='.txt') as tmp: | ||
dnsx_path = Path(tempfile.gettempdir()) / 'dnsx.txt' | ||
domains_punycode = [domain.encode('idna').decode() + '\n' for domain in domains] | ||
tmp.writelines(domains_punycode) | ||
run_command(f'dnsx -l {tmp.name} -re -silent -o {dnsx_path}') | ||
|
||
with open(dnsx_path) as f: | ||
dnsx = f.read().splitlines() | ||
|
||
Logger.print_info(f'Resolved {len(dnsx)} DNS records') | ||
os.remove(dnsx_path) | ||
|
||
domains = defaultdict(set) | ||
for line in dnsx: | ||
domain, ip = line.split() | ||
domain = domain.encode().decode('idna') | ||
ip = ip.replace('[', '').replace(']', '') | ||
domains[ip].add(domain) | ||
|
||
items = [{'ip': ip, 'port': int(port), 'domains': list(domains[ip])} for ip, port in (line.split(':') for line in scan)] | ||
else: | ||
items = [{'ip': ip, 'port': int(port), 'domains': []} for ip, port in (line.split(':') for line in scan)] | ||
|
||
self.db.truncate() | ||
self.db.insert_multiple(items) | ||
|
||
return len(set([i['ip'] for i in items])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from das.parsers import IAddPortscanOutput | ||
|
||
|
||
class AddPortscanOutput(IAddPortscanOutput): | ||
"""Child class for processing Masscan output (import).""" | ||
|
||
def parse(self): | ||
""" | ||
Masscan (import) raw output parser. | ||
:return: a pair of values (portscan raw output filename, number of hosts added to DB) | ||
:rtype: tuple | ||
""" | ||
items, hosts = [], set() | ||
for line in self.portscan_raw: | ||
try: | ||
ip = line.split()[3] | ||
port, _, proto = line.split()[-1].split('/')[0:3] | ||
except Exception: | ||
pass | ||
else: | ||
if proto == 'tcp': | ||
items.append({'ip': ip, 'port': int(port), 'domains': []}) | ||
hosts.add(ip) | ||
|
||
self.db.insert_multiple(items) | ||
|
||
return (self.portscan_out, len(hosts)) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters