在分析日志的時候發(fā)現(xiàn)有些日志中參數(shù)中包含其他的URL,例如:
http://www.xxx.cn/r/common/register_tpl_shortcut.php?ico_url=http://www.abcfdsf.com/tg_play_1121.php&supplier_id=3&ep=tg&style=szsg_reg_tg03
http://b.xxx.cn?c=<IMGsrc="http://www.thesiteyouareon.com/somecommand.php?somevariables=maliciouscode">
http://b.xxx.cn?c=<SCRIPTa=">"src="http://xss.ha.ckers.org/a.js"></SCRIPT>
提取請求參數(shù)中的URL(xss.ha.ckers.org),再對比威脅情報數(shù)據(jù)庫,如果命中黑名單直接標黑。如果不在黑名單,也不在公司的白名單里可以先做個標記,后續(xù)著重分析。
提取URL
關于URL的提取網(wǎng)上有很多文章,大部分都是是使用正則表達式,方法簡單但是不太準確。我這里提供一種方法:采用詞法分析,提取域名和IP。思路借鑒了這篇文章:https://blog.csdn.net/breaksoftware/article/details/7009209,有興趣的可以去看看,事實證明跟著大神確實漲姿勢。
原文是用C++版本,這里我用Python寫了一個類似的,供大家參考。
常見的URL分類
IP形式:192.168.1.1,10.20.11.1
Domain形式:baidu.com、www.sina.com,freebuf.com
觀察可以見得:IP形式的URL結構最為簡單:4個小于255的數(shù)字被.分割;domain形式比較復雜,但是它們有共性:都具有頂級域名.com。
定義合法字符:
legalChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_"legalNumers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
頂級域名列表:
topLevelDomain = ['biz', 'com', 'edu', 'gov', 'info', 'int', 'mil', 'name', 'net', 'org', 'pro', 'aero', 'cat', 'coop', 'jobs', 'museum', 'travel', 'arpa', 'root', 'mobi', 'post', 'tel', 'asia', 'geo', 'kid', 'mail', 'sco', 'web', 'xxx', 'nato', 'example', 'invalid', 'test', 'bitnet', 'csnet', 'onion', 'uucp', 'ac', 'ad', 'ae', 'af', 'ag', 'ai', 'al', 'am', 'an', 'ao', 'aq', 'ar', 'as', 'at', 'au', 'aw', 'ax', 'az', 'ba', 'bb', 'bd', 'be', 'bf', 'bg', 'bh', 'bi', 'bj', 'bm', 'bn', 'bo', 'br', 'bs', 'bt', 'bv', 'bw', 'by', 'bz', 'ca', 'cc', 'cd', 'cf', 'cg', 'ch', 'ci', 'ck', 'cl', 'cm', 'cn', 'co', 'cr', 'cu', 'cv', 'cx', 'cy', 'cz', 'de', 'dj', 'dk', 'dm', 'do', 'dz', 'ec', 'ee', 'eg', 'eh', 'er', 'es', 'et', 'eu', 'fi', 'fj', 'fk', 'fm', 'fo', 'fr', 'ga', 'gb', 'gd', 'ge', 'gf', 'gg', 'gh', 'gi', 'gl', 'gm', 'gn', 'gp', 'gq', 'gr', 'gs', 'gt', 'gu', 'gw', 'gy', 'hk', 'hm', 'hn', 'hr', 'ht', 'hu', 'id', 'ie', 'il', 'im', 'in', 'io', 'iq', 'ir', 'is', 'it', 'je', 'jm', 'jo', 'jp', 'ke', 'kg', 'kh', 'ki', 'km', 'kn', 'kp', 'kr', 'kw', 'ky', 'kz', 'la', 'lb', 'lc', 'li', 'lk', 'lr', 'ls', 'lt', 'lu', 'lv', 'ly', 'ma', 'mc', 'md', 'me', 'mg', 'mh', 'mk', 'ml', 'mm', 'mn', 'mo', 'mp', 'mq', 'mr', 'ms', 'mt', 'mu', 'mv', 'mw', 'mx', 'my', 'mz', 'na', 'nc', 'ne', 'nf', 'ng', 'ni', 'nl', 'no', 'np', 'nr', 'nu', 'nz', 'om', 'pa', 'pe', 'pf', 'pg', 'ph', 'pk', 'pl', 'pm', 'pn', 'pr', 'ps', 'pt', 'pw', 'py', 'qa', 're', 'ro', 'rs', 'ru', 'rw', 'sa', 'sb', 'sc', 'sd', 'se', 'sg', 'sh', 'si', 'sj', 'sk', 'sl', 'sm', 'sn', 'so', 'sr', 'st', 'su', 'sv', 'sy', 'sz', 'tc', 'td', 'tf', 'tg', 'th', 'tj', 'tk', 'tl', 'tm', 'tn', 'to', 'tp', 'tr', 'tt', 'tv', 'tw', 'tz', 'ua', 'ug', 'uk', 'um', 'us', 'uy', 'uz', 'va', 'vc', 've', 'vg', 'vi', 'vn', 'vu', 'wf', 'ws', 'ye', 'yt', 'yu', 'za', 'zm', 'zw']
域名形式提。喝鐆ww.baidu.com。

ifself.isLegalChar(zv): i = 0 reti = 0 tokenType = TK_OTHER while (i < len(z) andself.isLegalChar(z[i])): i = i + 1 reti = i while i < len(z) and z[i] == '.': i = i + 1 urltoken_str = z[i:len(z)] urltoken_str = urltoken_str.lower() if urltoken_str intopLevelDomain: i = i + len(urltoken_str) reti = i tokenType = TK_DOMAIN while (i < len(z) andself.isLegalChar(z[i])): i = i + 1 reti = i if i < len(z) and z[i] == ':': i = i + 1while (i < len(z) and z[i].isdigit()): i = i + 1 reti = i if tokenType == TK_DOMAIN: check_url = z[0:i] if check_url.find(':') >= 0: check_url = check_url[0:check_url.find(':')] for item intopLevelDomain: pos = check_url.find('.' + item) if pos > -1and (pos + len(item) + 1 == len(check_url)):self.urls.append(z[0:i])
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 = Trueif i < len(z) and z[i] == '.': i = i + 1else: if tokenType != TK_DOMAIN: tokenType = TK_OTHER reti = 1while (i < len(z) and z[i].isdigit()): i = i + 1 ip_v3 = Trueif i < len(z) and z[i] == '.': i = i + 1else: if tokenType != TK_DOMAIN: tokenType = TK_OTHER reti = 1while (i < len(z) and z[i].isdigit()): i = i + 1 ip_v4 = Trueif i < len(z) and z[i] == ':': i = i + 1while (i < len(z) and z[i].isdigit()): i = i + 1if 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形式的特征,但是發(fā)現(xiàn)代碼會報異常,所以需要IP處理代碼段添加判斷:判斷后綴是否是頂級域名:
urltoken_str = z[i:len(z)] urltoken_str = urltoken_str.lower() if urltoken_str in topLevelDomain: i = i + len(urltoken_str) reti = i tokenType = TK_DOMAIN
結果測試
測試數(shù)據(jù):
192.168.1.1
mp3.com
http:www.g.cn
http:\www.g.cn
http:\\/\www.g.cn
admin:@www.g.cn
http://10.10.10.10:8080/?a=1
file://192.168.1.1:8090/file
mailto:majy@corp.com
username:password@g.cn
運行結果:
192.168.1.1 ['192.168.1.1']mp3.com ['mp3.com']http:www.g.cn ['www.g.cn']http:\www.g.cn ['www.g.cn']http:\/\www.g.cn ['www.g.cn']admin:@www.g.cn ['www.g.cn']http://10.10.10.10:8080/?a=1 ['10.10.10.10:8080']file://test11.com:8090/file ['test11.com:8090']mailto:majy@corp.com ['corp.com']username:password@g.cn ['g.cn']
這只是個初步的版本,如果有BUG歡迎大家指正。
結束語
以前只顧著悶著頭的寫代碼,忽略了事后的思考和總結,F(xiàn)在嘗試著改變一下,一邊工作,一邊提煉和總結,遇到感覺不錯的,嘗試寫成工具開源出來,與大家共勉。
代碼傳送門:
https://github.com/skskevin/UrlDetect/blob/master/tool/domainExtract/domainExtract.py
*本文作者:littlegrass,轉(zhuǎn)載自FreeBuf.COM