Skip to content

Instantly share code, notes, and snippets.

@pjlantz
Created July 13, 2010 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pjlantz/473927 to your computer and use it in GitHub Desktop.
Save pjlantz/473927 to your computer and use it in GitHub Desktop.
import socket, threading
from twisted.internet import defer, reactor
class IRC(object):
"""
Implementation of a irc client to do irc based
botnet monitoring
"""
def __init__(self):
"""
Constructor sets up configs, threadManager object
and various booleans used for network registration
when connecting to a IRC server
"""
self.continueThread = True
self.doJoin = False
self.firstPing = True
def doConnect(self, nickname):
"""
Setup socket and connect to irc server
"""
self.irc = socket.socket()
self.irc.connect(('irc.freenode.net', 6667))
self.irc.send('NICK '+ nickname + '\r\n')
self.irc.send('USER Spybot SpyBot Spybot :SpyBot\r\n')
self.start()
def __doJoin(self):
"""
Join channel specified in the configuration
"""
self.irc.send('JOIN #testasdf kluczbork\r\n')
def doStop(self):
"""
Stop this thread
"""
self.continueThread = False
self.irc.shutdown(socket.SHUT_RDWR)
self.irc.close()
def start(self):
"""
Make connection and define thread loop
"""
d = self.handleProtocol()
if d != None:
d.addCallback(printData)
reactor.callWhenRunning(self.start)
def handleProtocol(self):
"""
Handle incoming irc protocol and responses
"""
d = defer.Deferred()
if self.doJoin: # after registration
self.__doJoin()
data = self.irc.recv(1024)
if data.find('PING') != -1: # Ping
self.irc.send('PONG ' + data.split()[1] + '\r\n') # Pong
if self.firstPing:
self.doJoin = True # registered and ready to join
self.firstPing = False
return
elif data.find('TOPIC') != -1: # Topic
# Log topic info
#print "IRC: Got new topic set"
pass
elif data.find('332') != -1: # Current topic
# Log current topic info
#print "IRC: Got current channel topic on join"
pass
elif data.find('PRIVMSG') != -1: # privmsg
# Log privmsg
#print "IRC: Got message"
pass
else:
# log unrecognized commands, can also be MOTD and NAMES list
pass
reactor.callWhenRunning(d.callback, data)
return d
class Monitor:
def __init__(self):
self.counter = 0
def run(self):
reactor.callInThread(IRC().doConnect, 'testing1234' + str(self.counter))
self.counter = self.counter + 1
class React(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
reactor.run(installSignalHandlers=0)
def printData(data):
pass
m = Monitor()
React().start()
while True:
irc = IRC()
enter = raw_input("Press enter to start bot: ")
m.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment