Skip to content

Releases: GistIcon/md.md

itu

11 Jan 07:18

Choose a tag to compare

itu Pre-release
Pre-release
environment:
  my_variable:
    secure: n8vG2vCzcgIR3HijyNu5AhJWPHeKJhta2XLad06bSLGtXqpARX4Rd7AF/d21p2IZEIdPaGCUaGS7C7RZAwaZcPMHWS6QxhB7b1lCU/v8IrEIajlvDpdzKUg+UcEMmkwut9OAgMsnNMxcNrq6EW9CXGNcQBptO0a6iNF0fSqt9GgAeU+yBZO17NADy38RKjCvub1iKhpdQZPSR9YMJO32CccgDGQ7Fv0q9TPvF/rrP3IQX8q6v7SAtLKgtB80zX4lGA4br4sXnoqwPrV9FinueOOkok0vHHfy2Pvz3vEfBYo=

#!/usr/bin/python

import sys
import os
import subprocess
import random
import time
import sqlite3
import threading
import hashlib
import gzip
import json
import datetime
import re

if sys.version_info[0] >= 3:
    from socketserver import ThreadingTCPServer
    from urllib.request import urlopen, URLError
    from urllib.parse import urlparse, parse_qs
    from http.client import HTTPConnection
    from http.server import SimpleHTTPRequestHandler
else:
    from SocketServer import ThreadingTCPServer
    from urllib2 import urlopen, URLError
    from urlparse import urlparse, parse_qs
    from httplib import HTTPConnection
    from SimpleHTTPServer import SimpleHTTPRequestHandler

    bytes = lambda a, b : a

port = 1337
url = None
cid = None
tls = threading.local()
nets = {}
cracker = None

class ServerHandler(SimpleHTTPRequestHandler):
    def do_GET(s):
        result = s.do_req(s.path)

        if not result:
            return

        s.send_response(200)
        s.send_header("Content-type", "text/plain")
        s.end_headers()
        s.wfile.write(bytes(result, "UTF-8"))

    def do_POST(s):
        if ("dict" in s.path):
            s.do_upload_dict()

        if ("cap" in s.path):
            s.do_upload_cap()

        s.send_response(200)
        s.send_header("Content-type", "text/plain")
        s.end_headers()
        s.wfile.write(bytes("OK", "UTF-8"))

    def do_upload_dict(s):
        con = get_con()

        f = "dcrack-dict"
        c = f + ".gz"
        o = open(c, "wb")
        cl = int(s.headers['Content-Length'])
        o.write(s.rfile.read(cl))
        o.close()

        decompress(f)

        sha1 = hashlib.sha1()
        x = open(f, "rb")
        sha1.update(x.read())
        x.close()
        h = sha1.hexdigest()

        x = open(f, "rb")
        for i, l in enumerate(x):
            pass

        i = i + 1
        x.close()

        n = "%s-%s.txt" % (f, h)
        os.rename(f, n)
        os.rename(c, "%s.gz" % n)

        c = con.cursor()
        c.execute("INSERT into dict values (?, ?, 0)", (h, i))
        con.commit()

    def do_upload_cap(s):
        cl = int(s.headers['Content-Length'])
        f = open("dcrack.cap.tmp.gz", "wb")
        f.write(s.rfile.read(cl))
        f.close()

        decompress("dcrack.cap.tmp")
        os.rename("dcrack.cap.tmp.gz", "dcrack.cap.gz")
        os.rename("dcrack.cap.tmp", "dcrack.cap")

    def do_req(s, path):
        con = get_con()

        c = con.cursor()

        c.execute("""DELETE from clients where 
                (strftime('%s', datetime()) - strftime('%s', last))
                > 300""")

        con.commit()

        if ("ping" in path):
            return s.do_ping(path)

        if ("getwork" in path):
            return s.do_getwork(path)

        if ("dict" in path and "status" in path):
            return s.do_dict_status(path)

        if ("dict" in path and "set" in path):
            return s.do_dict_set(path)

        if ("dict" in path):
            return s.get_dict(path)

        if ("net" in path and "/crack" in path):
            return s.do_crack(path)

        if ("net" in path and "result" in path):
            return s.do_result(path)

        if ("cap" in path):
            return s.get_cap(path)

        if ("status" in path):
            return s.get_status()

        if ("remove" in path):
            return s.remove(path)

        return "error"

    def remove(s, path):
        con = get_con()

        p = path.split("/")
        n = p[4].upper()

        c = con.cursor()
        c.execute("DELETE from nets where bssid = ?", (n,))
        con.commit()

        c.execute("DELETE from work where net = ?", (n,))
        con.commit()

        return "OK"

    def get_status(s):
        con = get_con()

        c = con.cursor()
        c.execute("SELECT * from clients")

        clients = []

        for r in c.fetchall():
            clients.append(r['speed'])

        nets = []

        c.execute("SELECT * from dict where current = 1")
        dic = c.fetchone()

        c.execute("SELECT * from nets")

        for r in c.fetchall():
            n = { "bssid" : r['bssid'] }
            if r['pass']:
                n["pass"] = r['pass']

            if r['state'] != 2:
                n["tot"] = dic["lines"]

                did = 0
                cur = con.cursor()
                cur.execute("""SELECT * from work where net = ?
                        and dict = ? and state = 2""",
                        (n['bssid'], dic['id']))
                for row in cur.fetchall():
                    did += row['end'] - row['start']

                n["did"] = did

            nets.append(n)

        d = { "clients" : clients, "nets" : nets }

        return json.dumps(d)

    def do_result_pass(s, net, pw):
        con = get_con()

        pf = "dcrack-pass.txt"

        f = open(pf, "w")
        f.write(pw)
        f.write("\n")
        f.close()

        cmd = ["aircrack-ng", "-w", pf, "-b", net, "-q", "dcrack.cap"]
        p = subprocess.Popen(cmd, stdout=subprocess.PIPE, \
            stdin=subprocess.PIPE)

        res = p.communicate()[0]
        res = str(res)

        os.remove(pf)

        if not "KEY FOUND" in res:
            return "error"

        s.net_done(net)

        c = con.cursor()
        c.execute("UPDATE nets set pass = ? where bssid = ?", \
            (pw, net))

        con.commit()

        return "OK"

    def net_done(s, net):
        con = get_con()

        c = con.cursor()
        c.execute("UPDATE nets set state = 2 where bssid = ?",
            (net,))

        c.execute("DELETE from work where net = ?", (net,))
        con.commit()

    def do_result(s, path):
        con = get_con()

        p = path.split("/")
        n = p[4].upper()

        x  = urlparse(path)
        qs = parse_qs(x.query)

        if "pass" in qs:
            return s.do_result_pass(n, qs['pass'][0])

        wl = qs['wl'][0]

        c = con.cursor()
        c.execute("SELECT * from nets where bssid = ?", (n,))
        r = c.fetchone()
        if r and r['state'] == 2:
            return "Already done"

        c.execute("""UPDATE work set state = 2 where 
            net = ? and dict = ? and start = ? and end = ?""",
            (n, wl, qs['start'][0], qs['end'][0]))

        con.commit()

        if c.rowcount == 0:
            c.execute("""INSERT into work values
                (NULL, ?, ?, ?, ?, datetime(), 2)""",
                    (n, wl, qs['start'][0], qs['end'][0]))
            con.commit()

        # check status
        c.execute("""SELECT * from work where net = ? and dict = ?
            and state = 2 order by start""", (n, wl))

        i = 0
        r = c.fetchall()
        for row in r:
            if i == row['start']:
                i = row['end']
            else:
                break

        c.execute("SELECT * from dict where id = ? and lines = ?",
            (wl, i))

        r = c.fetchone()

        if r:
            s.net_done(n)

        return "OK"

    def get_cap(s, path):
        return s.serve_file("dcrack.cap.gz")

    def get_dict(s, path):
        p = path.split("/")
        n = p[4]

        fn = "dcrack-dict-%s.txt.gz" % n

        return s.serve_file(fn)

    def serve_file(s, fn):
        s.send_response(200)
        s.send_header("Content-type", "application/x-gzip")
        s.end_headers()

        # XXX openat
        f = open(fn, "rb")
        s.wfile.write(f.read())
        f.close()

        return None

    def do_crack(s, path):
        con = get_con()

        p = path.split("/")

        n = p[4].upper()

        c = con.cursor()
        c.execute("INSERT into nets values (?, NULL, 1)", (n,))
        con.commit()

        return "OK"

    def do_dict_set(s, path):
        con = get_con()

        p = path.split("/")

        h = p[4]

        c = con.cursor()
        c.execute("UPDATE dict set current = 0")
        c.execute("UPDATE dict set current = 1 where id = ?", (h,))
        con.commit()

        return "OK"

    def do_ping(s, path):
        con = get_con()

        p = path.split("/")

        cid = p[4]

        x  = urlparse(path)
        qs = parse_qs(x.query)

        speed = qs['speed'][0]

        c = con.cursor()
        c.execute("SELECT * from clients where id = ?", (cid,))
        r = c.fetchall()
        if (not r):
            c.execute("INSERT into clients values (?, ?, datetime())",
                  (cid, int(speed)))
        else:
            c.execute("""UPDATE clients set speed = ?, 
                    last = datetime() where id = ?""",
                    (int(speed), cid))

        con.commit()

        return "60"

    def try_network(s, net, d):
        con = get_con()

        c = con.cursor()
        c.execute("""SELECT * from work where net = ? and dict = ?
                order by start""", (net['bssid'], d['id']))

        r = c.fetchall()

        s     = 5000000
        i     = 0
        found = False

        for row in r:
            if found:
                if i + s > row['start']:
                    s = row['start'] - i
                break

            if (i >= row['start'] and i <= row['end']):
                i = row['end']
            else:
                found = True

        if i + s > d['lines']:
            s = d['lines'] - i

        if s == 0:
            return None

        c.execute("INSERT into work values (NULL, ?, ?, ?, ?, datetime(), 1)",
            (net['bssid'], d['id'], i, i + s))

        con.commit()

        crack = { "net"   : net['bssid'], \
              "dict"  : d['id'], \
              "start" : i, \
              ...
Read more