Guest User

Untitled

a guest
May 10th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.38 KB | None | 0 0
  1.  
  2. #InsoBot: An IRC bot that allows for basic file upload by an authorized
  3. #user via use of an IRC network of one's choice.
  4.  
  5. import socket,os,string,sys,ftplib,hashlib
  6. import time
  7.  
  8. #Define Variables that will be needed throughout the program.
  9. #Only change the variables in the box unless you've read the code
  10. #and understand what you're doing!
  11. ###############################################
  12. Host = "irc.example.com"                 #
  13. Port = 6667                                   #
  14. Filename = ""                                 #
  15. chanpass = ""                      #
  16. FTPserver = "server.example.com"   #
  17.                                               #
  18. ftpuser="exampleuser"                        #
  19. ftppass="example_pass"                              #
  20. passhash = "example Hash" #
  21. ###############################################
  22. Auth = 0
  23. Filename = ""
  24. NICK="insobot"
  25. IDENT="insobot"
  26. REALNAME="insobot"
  27. readbuffer=""
  28. joined = 0
  29. perm = 0
  30.  
  31. ############################
  32. #Define Essential Functions#
  33. ############################
  34.  
  35. #Determines if user is allowed to perform an action.
  36. def getperm(pswd):
  37.     global perm
  38.     password = hashlib.md5()
  39.     password.update(pswd)
  40.     a = password.hexdigest()
  41.     if a == passhash:
  42.         user = line[0]
  43.         perm = 1
  44.        
  45.         s.send("PRIVMSG %s \r\n" % "#insobot, You are authorized")
  46.     else:
  47.         s.send("PRIVMSG %s \r\n" % "#insobot, Wrong password")
  48.  
  49. #Upload file to an FTP Server of your choice.
  50. def upfile(server,perm,name,ftpuser,ftppass,fname):
  51.  
  52.  if perm == 1:
  53.    try:
  54.     ftp=ftplib.FTP(server)
  55.     ftp.login(ftpuser,ftppass)
  56.     f = open(name,'rb')
  57.     ftp.storbinary("STOR `Filename`",f)
  58.     ftp.rename("`Filename`",fname)
  59.    
  60.    except ftplib.all_errors,e:  
  61.     ermsg = str(e)
  62.    
  63.    
  64.     s.send("PRIVMSG %s \r\n" % "#insobot, There was an error with the FTP server")
  65.     s.send("PRIVMSG %s \r\n" % "#insobot, Server Response: %(server_response)s" % {"server_response": ermsg})
  66.     pass
  67.  else:
  68.   s.send("PRIVMSG %s \r\n" % "#insobot, You are not authorized")
  69.   pass
  70.  perm = 0
  71.  
  72. #Connect to the Server
  73. global s
  74. s = socket.socket( )
  75. s.connect((Host,Port))
  76. s.send("NICK %s\r\n" % NICK)
  77. s.send("USER %s %s bla :%s\r\n" % (IDENT, Host, REALNAME))
  78. time.sleep(3)
  79.  
  80. #Receive and Process data
  81. while 1:
  82.     readbuffer=readbuffer+s.recv(1024)
  83.     temp=string.split(readbuffer, "\n")
  84.     readbuffer=temp.pop( )
  85.  
  86.     for line in temp:
  87.         line=string.rstrip(line)
  88.         print line
  89.     line=string.split(line)
  90.     try:
  91.  
  92. #Tell the server we are still here if it asks.
  93.         if(line[0]=="PING"):
  94.                 s.send("PONG %s\r\n" % line[1])
  95.  
  96.     #Join our channel.
  97.         if joined == 0:
  98.             s.send("JOIN %s\r\n" % "#insobot")
  99.             s.send("MODE insobot +i %s \r\n")
  100.             mode = "MODE #insobot +k " + chanpass +  " %s \r\n"
  101.             s.send(mode)
  102.             joined == 1
  103.        
  104.         if (line[3]==":+login"):
  105.             getperm(line[4])
  106.         #Change Filename if no dangerous chars are present.
  107.         if (line[3]==":+filename"):
  108.             allow = 1
  109.             for a in ["../","%","..","..\""]:
  110.                 if a in line[4]:
  111.                     s.send("PRIVMSG %s \r\n" % "#insobot, I don't think so.")
  112.                     allow = 0
  113.                
  114.             if allow == 1:
  115.                 Filename = "/home/patrick/Documents/" + line[4]
  116.                 s.send("PRIVMSG %s \r\n" % "#insobot, Filenamed changed.")
  117.                 print Filename
  118.                 ftpfname = line[4]
  119.        
  120.             else:
  121.                 pass
  122.         if (line[3]==":+upload"):
  123.             upfile(FTPserver,perm,Filename,ftpuser,ftppass,ftpfname)
  124.            
  125.            
  126.     except IndexError:
  127.         pass
Add Comment
Please, Sign In to add comment