在江南研究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,一步步下来就可以安装完成。

这里说一下,

(1)pynids的源代码没有被编译

编译过程如下

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

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

(2)pynids需要一些其他的package,这些在INSTALL中被列出,使用apt-get安装

(3)spiderMonkey需要吧js加入到PATH中以供jsunpackn调用,方法不一而是,讲到的方法

大体需要修改3个文件

/etc/profile      第一次登陆时被执行
~/.bash_profile   用户登录时仅被执行一次 
~/.bashrc	  每次打开新shell时被读取

在文件末尾加上,这里$PATH是之前的值,以保证其不被覆盖,:为分隔符

export PATH="$PATH:/etc/apache/bin"

这里修改~/.bashrc是比较好的办法

使用source /etc/profile 是治标不治本的方法,新开shell之后PATH会默认在~/.bashrc内读取信息


关于bash的详细介绍http://wenku.baidu.com/view/463276fa0242a8956bece426.html

或者使用 man bash

(4)

ImportError: libyara.so.0: cannot open shared object file: No such file or directory

这种报错只要执行下面几条指令就可以了,

/etc/ld.so.conf的介绍

http://hi.baidu.com/keglqmaqkidgktr/item/f650bbd27374013ae3108feb

$ sudo su
$ echo "/usr/local/lib" >> /etc/ld.so.conf
$ ldconfig


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

先说一下这些模块的作用

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 Movie extends Sprite{

public function Movie()  {
var myjs:String="alert('see me?');";//换成你的js
ExternalInterface.call("(function(){"+myjs+"})()");

}
}
}
或者使用,tostreing重载,tovalue重载等,这就是说,只hook eval是不能解决问题的

This modification is important! If you don't want to make this modification, you should follow step 99 at the bottom of this file.

After fetching and spidermonkey and locating the correct file follow steps 1, 2 and 3.
$ wget http://ftp.mozilla.org/pub/mozilla.org/js/js-1.8.0-rc1.tar.gz
$ cd js/src
$ vi jsobj.c


1) Add around line 1184 (beginning of function body obj_eval):
    size_t n, i;
    jschar *s;

2) Add around line 1314 (within function obj_eval):

|    principals = NULL;
|    }

    //added
    if (JSSTRING_IS_DEPENDENT(str)) {
        n = (size_t)JSSTRDEP_LENGTH(str);
        s = JSSTRDEP_CHARS(str);
    } else {
        n = (size_t)str->length;
        s = str->u.chars;
    }
    printf("\n//eval\n");
    for (i = 0; i < n; i++){
        if (s[i] == '\0'){
                break;
        }
        printf("%c",s[i]);
    }
    printf("\n");
    //end added

|    /*
|     * Set JSFRAME_EVAL on fp and any frames (e.g., fun_call if eval.call was


3) Then build with:
$ make BUILD_OPT=1 -f Makefile.ref

After this, you will find a "./Linux_All_OPT.OBJ/js" binary file.

99) Note: If you cannot use "./Linux_All_OPT.OBJ/js" as the default 'js' command, then you should 
uncomment the following code within pre.js below to make it active. (If you just built spidermonkey, you do not want to uncomment the following code)

/* //Comment out because spidermonkey handles eval now
var my_eval = this.eval;
this.eval=function (str){
        print('\n//eval\n'+str);
        return my_eval(str);
}
*/


pycrypto
这个模块,包含了很多加密解密算法,应该是crypto++的python封装,里面的加密方式比较复杂,都是些RSA,椭圆曲线,不对称加密,分组密码

哈希密码之类的东西,不知道有什么用处。

文档在这里https://www.dlitz.net/software/pycrypto/api/current/

pynids

这个模块,是libnids的python封装,提供了包的重组功能,应该是为了输入文件中的packet captures,不过libnids应该是一个安全方面的工具,

在这里如果只负责,输入文件中的包重组,有些大材小用的感觉。

可能有事嗅探会发现很多平时注意不到的JS,可能是重定向或是跨域回传之类的(只是猜测)

源码包里有一个Demo,不过基本没有注释

yara

这个东西是负责,关键代码的定义的,也是比较核心的模块,也是唯一有用户手册的模块,这里建国做了翻译,我就不赘述了

http://blog.csdn.net/xihuanqiqi


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值