def execute_command(logger, cmd, answer_section_regex, flags): binary = "/usr/bin/dig" cmd = f"{binary} {cmd}" logger.info(f"Executing command {cmd}") r = insightconnect_plugin_runtime.helper.exec_command(cmd) stdout = r["stdout"].decode("utf-8") # Grab query status status = util.safe_parse(re.search("status: (.+?),", stdout)) # Grab nameserver ns = util.safe_parse(re.search("SERVER: (.+?)#", stdout)) # Grab number of answers answers = util.safe_parse(re.search(r"ANSWER: ([0-9]+)", stdout)) answer_section = None if util.not_empty(answers): answers = int(answers) # We need answers to continue if answers > 0: # Grab resolved address section if flags is None: answer_section = util.safe_parse(re.search(answer_section_regex, stdout)) else: answer_section = util.safe_parse(re.search(answer_section_regex, stdout, flags=flags)) if status != "NOERROR": stdout = f"Resolution failed, nameserver {ns} returned {status} status" return { "answer_section": answer_section, "fulloutput": stdout + r["stderr"].decode("utf-8"), "nameserver": ns, "status": status, }
def execute_command(logger, cmd, answer_section_regex, flags): binary = "/usr/bin/dig" cmd = f"{binary} {cmd}" logger.info(f"Executing command {cmd}") r = insightconnect_plugin_runtime.helper.exec_command(cmd) stdout = r['stdout'].decode('utf-8') # Grab query status status = util.safe_parse(re.search('status: (.+?),', stdout)) # Grab nameserver ns = util.safe_parse(re.search('SERVER: (.+?)#', stdout)) # Grab number of answers answers = util.safe_parse(re.search(r'ANSWER: ([0-9]+)', stdout)) answer_section = None if util.not_empty(answers): answers = int(answers) # We need answers to continue if answers > 0: # Grab resolved address section if flags is None: answer_section = util.safe_parse(re.search(answer_section_regex, stdout)) else: answer_section = util.safe_parse(re.search(answer_section_regex, stdout, flags=flags)) if status != "NOERROR": stdout = f'Resolution failed, nameserver {ns} returned {status} status' return { 'answer_section': answer_section, 'fulloutput': stdout + r['stderr'].decode('utf-8'), 'nameserver': ns, 'status': status }
def run(self, params={}): if len(params.get(Input.RESOLVER, '')) > 0: cmd = f"@{params.get(Input.RESOLVER)} {params.get(Input.DOMAIN)} {params.get(Input.QUERY)}" else: cmd = f"{params.get(Input.DOMAIN)} {params.get(Input.QUERY)}" command_output = util.execute_command(self.logger, cmd, r'ANSWER SECTION:\n(.*)\n\n;;', re.DOTALL) answer_section = command_output['answer_section'] if answer_section is None: ans = ['Not found'] else: # Grab address if params[Input.QUERY] == "SOA": domain = util.safe_parse(re.search(r'SOA\t(\S+)', answer_section)) if util.not_empty(domain): domain = domain.rstrip('.') ans = [domain] else: ans = answer_section.split('\n') if len(ans) == 0: ans.append('NO MATCHES FOUND') ans = [util.safe_parse(re.search(r'\s(\S+)$', answer)) for answer in ans] ans = [answer.rstrip('.') for answer in ans] return { Output.FULLOUTPUT: command_output['fulloutput'], Output.QUESTION: params.get(Input.DOMAIN), Output.NAMESERVER: command_output['nameserver'], Output.STATUS: command_output['status'], Output.ANSWER: ans[0], Output.LAST_ANSWER: ans[len(ans) - 1], Output.ALL_ANSWERS: ans }
def run(self, params={}): binary = "/usr/bin/dig" if len(params.get('resolver', '')) > 0: cmd = "%s @%s -x %s" % (binary, params.get('resolver'), params.get('address')) else: cmd = "%s -x %s" % (binary, params.get('address')) self.logger.info('Executing command %s' % cmd) r = komand.helper.exec_command(cmd) # Grab query status status = util.safe_parse(re.search('status: (.+?),', r['stdout'])) # Grab nameserver ns = util.safe_parse(re.search('SERVER: (.+?)#', r['stdout'])) # Grab number of answers answers = util.safe_parse(re.search(r'ANSWER: ([0-9]+)', r['stdout'])) if util.not_empty(answers): answers = int(answers) # We need answers to continue address = "" if answers > 0: # Grab address section answer_section = util.safe_parse( re.search(r'ANSWER SECTION:\n(.*\n)', r['stdout'])) # Grab address address = util.safe_parse(re.search(r'\s(\S+)\n', answer_section)) if util.not_empty(address): address = address.rstrip('.') if status != "NOERROR": r['stdout'] = 'Resolution failed, nameserver %s returned %s status' % ( ns, status) address = 'Not found' return { 'fulloutput': r['stdout'] + r['stderr'], 'question': params.get('address'), 'nameserver': ns, 'status': status, 'answer': address }
def run(self, params={}): if len(params.get(Input.RESOLVER, "")) > 0: cmd = f"@{params.get(Input.RESOLVER)} {params.get(Input.DOMAIN)} {params.get(Input.QUERY)}" else: cmd = f"{params.get(Input.DOMAIN)} {params.get(Input.QUERY)}" command_output = util.execute_command(self.logger, cmd, r"ANSWER SECTION:\n(.*)\n\n;;", re.DOTALL) answer_section = command_output["answer_section"] if answer_section is None: ans = ["Not found"] else: # Grab address if params[Input.QUERY] == "SOA": domain = util.safe_parse( re.search(r"SOA\t(\S+)", answer_section)) if util.not_empty(domain): domain = domain.rstrip(".") ans = [domain] else: ans = answer_section.split("\n") if len(ans) == 0: ans.append("NO MATCHES FOUND") ans = [ util.safe_parse(re.search(r"\s(\S+)$", answer)) for answer in ans ] ans = [answer.rstrip(".") for answer in ans] return { Output.FULLOUTPUT: command_output["fulloutput"], Output.QUESTION: params.get(Input.DOMAIN), Output.NAMESERVER: command_output["nameserver"], Output.STATUS: command_output["status"], Output.ANSWER: ans[0], Output.LAST_ANSWER: ans[len(ans) - 1], Output.ALL_ANSWERS: ans, }
def run(self, params={}): if len(params.get(Input.RESOLVER, '')) > 0: cmd = f"@{params.get(Input.RESOLVER)} -x {params.get(Input.ADDRESS)}" else: cmd = f"-x {params.get(Input.ADDRESS)}" command_output = util.execute_command(self.logger, cmd, 'ANSWER SECTION:\n(.*\n)', None) answer_section = command_output['answer_section'] if answer_section is None: address = 'Not found' else: # Grab address address = util.safe_parse(re.search(r'\s(\S+)\n', answer_section)) if util.not_empty(address): address = address.rstrip('.') return { Output.FULLOUTPUT: command_output['fulloutput'], Output.QUESTION: params.get(Input.ADDRESS), Output.NAMESERVER: command_output['nameserver'], Output.STATUS: command_output['status'], Output.ANSWER: address }
def run(self, params={}): if len(params.get(Input.RESOLVER, "")) > 0: cmd = f"@{params.get(Input.RESOLVER)} -x {params.get(Input.ADDRESS)}" else: cmd = f"-x {params.get(Input.ADDRESS)}" command_output = util.execute_command(self.logger, cmd, "ANSWER SECTION:\n(.*\n)", None) answer_section = command_output["answer_section"] if answer_section is None: address = "Not found" else: # Grab address address = util.safe_parse(re.search(r"\s(\S+)\n", answer_section)) if util.not_empty(address): address = address.rstrip(".") return { Output.FULLOUTPUT: command_output["fulloutput"], Output.QUESTION: params.get(Input.ADDRESS), Output.NAMESERVER: command_output["nameserver"], Output.STATUS: command_output["status"], Output.ANSWER: address, }
def run(self, params={}): binary = "/usr/bin/dig" if len(params.get('resolver', '')) > 0: cmd = "%s @%s %s %s" % (binary, params.get('resolver'), params.get('domain'), params.get('query')) else: cmd = "%s %s %s" % (binary, params.get('domain'), params.get('query')) self.logger.info("Executing command %s" % cmd) r = komand.helper.exec_command(cmd) # Grab query status status = util.safe_parse(re.search('status: (.+?),', r['stdout'])) # Grab nameserver ns = util.safe_parse(re.search('SERVER: (.+?)#', r['stdout'])) # Grab number of answers answers = util.safe_parse(re.search(r'ANSWER: ([0-9]+)', r['stdout'])) if util.not_empty(answers): answers = int(answers) # We need answers to continue if answers > 0: # Grab resolved address section answer_section = util.safe_parse( re.search(r'ANSWER SECTION:\n(.*)\n\n;;', r['stdout'], flags=re.DOTALL)) # Grab address if params['query'] == "SOA": domain = util.safe_parse( re.search(r'SOA\t(\S+)', answer_section)) if util.not_empty(domain): domain = domain.rstrip('.') ans = [domain] else: ans = answer_section.split('\n') if len(ans) == 0: ans.append('NO MATCHES FOUND') ans = [ util.safe_parse(re.search(r'\s(\S+)$', answer)) for answer in ans ] ans = [answer.rstrip('.') for answer in ans] else: ans = ['Not found'] if status != "NOERROR": r['stdout'] = 'Resolution failed, nameserver %s returned %s status' % ( ns, status) resolved = 'Not found' return { 'fulloutput': r['stdout'] + r['stderr'], 'question': params.get('domain'), 'nameserver': ns, 'status': status, 'answer': ans[0], 'last_answer': ans[len(ans) - 1], 'all_answers': ans }