python 匿名ftp扫描

freebuf上面的一个py代码。仅供思想参考
#!/usr/bin/env python
 
"""
FtpScan.py - Scans for FTP servers allowing Anonymous Login
          Written by Sotd - twitter.com/#!/Sotd_
          Performance update by q8r9e4
"""
import sys
import threading
import Queue
import ftplib
import socket
import time
 
 
 
global ftpqueue
ftpqueue = Queue.Queue()
 
 
class Ftp(threading.Thread):
    """Handles connections"""
 
    def __init__(self, queue):
        threading.Thread.__init__(self)  
        self.queue = queue
         
    def run(self):
        """Connects and checks for anonymous login"""
        while True:
            try:
                ip_add = self.queue.get(False)
            except Queue.Empty:
                break
            try:
                ftp = ftplib.FTP(ip_add)
                ftp.login()
            except ftplib.all_errors:
                print 'Not Working: %s' % (ip_add)
            else:
                print 'Working: %s' % (ip_add)
                write = open('Ftp.txt', "a+")
                write.write(ip_add + '\n')
                write.close()  
                ftp.quit()
            finally:
                self.queue.task_done()  
 
class Scanner(threading.Thread):
    def __init__(self, queue):
        threading.Thread.__init__(self)  
        self.queue = queue
 
    def run(self):
        """Connects and checks for anonymous login"""
        global ftpqueue
        while True:
            try:
                ip_add = self.queue.get(False)
            except Queue.Empty:
                break
 
            try:
                s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                check = s.connect_ex((str(ip_add), 21))
                if check == 0:
                    ftpqueue.put(ip_add)
                else:
                    print 'No FTP: %s' % (ip_add)
                s.close()
 
            finally:
                self.queue.task_done()
       
 
def iprange():
    """Creates list of Ip's from Start_Ip to End_Ip and checks for port 21"""
    start_ip = sys.argv[1]
    end_ip = sys.argv[2]
    ip_range = Queue.Queue()
    start = list(map(int, start_ip.split(".")))
    end = list(map(int, end_ip.split(".")))
    tmp = start
    socket.setdefaulttimeout(3)
   
    ip_range.put(start_ip)
    while tmp != end:
        start[3] += 1
        for i in (3, 2, 1):
            if tmp[i] == 256:
                tmp[i] = 0
                tmp[i-1] += 1
        ip_range.put(".".join(map(str, tmp)))
 
    for i in range(10):
        time.sleep(0.1)
        thread = Scanner(ip_range)
        thread.setDaemon(True)
        thread.start()
    ip_range.join()
 
    if ftpqueue.empty():
        print '\nNo FTP servers found\n'
        sys.exit(0)
 
    for i in range(10):
        thread = Ftp(ftpqueue)
        thread.setDaemon(True)
        thread.start()
    ftpqueue.join()
 
if __name__ == '__main__':
    if len(sys.argv) != 3:
        print 'Usage: ./FtpScan <start_ip> <end_ip>'
        print 'Example: ./FtpScan 127.0.0.1 127.0.0.5'
        sys.exit(1)
    else:
        iprange()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值