Advertisement
Sotd

FtpScan.py

Jun 29th, 2012
442
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.30 KB | None | 0 0
  1. """
  2. FtpScan.py - Scans for FTP servers allowing Anonymous Login
  3.          Written by Sotd - twitter.com/#!/Sotd_
  4. """
  5. import sys
  6. import threading
  7. import Queue
  8. import ftplib
  9. import socket
  10.  
  11. class Ftp(threading.Thread):
  12.     """Handles connections"""
  13.  
  14.     def __init__(self, queue):
  15.         threading.Thread.__init__(self)  
  16.         self.queue = queue
  17.          
  18.     def run(self):
  19.         """Connects and checks for anonymous login"""
  20.         while True:
  21.             try:
  22.                 ip_add = self.queue.get(False)
  23.             except Queue.Empty:
  24.                 break
  25.             try:
  26.                 ftp = ftplib.FTP(ip_add)
  27.                 ftp.login()
  28.             except ftplib.all_errors:
  29.                 print 'Not Working: %s' % (ip_add)
  30.             else:
  31.                 print 'Working: %s' % (ip_add)
  32.                 write = open('Ftp.txt', "a+")
  33.                 write.write(ip_add + '\n')
  34.                 write.close()
  35.                 ftp.quit()
  36.             finally:
  37.                 self.queue.task_done()
  38.  
  39.        
  40. def iprange():
  41.     """Creates list of Ip's from Start_Ip to End_Ip and checks for port 21"""
  42.     queue = Queue.Queue()
  43.     start_ip = sys.argv[1]
  44.     end_ip = sys.argv[2]
  45.     ip_range = []
  46.     start = list(map(int, start_ip.split(".")))
  47.     end = list(map(int, end_ip.split(".")))
  48.     tmp = start
  49.     socket.setdefaulttimeout(3)
  50.    
  51.     ip_range.append(start_ip)
  52.     while tmp != end:
  53.         start[3] += 1
  54.         for i in (3, 2, 1):
  55.             if tmp[i] == 256:
  56.                 tmp[i] = 0
  57.                 tmp[i-1] += 1
  58.         ip_range.append(".".join(map(str, tmp)))
  59.  
  60.     for add in ip_range:
  61.         s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  62.         check = s.connect_ex((str(add), 21))
  63.         if check == 0:
  64.             queue.put(add)
  65.         else:
  66.             print 'No FTP: %s' % (add)
  67.         s.close()
  68.  
  69.     if queue.empty():
  70.         print '\nNo FTP servers found\n'
  71.         sys.exit(0)
  72.  
  73.     for i in range(10):
  74.         thread = Ftp(queue)
  75.         thread.setDaemon(True)
  76.         thread.start()
  77.     queue.join()
  78.  
  79. if __name__ == '__main__':
  80.     if len(sys.argv) != 3:
  81.         print 'Usage: ./FtpScan <start_ip> <end_ip>'
  82.         print 'Example: ./FtpScan 127.0.0.1 127.0.0.5'
  83.         sys.exit(1)
  84.     else:
  85.         iprange()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement