Machine Learning & Security
講師: 奧義智慧科技 陳仲寬
FaceBook:陳仲寬
Gmail: bletchley13@gmail.com
recommand books
大演算
Machine Learning & Security
Docker init
簡單的VM(輕量版w)
docker hub
Pull docker mlsec image
下載好Ubuntu,並裝好docker後,執行(約6G大小)
下載完後,進到 docker 輸入
帶有 token 的網址丟進瀏覽器,網址的部分要修改一下
將 http://(container_id
or 127.0.0.1
):8888 => http://127.0.0.1:8888
如果要確認 docker 現在有開哪些服務,輸入
Goals
- Understand fundamental concept of machine learning
- Realize different machine learning schools
- A detail example of network traffic analysis
- Several new research related to machine learning and traffic analysis
Machine Learning
Definition
Data Engineering pharse
如何用E(Experience)提升P(Performance)
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
- Data mining(資料探勘)
尋找所需的資料
- Data exploration(數據探勘)
對於資料先初步將不合理資料篩除
- Feature generation(特徵生成)
針對剩下的資料擷取特徵
- Feture selection(特徵擷取)
了解Data後,找出好的feature
- Cross validation(交叉驗證)
將資料分成多個部分,進行比較,可避免過擬合的發生
- Training data(訓練資料)
- Test data(測試資料)
Model Training pharse
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
- Model selection(選擇模型)
- Training data(訓練資料)
- Model training(模型訓練)
- Model tuning(模型校正)
- Resulting model(最終模型)
Model Validation pharse

- Test data(測試資料)
- Ground truth(期望答案)
- Resulting model(最終模型)
- Evaluate(評估)
Model
可以想像成一個最佳化的function
輸入後會得到最佳解
Any machine learning algorithm consists of
- A model family/representation, which describes the universe of models from which we can choose
- A loss function, which allow us to quantitatively compare different models
- An optimization procedure, where allow us to improve/find the model based according to loss function.
Cross-validation
資料會分成Test Data和Training Data
如果Training Data 和 Test Data 篩選不夠好,會造成ML結果不佳
這裡的交叉驗證是使用k-fold validation
將訓練集分割成k個子樣本,一個單獨的子樣本被保留作為驗證模型的數據,其他k − 1個樣本用來訓練
每個子樣本都驗證一次,平均結果獲得一個單一值
適合用在資料數量少

Train, validate, test
將資料直接分類成Train data和Test data,然後進行訓練
Out-of-time validation
資料跟時間相關,無法一開始就知道有哪些資料
像是Twitter,每天都會更新資料上去,其中分成兩種訓練方式
- 先訓練出模型,將資料直接分成Validate和Test
- 先訓練模型,先集中資料(像事先蒐集3天份的資料),最後再進一步Test

各個ML系統在不同的threshold設定下會有不同的
- False positive
- False negtive
- True positive
- True negtive
可以透過以下方式理解
假設一個情境,並令此情境成立時為 positive class,不成立時為 negative class
當情境與事實相同時為 True ,反之為 False
可以參考google developers的文件,有更詳細的介紹

通常False的兩個值和True的兩個值會一高一低
我們可以透過觀察這幾個數據的大小關係,去判斷一個ML的好壞
以下有幾種常見方法:
- Accuracy
- F-score
- Confusion matrix
- ROC/AUC
Accuracy
正確率

F-score
F-score的基本式為
F-score是利用recall和percision兩個值互相抵制的關係做成的綜合結果
當數值越接近1則表示準確率越高
而F1-score則通指當 F-score 的狀況
而 Recall 及 precision 分別可由以下算式求得
Recall=
precision=

Confusion matrix
盡量將結果為正確率提升,降低錯誤率
正確率與錯誤率兩者之間互相抗衡
可以根據不同情況可以調整參數
在某些特殊情況也許寧可誤報也不要沒報到
利用資料視覺化可以清楚了解正確率以及錯誤率
- heatmap

ROC & AUC
- 受試者工作特徵曲線(ROC, Receiver operating characteristic)
- ROC 曲線下面積(AUC, area under the curve)

在ROC中,是False positive 和 True positive的變化圖
AUC越大,代表在綜合狀況中這個model的準確度比較好

ML Models

Lab 0-1
練習1 /mlsec/frauddetect/logistic-regression-fraud-detection.ipynb
One hot encoding
training/testing set
Model
- What you should understand
- Basic usage about pandas dataframe
- Basic usage of sklearn
- A standard procedure for ML
- One-hot encoding
有些資料做加減是無意義的,可以對它做One-hot encoding
例如:班級是重要的資料,但是數值上的距離沒有任何意義
所以我們可以把他表示成類似bool的樣貌,讓他的+/-存在意義
machine learning 101

- Types of machine learning use cases:
- Regression
- Classification
- Anomaly detection
- Recommendation
web
Regression

Classification

Anomaly Detection
偵測異常狀況
可以觀察資料的變化週期、頻率、規律 當出現不符合規律的值,就會被偵測出來
但是實際上要到變化差異多大才偵測,也要依情況不同做調整

較類似於對於不平常的狀況提出警告,實際上是好或壞無法辨認
Method
Problems in ML
over fitting
- 學得太多
- when a model learns the detail and noise in the training data to the extent that it negatively impacts the performance of the model on new data.
How to solve?

將訓練資料分類成兩個部分 Training Set Validate Set
真正會去訓練的只有Training Set
而Validate Set會拿來做驗證
參考資料
under fitting
- 學得太少
- not a suitable model and will be obvious as it will have poor performance on the training data
- a model that can neither model the training data nor generalize to new data
參考資料
School of ML
- 根據演算法分類
- 符號理論學派(Symbolists)
- 類比推論學派(logizers)
- 貝氏定理學派(Bayesians)
- 演化論學派(Evolutionaries)
- 類神經網路學派(Connectionists)
不同學派之間共享許多屬性
符號理論學派(Symbolists)
所有智慧都能變成符號,透過邏輯運算就能求解所有答案
- Rule量太過於龐大,難以執行,但概念還是存在
=> 自動Mining Rule

Rule mining
嘗試將物品綁成一個集合(set),看看彼此間是否具有關聯性,計算彼此間support好壞
將全部計算過一遍之後去計算兩者之間的關聯性

我們可以全部Travasal過一遍,但是非常耗時
=>剪枝


如果上面的資料已經確定比其他還要小,那麼它底下的也不用去判斷
Building Decision Tree
在每個node提出詢問,再將資料分成兩群(切割)
Emptropy
資訊:熵_wiki
以物理熱理學角度去衡量資料(亂度or熵)

計算Information Gain,篩選出比較好的
如果在屬性A上做拆分 則S的熵較小
Sv = S的子集,其屬性A具有值v
gain(SA)=原本的熵-sum(A上的熵)

動態圖表(切割資料)

上面綠色區塊以及下面藍色區塊比較集中
左下的資料比較雜亂,因此切割成三個區域
切得太深可能會發生Over Fitting

這裡的準確度差異就有點大,可能是切得太深造成Over Fitting
盡量要使資料能比較General(放諸四海皆準)
Lab 1-2
練習2 /mlsec/malware/malware-classification.ipynb
- What to do?
- Using Decision Tree to detect malware
- Play tree-based algorithm
- Which algorithm is best, why?
在windows底下的檔案屬於PE
在linux底下檔案屬於ELF

最後一個欄位legitimate是最重要的(我們關注的欄位)
Lab 1-3
練習3 /mlsec/network/nsl-kdd-classification.ipynb
challange of Symbolists
符號理論學派ㄉ挑戰
-
Knowledge Acquisition
-
Limitation
- difficult to address uncertaincy
補充 cyber grant challenge
做出一個機器能自動 攻擊/防守
類比推理學派(Analogizers)
學習的關鍵是認識各種情況的相似之處,從而推斷其他情境的相似地方。
以距離及相似度來判斷
維度越高越可能造成overfitting
Regresion
regression = finding relationships between variables

找到一條線,使得所有點到線的距離總和最低
以下有各種維度
Liner

Polynomial

Model optimization
最優化模型
grandient descent(梯度下降法)
(做Regression最常用)
沿著斜率逐漸往下走直到最低點,使得各點ERROR值總合為最小

當數值皆穩定下降,則數值(alpha、iters)為佳
alpha:學習速率
iters:學習次數
練習學習速率的好網站 :)
Grandient descent

Lab 2-1
練習4 /mlsec/intro/00-linear-regression.ipynb
- What to do
- Understand Gradient Descent Algo
- Adjust alpha/learning rate
Learning Rate

Learning Rate的大小會影響模型Accuracy的穩定度
- Learning Rate 太大
雖然知道數值應該往哪邊調整,但是會一直無法收斂
- Learning Rate 太小
每次數值調整的幅度太小,會使得數據調整時間太久
我們可以每次根據不同的情況來調整Learning Rate
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Lab 2-2
練習5 /mlsec/intro/01-logistic-regression.ipynb
Decision Boundary
決策邊界
K-nearest neighbor classifier(KNN)

練習
練習 /mlsec/network/nsl-kdd-classification.ipynb
K-means

Lab 2-3
Kmean clustering
練習 /mlsec/intro/04-kmeans-pca.ipynb
Support vector machines(SVM)
由低維度投影至高維度,並用平面在上面做切割
介紹影片
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
The Curse of Dimensionality
維度越低越好
對於高維度的各點,距離都很遠

貝式定理學派(Bayesians)
學習是機率推理的一種形式
好的假設一直更新 => 越來越好
- 根據 時間 證據 更新 hypothesis 可信度
透過經驗作調整,證據夠多時間夠久就可以找最好的答案

更新機率

事前機率很重要

好的假設會一直更新,且越來越好
why Bayesians?
人們往往會透過機率去做選擇
像是當降雨機率高時,人們會較常帶雨傘出門
所以可以利用貝式定理來做機器學習
同樣的機器,在不同環境條件下,會造成不同結果 => 事前機率很重要(因為很重要,所以說兩遍)
要有好的事前機率 不然事後機率會誤導你?
(目前只能透過 經驗 或 猜? 來得到事前機率)
Lab 3-1
練習6 /mlsec/spam/spam-fighting-blacklist.ipynb
- What to do
- Implement blacklist mechanism
- Implement Bayes detector
Markov chain
將貝式的結果轉成圖

將矩陣相乘

Hidden Markov
資料中有可以觀察到的/不能觀察到的
不能觀察到的資料之間可能也會互相影響

蒙地卡羅樹搜尋 Monte Carlo Tree Search
wikipedia
alphaGO會用的演算法(下棋類)

補充 Google Page Rank
利用網頁上超連結個數和品質分析網頁的演算法

演化論學派(Evolutionaries)
- 物競天擇
- Change and Select
- Fitness
- Mutate
- Cross-over
隨機挑選出population,看fittness高低選擇誰留下
繁衍下一代,選擇突變…

最重要的是 Crossover 和 Mutation

基因演算法(GA, genetic algorithm)

- 編碼
每一條染色體是由許多基因所組成
可以用0/1或是實數來編碼
- 產生初始母體
- 計算合適度函數
針對每一條染色體,計算合適度函數(Fitness function)
評估每一個解的好壞程度,判斷是否要保留
- 選擇與複製
由合適度函數的數值,把每一條染色體由數值大至小(優至劣)進行排列,選擇最優秀的一定百分比的染色體進行複製。
- 交配
由選擇與複製所留下的染色體,選出兩條染色體,進行交配。
- 突變
設定一突變率,小部分的染色體會互換基因
類神經網路學派(Connectionists)
模擬人腦,設計網路架構
有很多神經元(neural)互相傳遞資訊
鄭皓玶轉成gifㄌ
激勵函數
S函數(sigmoid)


從圖中可以發現到Sigmoid的Accuracy上升速度很慢
修正線性單元(ReLU, Rectified Linear Unit)
用於對上一層的所有輸入求加權和,然後生成一個輸出值(通常為非線性值),並將其傳遞給下一層,此函數是為解決梯度爆炸問題。
低於零的時候很緩慢,超過 0 的時候就上升比較快


從圖中可以發現ReLU的Accuracy上升速度很快
ReLU可以比sigmoid更快速訓練模型
Demo Overfitting

因為learning rate固定,造成Accuracy的不穩定
有時候明明已經走到終點,但是又會走超過
testing & training 差距太多可能是overfitting
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Solution

將learning rate修正為不定值
在經過判斷cross entropy大小來調整learning rate,如果cross entropy較小就讓learning rate調低,反之亦同
cross entropy

卷積神經網路 (CNN, Convolution Neural Networks)
CNN=Convolution + Neural Networks

專用於擷取圖片特徵

convolution

遞歸神經網路 (RNN, Recurrent Neural Networks)

專門用於擷取文章特徵
長短期記憶模型 (LSTM, Long short-term memory)

適合於處理和預測時間序列中間隔和延遲非常長的重要事件
RNN 與 LSTM 的介紹
Other
PCA(Principal Component Analysis)
去除相依性,避免從不同的feature裡面學到錯誤
如果x和y呈線性關係,可以以其中一個當作feature,做出剩下1維的圖
- 將坐標軸中心移到數據的中心,然後旋轉坐標軸,使得數據在C1軸上的變異數最大,即全部n個數據個體在該方向上的投影最為分散。意味著更多的信息被保留下來。C1成為第一主成分。
- C2第二主成分:找一個C2,使得C2與C1的共變異數(相關係數)為0,以免與C1信息重疊,並且使數據在該方向的變異數儘量最大

模型攻擊
練習一
[練習一 (/mlsec/adversarial_learn/classifier-poisoning.ipynb)]
train一個machine learning 的model去對抗
adversarial_learn 對抗式
直接更改model本身,使得model的判斷出現錯誤
(http://127.0.0.1:8888/notebooks/mlsec/adversarial_learn/classifier-poisoning.ipynb)
原圖

攻擊後
在原模型中塞入幾個異常點*
使得判斷線移動

練習二
練習二 (/mlsec/adversarial_learn/binary-classifier-evasion.ipynb)
用樣本(sample)錯誤訊息去誤導
- vectorizer 一個字出現的頻率
- classifier 惡意xx?
如果拿到一個model 可以reverse 別人的演算法
在垃圾郵件中塞入一堆不是垃圾訊息的片段,可能會讓Model誤以為不是垃圾郵件

在[13]可以找到垃圾訊息比重最低的字串t/s
原本的垃圾訊息(1)<script>alert(1)</script>
再加上1000個t/s
後會被誤判為一般訊息(0)
練習三
練習三(/mlsec/log_attack_traffic_analysis/NetworkMonitor.ipynb)
Auto ML
Difficult Task in ML
- Data Analysis/Data Purification
資料分析/資料純化
- (If NN is used) Find the best network structure
(如果使用NN)找到最佳的網絡結構
- Hyperparameter Tuning
超參數調整
Importance of architectures for Vision
- Designing neural network architectures is hard
設計神經網絡架構很困難
- Lots of human efforts go into tuning them
許多人努力調整它們
- There is not a lot of intuition into how to design them well
如何很好地設計它們並沒有很多直覺
- Can we try and learn good architectures automatically?
我們可以嘗試自動學習良好的架構嗎?
嘗試使用ML來訓練ML
原始: Solution = ML expertise + Data + Computation
新: Solution = Data + 100*Computation
重複多次,將參數做小量修正,將所有可能情況列舉,找出最佳

練習1 /mlsec/preprocess/sklearn-gridsearch.ipynb
練習2 /mlsec/preprocess/missing-values-imputer.ipynb
Interpretable Machine Learning

我們不能只是以"那是機器做出的選擇"當作理由太過不負責任
如果我們不知道ML到底學到了什麼,如果出錯了我們就不能知道問題出在哪裡
如果我們能知道機器在想什麼,或是我們能知道機器學了什麼,就能解決這樣的問題
- Problems
- Debugging the model
- Determine whether the model is synchronized with our knowledge
- Judge the decision of a model
- What knowledge, information the model can teach us
We need to understand the model we trained – Interpretability, or reasoning
Explain Data
去理解資料
- Before building any model
建立任何模型之前
- Visualization for data exploration
可視化數據探索
- Exploratory data analysis
探索性數據分析

Interpretable models
一開始就將Model設定的簡單容易理解
Algorithm
- SVM
- Decision Tree
- Bayesian
- Markov Chain
- NN
可能會造成太複雜

Explain Model
Local explaination
LIME

將整個圖片分成幾塊,嘗試拿掉幾塊去看看整個判斷是否有改變

練習 (/mlsec/model_reasoning/lime-explainability-spam-fighting.ipynb)
將單一郵件的資料丟進去,去看看為甚麼會被標記成spam

SHAP
嘗試移除幾個feature,看看兩者之間的差異
- SHAP is black-box agnostic and provides local explainability via additive feature attribution
- Shap values quantify feature importance
- They correspond to the average distance between the model’s answer and all possible reduced models that omit a feature

Global explaination
Global interpretability 幫助我們了解每個 feature 與預測值之間的關係。除此之外,global interpretability 給予我們對於預測值而言,每個 feature 影響的方向 & 數字尺度的概念。
先訓練出複雜的classifer,再輸入資料到classifer裡面,得到相對應的輸出

再利用上面拿到的資料建立一個Decision Tree,Train出一個簡單的classifer

Lab詳細
Lab 0-1
Lab 1-2
Lab 1-3