日期:2014-05-16  浏览次数:20327 次

在江南研究jsunpackn (运行原理)

感觉江南这个称呼不错,就到盗用了过来,关于这个jsunpackn是一个decode网页js脚本的东西,因为js脚本挂马需要依赖于浏览器漏洞,主要集中与ie,

而chrome与firefox的漏洞不算特别多,还有就是弹出广告,之类的一些流氓功能。

jsunpack有一个网页版本和一个本地版本,本地版本配置需要python环境和几个python模块,官网介绍如下

jsunpack-n emulates browser functionality when visiting a URL. It's purpose is to detect exploits that target browser and browser plug-in vulnerabilities. It accepts many different types of input:

PDF files - samples/sample-pdf.file
Packet Captures - samples/sample-http-exploit.pcap
HTML files
JavaScript files
SWF files

输入文件中支持swf文件和packet captures文件,后者应该是使用pynids模块来进行分析,前者使用swf来封装,也是一种常
用的手法,不过不管怎样加密js都会被加载到

内存并执行,我们可以hook eval这个函数,根据观察jsunpackn就是这样做的,但这也不是全权之计,比如setTimeout也可以执行代码,或者使用匿名函数,也可以执行

js。具体可以看这篇文章http://huaidan.org/archives/3328.html

这里顺便吐槽一下,python版本之间的差异非常大,比如Exception , e 和 Exception as e    print('a') 和 print 'a'    import的隐试声明

再读源码的时候经常看到这句话,python __name__='__main__',他的意思是,自己调用时才执行main函数,

当别的模块调用它是,python__name__是他自己的名字,当自己调用自己时,python__name__才是__main__


之后是安装模块,在jsunpackn的目录下有模块的介绍

These packages are the original upstream packages provided by the authors with
the only exception being the spidermonkey version which is patched to fit it's
purpose to detect malicious JavaScript files.

The reason for these copies is to have a known working dependency set to be able
to compile and run JsUnpack on a usual Linux Distribution.

The original source of the files are:

BeautifulSoup-3.2.0.tar.gz  Beautiful Soup is a Python HTML/XML parser http://www.crummy.com/software/BeautifulSoup/
js-1.8.0-rc1-src.tar.gz     A modified version of SpiderMonkey which is Mozilla's JavaScript engine https://developer.mozilla.org/en/SpiderMonkey
pycrypto-2.4.1.tar.gz       The Python Cryptography Toolkit https://www.dlitz.net/software/pycrypto/
pynids-0.6.1.tar.gz         pynids a python wrapper for libnids http://jon.oberheide.org/pynids/
yara-1.6.tar.gz             YARA a tool to identify and classify malware samples http://code.google.com/p/yara-project/
yara-python-1.6.tar.gz      The python bindings for YARA http://code.google.com/p/yara-project/

安装没有什么问题,只要阅读INSTALL,一步步下来就可以安装完成。

这里说一下,SpiderMonkey的源代码没有被编译,可能是因为作者修改了源码的问题

编译过程如下

configure是一个shell脚本,它可以自动设定源程序以符合各种不同平台上Unix系统的特性,并且根据系统叁数及环境产生合适的Makefile文件或是C的头文件(header file),让源程序可以很方便地在这些不同的平台上被编译连接。
所以一般的编译过程是

configure  
make(在MAKEFILE中读取指令,用来编译)
make install (MAKEFILE中读取,安装到指定位置)
最后用make clean 来清除一些中间文件就可以了

安装完这些模块之后,程序应该就可以执行了。

先说一下这些模块的作用

BeatuifulSoup

这是一个可以解析HTML/XML的模块,需要使用urllib把网页信息下载下来再进行解析,这个模块的作用应该是分析HTML信息,并找出script标签,

但如果是单纯的寻找标签名,但对一些重定向之类的方法,应该不能发觉。

使用方法大概如下

import BeautifulSoup
import urllib

a = 'http://www.icafe8.com/icafe/archive/DisklessServer1.1.0.0.exe'
b = 'c:/DiskLess.exe'
#urllib.urlretrieve(a, b)

url = 'http://www.baidu.com'
f = urllib.urlopen(url)

#response = urllib.request.urlopen(url)
#print response
all_url_text = f.read()

soup = BeautifulSoup.BeautifulSoup(all_url_text)
#print(soup.prettify())
soup.findAll('script')

SpiderMonkey

这就是火狐使用的js解析器了,编辑完成时会出现一个叫做js的可执行文件,使用它就可以执行js脚本啦~

js -h 会显示帮助

在jsunpackn之中的语句如下

po = subprocess.Popen(['js', '-f', self.OPTIONS.pre, '-f', current_filename + '.js', '-f', self.OPTIONS.post], shell=False, stdout=js_stdout, stderr=js_stderr)
但是简单的执行js脚本怎么达到解密功能那,作者应该是hoook了eval函数,在主目录下INSTALL.spidermonkey下有说明,

根据下面的修改,应该是只hook了eval函数,那么根据网络水友介绍,加密js函数不只有使用eval函数

还可以使用settimeout函数,

或者使用匿名函数,这样根本无法hook

package{
import flash.external.ExternalInterface;

public class