ロード・トゥ・ザ・ホワイトハッカー

ホワイトハッカーはじめました

ログの解析について

ログ分析に関してのまとめ

・HTTPのログからのSQLインジェクションの検出について以下の2つにまとめた。

chikuwamarux.hatenablog.com

 

chikuwamarux.hatenablog.com

 

SQLインジェクションの検出では以下のデータセットを利用した。

github.com

 

それ以外に使用できそうなデータセットとして、以下の2つがあった。

 

・FWAF

github.com

 

・HTTP DATASET CSIC 2010

www.tic.itefi.csic.es

 

各データセットの概要は以下の通り。

引用:IEEE Xplore Full-Text PDF:

 

データ量としては申し分ないが、若干古い気もする。Kaggleでも、SQL injectionやXSSのデータセットを見つけられるが、多くはない。

ログ自体は、身の回りに大量に存在しているので、教師あり学習ではなく、教師無し学習がしたくなる。

 

以下の記事でIsolation Forestアルゴリズムを使用した、教師無し学習による異常検知について説明があります。

www.scutum.jp

 

ここまで、

異常検知に関して、時系列・教師無し学習による分類、

検出器に関して、教師あり学習による分類、ができることが分かった。

特徴量エンジニアリング

ドメイン知識に基づく場合は、いろんなモノをカウントして、特徴量にしていることが多い。

以下のサイトでは、SQLインジェクションの検出ために、21個の特徴量を用いている。

https://meral.edu.mm/record/4658/file_preview/201640.pdf?allow_aggs=True

1. URI
2. Method identifier
3. Number of arguments
4. Length of the arguments
5. Number of digits in the arguments
6. Number of other char in the arguments
7. Number of letters in the arguments
8. Length of the Host
9. Length of the header “Accept-Encoding”
10. Length of the header “Accept”
11. Length of the header “Accept-Language”
12. Length of the header “Accept-Charset”
13. Length of the header “Referer
14. Length of the header “User-Agent”
15. Number of cookies
16. Length of the header “Cookie
17. Content Length
18. Request Resource Type
19. Received Bytes
20. Possibility
21. Pattern Result

 

先に挙げた、

https://www.scutum.jp/information/waf_tech_blog/2021/01/waf-blog-077.html

では、以下の29個を特徴量としている。

  1. %が最初に出現する場所
  2. :が最初に出現する場所
  3. :の個数(いくつ含まれるか)
  4. (の個数
  5. ;の個数
  6. %の個数
  7. /の個数
  8. 'の個数
  9. <の個数
  10. ?の個数
  11. .の個数
  12. #の個数
  13. %3dの個数
  14. %2fの個数
  15. %5cの個数
  16. %25の個数
  17. %20の個数
  18. メソッドがPOSTかどうか
  19. URLのパス部分に含まれるアルファベットと数値以外の文字の個数
  20. クエリ部分に含まれるアルファベットと数値以外の文字の個数
  21. アルファベットと数値以外の文字が最も連続している部分の長さ
  22. アルファベットと数値以外の文字の個数
  23. /%の個数
  24. //の個数
  25. /.の個数
  26. ..の個数
  27. =/の個数
  28. ./の個数
  29. /?の個数

ドメイン知識に基づかない場合、

1.エントロピーを算出

2.N-gramとtr-idfを用いたベクトル化

3.文字列のインデックス化

4.文字列のアスキーコード化

などで、ログを数値化している。

1と2は、前回と前々回にこのブログで実施している。

 

3.文字列のインデックス化

については、使用する文字列を決めて、その位置番号により文字列化する方法。

コードとしては、以下のようになる。

import tensorflow as tf
import numpy as np

def data2char_index(Xmax_len):
    alphabet = " abcdefghijklmnopqrstuvwxyz0123456789-,;.!?:'\"/\\|_@#$%^&*~`+-=<>(){}"
    result =  
    for data in X:
        mat = 
        for ch in data:
            ch = ch.lower()
            if ch not in alphabet:
                continue
            mat.append(alphabet.index(ch))
        result.append(mat)
    X_char = tf.keras.preprocessing.sequence.pad_sequences(np.array(result, dtype=object),
            padding='post',truncating='post', maxlen=max_len)
    return X_char

 

4.文字列のアスキーコード化

これはPythonのord関数を使用して、文字コードを数値化するもの。

def convert_to_ascii(sentence):
    sentence_ascii=

    for i in sentence:  
        if(ord(i)<8222):      # ” has ASCII of 8221            
            if(ord(i)==8217): # ’  :  8217
                sentence_ascii.append(134)            
            if(ord(i)==8221): # ”  :  8221
                sentence_ascii.append(129)                
            if(ord(i)==8220): # “  :  8220
                sentence_ascii.append(130)   
            if(ord(i)==8216): # ‘  :  8216
                sentence_ascii.append(131)
            if(ord(i)==8217): # ’  :  8217
                sentence_ascii.append(132)
            if(ord(i)==8211): # –  :  8211
                sentence_ascii.append(133)
            if (ord(i)<=128):
                    sentence_ascii.append(ord(i))    
            else:
                pass            

    zer=np.zeros*1

    for i in range(len(sentence_ascii)):
        zer[i]=sentence_ascii[i]

    zer.shape=(100100)
    return zer

正規表現での判断が有効な場面はまだあるようですし、機械学習も万能ではないので、いろんな手法を組み合わせて、総合的に判断することが必要なんだと思う。

 

*1:10000