| 47 | error('{bright}[{yellow}' + self.address + '{crst}/{bgreen}' + plugin.slug + '{crst}]{rst} ' + msg) |
| 48 | |
| 49 | async def execute(self, cmd, blocking=True, outfile=None, errfile=None, future_outfile=None): |
| 50 | target = self |
| 51 | |
| 52 | # Create variables for command references. |
| 53 | address = target.address |
| 54 | addressv6 = target.address |
| 55 | ipaddress = target.ip |
| 56 | ipaddressv6 = target.ip |
| 57 | scandir = target.scandir |
| 58 | |
| 59 | nmap_extra = target.autorecon.args.nmap |
| 60 | if target.autorecon.args.nmap_append: |
| 61 | nmap_extra += ' ' + target.autorecon.args.nmap_append |
| 62 | |
| 63 | if target.ipversion == 'IPv6': |
| 64 | nmap_extra += ' -6' |
| 65 | if addressv6 == target.ip: |
| 66 | addressv6 = '[' + addressv6 + ']' |
| 67 | ipaddressv6 = '[' + ipaddressv6 + ']' |
| 68 | |
| 69 | plugin = inspect.currentframe().f_back.f_locals['self'] |
| 70 | |
| 71 | if config['proxychains']: |
| 72 | nmap_extra += ' -sT' |
| 73 | |
| 74 | cmd = e(cmd) |
| 75 | tag = plugin.slug |
| 76 | |
| 77 | info('Port scan {bblue}' + plugin.name + ' {green}(' + tag + '){rst} is running the following command against {byellow}' + address + '{rst}: ' + cmd, verbosity=2) |
| 78 | |
| 79 | if outfile is not None: |
| 80 | outfile = os.path.join(target.scandir, e(outfile)) |
| 81 | |
| 82 | if errfile is not None: |
| 83 | errfile = os.path.join(target.scandir, e(errfile)) |
| 84 | |
| 85 | if future_outfile is not None: |
| 86 | future_outfile = os.path.join(target.scandir, e(future_outfile)) |
| 87 | |
| 88 | target.scans['ports'][tag]['commands'].append([cmd, outfile if outfile is not None else future_outfile, errfile]) |
| 89 | |
| 90 | async with target.lock: |
| 91 | with open(os.path.join(target.scandir, '_commands.log'), 'a') as file: |
| 92 | file.writelines(cmd + '\n\n') |
| 93 | |
| 94 | process, stdout, stderr = await target.autorecon.execute(cmd, target, tag, patterns=plugin.patterns, outfile=outfile, errfile=errfile) |
| 95 | |
| 96 | target.running_tasks[tag]['processes'].append({'process': process, 'stderr': stderr, 'cmd': cmd}) |
| 97 | |
| 98 | # If process should block, sleep until stdout and stderr have finished. |
| 99 | if blocking: |
| 100 | while (not (stdout.ended and stderr.ended)): |
| 101 | await asyncio.sleep(0.1) |
| 102 | await process.wait() |
| 103 | |
| 104 | return process, stdout, stderr |
| 105 | |
| 106 | class Service: |