Repository navigation
Expand file tree
/
Copy path4_2_demo_model.py
More file actions
75 lines (60 loc) · 2.56 KB
/
Copy path4_2_demo_model.py
File metadata and controls
75 lines (60 loc) · 2.56 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import pandas as pd
import joblib
import warnings
import os
import sys
try:
import feature_utils
except ImportError:
print("LOI: Khong tim thay file 'feature_utils.py'.")
print("Vui long tao file nay truoc khi chay demo.")
sys.exit()
warnings.filterwarnings("ignore")
model_filename = 'dga_xgboost_model.pkl'
print(f"[*] Dang tai mo hinh '{model_filename}'...")
if not os.path.exists(model_filename):
print(f"[!] LOI: Khong tim thay file '{model_filename}'.")
print(" Vui long chay file '3_train_xgboost.py' de huan luyen lai mo hinh voi 6 dac trung moi!")
sys.exit()
try:
model = joblib.load(model_filename)
print(" -> [OK] Da tai model thanh cong!")
if hasattr(model, "n_features_in_") and model.n_features_in_ != len(feature_utils.FEATURE_NAMES):
print(f"\n[CANH BAO] Model hien tai duoc train voi {model.n_features_in_} dac trung.")
print(f" Nhung code demo dang dung {len(feature_utils.FEATURE_NAMES)} dac trung.")
print(" -> VUI LONG CHAY LAI FILE TRAIN DE CAP NHAT MODEL!")
except Exception as e:
print(f"[!] Loi khi tai model: {e}")
sys.exit()
print("\nHe thong san sang! Nhap 'exit' de thoat.")
print("-" * 60)
while True:
try:
user_input = input("\nNhap ten mien (VD: google.com, xq99z.net): ").strip().lower()
except KeyboardInterrupt:
break
if user_input == 'exit':
print("Tam biet!")
break
if not user_input:
continue
feat_dict = feature_utils.get_features_dict(user_input)
main_domain = feature_utils.extract_main_domain(user_input)
features_df = pd.DataFrame([feat_dict], columns=feature_utils.FEATURE_NAMES)
print(f"\n-> Phan tich domain: '{main_domain}'")
print(" [Thong so ky thuat]:")
print(f" 1. Do dai (Length): {feat_dict['length']}")
print(f" 2. Ty le so (Digits): {feat_dict['digit_ratio']*100:.1f}%")
print(f" 3. Nguyen am (Vowels): {feat_dict['vowel_ratio']*100:.1f}%")
print(f" 4. Phu am lien tiep (Max): {feat_dict['max_consonant_len']}")
print(f" 5. Entropy (Do hon loan): {feat_dict['entropy']:.2f}")
print(f" 6. Co nghia (Meaningful): {feat_dict['meaningful_ratio']*100:.1f}%")
try:
prediction = int(model.predict(features_df)[0])
print("\n [KET QUA CUOI CUNG]:")
if prediction == 1:
print(f" >>> [CANH BAO] DGA MALWARE DETECTED! <<<")
else:
print(f" >>> [AN TOAN] Ten mien sach (Benign). <<<")
except Exception as e:
print(f"\n[!] Loi khi du doan: {e}")