﻿import asyncio, os, ssl, sys

SRC = '/root/recon/ips443_all.txt'
HITS = '/root/recon/gp_hits.tsv'
W = int(sys.argv[1]) if len(sys.argv) > 1 else 0
N = int(sys.argv[2]) if len(sys.argv) > 2 else 1
POS = '/root/recon/gp_scan_%d.pos' % W
CONC = 1200
TIMEOUT = 4
REQ = b'GET / HTTP/1.1\r\nHost: %s\r\nUser-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64)\r\nAccept: */*\r\nConnection: close\r\n\r\n'

ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
ctx.set_ciphers('DEFAULT@SECLEVEL=1')

counter = {'n': 0, 'hit': 0, 'err': 0}
out = open(HITS, 'a', buffering=1)


async def probe(ip, sem):
    async with sem:
        counter['n'] += 1
        try:
            r, w = await asyncio.wait_for(asyncio.open_connection(ip, 443, ssl=ctx, server_hostname=ip), TIMEOUT)
        except Exception:
            counter['err'] += 1
            return
        try:
            w.write(REQ % ip.encode())
            await w.drain()
            data = await asyncio.wait_for(r.read(2048), TIMEOUT)
        except Exception:
            counter['err'] += 1
            try:
                w.close()
            except Exception:
                pass
            return
        try:
            w.close()
        except Exception:
            pass
        low = data.lower()
        if b'global-protect' in low or b'globalprotect' in low or b'pan-os' in low:
            loc = ''
            for line in data.split(b'\r\n'):
                if line.lower().startswith(b'location:'):
                    loc = line.split(b':', 1)[1].strip().decode('latin1', 'replace')
            status = data.split(b'\r\n')[0].decode('latin1', 'replace')
            out.write('%s\t%s\t%s\n' % (ip, status, loc))
            counter['hit'] += 1


async def main():
    start = 0
    if os.path.exists(POS):
        try:
            start = int(open(POS).read().strip())
        except Exception:
            start = 0
    print('worker %d/%d resume %d' % (W, N, start), flush=True)
    sem = asyncio.Semaphore(CONC)
    batch = []
    idx = 0
    with open(SRC) as f:
        for line in f:
            idx += 1
            if idx <= start:
                continue
            if (idx - 1) % N != W:
                continue
            p = line.split()
            if not p:
                continue
            ip = p[3] if (p[0] == 'open' and len(p) >= 4) else p[0]
            if ip.count('.') != 3:
                continue
            batch.append(ip)
            if len(batch) >= 10000:
                await asyncio.gather(*[probe(ip, sem) for ip in batch])
                batch = []
                open(POS, 'w').write(str(idx))
                print('done %d hit %d err %d' % (counter['n'], counter['hit'], counter['err']), flush=True)
    if batch:
        await asyncio.gather(*[probe(ip, sem) for ip in batch])
    open(POS, 'w').write(str(idx))
    print('finished', idx, counter, flush=True)

asyncio.run(main())
