目前进行的一个项目,需要对网页中的JavaScript进行分析,从而甄别出敏感的内容。
但是由于很多网页内部进行了加密和混淆,对于这样子的网页脚本呢,处理起来非常的困难。
之前有尝试使用JavaScript引擎进行模拟运行,从而得出解密后的内容。
再后来我想到一个好的办法。
因为无论时加密还是混淆了的代码,其仍旧需要时一个浏览器能够看懂的脚本才可以。
那么我只需要在关键的函数处实现注入,就能很简单的识别出敏感的信息了。
因此尝试着对eval和document.write方法进行了重写经过测试呢,也确实可以运行。现在把代码贴出来:
<script>
var tempeval = eval;
var tempwrite = document.write;
</script>
<script>
//重写eval
eval = function(str) {
alert('eval:'+str);
tempeval(str);
};
</script>
<script>
//重写document.write
function newwrite(str){
alert('write:'+str);
document.write = tempwrite;
document.write(str);
document.write = newwrite;
};
document.write = newwrite;
</script>
<script>
//测试
eval('document.write("<h1>This is true string</h1>");');
document.write("<h1>This is true string too</h1>");
</script>