前言

对于庞大的网络空间来说,存在着各式各样的应用、设备等等资产,而对这些资产进行识别,无论拿来做扫描器还是批量分析,都是非常有价值的,高效的应用指纹识别是一个长久可研究的课题,本文主要探讨如何建立高效可靠的指纹识别方法。

传统的指纹识别

一些扫描器里面使用的比较多的都是通过特殊路径 静态文件的md5 值或者大小 关键词等,例如使用比较广泛的一个指纹数据库 。

img
存在几个问题

  1. 效率太低,每一个路径都需要访问一次。容易被waf拦截
  2. 目前很多网站的静态资源 cdn于gzip 压缩,md5 其实是不一样的 ,识别率很低

现代化指纹识别

怎么来快速的精确的指纹识别,观察了大部分的web系统发现,访问首页并且获取response header 能识别出80% 的app
例如WordPress,discuz 会把独特的cookie放到header 里面。
discuz 的关键词 _saltkey=
img

默认首页也会有很多固定的关键词, WordPress wp-conetnt
img

剩下的指纹再去特殊url提取,这样效率和准确率会提高很多很多。

现代化指纹识别方案

指纹库设计

数据库结构

img
指纹库类型目前有3种 ,当然后续还需要加上正则。
finger 规则是一个 string 之后的python 字典,里面有例如状态吗,规则等字段,例如 weblogic 的指纹规则

1
{'port':7001,'url': '/console/login/LoginForm.jsp', 'code': 200, 'grep': 'WebLogic Server'}

下面介绍每一种指纹库实例

指纹规则

web_header_contain

首页 response 里面的 header 查找特征符
速度最快,优先极最高。很多web cms 都会写特殊的cookie 键值。
例如 discuz,jboss,wordpress 等。
规则编写,以discuz 为例

1
2
3
4
5
6
7
8
9
10
11
12
13
➜  tools  curl  -I    www.cctry.com
HTTP/1.1 200 OK
Server: Microsoft-IIS/6.0
Connection: keep-alive
Date: Thu, 07 Jan 2016 18:22:34 GMT
Content-Type: text/html; charset=gbk
Content-Length: 0
X-Powered-By: PHP/5.2.17
Set-Cookie: Vguy_2132_saltkey=O6srsEYk; expires=Sat, 06-Feb-2016 18:22:22 GMT; path=/; httponly
Set-Cookie: Vguy_2132_lastvisit=1452187342; expires=Sat, 06-Feb-2016 18:22:22 GMT; path=/
Set-Cookie: Vguy_2132_sid=MwE6e0; expires=Fri, 08-Jan-2016 18:22:22 GMT; path=/
Set-Cookie: Vguy_2132_lastact=1452190942%09index.php%09; expires=Fri, 08-Jan-2016 18:22:22 GMT; path=/
X-Daa-Tunnel: hop_count=1

经过分析,字符串 _saltkey= 为discuz header 中的规则
具体规则如下
img

web_index_contain

在缓存的首页里面查找关键词或者正则匹配
优先级次之,只需要 get请求一次即可。
例如Office Anywhere 指纹 编写流程
数据包分析

1
2
3
4
5
➜  tools  curl  http://125.91.218.186:8000/   |  grep   '/images/tongda.ico'
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 2001 100 2001 0 0 11795 0 --:--:-- --:--:-- --:--:-- 11840
<link rel="shortcut icon" href="/images/tongda.ico">

规则编写
img

web_url_contain

特殊url 查找指定的关键词(返回状态码也指定)。
优先级最低。
例如 weblogic 的指纹 finger 过程
数据包分析

1
2
3
4
5
6
7
8
9
10
➜  tools  curl  -I     http://202.97.194.9:7001/console/login/LoginForm.jsp
HTTP/1.1 200 OK
Cache-Control: no-cache
Date: Thu, 07 Jan 2016 18:53:28 GMT
Pragma: no-cache
Content-Type: text/html; charset=UTF-8
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Set-Cookie: ADMINCONSOLESESSION=XQXrWT0LQTvpf8Jv75nMnQB9vN0cGppy7bTfJxfH9S673VTGP1Wl!1715621632; path=/
Content-Language: zh-CN
X-Powered-By: Servlet/2.5 JSP/2.1

规则

1
{'url': '/console/login/LoginForm.jsp', 'code': 200, 'grep': 'WebLogic Server'}


img

程序编写

指纹识别模块代码

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# coding:utf-8
"""
*@Projet Yafinger
*@Author yaseng@uauc.net
*@Desc playweb finger modules
"""
import ast,time
import config
from lib import util

def get_web_app(url):
rsp_index = util.http_get(url)
str_index = ""
if rsp_index == None :
return None
list_app=[]
fingers=config.get('fingers');
for finger in fingers :
rule = ast.literal_eval(finger['finger'])
if finger['type'] == 'web_index_contain':
# limit header and body and code exp : {'header':'jsessionid=','code': 200, 'grep': '.action'}
if rule.has_key('header') and rule['header'] not in str(rsp_index['headers']).lower() :
continue
if rsp_index['code'] == rule['code'] and rule['grep'] in rsp_index['data'] :
list_app.append({'app_id':finger['id'], 'url':url})
util.log("url:%s app:%s" % (url, finger['app_name']))

elif finger['type'] == 'web_url_contain' :
rsp_tmp = util.http_get(url + rule['url'])
if rsp_tmp == None :
continue
if rsp_tmp['code'] == rule['code'] and rule['grep'] in rsp_tmp['data'] :
list_app.append({'app_id':finger['id'], 'url':url})
util.log("url:%s app:%s" % (url + rule['url'], finger['app_name']))
elif finger['type'] == 'web_header_contain' :

if rule['grep'] in str(rsp_index['headers']).lower() :
list_app.append({'app_id':finger['id'], 'url':url})
util.log("url:%s app:%s" % (url , finger['app_name']))


time.sleep(0.01)
return list_app

单独识别

简单调用指纹识别模块代码

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# coding:utf-8
"""
*@Projet Yafinger
*@Author yaseng@uauc.net
*@Desc yafinger test
__ __ ___
/\ \ /\ \ /'___\ __
\ `\`\\/'/ __ /\ \__/ /\_\ ___ __ __ _ __
`\ `\ /' /'__`\ \ \ ,__\\/\ \ /' _ `\ /'_ `\ /'__`\/\`'__\
`\ \ \ /\ \L\.\_\ \ \_/ \ \ \ /\ \/\ \ /\ \L\ \ /\ __/\ \ \/
\ \_\\ \__/.\_\\ \_\ \ \_\\ \_\ \_\\ \____ \\ \____\\ \_\
\/_/ \/__/\/_/ \/_/ \/_/ \/_/\/_/ \/___L\ \\/____/ \/_/
/\____/
\_/__/
"""
import os, time, sys, Queue, threading, ast
import config
from lib import util
from lib.db import *
from optparse import OptionParser
from modules import finger

if __name__ == "__main__":
usage= '''%prog --host host --port port --finger <all|app_name> \r\nExample:%prog --url http://127.0.0.1 --finger phpmyadmin '''
parser = OptionParser(usage=usage)
parser.add_option("-u", "--url", dest="url", help="target url")
parser.add_option("-f", "--finger", dest="finger", help="finger_db app_name,default all ", default="all")
options, arguments = parser.parse_args()
if options.url == None :
parser.print_help()
exit(0)
db = MySQL(config.db_config)
sql_finger_where=' ' if options.finger == 'all' else " and app_name='%s' " % options.finger
db.query("SELECT * from pw_finger_db where `enable`=1 %s " % sql_finger_where)
fingers = db.fetch_all()
if len(fingers) == 0 :
util.log('finger app_name %s not found' % options.finger ,3,'finger')
config.set("fingers",fingers)
util.log("load fingers count %d" % len(fingers),1,'finger')
finger.get_web_app(options.url)

img

批量识别

可以使用线程池来实现批量指纹识别。

指纹结果

融合到系统中指纹保存在数据库中,本模块可以快速整合到扫描器或者其他项目中。

yafinger

yet another web fingerprinter
https://github.com/yaseng/yafinger

issue

  1. 指纹库需要补充
  2. 可以不局限于web指纹
  3. 某些情况可能目前的指纹规则不符合,还需要添加新的指纹规则,例如正则