前言
前一阵子开始研究了一下xssbot,在网上找了许多文章,但这些文章重点都放在bot代码部分,对其完整网站的实现并不是说的挺清楚,这篇文章总结了我从入门到编写一个完整xss demo的心得。
bot原型
bot做些什么?
在我看来,bot就是代替管理员去点击页面,然后获取内容,执行js。所以对于bot来说,就是能够执行js。
bot准备
一般来说现在的xssbot基本上是通过python或者java实现。这里我们采用python+selenium+webdriver。
而webdriver又有chrome webdriver,Firefox webdriver,phantomjs。
selenium
selenium是用来控制webdriver的,在python中直接使用pip安装之后便可导入。
1 2 3
| import selenium from selenium import webdriver from selenium.common.exceptions import WebDriverException
|
chrome webdriver
如果bot运行在windows上,我建议使用chrome webdriver。如果要使用chrome webdriver,不仅要安装chrome,还必须在谷歌官网下载webdriver。
https://sites.google.com/a/chromium.org/chromedriver/downloads
1 2 3 4 5 6 7 8
| import selenium from selenium import webdriver from selenium.common.exceptions import WebDriverException chromedriver = "C:\chromedriver.exe" browser = webdriver.Chrome(chromedriver) url = "http://www.baidu.com" browser.get(url) browser.quit()
|
firefox webdriver
firefox与chrome类似,需要一个geckodriver。
https://github.com/mozilla/geckodriver/releases/
在ubuntu下使用firefox较为简单,因为firebox可直接使用apt安装,然后把geckodriver添加到/bin/并赋予执行权限就可以了。
1 2 3 4 5 6 7
| import selenium from selenium import webdriver from selenium.common.exceptions import WebDriverException browser = webdriver.Firefox() url = "http://www.baidu.com" browser.get(url) browser.quit()
|
phantomjs
phantomjs的优势是并不需要浏览器的支持,而且支持多平台。由于网上爬虫案例很多这里便不再多讲。
bot实现
chrome和firebox的webdriver都需要桌面,而由于我们部署ctf题目通常是在linux服务器上,所以通常需要一个虚拟的桌面。
1 2 3
| from pyvirtualdisplay import Display display = Display(visible=0, size=(800,800)) display.start()
|
再加上上面的两种webdriver代码,如此我们便可以运行一个具有浏览器特性的bot。
1 2 3 4 5 6 7 8 9 10
| from pyvirtualdisplay import Display import selenium from selenium import webdriver from selenium.common.exceptions import WebDriverException display = Display(visible=0, size=(800,800)) display.start() browser = webdriver.Firefox() url = "http://www.baidu.com" browser.get(url) browser.quit()
|
实战
上面我们讲了如何实现bot本身,现在来讲讲如何完成一个完整的留言型xss demo。一种基本的思路如下:
1.用户提交留言到数据库,并设置验证码保证恶意刷payload导致bot卡死。
2.数据库添加一栏是否被bot访问的标志位。
3.后端提供访问接口,避免bot远程访问数据库,引起不必要的麻烦。
4.后端以cookie或者ip来判断是否是bot访问。
5.bot访问一条数据后,将标志位update,说明该条留言已经被bot访问过。
按照此思路我们可以写出后端管理员页面的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| <?php
require_once("db.php"); error_reporting(0); if(!isset($_COOKIE['key']) && $_COOKIE['key'] !== "admin123!@#") { die("you are not admin!"); } $query1 = "select * from content where isread = '0' limit 1"; $result = $mysqli->query($query1); if($result->num_rows == 0) { die("no message unread."); } else { $row = $result->fetch_array(); $contentid = $row['contentid']; $content = $row['content']; echo($content); $query2 = "update content set isread = 1 where contentid ='".$contentid."'"; $mysqli->query($query2); } ?>
|
所以我们也可以完成bot部分的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| import selenium from selenium import webdriver from selenium.common.exceptions import WebDriverException import time from pyvirtualdisplay import Display url='http://127.0.0.1/admin666.php' display = Display(visible=0,size=(800,600)) display.start() while 1: try: browser = webdriver.Firefox() browser.get(url) browser.add_cookie({'name':'key','value':'admin123!@#','path':'/'}) browser.get(url) time.sleep(1) browser.quit() except Exception as e: print (e) continue
|
如此我们便实现了一个简单的留言型xss demo,虽然需要后端支持,但相对来说较为安全。希望能有更好的方法来实现。
代码已上传至GitHub:
https://github.com/zer0e/xssbot_1/
参考
学长的文章—>>http://www.freebuf.com/articles/web/133456.html
https://www.bubbles966.cn/blog/2017/10/29/xssbot-with-firefox/