python 到 poc
0x01 特殊函數
0x02 模塊
0x03 小工具開發記錄
特殊函數
# -*- coding:utf-8 -*- #內容見POC.demo; POC.demo2 ;def add(x,y):a=x+yprint(a)add(3,5) print('------------引入lambad版本:') add = lambda x,y : x+y print(add(3,5)) #lambda函數,在lambda函數后面直接加變量,變量后直接冒號,冒號后面是表達式,完美解決 #lambda表達式:lambda arg1,arg2,....,argn : expression using argumentsnumber = range(-2,11) print("map(函數)"),map(lambda x : x*2,number) print("map(函數)"),map(lambda x : x**2,number) '''**為次方'''#map()函數 help(map) 查看功能: map(函數,序列對象)
#返回將函數應用于以下項的結果列表:參數序列。如果給出了多個序列,
#則函數調用時使用由相應每個序列的項,如果不是全部,則用“無”替換缺失的值序列具有相同的長度。
#如果函數為None,則返回序列的項(如果有多個序列,則為元組列表)。
‘’‘zip()是Python的一個內建函數,它接受一系列可迭代的對象作為參數,將對象中對應的元素打包成一個個tuple(元組),然后返回由這些tuples組成的list(列表)。若傳入參數的長度不等,則返回list的長度和參數中長度最短的對象相同。利用*號操作符,可以將list unzip(解壓)。’’’
a=[1,2,3] b=[3,2,1] print(zip(a,b)) c='hello' d='123' print(zip(c,d)) '''長度不等時,取長度最小的''' demo1=['a','b','c'] demo2=['1','2','3'] demo3=zip(demo1,demo2) demo=dict(demo3) print(demo) '''構造字典'''如何導入模塊:
''' # 【1】導入整個模塊 >>> import sys >>> print sys.argv [''] #【2】只導入我們要用到的 >>> from sys import argv >>> print argv [''] 【3】模塊名太長,可以起個別名 >>> import sys as s >>> print s.argv [''] 【4】從模塊中導入所有 >>> from sys import * >>> print path #輸出sys模塊中的path ['', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/PILcompat', '/usr/lib/python2.7/dist-packages/gtk-2.0', '/usr/lib/python2.7/dist-packages/ubuntu-sso-client'] 最后一種導入方法不建議使用,原因:若自己定義的變量或函數與所導入模塊中的變量或函數同名,易產生混淆。 ''' # -*- coding:utf-8 -*-import sys import test.xiongzhang import test.yusys.path.append(r"C:\Users\30538\PycharmProjects\secure_POC\POC.demo.py") import demo r'''如果不是同目錄用這種方法告訴Python解釋器,我寫的文件在哪里, ("C:\Users\30538\PycharmProjects\secure_POC\POC.demo.py")是我的文件路徑然后就可以直接引入了 因為當Python解釋器都去了.py的文件,先將它變成由字節碼組成的pyc文件,然后這個pyc文件把文件給了一個叫Python虛擬機的東西運行繼續深入''' print(demo.lang) print(dir(demo)) """查看模塊屬性""" print(__name__) '''當作main程序執行''' print(demo.__name__) '''當作模塊,即為模塊的名稱''' # coding = utf-8lang='hello world' #模塊'''那么如何引用包中的庫呢''' #舉個栗子建立一個目錄為ichunqiu,里面放兩個py文件 ''''BaZong.py源碼如下:''' # #coding = utf-8 # # def BaZong(): # print 'HELL0 MOMO!!!' '''MoMo.py源碼如下:''' # coding = utf-8 # def MoMo(): # print # 'HELL0 BaZong!!!'‘’‘然后,咱們在與ichunqiu同級的目錄中,創建一個demo.py調用這個ichunqiu的包’’’
# # coding = utf-8 # # import ichunqiu.BaZong # import ichunqiu.MoMo # # ichunqiu.BaZong.BaZong() # ichunqiu.MoMo.MoMo()模塊
sys庫
-*- coding: utf-8 -*import sysprint ("THE FILE NAME:"),sys.argv[0] '''sys.argv是變量,專門向Python解釋器傳遞參數,他的功能是獲取程序外部向程序傳遞的參數''' # coding = utf-8import sysdef demo(): if len(sys.argv) < 2: print 'no action' sys.exit() # 退出當前程序 for i in range(len(sys.argv)): print 'arg[' + str(i) + ']', sys.argvif __name__ == '__main__': demo() '''sys.stdin, sys.stdout,sys.stderr 處理標準輸入,標準輸出, 標準錯誤''' '''sys.stdout,標準輸出,print的本質就是sys.stdout.write''' import sysfor i in range(6): print ('helloworld') for i in range(6): sys.stdout.write('worldhello\n')‘’‘stdout是一個類文件對象,調用了他的write函數就可以打印任何的字符串了,他們不會自己為你添加回車,要自己添加\n但是只有write的辦法,木有read的方法
還是由于是類文件對象,因此你可以講任何類文件賦值,然后重定向輸出
那可能會問,啥時重定向輸出,簡而言之,就是將輸出內容從"控制臺"轉到"文件"’’’
‘’‘os模塊’’’
import os print dir(os) print os.name #判斷正在使用的平臺,windows為’nt‘,linux為’posix‘ print os.getcwd() #獲取當前目錄 print os.listdir(r"C:\Users\30538\PycharmProjects\secure_POC\POC") #列出次目錄下的文件 # os.mkdir("demo3") #在本目錄下建立一個叫demo3的文件夾#os.rmdir(“demo3”) # 刪除demo3
#os.rename(“test.txt”,“test1.txt”) #修改文件名
#os.remove(“test1.txt”) # 刪除該文件
‘’‘os庫提供了在Python中使用操作系統的命令的方法就是用os.system()
以下是Winods系統’’’
comand = ‘dir’
os.system(comand)
‘’‘time模塊’’’
'''time模塊很常用的,比如說在延時注入中,就要用到他,他可以精確的知道程序的運行長短,老樣子,常用的 ''' import time print time.time() #獲得當前時間的時間戳 print time.clock() # 獲取進程時間 print time.localtime() #時間戳轉換成當地時間 print time.asctime() #元組表示時間:Wed Sep 15 21:43:14 2021‘’‘json模塊’’’
‘’‘說到傳遞數據,咱們會想到什么呢?XML和JSON
JSON(JavaScript Object Notation, JS 對象標記)
是一種輕量級的數據交換格式。
它基于 ECMAScript (w3c制定的js規范)的一個子集,
采用完全獨立于編程語言的文本格式來存儲和表示數據。
簡潔和清晰的層次結構使得 JSON 成為理想的數據交換語言。
易于人閱讀和編寫,同時也易于機器解析和生成,
并有效地提升網絡傳輸效率。’’’
‘’‘Python標準庫中有JSON模塊,主要是兩個功能,序列化(encoding)與反序列化(decoding)
encoding操作 :dumps()’’’
‘’‘hashlib模塊’’’
'''Python中的hashlib庫提供了大量的摘要算法,又叫散列算法''' import hashlib md5 = hashlib.md5() md5.update('admin')print md5.hexdigest() # update() 方法用于更新字典中的鍵/值對,可以修改存在的鍵對應的值,也可以添加新的鍵/值對到字典中。‘’‘random模塊’’’
import random print ('屬性:'),dir(random) #展示屬性 print random.random() #生成大于等于0,小于等于1的隨機浮點數 print random.uniform(88,99) #88到99的隨機浮點數 print random.randint(88,99) #88~99間的整數 print random.choice('xing') #生成xing之中的隨機字符 demo=[1,3,4,6] random.shuffle(demo) #打亂數字 print demo‘’‘requests模塊’’’
import requests print(dir(requests)) demo = requests.get("http://www.baidu.com") #請求實例 print(demo.cookies) #獲取cookie print(demo.headers) #獲取頭部信息 print((demo.status_code)) #查看http狀態碼 print(demo.text)# 查看網頁內容 print(demo.content) #以二進制打開服務器返回數據 print("post請求\n") payload = {'key1':'value1','key2':'value2'} r = requests.post("http://www.baidu.com",data=payload) #發送post請求 print(r.text) #data被賦值了之后,結果中才會多了form值,而form值就是post給服務器的內容‘’‘get請求’’’
# import requests # # url="http://127.0.0.1/brute/brute/brute_get.php" #假設網站有一個登陸界面 # payload = {'usernam':'admin','password':'admin','submit':'登陸'} #url中的參數 # r = requests.get(url,params=payload) #params參數收集傳入是不定個數的參數,并將收集的到參數以元組的方式存儲在params中,如果沒有傳入參數params就是個空元組 # result = r.content # if str(result).find('succ'): #str將二進制數據轉化成字符串 # print("admin:admin"+'successful')‘’‘post請求’’’
import requests url="http://127.0.0.1/brute/brute/brute_get.php" data = {'usernam':'admin','password':'admin','submit':'登陸'} r = requests.post(url,data=data) print(r.status_code) if r.test.find('succ'): print('admin:admin'+'successful')‘’‘pillow模塊’’’
‘’‘免費的圖像處理工具包,是python下的圖像處理模塊,支持多種格式,
并提供強大的圖形與圖像處理功能。對于簡單的圖像處理或者大批量的簡單圖像處理任務,
pPillow是PIL的一個派生分支,但如今已經發展成為比PIL本身更具活力的圖像處理庫。
pillow可以說已經取代了PIL’’’
小工具記錄:
目錄掃描:
‘’‘目錄掃描’’’
import requests import sysurl=sys.argv[1] with open("dir.txt","r")as f: for line in f.readlines(): line = line.strip() r = requests.get(url+line) if strr.status_code == 2*: print("url:"+r.url+"exist")使用說明:將字典文件放在同目錄下,
Terminal命令:python poc6.py
https://www.baidu.com/
計算目標網站的icon hash值用于搜素工具搜索:atp-get install python-dev
pip install mmh3
pip install requests
編寫python腳本
vim icon_hash.py
import mmh3
import requests
response = requests.get(‘https://xxx.xxx.xxx/favicon.ico’)
favicon = response.content.encode(‘base64’)
hash = mmh3.hash(favicon)
print hash
指紋識別:
(指紋自己收集一波,暫時先不放了。。。。。太長了)
#!/usr/bin/env python3# -*- coding: utf-8 -*-# @Time : 2019/7/27 10:00 PM# @Author : w8ay# @File : fofacms.pyimport jsonimport reimport osdef read_config():config_file = os.path.join("fofacms.json")with open(config_file, 'r') as f:mark_list = json.load(f)return mark_listclass Fofacms:def __init__(self, html, title):self.html = html.lower()self.title = title.lower()def get_result(self, a):builts = ["(body)\s*=\s*\"", "(title)\s*=\s*\""]if a is True:return Trueif a is False:return Falsefor regx in builts:match = re.search(regx, a, re.I | re.S | re.M)if match:name = match.group(1)length = len(match.group(0))content = a[length: -1]if name == "body":if content.lower() in self.html:return Trueelse:return Falseelif name == "title":if content.lower() in self.title:return Trueelse:return Falseraise Exception("不能識別的a:" + str(a))def calc_express(self, expr):# title="NBX NetSet" || (header="Alternates" && body="NBX")# 1||(2&&3) => 1 2 3 && ||# header="X-Copyright: wspx" || header="X-Powered-By: ANSI C"# header="SS_MID" && header="squarespace.net"expr = self.in2post(expr)# print("后綴表達式", expr)stack = []special_sign = ["||", "&&"]if len(expr) > 1:for exp in expr:if exp not in special_sign:stack.append(exp)else:a = self.get_result(stack.pop())b = self.get_result(stack.pop())c = Noneif exp == "||":c = a or belif exp == "&&":c = a and bstack.append(c)if stack:return stack.pop()else:return self.get_result(expr[0])def in2post(self, expr):""" :param expr: 前綴表達式:return: 后綴表達式Example:1||(2&&3) => 1 2 3 && ||"""stack = [] # 存儲棧post = [] # 后綴表達式存儲special_sign = ["&&", "||", "(", ")"]builts = ["body\s*=\s*\"", "title\s*=\s*\""]exprs = []tmp = ""in_quote = 0 # 0未發現 1發現 2 待驗證狀態for z in expr:is_continue = Falsetmp += zif in_quote == 0:for regx in builts:if re.search(regx, tmp, re.I):in_quote = 1is_continue = Truebreakelif in_quote == 1:if z == "\"":in_quote = 2if is_continue:continuefor i in special_sign:if tmp.endswith(i):if i == ")" and in_quote == 2:# 查找是否有左括號zuo = 0you = 0for q in exprs:if q == "(":zuo += 1elif q == ")":you += 1if zuo - you < 1:continue# print(": " + tmp + " " + str(in_quote))length = len(i)_ = tmp[0:-length]if in_quote == 2 or in_quote == 0:if in_quote == 2 and not _.strip().endswith("\""):continueif _.strip() != "":exprs.append(_.strip())exprs.append(i)tmp = ""in_quote = 0breakif tmp != "":exprs.append(tmp)if not exprs:return [expr]# print("分離字符", exprs)for z in exprs:if z not in special_sign: # 字符直接輸出post.append(z)else:if z != ')' and (not stack or z == '(' or stack[-1] == '('): # stack 不空;棧頂為(;優先級大于stack.append(z) # 運算符入棧elif z == ')': # 右括號出棧while True:x = stack.pop()if x != '(':post.append(x)else:breakelse: # 比較運算符優先級,看是否入棧出棧while True:if stack and stack[-1] != '(':post.append(stack.pop())else:stack.append(z)breakwhile stack: # 還未出棧的運算符,需要加到表達式末尾post.append(stack.pop())return postdef fingerprint(body):mark_list = read_config()# titlem = re.search('<title>(.*?)<\/title>', resp, re.I | re.M | re.S)title = ""if m:title = m.group(1).strip()fofa = Fofacms(body, title)whatweb = ""for item in mark_list:express = item["rule"]name = item["name"]# print("rule:" + express)try:if fofa.calc_express(express):whatweb = name.lower()breakexcept Exception:print("fofacms error express:{} name:{}".format(express, name))return whatwebimport requestsurl = "https://x.hacking8.com"print("識別:" + url)resp = requests.get(url).textprint(fingerprint(resp))更新中:
看完點贊關注不迷路!!! 后續繼續更新優質安全內容!!!
總結
以上是生活随笔為你收集整理的python 到 poc的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 中国首款&quot;信息化”坦克
- 下一篇: 【CVE-2018-12613】phpm