首先,看一下程序截圖:
接着,再看一下下載的整個過程:
項目源碼及exe文件:https://github.com/xiexupang/free_wenku
簡要介紹這個項目的完成過程:
1.構建爬蟲,模擬瀏覽器頭信息
2.向漏洞提交鏈接及郵箱信息,實現下載併發送到郵箱的過程。
3.核心部分完成後,加上一個簡單的UI界面,大功告成。
源碼如下:
注意:運行源碼時,會用到一張名爲logo的png圖片,該圖片在上面github鏈接中。
import requests
import tkinter as tk
from tkinter import messagebox # import this to fix messagebox error
import threading # 引入多線程,使程序能同時進行爬蟲和進度條顯示
import time
# 創建主窗口
window = tk.Tk()
window.title('百度文庫下載神器')
window.geometry('630x250')
# 添加背景圖
canvas = tk.Canvas(window, height=200, width=600) # 創建畫布
image_file = tk.PhotoImage(file='logo.png') # 加載圖片文件
image = canvas.create_image(130, 20, anchor='nw', image=image_file) # 將圖片置於畫布上
canvas.pack() # 放置畫布
# 設置文檔鏈接輸入框
tk.Label(window, text='文檔鏈接:').place(x=50, y=80)
var_doc_url = tk.StringVar() # 定義變量,用於輸入用戶名
entry_doc_url = tk.Entry(window, width=66, textvariable=var_doc_url)
entry_doc_url.place(x=110, y=80)
# 設置郵箱輸入框
tk.Label(window, text='收件郵箱:').place(x=50, y=120)
var_mailbox = tk.StringVar() # 定義變量,用於輸入用戶名
entry_mailbox = tk.Entry(window, width=66, textvariable=var_mailbox).place(x=110, y=120)
# 設置下載進度條
tk.Label(window, text='下載進度:', ).place(x=50, y=160)
canvas = tk.Canvas(window, width=465, height=22, bg="white")
canvas.place(x=110, y=160)
# 顯示下載進度
def progress():
# 填充進度條
fill_line = canvas.create_rectangle(1.5, 1.5, 0, 23, width=0, fill="green")
x = 500 # 未知變量,可更改
n = 465 / x # 465是矩形填充滿的次數
for i in range(x):
n = n + 465 / x
canvas.coords(fill_line, (0, 0, n, 60))
window.update()
time.sleep(0.02) # 控制進度條流動的速度
tk.messagebox.showinfo( # 通知型彈窗
title='下載成功', # 窗口標題
message='文檔已發送,郵箱可能會對其進行攔截,請注意查看垃圾箱',
)
# 清空進度條
fill_line = canvas.create_rectangle(1.5, 1.5, 0, 23, width=0, fill="white")
x = 500 # 未知變量,可更改
n = 465 / x # 465是矩形填充滿的次數
for t in range(x):
n = n + 465 / x
# 以矩形的長度作爲變量值更新
canvas.coords(fill_line, (0, 0, n, 60))
window.update()
time.sleep(0) # 時間爲0,即飛速清空進度條
# 清空框內的文檔鏈接
def clear_url():
entry_doc_url.delete(0, tk.END)
# 檢查文檔鏈接與郵箱是否均已輸入
def check():
# 獲取輸入的文檔鏈接和郵箱
doc_url = str(var_doc_url.get())
mailbox = str(var_mailbox.get())
if doc_url == '':
tk.messagebox.showwarning(title='填寫不完整', message='文檔鏈接還沒輸入')
elif mailbox == '':
tk.messagebox.showwarning(title='填寫不完整', message='郵箱還沒輸入')
else: # 當鏈接與郵箱均已輸入後,啓動雙線程
two_threading()
# 下載文檔
def download_doc():
# 獲取輸入的文檔鏈接和郵箱
doc_url = str(var_doc_url.get())
mailbox = str(var_mailbox.get())
# 構建並僞裝爬蟲,讓對方以爲這是來自一般用戶的正常訪問
header = {'Accept': 'text/plain, */*; q=0.01',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh;q=0.9',
'Connection': 'keep-alive',
'Content-Length': '135',
'Content-Type': 'application/x-www-form-urlencoded',
'Host': '39.108.149.27:9999',
'Origin': 'http://39.108.149.27:9999',
'Referer': 'http://39.108.149.27:9999/',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36',
'X-Requested-With': 'XMLHttpRequest'
}
# 漏洞
url = 'http://39.108.149.27:9999/default.aspx'
# 爬蟲要向該漏洞提交的數據
datas = {
'username': '',
'password': '',
'txtUrl': doc_url,
'mail': mailbox,
}
# 爬蟲訪問該漏洞,提交攜帶的數據
response = requests.request("POST", url, data=datas, headers=header)
# 雙線程
def two_threading():
threads = []
t1 = threading.Thread(target=progress)
threads.append(t1)
t2 = threading.Thread(target=download_doc)
threads.append(t2)
for t in threads:
t.setDaemon(True)
t.start()
# 設置清空鏈接和一鍵下載按鈕
btn_clear = tk.Button(window, text='清空鏈接', command=clear_url)
btn_clear.place(x=150, y=205)
btn_download = tk.Button(window, text='一鍵下載', command=check)
btn_download.place(x=400, y=205)
window.mainloop()