Skip to content

Instantly share code, notes, and snippets.

@Resisty
Created May 6, 2015 20:04
Show Gist options
  • Save Resisty/7a3c75150e0a2a6ceda7 to your computer and use it in GitHub Desktop.
Save Resisty/7a3c75150e0a2a6ceda7 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import ldap
import sys
import threading
import argparse
import signal
import time
import os
import Queue
def bind_once(uri,starttls):
con = ldap.initialize(uri)
try:
if starttls:
con.start_tls_s()
con.simple_bind()
except ldap.LDAPError, err:
if type(err.message) == dict :
for (k,v) in err.message.iteritems():
print("{0}: {1}\n".format(k,v))
else:
print err.message
con.unbind()
def bind_loop(q,n,uri,starttls):
while True:
bind_once(uri=uri,starttls=starttls)
q.put(n)
parser = argparse.ArgumentParser()
parser.add_argument('--starttls','-Z',help='Enable start tls',action="store_true")
parser.add_argument('--verify',help='verify server cert',action="store_true")
parser.add_argument('--CACert','-C',help='CA Cert file for verifying server cert')
parser.add_argument('--uri','-H',help="Target URI")
parser.add_argument('--threads','-t',help="Number of concurrent threads to run",type=int)
args = parser.parse_args()
if args.verify:
ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT,ldap.OPT_X_TLS_DEMAND)
if args.CACert:
ldap.set_option(ldap.OPT_X_TLS_CACERTFILE,args.CACert)
if not args.uri or not args.threads:
print "You must give a URI and a number of conccurrent threads"
sys.exit()
threads = []
counts = {}
q = Queue.Queue()
for i in range(args.threads):
t = threading.Thread(target=bind_loop,kwargs=dict(q = q, n = i+1, uri = args.uri, starttls = args.starttls))
t.start()
print 'Thread id {0} started.'.format(t.name)
while True:
time.sleep(10)
t_n = q.get_nowait()
try:
counts[t_n] += 1
except KeyError:
counts[t_n] = 1
for i, j in counts.iteritems():
print 'Thread-{0} has bind\'d {1} times.'.format(i, j)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment