創(chuàng)新互聯(lián)長期為上1000+客戶提供的網(wǎng)站建設服務,團隊從業(yè)經(jīng)驗10年,關注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務;打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為西崗企業(yè)提供專業(yè)的網(wǎng)站建設、網(wǎng)站制作,西崗網(wǎng)站改版等技術(shù)服務。擁有十余年豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。
關注微信公眾號:K哥爬蟲,持續(xù)分享爬蟲進階、JS/安卓逆向等技術(shù)干貨!
本文章中所有內(nèi)容僅供學習交流,抓包內(nèi)容、敏感網(wǎng)址、數(shù)據(jù)接口均已做脫敏處理,嚴禁用于商業(yè)用途和非法用途,否則由此產(chǎn)生的一切后果均與作者無關,若有侵權(quán),請聯(lián)系我立即刪除!
JSFuck、AAEncode、JJEncode 都是同一個作者,JSFuck 由日本的 Yosuke HASEGAWA 在 2010 創(chuàng)造,它可以將任意 JavaScript 編碼為僅使用 6 個符號的混淆形式 []()!+
,2012 年,Martin Kleppe 在 GitHub 上創(chuàng)建了一個 jsfuck 項目和一個 JSFuck.com 網(wǎng)站,其中包含使用該編碼器實現(xiàn)的 Web 應用程序。JSFuck 可用于繞過對網(wǎng)站上提交的惡意代碼的檢測,例如跨站點腳本(XSS)攻擊。JSFuck 的另一個潛在用途在于代碼混淆,目前的 jQuery 就已經(jīng)有經(jīng)過 JSFuck 混淆后的功能齊全的版本。
在線體驗地址:https://utf-8.jp/public/jsfuck.html http://www.jsfuck.com/
正常的一段 JS 代碼:
alert(1)
經(jīng)過 JSFuck 混淆之后的代碼類似于:
[][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]][([][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+([][[]]+[])[+!+[]]+(![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[+!+[]]+([][[]]+[])[+[]]+([][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+(!![]+[])[+!+[]]]((!![]+[])[+!+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+([][[]]+[])[+[]]+(!![]+[])[+!+[]]+([][[]]+[])[+!+[]]+(+[![]]+[][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+!+[]]]+(!![]+[])[!+[]+!+[]+!+[]]+(+(!+[]+!+[]+!+[]+[+!+[]]))[(!![]+[])[+[]]+(!![]+[][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+([]+[])[([][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+([][[]]+[])[+!+[]]+(![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[+!+[]]+([][[]]+[])[+[]]+([][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+(!![]+[])[+!+[]]][([][[]]+[])[+!+[]]+(![]+[])[+!+[]]+((+[])[([][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+([][[]]+[])[+!+[]]+(![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[+!+[]]+([][[]]+[])[+[]]+([][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+(!![]+[])[+!+[]]]+[])[+!+[]+[+!+[]]]+(!![]+[])[!+[]+!+[]+!+[]]]](!+[]+!+[]+!+[]+[!+[]+!+[]])+(![]+[])[+!+[]]+(![]+[])[!+[]+!+[]])()((![]+[])[+!+[]]+(![]+[])[!+[]+!+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]+(!![]+[])[+[]]+([][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[+!+[]+[!+[]+!+[]+!+[]]]+[+!+[]]+([+[]]+![]+[][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]])[!+[]+!+[]+[+[]]])
JSFuck 中常見的元素、數(shù)字、符號轉(zhuǎn)換如下表,更多元素可參考 JSFuck 官方 GitHub 或 JSFuck 維基百科:
Value | JSFuck |
---|---|
false | ![] |
true | !![] or !+[] |
NaN | +[![]] |
undefined | [][[]] |
Infinity | +(+!+[]+(!+[]+[])[!+[]+!+[]+!+[]]+[+!+[]]+[+[]]+[+[]]+[+[]]) |
Array | [] |
Number | +[] |
String | []+[] |
Boolean | ![] |
Function | []["filter"] |
eval | []["filter"]["constructor"]( CODE )() |
window | []["filter"]["constructor"]("return this")() |
+ | (+(+!+[]+(!+[]+[])[!+[]+!+[]+!+[]]+[+!+[]]+[+[]]+[+[]])+[])[!+[]+!+[]] |
. | (+(+!+[]+[+!+[]]+(!![]+[])[!+[]+!+[]+!+[]]+[!+[]+!+[]]+[+[]])+[])[+!+[]] |
0 | +[] |
1 | +!![] or +!+[] |
2 | !![]+!![] or !+[]+!+[] |
3 | !![]+!![]+!![] or !+[]+!+[]+!+[] |
a | (![]+[])[+!+[]] |
d | ([][[]]+[])[!+[]+!+[]] |
e | (!![]+[])[!+[]+!+[]+!+[]] |
f | (![]+[])[+[]] |
我們以字母 a 為例,來演示一遍其混淆的流程:
"false"[1]
:字母 a 取自字符串 false,在 false 中,a 的索引值是 1;(false+[])[1]
:false 可以寫成 false+[],即布爾常量 false 加上一個空數(shù)組;(![]+[])[1]
:false 又可以寫成 ![],即否定應用于空數(shù)組;(![]+[])[+true]
:1 是一個數(shù)字,我們可以把它寫成 +true;(![]+[])[+!![]]
:由于 false 是 ![],所以 true 就是 !![],生成最終混淆代碼。JSFuck 在調(diào)用方法時通常都是通過 Function(xxx)() 和 eval(xxx) 的形式來執(zhí)行,因此 JSFuck 常見解混淆的方式如下:
如前面 alert(1)
的混淆代碼,復制最外層最后一個括號內(nèi)的內(nèi)容到瀏覽器,就可以看到源代碼:
逆向的目標主要是翻頁接口 _signature
參數(shù),調(diào)用的加密方法仍然是 window.get_sign()
,和前面幾題是一樣的,本文不再贅述,不清楚的可以去看 K 哥上期的文章。
繼續(xù)跟進,會發(fā)現(xiàn)是一個 JSFuck 混淆:
我們將這段代碼復制出來,放到編輯器里面,這里以 PyCharm 為例,由于我們要選中匹配括號里的內(nèi)容,所以我們可以設置一下 PyCharm 括號匹配高亮為紅色,便于我們查找,依次點擊 File - Settings - Editor - Color Scheme - General - Code - Matched brace,設置 Background 為顯眼的顏色:
此時我們選中最后一個括號,往上找,就可以非常明顯地看到與之匹配的另一個括號,如下圖所示:
我們將括號里面的內(nèi)容復制出來(可以包含括號,也可以不包含),放到瀏覽器控制臺運行一下,就可以看到源碼了:
除了這種方法以外,我們還可以使用 Hook 的方式,直接捕獲源碼然后打印輸出,注意到這段混淆代碼最后沒有 ()
括號,那就是 eval 的方式執(zhí)行的,我們編寫 Hook eval 代碼如下:
eval_ = eval;
eval = function (a){
debugger;
return eval_()
}
// 另外提供一個 Hook Function 的代碼
// Function.prototype.constructor_ = Function.prototype.constructor;
// Function.prototype.constructor = function (a) {
// debugger;
// return Function.prototype.constructor_(a);
// };
刷新網(wǎng)頁,直接斷下,此時 a 的值就是源碼:
將源碼復制下來,本地分析一下:
(function () {
let time_tmp = Date.now();
let date = Date.parse(new Date());
window = {};
let click = window.document.onclick;
let key_tmp;
let iv_tmp;
if (!click) {
key_tmp = date * 1234;
} else {
key_tmp = date * 1244;
}
if (time_tmp - window.time < 1000) {
iv_tmp = date * 4321;
} else {
iv_tmp = date * 4311;
}
const key = CryptoJS.enc.Utf8.parse(key_tmp);
var iv = CryptoJS.enc.Utf8.parse(iv_tmp);
(function tmp(date, key, iv) {
function Encrypt(word) {
let srcs = CryptoJS.enc.Utf8.parse(word);
let encrypted = CryptoJS.AES.encrypt(srcs, key, {
iv: iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
});
return encrypted.ciphertext.toString().toUpperCase();
}
window.sign = Encrypt(date);
})(date, key, iv);
})();
可以看到就是一個 AES 加密,這里主要注意有兩個 if-else 語句,第一個判斷是否存在 window.document.onclick
,第二個是時間差的判斷,我們可以在控制臺去嘗試取一下 window.document.onclick
和 window.time
,看一下到底走的是 if 還是 else,在本地把這兩個值也補全即可,實際上經(jīng)過K哥測試 window.document.onclick
為 null,然后不管是走 if 還是 else 都是可以拿到結(jié)果的,所以對于本題來說,兩個 window 對象都無所謂,直接去掉,key_tmp
和 iv_tmp
任意取值都可以。
自此本題分析完畢,本地改寫之后,配合 Python 代碼攜帶 _signature 挨個計算每一頁的數(shù)據(jù),最終提交成功:
GitHub 關注 K 哥爬蟲,持續(xù)分享爬蟲相關代碼!歡迎 star !https://github.com/kgepachong/
以下只演示部分關鍵代碼,不能直接運行! 完整代碼倉庫地址:https://github.com/kgepachong/crawler/
/* ==================================
# @Time : 2021-12-13
# @Author : 微信公眾號:K哥爬蟲
# @FileName: challenge_4.js
# @Software: PyCharm
# ================================== */
var CryptoJS = require('crypto-js')
let date = Date.parse(new Date());
window = {};
let key_tmp = date * 1234;
// let key_tmp = date * 1244;
let iv_tmp = date * 4321;
// let iv_tmp = date * 4311;
const key = CryptoJS.enc.Utf8.parse(key_tmp);
var iv = CryptoJS.enc.Utf8.parse(iv_tmp);
(function tmp(date, key, iv) {
function Encrypt(word) {
let srcs = CryptoJS.enc.Utf8.parse(word);
let encrypted = CryptoJS.AES.encrypt(srcs, key, {
iv: iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
});
return encrypted.ciphertext.toString().toUpperCase();
}
window.sign = Encrypt(date);
})(date, key, iv);
function getSign() {
return window.sign
}
// 測試輸出
// console.log(getSign())
# ==================================
# --*-- coding: utf-8 --*--
# @Time : 2021-12-13
# @Author : 微信公眾號:K哥爬蟲
# @FileName: challenge_4.py
# @Software: PyCharm
# ==================================
import execjs
import requests
challenge_api = "http://spider.wangluozhe.com/challenge/api/4"
headers = {
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
"Cookie": "將 cookie 值改為你自己的!",
"Host": "spider.wangluozhe.com",
"Origin": "http://spider.wangluozhe.com",
"Referer": "http://spider.wangluozhe.com/challenge/4",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36",
"X-Requested-With": "XMLHttpRequest"
}
def get_signature():
with open('challenge_4.js', 'r', encoding='utf-8') as f:
ppdai_js = execjs.compile(f.read())
signature = ppdai_js.call("getSign")
print("signature: ", signature)
return signature
def main():
result = 0
for page in range(1, 101):
data = {
"page": page,
"count": 10,
"_signature": get_signature()
}
response = requests.post(url=challenge_api, headers=headers, data=data).json()
for d in response["data"]:
result += d["value"]
print("結(jié)果為: ", result)
if __name__ == '__main__':
main()