Added lookupdns.py

This commit is contained in:
emdee 2022-11-14 11:59:33 +00:00
parent 2ea65cc181
commit 94c0834092
3 changed files with 97 additions and 86 deletions

View File

@ -198,7 +198,7 @@ def lYamlGoodNodes(sFile='/etc/tor/torrc-goodnodes.yaml'):
# yq '.Nodes.IntroductionPoints|.[]' < /etc/tor/torrc-goodnodes.yaml # yq '.Nodes.IntroductionPoints|.[]' < /etc/tor/torrc-goodnodes.yaml
return l return l
def bdomain_is_bad(domain): def bdomain_is_bad(domain, fp):
global lKNOWN_NODNS global lKNOWN_NODNS
if domain in lKNOWN_NODNS: return True if domain in lKNOWN_NODNS: return True
if domain in lMAYBE_NODNS: if domain in lMAYBE_NODNS:
@ -209,9 +209,10 @@ def bdomain_is_bad(domain):
lMAYBE_NODNS.remove(domain) lMAYBE_NODNS.remove(domain)
return True return True
if '@' in domain: for elt in '@(){}$!':
LOG.warn(f"@ in domain {domain}") if elt in domain:
return True LOG.warn(f"{elt} in domain {domain}")
return True
return False return False
tBAD_URLS = set() tBAD_URLS = set()
@ -273,10 +274,11 @@ def aVerifyContact(a, fp, https_cafile, timeout=20, host='127.0.0.1', port=9050)
# domain should be a unique key for contacts # domain should be a unique key for contacts
domain = a['url'][8:] domain = a['url'][8:]
if bdomain_is_bad(domain): if bdomain_is_bad(domain, fp):
LOG.warn(f"{domain} is bad from {a['url']}") LOG.warn(f"{domain} is bad from {a['url']}")
LOG.info(f"{domain} is bad from {a}") LOG.debug(f"{fp} is bad from {a}")
return a return a
ip = zResolveDomain(domain) ip = zResolveDomain(domain)
if ip == '': if ip == '':
aFP_EMAIL[fp] = a['email'] aFP_EMAIL[fp] = a['email']
@ -357,7 +359,7 @@ def aParseContactYaml(contact, fp):
LOG.debug(f"{fp} {a}") LOG.debug(f"{fp} {a}")
return a return a
key = '' key = ''
for elt in lets: for elt in lelts:
if key == '': if key == '':
key = elt key = elt
continue continue
@ -424,22 +426,22 @@ def vsetup_logging(log_level, logfile=''):
LOG.info(f"SSetting log_level to {log_level!s}") LOG.info(f"SSetting log_level to {log_level!s}")
logging._levelToName = { logging._levelToName = {
CRITICAL: 'CRITICAL', logging.CRITICAL: 'CRITICAL',
ERROR: 'ERROR', logging.ERROR: 'ERROR',
WARNING: 'WARN', logging.WARNING: 'WARN',
INFO: 'INFO', logging.INFO: 'INFO',
DEBUG: 'DEBUG', logging.DEBUG: 'DEBUG',
NOTSET: 'NOTSET', logging.NOTSET: 'NOTSET',
} }
logging._nameToLevel = { logging._nameToLevel = {
'CRITICAL': CRITICAL, 'CRITICAL': logging.CRITICAL,
'FATAL': FATAL, 'FATAL': logging.FATAL,
'ERROR': ERROR, 'ERROR': logging.ERROR,
'WARN': WARNING, 'WARN': logging.WARNING,
'WARNING': WARNING, 'WARNING': logging.WARNING,
'INFO': INFO, 'INFO': logging.INFO,
'DEBUG': DEBUG, 'DEBUG': logging.DEBUG,
'NOTSET': NOTSET, 'NOTSET': logging.NOTSET,
} }
def oMainArgparser(_=None): def oMainArgparser(_=None):
@ -498,7 +500,7 @@ def oMainArgparser(_=None):
help="Seconds to wait for Tor to booststrap") help="Seconds to wait for Tor to booststrap")
parser.add_argument('--points_timeout', type=int, default=0, parser.add_argument('--points_timeout', type=int, default=0,
help="Timeout for getting introduction points - must be long >120sec. 0 means disabled looking for IPs") help="Timeout for getting introduction points - must be long >120sec. 0 means disabled looking for IPs")
parser.add_argument('--log_level', type=int, default=10, parser.add_argument('--log_level', type=int, default=20,
help="10=debug 20=info 30=warn 40=error") help="10=debug 20=info 30=warn 40=error")
parser.add_argument('--bad_sections', type=str, parser.add_argument('--bad_sections', type=str,
default='MyBadExit', default='MyBadExit',
@ -524,13 +526,13 @@ def vwrite_badnodes(oArgs, oBAD_NODES, slen):
os.rename(oArgs.bad_nodes, bak) os.rename(oArgs.bad_nodes, bak)
os.rename(tmp, oArgs.bad_nodes) os.rename(tmp, oArgs.bad_nodes)
def vwrite_goodnodes(oArgs, oGOOD_NODES, slen): def vwrite_goodnodes(oArgs, oGOOD_NODES, ilen):
if oArgs.good_nodes: if oArgs.good_nodes:
tmp = oArgs.good_nodes +'.tmp' tmp = oArgs.good_nodes +'.tmp'
bak = oArgs.good_nodes +'.bak' bak = oArgs.good_nodes +'.bak'
with open(tmp, 'wt') as oFYaml: with open(tmp, 'wt') as oFYaml:
yaml.dump(oGOOD_NODES, indent=2, stream=oFYaml) yaml.dump(oGOOD_NODES, indent=2, stream=oFYaml)
LOG.info(f"Wrote {slen} good nodes to {oArgs.good_nodes}") LOG.info(f"Wrote {ilen} good relays to {oArgs.good_nodes}")
oFYaml.close() oFYaml.close()
if os.path.exists(oArgs.good_nodes): if os.path.exists(oArgs.good_nodes):
os.rename(oArgs.good_nodes, bak) os.rename(oArgs.good_nodes, bak)
@ -628,7 +630,7 @@ def iMain(lArgs):
LOG.info(f"Preloaded {len(texclude_set)} bad fps") LOG.info(f"Preloaded {len(texclude_set)} bad fps")
ttrust_db_index = aTRUST_DB_INDEX.keys() ttrust_db_index = aTRUST_DB_INDEX.keys()
tdns_contacts = set() tdns_urls = set()
iFakeContact = 0 iFakeContact = 0
iTotalContacts = 0 iTotalContacts = 0
aBadContacts = {} aBadContacts = {}
@ -644,7 +646,7 @@ def iMain(lArgs):
continue continue
relay.fingerprint = relay.fingerprint.upper() relay.fingerprint = relay.fingerprint.upper()
sofar = f"G:{len(aTRUST_DB.keys())} U:{len(tdns_contacts)} F:{iFakeContact} BF:{len(texclude_set)} GF:{len(ttrust_db_index)} TC:{iTotalContacts} #{iR}" sofar = f"G:{len(aTRUST_DB.keys())} U:{len(tdns_urls)} F:{iFakeContact} BF:{len(texclude_set)} GF:{len(ttrust_db_index)} TC:{iTotalContacts} #{iR}"
if not relay.exit_policy.is_exiting_allowed(): if not relay.exit_policy.is_exiting_allowed():
if sEXCLUDE_EXIT_KEY == 'ExcludeNodes': if sEXCLUDE_EXIT_KEY == 'ExcludeNodes':
pass # LOG.debug(f"{relay.fingerprint} not an exit {sofar}") pass # LOG.debug(f"{relay.fingerprint} not an exit {sofar}")
@ -671,10 +673,11 @@ def iMain(lArgs):
continue continue
iTotalContacts += 1 iTotalContacts += 1
fp = relay.fingerprint
if relay.contact and not 'url:' in relay.contact: if relay.contact and not 'url:' in relay.contact:
LOG.info(f"{relay.fingerprint} skipping bad contact - no url: {sofar}") LOG.info(f"{fp} skipping bad contact - no url: {sofar}")
LOG.debug(f"{relay.fingerprint} {relay.contact} {sofar}") LOG.debug(f"{fp} {relay.contact} {sofar}")
texclude_set.add(relay.fingerprint) texclude_set.add(fp)
continue continue
c = relay.contact.lower() c = relay.contact.lower()
@ -688,17 +691,17 @@ def iMain(lArgs):
i = c.find('/') i = c.find('/')
if i >=0: c = c[:i] if i >=0: c = c[:i]
domain = c domain = c
if domain and bdomain_is_bad(domain): if domain and bdomain_is_bad(domain, fp):
LOG.info(f"{relay.fingerprint} skipping bad {domain} {sofar}") LOG.info(f"{fp} skipping bad {domain} {sofar}")
LOG.debug(f"{relay.fingerprint} {relay.contact} {sofar}") LOG.debug(f"{fp} {relay.contact} {sofar}")
texclude_set.add(relay.fingerprint) texclude_set.add(fp)
continue continue
if domain: if domain:
ip = zResolveDomain(domain) ip = zResolveDomain(domain)
if not ip: if not ip:
LOG.warn(f"{relay.fingerprint} {domain} did not resolve {sofar}") LOG.warn(f"{fp} {domain} did not resolve {sofar}")
texclude_set.add(relay.fingerprint) texclude_set.add(fp)
lKNOWN_NODNS.append(domain) lKNOWN_NODNS.append(domain)
iFakeContact += 1 iFakeContact += 1
continue continue
@ -706,7 +709,7 @@ def iMain(lArgs):
if 'dns-rsa' in relay.contact.lower(): if 'dns-rsa' in relay.contact.lower():
target = f"{relay.fingerprint}.{domain}" target = f"{relay.fingerprint}.{domain}"
LOG.info(f"skipping 'dns-rsa' {target} {sofar}") LOG.info(f"skipping 'dns-rsa' {target} {sofar}")
tdns_contacts.add(target) tdns_urls.add(target)
elif 'proof:uri-rsa' in relay.contact.lower(): elif 'proof:uri-rsa' in relay.contact.lower():
a = aParseContact(relay.contact, relay.fingerprint) a = aParseContact(relay.contact, relay.fingerprint)
@ -769,6 +772,8 @@ def iMain(lArgs):
LOG.info(f"Filtered {len(twhitelist_set)} whitelisted relays") LOG.info(f"Filtered {len(twhitelist_set)} whitelisted relays")
texclude_set = texclude_set.difference(twhitelist_set) texclude_set = texclude_set.difference(twhitelist_set)
# accept the dns-rsa urls for now until we test them
texclude_set = texclude_set.difference(tdns_urls)
LOG.info(f"{len(list(aTRUST_DB.keys()))} good contacts out of {iTotalContacts}") LOG.info(f"{len(list(aTRUST_DB.keys()))} good contacts out of {iTotalContacts}")
if oArgs.proof_output and aTRUST_DB: if oArgs.proof_output and aTRUST_DB:
@ -785,7 +790,7 @@ def iMain(lArgs):
with open(oArgs.torrc_output, 'wt') as oFTorrc: with open(oArgs.torrc_output, 'wt') as oFTorrc:
oFTorrc.write(f"{sEXCLUDE_EXIT_KEY} {','.join(texclude_set)}\n") oFTorrc.write(f"{sEXCLUDE_EXIT_KEY} {','.join(texclude_set)}\n")
oFTorrc.write(f"{sINCLUDE_EXIT_KEY} {','.join(aTRUST_DB_INDEX.keys())}\n") oFTorrc.write(f"{sINCLUDE_EXIT_KEY} {','.join(aTRUST_DB_INDEX.keys())}\n")
oFTorrc.write(f"{sINCLUDE_GUARD_KEY} {','.join(o[oGOOD_ROOT]['GuardNodes'])}\n") oFTorrc.write(f"{sINCLUDE_GUARD_KEY} {','.join(oGOOD_NODES[oGOOD_ROOT]['GuardNodes'])}\n")
LOG.info(f"Wrote tor configuration to {oArgs.torrc_output}") LOG.info(f"Wrote tor configuration to {oArgs.torrc_output}")
oFTorrc.close() oFTorrc.close()
@ -801,7 +806,7 @@ def iMain(lArgs):
oGOOD_NODES['GoodNodes']['Relays']['ExitNodes'] = list(aTRUST_DB_INDEX.keys()) oGOOD_NODES['GoodNodes']['Relays']['ExitNodes'] = list(aTRUST_DB_INDEX.keys())
# GuardNodes are readonl # GuardNodes are readonl
vwrite_goodnodes(oArgs, oGOOD_NODES, str(len(ttrust_db_index))) vwrite_goodnodes(oArgs, oGOOD_NODES, len(aTRUST_DB_INDEX.keys()))
retval = 0 retval = 0
try: try:
logging.getLogger('stem').setLevel(30) logging.getLogger('stem').setLevel(30)
@ -838,7 +843,7 @@ def iMain(lArgs):
LOG.errro(f"Failed setting {sINCLUDE_EXIT_KEY} good exit nodes in Tor") LOG.errro(f"Failed setting {sINCLUDE_EXIT_KEY} good exit nodes in Tor")
retval += 1 retval += 1
LOG.info("dns-rsa domains:\n{'\n'.join(tdns_contacts)}") sys.stdout.write("dns-rsa domains:\n" +'\n'.join(tdns_urls) +'\n')
return retval return retval
except InvalidRequest as e: except InvalidRequest as e:

View File

@ -27,9 +27,8 @@ LOG = logging.getLogger()
bHAVE_TORR = shutil.which('tor-resolve') bHAVE_TORR = shutil.which('tor-resolve')
# maybe we should check these each time but we # we check these each time but we got them by sorting bad relays
# got them by sorting bad relays in the wild # in the wild we'll keep a copy here so we can avoid restesting
# we'll keep a copy here
yKNOWN_NODNS = """ yKNOWN_NODNS = """
--- ---
- 0x0.is - 0x0.is
@ -50,6 +49,7 @@ yKNOWN_NODNS = """
- or.wowplanet.de - or.wowplanet.de
- ormycloud.org - ormycloud.org
- plied-privacy.net - plied-privacy.net
- rivacysvcs.net
- redacted.org - redacted.org
- rification-for-nusenu.net - rification-for-nusenu.net
- rofl.cat - rofl.cat

View File

@ -140,7 +140,11 @@ def get_controller(address='127.0.0.1', port=9151, password=''):
return controller return controller
def find_validation_candidates(controller, trusted_domains=[],validation_cache=[],accept_all=False): def find_validation_candidates(controller,
trusted_domains=[],
validation_cache=[],
CAfile='/etc/ssl/certs/ca-certificates.crt',
accept_all=False):
''' '''
connect to a tor client via controlport and return a dict of all connect to a tor client via controlport and return a dict of all
not yet validated fingerprints per trusted operators not yet validated fingerprints per trusted operators
@ -529,6 +533,7 @@ def configure_tor(controller, trusted_fingerprints, exitonly=True):
if __name__ == '__main__': if __name__ == '__main__':
CAfile = '/etc/ssl/certs/ca-certificates.crt'
trust_config = 'trust_config' trust_config = 'trust_config'
assert os.path.exists(trust_config) assert os.path.exists(trust_config)
trusted_domains = read_local_trust_config(trust_config) trusted_domains = read_local_trust_config(trust_config)
@ -546,7 +551,8 @@ if __name__ == '__main__':
r = find_validation_candidates(controller, r = find_validation_candidates(controller,
validation_cache=trusted_fingerprints, validation_cache=trusted_fingerprints,
trusted_domains=trusted_domains) trusted_domains=trusted_domains,
CAfile=CAfile)
validate_proofs(r, validation_cache_file, validate_proofs(r, validation_cache_file,
timeout=timeout, timeout=timeout,
host=controller_address, host=controller_address,