SHARE
TWEET

Anonymous FTP Scanner - Python Script ( Tools Yard )

a guest Jul 22nd, 2012 4,744 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2.  
  3. """
  4. FtpScan.py - Scans for FTP servers allowing Anonymous Login
  5.           Written by Sotd - twitter.com/#!/Sotd_
  6.           Performance update by q8r9e4
  7. """
  8. import sys
  9. import threading
  10. import Queue
  11. import ftplib
  12. import socket
  13. import time
  14.  
  15.  
  16.  
  17. global ftpqueue
  18. ftpqueue = Queue.Queue()
  19.  
  20.  
  21. class Ftp(threading.Thread):
  22.     """Handles connections"""
  23.  
  24.     def __init__(self, queue):
  25.         threading.Thread.__init__(self)  
  26.         self.queue = queue
  27.          
  28.     def run(self):
  29.         """Connects and checks for anonymous login"""
  30.         while True:
  31.             try:
  32.                 ip_add = self.queue.get(False)
  33.             except Queue.Empty:
  34.                 break
  35.             try:
  36.                 ftp = ftplib.FTP(ip_add)
  37.                 ftp.login()
  38.             except ftplib.all_errors:
  39.                 print 'Not Working: %s' % (ip_add)
  40.             else:
  41.                 print 'Working: %s' % (ip_add)
  42.                 write = open('Ftp.txt', "a+")
  43.                 write.write(ip_add + '\n')
  44.                 write.close()  
  45.                 ftp.quit()
  46.             finally:
  47.                 self.queue.task_done()  
  48.  
  49. class Scanner(threading.Thread):
  50.     def __init__(self, queue):
  51.         threading.Thread.__init__(self)  
  52.         self.queue = queue
  53.  
  54.     def run(self):
  55.         """Connects and checks for anonymous login"""
  56.         global ftpqueue
  57.         while True:
  58.             try:
  59.                 ip_add = self.queue.get(False)
  60.             except Queue.Empty:
  61.                 break
  62.  
  63.             try:
  64.                 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  65.                 check = s.connect_ex((str(ip_add), 21))
  66.                 if check == 0:
  67.                     ftpqueue.put(ip_add)
  68.                 else:
  69.                     print 'No FTP: %s' % (ip_add)
  70.                 s.close()
  71.  
  72.             finally:
  73.                 self.queue.task_done()
  74.        
  75.  
  76. def iprange():
  77.     """Creates list of Ip's from Start_Ip to End_Ip and checks for port 21"""
  78.     start_ip = sys.argv[1]
  79.     end_ip = sys.argv[2]
  80.     ip_range = Queue.Queue()
  81.     start = list(map(int, start_ip.split(".")))
  82.     end = list(map(int, end_ip.split(".")))
  83.     tmp = start
  84.     socket.setdefaulttimeout(3)
  85.    
  86.     ip_range.put(start_ip)
  87.     while tmp != end:
  88.         start[3] += 1
  89.         for i in (3, 2, 1):
  90.             if tmp[i] == 256:
  91.                 tmp[i] = 0
  92.                 tmp[i-1] += 1
  93.         ip_range.put(".".join(map(str, tmp)))
  94.  
  95.     for i in range(10):
  96.         time.sleep(0.1)
  97.         thread = Scanner(ip_range)
  98.         thread.setDaemon(True)
  99.         thread.start()
  100.     ip_range.join()
  101.  
  102.     if ftpqueue.empty():
  103.         print '\nNo FTP servers found\n'
  104.         sys.exit(0)
  105.  
  106.     for i in range(10):
  107.         thread = Ftp(ftpqueue)
  108.         thread.setDaemon(True)
  109.         thread.start()
  110.     ftpqueue.join()
  111.  
  112. if __name__ == '__main__':
  113.     if len(sys.argv) != 3:
  114.         print 'Usage: ./FtpScan <start_ip> <end_ip>'
  115.         print 'Example: ./FtpScan 127.0.0.1 127.0.0.5'
  116.         sys.exit(1)
  117.     else:
  118.         iprange()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top