背景
在分析日志的时候发现有些日志中参数中包含其他的URL,例如:
提取请求参数中的URL(xss.ha.ckers.org),再对比威胁情报数据库,如果命中黑名单直接标黑。如果不在黑名单,也不在公司的白名单里可以先做个标记,后续着重分析。
提取URL
关于URL的提取网上有很多文章,大部分都是是使用正则表达式,方法简单但是不太准确。我这里提供一种方法:采用词法分析,提取域名和IP。思路借鉴了这篇文章:https://blog.csdn.net/breaksoftware/article/details/7009209,有兴趣的可以去看看,事实证明跟着大神确实涨姿势。
原文是用C++版本,这里我用Python写了一个类似的,供大家参考。
常见的URL分类
观察可以见得:IP形式的URL结构最为简单:4个小于255的数字被.分割;domain形式比较复杂,但是它们有共性:都具有顶级域名.com。
定义合法字符:
顶级域名列表:
域名形式提取:如www.baidu.com
。
IP形式提取:如192.168.1.1。
while (i < len(z) and z[i].isdigit()): i = i + 1 ip_v1 = True reti = i if i < len(z) and z[i] == '.': i = i + 1 reti = i else: tokenType = TK_OTHER reti = 1while (i < len(z) and z[i].isdigit()): i = i + 1 ip_v2 = True if i < len(z) and z[i] == '.': i = i + 1 else: if tokenType != TK_DOMAIN: tokenType = TK_OTHER reti = 1while (i < len(z) and z[i].isdigit()): i = i + 1 ip_v3 = True if i < len(z) and z[i] == '.': i = i + 1 else: if tokenType != TK_DOMAIN: tokenType = TK_OTHER reti = 1while (i < len(z) and z[i].isdigit()): i = i + 1 ip_v4 = True if i < len(z) and z[i] == ':': i = i + 1 while (i < len(z) and z[i].isdigit()): i = i + 1 if ip_v1 and ip_v2 and ip_v3 and ip_v4: self.urls.append(z[0:i]) return reti, tokenType else: if tokenType != TK_DOMAIN: tokenType = TK_OTHER reti = 1
混合形式提取:如1234.com。
扫描前半部分1234,符合IP形式的特征,但是发现代码会报异常,所以需要IP处理代码段添加判断:判断后缀是否是顶级域名:
结果测试
测试数据:
运行结果:
这只是个初步的版本,如果有BUG欢迎大家指正。
结束语
以前只顾着闷着头的写代码,忽略了事后的思考和总结。现在尝试着改变一下,一边工作,一边提炼和总结,遇到感觉不错的,尝试写成工具开源出来,与大家共勉。
代码传送门:
https://github.com/skskevin/UrlDetect/blob/master/tool/domainExtract/domainExtract.py
相关文章教程推荐:web服务器安全
Atas ialah kandungan terperinci 采用词法分析提取域名和IP. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Alat AI Hot

Undresser.AI Undress
Apl berkuasa AI untuk mencipta foto bogel yang realistik

AI Clothes Remover
Alat AI dalam talian untuk mengeluarkan pakaian daripada foto.

Undress AI Tool
Gambar buka pakaian secara percuma

Clothoff.io
Penyingkiran pakaian AI

AI Hentai Generator
Menjana ai hentai secara percuma.

Artikel Panas

Alat panas

PhpStorm versi Mac
Alat pembangunan bersepadu PHP profesional terkini (2018.2.1).

Hantar Studio 13.0.1
Persekitaran pembangunan bersepadu PHP yang berkuasa

EditPlus versi Cina retak
Saiz kecil, penyerlahan sintaks, tidak menyokong fungsi gesaan kod

SublimeText3 versi Inggeris
Disyorkan: Versi Win, menyokong gesaan kod!

Penyesuai Pelayan SAP NetWeaver untuk Eclipse
Integrasikan Eclipse dengan pelayan aplikasi SAP NetWeaver.