首先,從問題出發:
在谷歌學術鏡像網收集着多個谷歌鏡像的鏈接。我們目標就是要把這些鏈接拿到手。
F12查看源碼可以發現,對應的a標簽並不是我們想要的鏈接,而是一個js點擊函數。
其實在
onclick="visit('AD0mWAw2VVYgWiAdDB4LHQwqaxY2XxcVL0M9FiEYTxM=')"
上面一段代碼里,AD0mWAw2VVYgWiAdDB4LHQwqaxY2XxcVL0M9FiEYTxM=就是加密后的url鏈接。
visit函數的作用就是對這一串字符串進行了解密並訪問。
通過搜索我們可以清楚visit函數它的源碼:
就是這一段:
function visit(url) { var newTab = window.open('about:blank'); //打開新窗口 if(Gword!='') url = strdecode(url);//解密字符串,將它變成url newTab.location.href = url;//訪問這個url }
ok,又扯出來一個叫strdecode的函數,我們繼續找找看:
function strdecode(string) { string = base64decode(string);//base64decode函數處理參數 key = Gword + hn;//Gword和hn兩個變量可以在網頁源碼找到 len = key.length;//key的長度 code = ''; for (i = 0; i < string.length; i++) {//這個循環是中間的解密過程 var k = i % len; code += String.fromCharCode(string.charCodeAt(i) ^ key.charCodeAt(k)); } return base64decode(code);//再利用base64decode處理一次中間過程產出的code變量,其實這個就是真正的url了 }
其實sredecode的參數string就是類似一開始說的"AD0mWAw2VVYgWiAdDB4LHQwqaxY2XxcVL0M9FiEYTxM="
又有一個叫base64decode的函數,那我們再找找看:
ok,好長一段js代碼。我都不想去理解它了、、
怎么辦?
咱可以利用python的execjs庫來執行js代碼,只需把js代碼保存下來就好。那我們把能用到的js都給保存下來像這樣:
execjs安裝:pip install PyExecJS
var base64DecodeChars = new Array(-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1); function base64decode(str) { var c1, c2, c3, c4; var i, len, out; len = str.length; i = 0; out = ""; while (i < len) { do { c1 = base64DecodeChars[str.charCodeAt(i++) & 0xff] } while (i < len && c1 == -1); if (c1 == -1) break; do { c2 = base64DecodeChars[str.charCodeAt(i++) & 0xff] } while (i < len && c2 == -1); if (c2 == -1) break; out += String.fromCharCode((c1 << 2) | ((c2 & 0x30) >> 4)); do { c3 = str.charCodeAt(i++) & 0xff; if (c3 == 61) return out; c3 = base64DecodeChars[c3] } while (i < len && c3 == -1); if (c3 == -1) break; out += String.fromCharCode(((c2 & 0XF) << 4) | ((c3 & 0x3C) >> 2)); do { c4 = str.charCodeAt(i++) & 0xff; if (c4 == 61) return out; c4 = base64DecodeChars[c4] } while (i < len && c4 == -1); if (c4 == -1) break; out += String.fromCharCode(((c3 & 0x03) << 6) | c4) } return out } function strdecode(string) { string = base64decode(string); key = "author: link@scmor.com.ac.scmor.com"; len = key.length; code = ''; for (i = 0; i < string.length; i++) { var k = i % len; code += String.fromCharCode(string.charCodeAt(i) ^ key.charCodeAt(k)); } return base64decode(code); }
然后利用python請求網頁源碼,把AD0mWAw2VVYgWiAdDB4LHQwqaxY2XxcVL0M9FiEYTxM=這些密文用re匹配出來。
最后利用上面保存的js代碼解密:
最終代碼(PS:也放在github上面了,博客左上角點擊直達):
import requests from lxml import etree import re import execjs
with open('scmor.js', 'r', encoding='utf-8') as f: js = f.read() ctx = execjs.compile(js) #編譯保存的js def get_html(): #請求html try: r = requests.get("http://ac.scmor.com/") r.encoding = r.apparent_encoding return r.text except: print('產生異常') def parse_html(html): #解析html,並用re匹配密文 tree = etree.HTML(html) script_text = tree.xpath('//script/text()')[0] autourls = re.findall(r'AD0mWAw[a-zA-Z0-9]+=*', script_text) return autourls def decode(string): #利用保存的js解密密文 return ctx.call('strdecode', string) if __name__ == '__main__': html = get_html() autourls = parse_html(html) for url in autourls: print(decode(url)) #print(decode('AD0mWAw2VVYgWiAdDB4LHQwqaxY2XxcVL0M9FiEYTxM=')) #decode將源碼中的加密字符串轉url
參考文章:https://www.cnblogs.com/happymeng/p/10755244.html
The end~