這篇文章給大家分享的是有關(guān)python如何實(shí)現(xiàn)代碼審計(jì)的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來(lái)看看吧。
def execute(request): context ={} ip= request.POST.get("ip") username= request.POST.get("username") password= request.POST.get("password") idnex= int(request.POST.get("index")) current_time=request.POST.get("time") context = {"ip":ip,"username":username,"password":password,"result":False} ippattern="(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)" if(re.match(ippattern,ip)): pass else: context['error']="ip格式不正確" log("error","[-]%s ip is error"%(ip)) print ("[-]%s ip格式不正確"%(ip)) #return render(request, 'test.html', context) return HttpResponse(json.dumps(context)) try: length,scripts=executeScript.getScriptNums() if(idnex>length or idnex<1): context['error']="腳本索引值錯(cuò)誤" log("error","[-]%s %s %s Script index value error"%(ip,str(length),str(index))) print ("[-]%s %s %s 腳本索引值錯(cuò)誤"%(ip,str(length),str(index))) return HttpResponse(json.dumps(context)) script=scripts[idnex-1] #判斷是否是端口掃描 if("port_scan" in script): current_path=os.getcwd() payload="python %s//jixianjiancha/check/%s %s"%(current_path,script,ip) commandResult=commands.getoutput(payload) result=ast.literal_eval(commandResult)
第一步:獲取前臺(tái)傳入的ip: ip= request.POST.get("ip")
第二步:判斷輸入的ip是否合法
ippattern="(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d).(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d).(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d).(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)"
if(re.match(ippattern,ip)): pass else:
這段代碼仔細(xì)一看,只是判斷了ip是否以正常ip開頭,比如 12.12.121.12,只要是以正常ip開頭的,就可以通過ip的檢測(cè)。比如輸入的ip為:127.0.0.1; ping -c 1 127.0.0.1
第三步:
payload="python %s//jixianjiancha/check/%s %s"%(current_path,script,ip)
commandResult=commands.getoutput(payload)
將判斷后的ip直接拼接到payload中,然后使用commands命令執(zhí)行函數(shù)執(zhí)行命令
由于ip過濾不嚴(yán)格,所以會(huì)造成任意命令執(zhí)行漏洞
修復(fù)改問題主要是要嚴(yán)格過濾ip,因此將ip以'.'分割成4份,判斷每一份是否是數(shù)字,如果不是全是則表名輸入的ip不合法。
ip= request.POST.get("ip") testip=ip.split(".") if(testip[0].isdigit() and testip[1].isdigit() and testip[2].isdigit()and testip[3].isdigit()): pass else: context['error']="ip格式不正確" log("error","[-] %s ip is error"%(ip))
感謝各位的閱讀!關(guān)于“python如何實(shí)現(xiàn)代碼審計(jì)”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
標(biāo)題名稱:python如何實(shí)現(xiàn)代碼審計(jì)-創(chuàng)新互聯(lián)
本文網(wǎng)址:http://www.2m8n56k.cn/article28/dhsccp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)公司、外貿(mào)建站、品牌網(wǎng)站建設(shè)、域名注冊(cè)、云服務(wù)器、企業(yè)網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:[email protected]。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容