File size: 53,392 Bytes
9ff5b8d |
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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 |
{
"cells": [
{
"cell_type": "markdown",
"id": "b8f03026",
"metadata": {},
"source": [
"# π‘οΈ Advanced Agentic AI Security Training\n",
"\n",
"## Real-Time Cyber Forge - High-Capability Security Models\n",
"\n",
"This notebook trains production-grade AI models for the Agentic AI security system with:\n",
"\n",
"1. **Real-World Datasets** - Downloads from multiple security intelligence sources\n",
"2. **Multi-Domain Detection** - Phishing, Malware, Intrusion, XSS, SQLi, DGA\n",
"3. **Deep Learning Models** - Neural networks for complex pattern recognition\n",
"4. **Ensemble Systems** - Combined models for high accuracy\n",
"5. **Real-Time Inference** - Optimized for production deployment\n",
"\n",
"---\n",
"\n",
"**Author:** Cyber Forge AI Team \n",
"**Version:** 3.0 - Agentic AI Edition \n",
"**Last Updated:** 2025"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bb02143c",
"metadata": {},
"outputs": [],
"source": [
"# π§ System Setup and Package Installation\n",
"import subprocess\n",
"import sys\n",
"\n",
"def install_packages():\n",
" packages = [\n",
" 'pandas>=2.0.0',\n",
" 'numpy>=1.24.0',\n",
" 'scikit-learn>=1.3.0',\n",
" 'tensorflow>=2.13.0',\n",
" 'xgboost>=2.0.0',\n",
" 'imbalanced-learn>=0.11.0',\n",
" 'matplotlib>=3.7.0',\n",
" 'seaborn>=0.12.0',\n",
" 'aiohttp>=3.8.0',\n",
" 'certifi',\n",
" 'joblib>=1.3.0',\n",
" 'tqdm>=4.65.0',\n",
" ]\n",
" \n",
" for pkg in packages:\n",
" try:\n",
" subprocess.check_call([sys.executable, '-m', 'pip', 'install', '-q', pkg])\n",
" except Exception as e:\n",
" print(f'Warning: {pkg} - {e}')\n",
" \n",
" print('β
Packages ready')\n",
"\n",
"install_packages()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "41d3fd54",
"metadata": {},
"outputs": [],
"source": [
"# π¦ Import Libraries\n",
"import os\n",
"import sys\n",
"import asyncio\n",
"import warnings\n",
"import numpy as np\n",
"import pandas as pd\n",
"import matplotlib.pyplot as plt\n",
"import seaborn as sns\n",
"from datetime import datetime\n",
"from pathlib import Path\n",
"import json\n",
"import joblib\n",
"from tqdm import tqdm\n",
"\n",
"# Machine Learning\n",
"from sklearn.model_selection import train_test_split, cross_val_score, StratifiedKFold\n",
"from sklearn.preprocessing import StandardScaler, LabelEncoder, MinMaxScaler\n",
"from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier\n",
"from sklearn.linear_model import LogisticRegression\n",
"from sklearn.metrics import (\n",
" classification_report, confusion_matrix, roc_auc_score, \n",
" roc_curve, precision_recall_curve, f1_score, accuracy_score,\n",
" precision_score, recall_score\n",
")\n",
"from sklearn.feature_selection import SelectKBest, f_classif, mutual_info_classif\n",
"\n",
"# Deep Learning\n",
"import tensorflow as tf\n",
"from tensorflow.keras.models import Sequential, Model\n",
"from tensorflow.keras.layers import (\n",
" Dense, Dropout, BatchNormalization, Input, \n",
" Conv1D, MaxPooling1D, Flatten, LSTM, GRU,\n",
" Attention, Concatenate, Embedding\n",
")\n",
"from tensorflow.keras.optimizers import Adam\n",
"from tensorflow.keras.callbacks import EarlyStopping, ReduceLROnPlateau, ModelCheckpoint\n",
"from tensorflow.keras.regularizers import l2\n",
"\n",
"# Advanced ML\n",
"import xgboost as xgb\n",
"from imblearn.over_sampling import SMOTE, ADASYN\n",
"from imblearn.under_sampling import RandomUnderSampler\n",
"from imblearn.combine import SMOTETomek\n",
"\n",
"# Configuration\n",
"warnings.filterwarnings('ignore')\n",
"os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'\n",
"np.random.seed(42)\n",
"tf.random.set_seed(42)\n",
"\n",
"# Add project path\n",
"sys.path.insert(0, str(Path.cwd().parent / 'app' / 'services'))\n",
"\n",
"# Visualization style\n",
"plt.style.use('dark_background')\n",
"sns.set_palette('viridis')\n",
"\n",
"print('π Libraries loaded successfully!')\n",
"print(f' TensorFlow: {tf.__version__}')\n",
"print(f' Pandas: {pd.__version__}')\n",
"print(f' NumPy: {np.__version__}')"
]
},
{
"cell_type": "markdown",
"id": "75e3575e",
"metadata": {},
"source": [
"## π₯ Section 1: Download Advanced Security Datasets\n",
"\n",
"Download real-world web security datasets from multiple sources including:\n",
"- Malicious URL databases\n",
"- Phishing detection datasets \n",
"- Network intrusion (NSL-KDD, CICIDS)\n",
"- Threat intelligence feeds\n",
"- Web attack payloads (XSS, SQLi)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "15f87f43",
"metadata": {},
"outputs": [],
"source": [
"# Import our advanced dataset manager\n",
"from web_security_datasets import WebSecurityDatasetManager\n",
"\n",
"# Initialize dataset manager\n",
"DATASET_DIR = Path.cwd().parent / 'datasets' / 'web_security'\n",
"dataset_manager = WebSecurityDatasetManager(str(DATASET_DIR))\n",
"\n",
"print('π Available Dataset Categories:')\n",
"info = dataset_manager.get_available_datasets()\n",
"print(f' Categories: {info[\"categories\"]}')\n",
"print(f' Configured datasets: {len(info[\"configured\"])}')\n",
"print(f' Total samples available: {info[\"total_configured_samples\"]:,}')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "779bc1a4",
"metadata": {},
"outputs": [],
"source": [
"# Download all security datasets\n",
"print('π₯ Downloading advanced web security datasets...')\n",
"print(' This may take a few minutes on first run.\\n')\n",
"\n",
"# Run async download\n",
"async def download_datasets():\n",
" results = await dataset_manager.download_all_datasets(force=False)\n",
" return results\n",
"\n",
"# For Jupyter notebooks\n",
"try:\n",
" # Check if we're in an async context\n",
" loop = asyncio.get_event_loop()\n",
" if loop.is_running():\n",
" import nest_asyncio\n",
" nest_asyncio.apply()\n",
" download_results = loop.run_until_complete(download_datasets())\n",
" else:\n",
" download_results = asyncio.run(download_datasets())\n",
"except:\n",
" download_results = asyncio.run(download_datasets())\n",
"\n",
"print('\\nπ Download Summary:')\n",
"print(f' β
Successful: {len(download_results[\"successful\"])}')\n",
"print(f' βοΈ Skipped (already exists): {len(download_results[\"skipped\"])}')\n",
"print(f' β Failed: {len(download_results[\"failed\"])}')\n",
"print(f' π Total samples: {download_results[\"total_samples\"]:,}')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "33e740c9",
"metadata": {},
"outputs": [],
"source": [
"# List downloaded datasets\n",
"print('\\nπ Downloaded Datasets:\\n')\n",
"for dataset_id, info in dataset_manager.downloaded_datasets.items():\n",
" samples = info.get('actual_samples', info.get('samples', 'N/A'))\n",
" category = info.get('category', 'unknown')\n",
" synthetic = ' (synthetic)' if info.get('synthetic') else ''\n",
" print(f' π¦ {dataset_id}: {samples:,} samples [{category}]{synthetic}')"
]
},
{
"cell_type": "markdown",
"id": "6b0defc0",
"metadata": {},
"source": [
"## π Section 2: Data Loading and Exploration"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "85f355a6",
"metadata": {},
"outputs": [],
"source": [
"# Load datasets by category for multi-domain training\n",
"\n",
"async def load_category_datasets(category: str, max_samples: int = 50000):\n",
" \"\"\"Load and combine datasets from a specific category\"\"\"\n",
" dfs = []\n",
" for dataset_id, info in dataset_manager.downloaded_datasets.items():\n",
" if info.get('category') == category:\n",
" df = await dataset_manager.load_dataset(dataset_id)\n",
" if df is not None:\n",
" if len(df) > max_samples:\n",
" df = df.sample(n=max_samples, random_state=42)\n",
" df['source_dataset'] = dataset_id\n",
" dfs.append(df)\n",
" \n",
" if dfs:\n",
" return pd.concat(dfs, ignore_index=True)\n",
" return pd.DataFrame()\n",
"\n",
"# Load datasets for each domain\n",
"async def load_all_domain_data():\n",
" domains = {}\n",
" categories = ['phishing', 'malware', 'intrusion', 'web_attack', 'dns', 'spam']\n",
" \n",
" for cat in categories:\n",
" df = await load_category_datasets(cat)\n",
" if len(df) > 0:\n",
" domains[cat] = df\n",
" print(f' β
{cat}: {len(df):,} samples')\n",
" \n",
" return domains\n",
"\n",
"print('π Loading domain-specific datasets...\\n')\n",
"\n",
"try:\n",
" loop = asyncio.get_event_loop()\n",
" if loop.is_running():\n",
" domain_datasets = loop.run_until_complete(load_all_domain_data())\n",
" else:\n",
" domain_datasets = asyncio.run(load_all_domain_data())\n",
"except:\n",
" domain_datasets = asyncio.run(load_all_domain_data())\n",
"\n",
"print(f'\\nπ Loaded {len(domain_datasets)} security domains')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "acefa098",
"metadata": {},
"outputs": [],
"source": [
"# Visualize dataset distributions\n",
"fig, axes = plt.subplots(2, 3, figsize=(15, 10))\n",
"axes = axes.ravel()\n",
"\n",
"for idx, (domain, df) in enumerate(domain_datasets.items()):\n",
" if idx >= 6:\n",
" break\n",
" \n",
" # Find target column\n",
" target_cols = [c for c in df.columns if 'malicious' in c.lower() or 'attack' in c.lower() \n",
" or 'is_' in c.lower() or 'label' in c.lower() or 'result' in c.lower()]\n",
" \n",
" if target_cols:\n",
" target = target_cols[0]\n",
" df[target].value_counts().plot(kind='bar', ax=axes[idx], color=['#4ecdc4', '#ff6b6b'])\n",
" axes[idx].set_title(f'{domain.upper()} - Target Distribution', color='white')\n",
" axes[idx].set_xlabel('Class', color='white')\n",
" axes[idx].set_ylabel('Count', color='white')\n",
" axes[idx].tick_params(colors='white')\n",
"\n",
"plt.tight_layout()\n",
"plt.suptitle('π― Security Domain Dataset Distributions', y=1.02, fontsize=16, color='white')\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"id": "e80c5117",
"metadata": {},
"source": [
"## π οΈ Section 3: Advanced Feature Engineering"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c6f87d02",
"metadata": {},
"outputs": [],
"source": [
"class AgenticSecurityFeatureEngineer:\n",
" \"\"\"\n",
" Advanced feature engineering for Agentic AI security models.\n",
" Creates domain-specific features optimized for real-time detection.\n",
" \"\"\"\n",
" \n",
" def __init__(self):\n",
" self.scalers = {}\n",
" self.encoders = {}\n",
" self.feature_stats = {}\n",
" \n",
" def engineer_phishing_features(self, df: pd.DataFrame) -> pd.DataFrame:\n",
" \"\"\"Create advanced phishing detection features\"\"\"\n",
" df = df.copy()\n",
" \n",
" # URL entropy (if URL text is available)\n",
" if 'url' in df.columns:\n",
" df['url_entropy'] = df['url'].apply(self._calculate_entropy)\n",
" df['url_digit_ratio'] = df['url'].apply(lambda x: sum(c.isdigit() for c in str(x)) / max(len(str(x)), 1))\n",
" df['url_special_ratio'] = df['url'].apply(lambda x: sum(not c.isalnum() for c in str(x)) / max(len(str(x)), 1))\n",
" \n",
" # Composite risk scores\n",
" numeric_cols = df.select_dtypes(include=[np.number]).columns\n",
" if len(numeric_cols) > 0:\n",
" df['risk_score'] = df[numeric_cols].mean(axis=1)\n",
" df['risk_variance'] = df[numeric_cols].var(axis=1)\n",
" \n",
" return df\n",
" \n",
" def engineer_malware_features(self, df: pd.DataFrame) -> pd.DataFrame:\n",
" \"\"\"Create advanced malware detection features\"\"\"\n",
" df = df.copy()\n",
" \n",
" # Entropy-based features\n",
" if 'entropy' in df.columns:\n",
" df['high_entropy'] = (df['entropy'] > 7.0).astype(int)\n",
" df['entropy_squared'] = df['entropy'] ** 2\n",
" \n",
" # Size-based features\n",
" if 'file_size' in df.columns:\n",
" df['log_file_size'] = np.log1p(df['file_size'])\n",
" df['size_category'] = pd.cut(df['file_size'], bins=[0, 10000, 100000, 1000000, np.inf], \n",
" labels=[0, 1, 2, 3]).astype(int)\n",
" \n",
" # API/Import analysis\n",
" if 'suspicious_api_calls' in df.columns and 'imports_count' in df.columns:\n",
" df['api_to_import_ratio'] = df['suspicious_api_calls'] / (df['imports_count'] + 1)\n",
" \n",
" return df\n",
" \n",
" def engineer_intrusion_features(self, df: pd.DataFrame) -> pd.DataFrame:\n",
" \"\"\"Create advanced network intrusion features\"\"\"\n",
" df = df.copy()\n",
" \n",
" # Traffic volume features\n",
" if 'src_bytes' in df.columns and 'dst_bytes' in df.columns:\n",
" df['total_bytes'] = df['src_bytes'] + df['dst_bytes']\n",
" df['bytes_ratio'] = df['src_bytes'] / (df['dst_bytes'] + 1)\n",
" df['log_total_bytes'] = np.log1p(df['total_bytes'])\n",
" \n",
" # Connection features\n",
" if 'duration' in df.columns:\n",
" df['log_duration'] = np.log1p(df['duration'])\n",
" df['short_connection'] = (df['duration'] < 1).astype(int)\n",
" \n",
" # Error rate features\n",
" if 'serror_rate' in df.columns:\n",
" df['high_error_rate'] = (df['serror_rate'] > 0.5).astype(int)\n",
" \n",
" return df\n",
" \n",
" def engineer_web_attack_features(self, df: pd.DataFrame) -> pd.DataFrame:\n",
" \"\"\"Create advanced web attack detection features\"\"\"\n",
" df = df.copy()\n",
" \n",
" # Payload analysis\n",
" if 'payload' in df.columns:\n",
" df['payload_length'] = df['payload'].apply(lambda x: len(str(x)))\n",
" df['payload_entropy'] = df['payload'].apply(self._calculate_entropy)\n",
" df['has_script_tag'] = df['payload'].apply(lambda x: 1 if '<script' in str(x).lower() else 0)\n",
" df['has_sql_keyword'] = df['payload'].apply(\n",
" lambda x: 1 if any(kw in str(x).lower() for kw in ['select', 'union', 'drop', 'insert']) else 0\n",
" )\n",
" \n",
" # URL features\n",
" if 'url_length' in df.columns:\n",
" df['long_url'] = (df['url_length'] > 100).astype(int)\n",
" \n",
" return df\n",
" \n",
" def engineer_dns_features(self, df: pd.DataFrame) -> pd.DataFrame:\n",
" \"\"\"Create advanced DNS/DGA detection features\"\"\"\n",
" df = df.copy()\n",
" \n",
" if 'domain' in df.columns:\n",
" df['domain_entropy'] = df['domain'].apply(self._calculate_entropy)\n",
" df['consonant_ratio'] = df['domain'].apply(self._consonant_ratio)\n",
" df['digit_ratio'] = df['domain'].apply(lambda x: sum(c.isdigit() for c in str(x)) / max(len(str(x)), 1))\n",
" \n",
" if 'entropy' in df.columns:\n",
" df['entropy_normalized'] = (df['entropy'] - df['entropy'].min()) / (df['entropy'].max() - df['entropy'].min() + 1e-8)\n",
" \n",
" return df\n",
" \n",
" def _calculate_entropy(self, text: str) -> float:\n",
" \"\"\"Calculate Shannon entropy of text\"\"\"\n",
" if not text or pd.isna(text):\n",
" return 0.0\n",
" text = str(text)\n",
" prob = [float(text.count(c)) / len(text) for c in set(text)]\n",
" return -sum(p * np.log2(p) for p in prob if p > 0)\n",
" \n",
" def _consonant_ratio(self, text: str) -> float:\n",
" \"\"\"Calculate consonant to vowel ratio\"\"\"\n",
" if not text or pd.isna(text):\n",
" return 0.0\n",
" text = str(text).lower()\n",
" vowels = set('aeiou')\n",
" consonants = sum(1 for c in text if c.isalpha() and c not in vowels)\n",
" total_letters = sum(1 for c in text if c.isalpha())\n",
" return consonants / max(total_letters, 1)\n",
" \n",
" def process_dataset(self, df: pd.DataFrame, domain: str) -> pd.DataFrame:\n",
" \"\"\"Apply domain-specific feature engineering\"\"\"\n",
" engineers = {\n",
" 'phishing': self.engineer_phishing_features,\n",
" 'malware': self.engineer_malware_features,\n",
" 'intrusion': self.engineer_intrusion_features,\n",
" 'web_attack': self.engineer_web_attack_features,\n",
" 'dns': self.engineer_dns_features,\n",
" }\n",
" \n",
" engineer_func = engineers.get(domain)\n",
" if engineer_func:\n",
" return engineer_func(df)\n",
" return df\n",
"\n",
"# Initialize feature engineer\n",
"feature_engineer = AgenticSecurityFeatureEngineer()\n",
"print('β
Feature engineer initialized')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "039a7ae5",
"metadata": {},
"outputs": [],
"source": [
"# Apply feature engineering to all domains\n",
"print('π§ Applying advanced feature engineering...\\n')\n",
"\n",
"engineered_datasets = {}\n",
"for domain, df in domain_datasets.items():\n",
" original_features = len(df.columns)\n",
" engineered_df = feature_engineer.process_dataset(df, domain)\n",
" new_features = len(engineered_df.columns)\n",
" engineered_datasets[domain] = engineered_df\n",
" print(f' {domain}: {original_features} β {new_features} features (+{new_features - original_features})')\n",
"\n",
"print('\\nβ
Feature engineering complete!')"
]
},
{
"cell_type": "markdown",
"id": "aa853980",
"metadata": {},
"source": [
"## π€ Section 4: Model Architecture Definitions"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8aa31308",
"metadata": {},
"outputs": [],
"source": [
"class AgenticSecurityModels:\n",
" \"\"\"\n",
" Advanced ML/DL model architectures for agentic AI security.\n",
" Optimized for real-time inference and high accuracy.\n",
" \"\"\"\n",
" \n",
" @staticmethod\n",
" def create_deep_neural_network(input_dim: int, \n",
" name: str = 'security_dnn',\n",
" hidden_layers: list = [256, 128, 64, 32],\n",
" dropout_rate: float = 0.3) -> Model:\n",
" \"\"\"Create a deep neural network for security classification\"\"\"\n",
" \n",
" inputs = Input(shape=(input_dim,), name='input')\n",
" x = inputs\n",
" \n",
" for i, units in enumerate(hidden_layers):\n",
" x = Dense(units, activation='relu', \n",
" kernel_regularizer=l2(0.001),\n",
" name=f'dense_{i}')(x)\n",
" x = BatchNormalization(name=f'bn_{i}')(x)\n",
" x = Dropout(dropout_rate * (1 - i * 0.1), name=f'dropout_{i}')(x)\n",
" \n",
" outputs = Dense(1, activation='sigmoid', name='output')(x)\n",
" \n",
" model = Model(inputs, outputs, name=name)\n",
" model.compile(\n",
" optimizer=Adam(learning_rate=0.001),\n",
" loss='binary_crossentropy',\n",
" metrics=['accuracy', 'precision', 'recall', 'AUC']\n",
" )\n",
" \n",
" return model\n",
" \n",
" @staticmethod\n",
" def create_wide_and_deep(input_dim: int, name: str = 'wide_deep') -> Model:\n",
" \"\"\"Create Wide & Deep architecture for combining memorization and generalization\"\"\"\n",
" \n",
" inputs = Input(shape=(input_dim,))\n",
" \n",
" # Wide component (linear)\n",
" wide = Dense(1, activation=None, name='wide')(inputs)\n",
" \n",
" # Deep component\n",
" deep = Dense(128, activation='relu')(inputs)\n",
" deep = BatchNormalization()(deep)\n",
" deep = Dropout(0.3)(deep)\n",
" deep = Dense(64, activation='relu')(deep)\n",
" deep = BatchNormalization()(deep)\n",
" deep = Dropout(0.2)(deep)\n",
" deep = Dense(32, activation='relu')(deep)\n",
" deep = Dense(1, activation=None, name='deep')(deep)\n",
" \n",
" # Combine wide and deep\n",
" combined = tf.keras.layers.Add()([wide, deep])\n",
" outputs = tf.keras.layers.Activation('sigmoid')(combined)\n",
" \n",
" model = Model(inputs, outputs, name=name)\n",
" model.compile(\n",
" optimizer=Adam(learning_rate=0.001),\n",
" loss='binary_crossentropy',\n",
" metrics=['accuracy', 'precision', 'recall', 'AUC']\n",
" )\n",
" \n",
" return model\n",
" \n",
" @staticmethod\n",
" def create_residual_network(input_dim: int, name: str = 'resnet') -> Model:\n",
" \"\"\"Create Residual Network for security classification\"\"\"\n",
" \n",
" def residual_block(x, units):\n",
" shortcut = x\n",
" \n",
" x = Dense(units, activation='relu')(x)\n",
" x = BatchNormalization()(x)\n",
" x = Dense(units, activation=None)(x)\n",
" x = BatchNormalization()(x)\n",
" \n",
" # Match dimensions if needed\n",
" if shortcut.shape[-1] != units:\n",
" shortcut = Dense(units, activation=None)(shortcut)\n",
" \n",
" x = tf.keras.layers.Add()([x, shortcut])\n",
" x = tf.keras.layers.Activation('relu')(x)\n",
" return x\n",
" \n",
" inputs = Input(shape=(input_dim,))\n",
" \n",
" # Initial projection\n",
" x = Dense(128, activation='relu')(inputs)\n",
" x = BatchNormalization()(x)\n",
" \n",
" # Residual blocks\n",
" x = residual_block(x, 128)\n",
" x = Dropout(0.3)(x)\n",
" x = residual_block(x, 64)\n",
" x = Dropout(0.2)(x)\n",
" x = residual_block(x, 32)\n",
" \n",
" # Output\n",
" outputs = Dense(1, activation='sigmoid')(x)\n",
" \n",
" model = Model(inputs, outputs, name=name)\n",
" model.compile(\n",
" optimizer=Adam(learning_rate=0.001),\n",
" loss='binary_crossentropy',\n",
" metrics=['accuracy', 'precision', 'recall', 'AUC']\n",
" )\n",
" \n",
" return model\n",
" \n",
" @staticmethod\n",
" def create_xgboost_classifier(n_estimators: int = 200) -> xgb.XGBClassifier:\n",
" \"\"\"Create optimized XGBoost classifier\"\"\"\n",
" return xgb.XGBClassifier(\n",
" n_estimators=n_estimators,\n",
" max_depth=10,\n",
" learning_rate=0.1,\n",
" subsample=0.8,\n",
" colsample_bytree=0.8,\n",
" reg_alpha=0.1,\n",
" reg_lambda=1.0,\n",
" random_state=42,\n",
" n_jobs=-1,\n",
" use_label_encoder=False,\n",
" eval_metric='logloss'\n",
" )\n",
" \n",
" @staticmethod\n",
" def create_random_forest(n_estimators: int = 200) -> RandomForestClassifier:\n",
" \"\"\"Create optimized Random Forest classifier\"\"\"\n",
" return RandomForestClassifier(\n",
" n_estimators=n_estimators,\n",
" max_depth=20,\n",
" min_samples_split=5,\n",
" min_samples_leaf=2,\n",
" max_features='sqrt',\n",
" class_weight='balanced',\n",
" random_state=42,\n",
" n_jobs=-1\n",
" )\n",
"\n",
"print('β
Model architectures defined')"
]
},
{
"cell_type": "markdown",
"id": "f0eeb16b",
"metadata": {},
"source": [
"## π― Section 5: Multi-Domain Model Training"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ff04c2d3",
"metadata": {},
"outputs": [],
"source": [
"class AgenticSecurityTrainer:\n",
" \"\"\"\n",
" Comprehensive training pipeline for multi-domain security models.\n",
" \"\"\"\n",
" \n",
" def __init__(self, models_dir: str = '../models/agentic_security'):\n",
" self.models_dir = Path(models_dir)\n",
" self.models_dir.mkdir(parents=True, exist_ok=True)\n",
" self.trained_models = {}\n",
" self.scalers = {}\n",
" self.feature_names = {}\n",
" self.metrics = {}\n",
" \n",
" def prepare_data(self, df: pd.DataFrame, domain: str) -> tuple:\n",
" \"\"\"Prepare data for training\"\"\"\n",
" \n",
" # Find target column\n",
" target_candidates = ['is_malicious', 'is_attack', 'is_malware', 'is_spam', \n",
" 'is_dga', 'is_miner', 'is_suspicious', 'label', 'result']\n",
" \n",
" target_col = None\n",
" for col in target_candidates:\n",
" if col in df.columns:\n",
" target_col = col\n",
" break\n",
" \n",
" if target_col is None:\n",
" # Try to find any binary column\n",
" for col in df.columns:\n",
" if df[col].nunique() == 2 and df[col].dtype in [np.int64, np.int32, np.float64]:\n",
" target_col = col\n",
" break\n",
" \n",
" if target_col is None:\n",
" raise ValueError(f'No suitable target column found for {domain}')\n",
" \n",
" # Select numeric features only\n",
" exclude_cols = [target_col, 'source_dataset', '_dataset_id', '_category',\n",
" 'url', 'payload', 'domain', 'ip_address', 'attack_type']\n",
" \n",
" feature_cols = [col for col in df.select_dtypes(include=[np.number]).columns \n",
" if col not in exclude_cols]\n",
" \n",
" X = df[feature_cols].fillna(0)\n",
" y = df[target_col].astype(int)\n",
" \n",
" # Remove infinite values\n",
" X = X.replace([np.inf, -np.inf], 0)\n",
" \n",
" self.feature_names[domain] = feature_cols\n",
" \n",
" return X, y, feature_cols\n",
" \n",
" def train_domain_models(self, df: pd.DataFrame, domain: str) -> dict:\n",
" \"\"\"Train all models for a specific security domain\"\"\"\n",
" \n",
" print(f'\\nπ― Training models for: {domain.upper()}')\n",
" print('=' * 50)\n",
" \n",
" # Prepare data\n",
" X, y, feature_cols = self.prepare_data(df, domain)\n",
" print(f' π Data: {X.shape[0]:,} samples, {X.shape[1]} features')\n",
" print(f' βοΈ Class balance: {y.value_counts().to_dict()}')\n",
" \n",
" # Split data\n",
" X_train, X_test, y_train, y_test = train_test_split(\n",
" X, y, test_size=0.2, random_state=42, stratify=y\n",
" )\n",
" \n",
" # Scale features\n",
" scaler = StandardScaler()\n",
" X_train_scaled = scaler.fit_transform(X_train)\n",
" X_test_scaled = scaler.transform(X_test)\n",
" self.scalers[domain] = scaler\n",
" \n",
" # Handle class imbalance\n",
" try:\n",
" smote = SMOTE(random_state=42)\n",
" X_train_balanced, y_train_balanced = smote.fit_resample(X_train_scaled, y_train)\n",
" print(f' βοΈ After SMOTE: {len(X_train_balanced):,} samples')\n",
" except:\n",
" X_train_balanced, y_train_balanced = X_train_scaled, y_train\n",
" print(' β οΈ SMOTE skipped')\n",
" \n",
" results = {}\n",
" \n",
" # 1. Train Random Forest\n",
" print('\\n π² Training Random Forest...')\n",
" rf = AgenticSecurityModels.create_random_forest()\n",
" rf.fit(X_train_balanced, y_train_balanced)\n",
" rf_pred = rf.predict(X_test_scaled)\n",
" rf_proba = rf.predict_proba(X_test_scaled)[:, 1]\n",
" results['random_forest'] = {\n",
" 'model': rf,\n",
" 'predictions': rf_pred,\n",
" 'probabilities': rf_proba,\n",
" 'accuracy': accuracy_score(y_test, rf_pred),\n",
" 'f1': f1_score(y_test, rf_pred),\n",
" 'auc': roc_auc_score(y_test, rf_proba)\n",
" }\n",
" print(f' Accuracy: {results[\"random_forest\"][\"accuracy\"]:.4f}, AUC: {results[\"random_forest\"][\"auc\"]:.4f}')\n",
" \n",
" # 2. Train XGBoost\n",
" print(' π Training XGBoost...')\n",
" xgb_model = AgenticSecurityModels.create_xgboost_classifier()\n",
" xgb_model.fit(X_train_balanced, y_train_balanced)\n",
" xgb_pred = xgb_model.predict(X_test_scaled)\n",
" xgb_proba = xgb_model.predict_proba(X_test_scaled)[:, 1]\n",
" results['xgboost'] = {\n",
" 'model': xgb_model,\n",
" 'predictions': xgb_pred,\n",
" 'probabilities': xgb_proba,\n",
" 'accuracy': accuracy_score(y_test, xgb_pred),\n",
" 'f1': f1_score(y_test, xgb_pred),\n",
" 'auc': roc_auc_score(y_test, xgb_proba)\n",
" }\n",
" print(f' Accuracy: {results[\"xgboost\"][\"accuracy\"]:.4f}, AUC: {results[\"xgboost\"][\"auc\"]:.4f}')\n",
" \n",
" # 3. Train Deep Neural Network\n",
" print(' π§ Training Deep Neural Network...')\n",
" dnn = AgenticSecurityModels.create_deep_neural_network(X_train_scaled.shape[1], name=f'{domain}_dnn')\n",
" \n",
" callbacks = [\n",
" EarlyStopping(monitor='val_loss', patience=10, restore_best_weights=True),\n",
" ReduceLROnPlateau(monitor='val_loss', factor=0.2, patience=5, min_lr=1e-6)\n",
" ]\n",
" \n",
" history = dnn.fit(\n",
" X_train_balanced, y_train_balanced,\n",
" epochs=50,\n",
" batch_size=64,\n",
" validation_split=0.2,\n",
" callbacks=callbacks,\n",
" verbose=0\n",
" )\n",
" \n",
" dnn_proba = dnn.predict(X_test_scaled, verbose=0).flatten()\n",
" dnn_pred = (dnn_proba > 0.5).astype(int)\n",
" results['deep_neural_network'] = {\n",
" 'model': dnn,\n",
" 'predictions': dnn_pred,\n",
" 'probabilities': dnn_proba,\n",
" 'accuracy': accuracy_score(y_test, dnn_pred),\n",
" 'f1': f1_score(y_test, dnn_pred),\n",
" 'auc': roc_auc_score(y_test, dnn_proba)\n",
" }\n",
" print(f' Accuracy: {results[\"deep_neural_network\"][\"accuracy\"]:.4f}, AUC: {results[\"deep_neural_network\"][\"auc\"]:.4f}')\n",
" \n",
" # 4. Create Ensemble\n",
" print(' π Creating Ensemble...')\n",
" weights = np.array([r['auc'] for r in results.values()])\n",
" weights = weights / weights.sum()\n",
" \n",
" ensemble_proba = (\n",
" weights[0] * rf_proba +\n",
" weights[1] * xgb_proba +\n",
" weights[2] * dnn_proba\n",
" )\n",
" ensemble_pred = (ensemble_proba > 0.5).astype(int)\n",
" \n",
" results['ensemble'] = {\n",
" 'weights': weights.tolist(),\n",
" 'predictions': ensemble_pred,\n",
" 'probabilities': ensemble_proba,\n",
" 'accuracy': accuracy_score(y_test, ensemble_pred),\n",
" 'f1': f1_score(y_test, ensemble_pred),\n",
" 'auc': roc_auc_score(y_test, ensemble_proba)\n",
" }\n",
" print(f' Accuracy: {results[\"ensemble\"][\"accuracy\"]:.4f}, AUC: {results[\"ensemble\"][\"auc\"]:.4f}')\n",
" \n",
" # Store metrics\n",
" self.metrics[domain] = {\n",
" model_name: {\n",
" 'accuracy': r['accuracy'],\n",
" 'f1': r['f1'],\n",
" 'auc': r['auc']\n",
" }\n",
" for model_name, r in results.items()\n",
" }\n",
" \n",
" self.trained_models[domain] = results\n",
" \n",
" return results\n",
" \n",
" def save_models(self):\n",
" \"\"\"Save all trained models\"\"\"\n",
" print('\\nπΎ Saving trained models...')\n",
" \n",
" for domain, results in self.trained_models.items():\n",
" domain_dir = self.models_dir / domain\n",
" domain_dir.mkdir(exist_ok=True)\n",
" \n",
" # Save sklearn models\n",
" if 'random_forest' in results:\n",
" joblib.dump(results['random_forest']['model'], domain_dir / 'random_forest.pkl')\n",
" if 'xgboost' in results:\n",
" joblib.dump(results['xgboost']['model'], domain_dir / 'xgboost.pkl')\n",
" \n",
" # Save Keras model\n",
" if 'deep_neural_network' in results:\n",
" results['deep_neural_network']['model'].save(domain_dir / 'deep_neural_network.keras')\n",
" \n",
" # Save scaler\n",
" if domain in self.scalers:\n",
" joblib.dump(self.scalers[domain], domain_dir / 'scaler.pkl')\n",
" \n",
" # Save feature names\n",
" if domain in self.feature_names:\n",
" joblib.dump(self.feature_names[domain], domain_dir / 'feature_names.pkl')\n",
" \n",
" # Save ensemble config\n",
" if 'ensemble' in results:\n",
" config = {\n",
" 'weights': results['ensemble']['weights'],\n",
" 'models': ['random_forest', 'xgboost', 'deep_neural_network'],\n",
" 'threshold': 0.5\n",
" }\n",
" joblib.dump(config, domain_dir / 'ensemble_config.pkl')\n",
" \n",
" print(f' β
Saved {domain} models to {domain_dir}')\n",
" \n",
" # Save overall metrics\n",
" with open(self.models_dir / 'training_metrics.json', 'w') as f:\n",
" json.dump(self.metrics, f, indent=2)\n",
" \n",
" print(f'\\nπ All models saved to {self.models_dir}')\n",
"\n",
"# Initialize trainer\n",
"trainer = AgenticSecurityTrainer()\n",
"print('β
Trainer initialized')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d21ba338",
"metadata": {},
"outputs": [],
"source": [
"# Train models for all security domains\n",
"print('π Starting Multi-Domain Security Model Training')\n",
"print('=' * 60)\n",
"\n",
"for domain, df in engineered_datasets.items():\n",
" if len(df) < 100:\n",
" print(f'\\nβ οΈ Skipping {domain} - insufficient data ({len(df)} samples)')\n",
" continue\n",
" \n",
" try:\n",
" trainer.train_domain_models(df, domain)\n",
" except Exception as e:\n",
" print(f'\\nβ Error training {domain}: {e}')\n",
" continue\n",
"\n",
"print('\\n' + '=' * 60)\n",
"print('π Multi-Domain Training Complete!')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "50fe57e8",
"metadata": {},
"outputs": [],
"source": [
"# Visualize training results\n",
"if trainer.metrics:\n",
" # Create comparison visualization\n",
" fig, axes = plt.subplots(1, 3, figsize=(18, 6))\n",
" \n",
" metrics_to_plot = ['accuracy', 'f1', 'auc']\n",
" colors = ['#4ecdc4', '#ff6b6b', '#ffe66d', '#95e1d3']\n",
" \n",
" for idx, metric in enumerate(metrics_to_plot):\n",
" data = []\n",
" labels = []\n",
" \n",
" for domain, models in trainer.metrics.items():\n",
" for model_name, model_metrics in models.items():\n",
" data.append(model_metrics[metric])\n",
" labels.append(f'{domain}\\n{model_name}')\n",
" \n",
" x = range(len(data))\n",
" axes[idx].bar(x, data, color=colors * 10)\n",
" axes[idx].set_xticks(x)\n",
" axes[idx].set_xticklabels(labels, rotation=45, ha='right', fontsize=8)\n",
" axes[idx].set_ylabel(metric.upper(), color='white')\n",
" axes[idx].set_title(f'{metric.upper()} Across Models', color='white', fontsize=14)\n",
" axes[idx].set_ylim(0, 1)\n",
" axes[idx].axhline(y=0.9, color='red', linestyle='--', alpha=0.5, label='90% threshold')\n",
" axes[idx].grid(True, alpha=0.3)\n",
" \n",
" plt.tight_layout()\n",
" plt.suptitle('π― Multi-Domain Security Model Performance', y=1.02, fontsize=16, color='white')\n",
" plt.show()\n",
"\n",
"# Print summary table\n",
"print('\\nπ Training Results Summary')\n",
"print('=' * 80)\n",
"print(f'{\"Domain\":<15} {\"Model\":<25} {\"Accuracy\":<12} {\"F1\":<12} {\"AUC\":<12}')\n",
"print('-' * 80)\n",
"\n",
"for domain, models in trainer.metrics.items():\n",
" for model_name, metrics in models.items():\n",
" print(f'{domain:<15} {model_name:<25} {metrics[\"accuracy\"]:<12.4f} {metrics[\"f1\"]:<12.4f} {metrics[\"auc\"]:<12.4f}')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3a12da59",
"metadata": {},
"outputs": [],
"source": [
"# Save all trained models\n",
"trainer.save_models()"
]
},
{
"cell_type": "markdown",
"id": "fdfb081b",
"metadata": {},
"source": [
"## π Section 6: Real-Time Inference API"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c2ef7b51",
"metadata": {},
"outputs": [],
"source": [
"class AgenticSecurityInference:\n",
" \"\"\"\n",
" Real-time inference engine for the Agentic AI security system.\n",
" Provides unified API for all security domains.\n",
" \"\"\"\n",
" \n",
" def __init__(self, models_dir: str = '../models/agentic_security'):\n",
" self.models_dir = Path(models_dir)\n",
" self.models = {}\n",
" self.scalers = {}\n",
" self.feature_names = {}\n",
" self.ensemble_configs = {}\n",
" self._load_models()\n",
" \n",
" def _load_models(self):\n",
" \"\"\"Load all trained models\"\"\"\n",
" print('π¦ Loading trained models...')\n",
" \n",
" for domain_dir in self.models_dir.iterdir():\n",
" if domain_dir.is_dir():\n",
" domain = domain_dir.name\n",
" self.models[domain] = {}\n",
" \n",
" # Load sklearn models\n",
" rf_path = domain_dir / 'random_forest.pkl'\n",
" if rf_path.exists():\n",
" self.models[domain]['random_forest'] = joblib.load(rf_path)\n",
" \n",
" xgb_path = domain_dir / 'xgboost.pkl'\n",
" if xgb_path.exists():\n",
" self.models[domain]['xgboost'] = joblib.load(xgb_path)\n",
" \n",
" # Load Keras model\n",
" dnn_path = domain_dir / 'deep_neural_network.keras'\n",
" if dnn_path.exists():\n",
" self.models[domain]['dnn'] = tf.keras.models.load_model(dnn_path)\n",
" \n",
" # Load scaler\n",
" scaler_path = domain_dir / 'scaler.pkl'\n",
" if scaler_path.exists():\n",
" self.scalers[domain] = joblib.load(scaler_path)\n",
" \n",
" # Load feature names\n",
" features_path = domain_dir / 'feature_names.pkl'\n",
" if features_path.exists():\n",
" self.feature_names[domain] = joblib.load(features_path)\n",
" \n",
" # Load ensemble config\n",
" config_path = domain_dir / 'ensemble_config.pkl'\n",
" if config_path.exists():\n",
" self.ensemble_configs[domain] = joblib.load(config_path)\n",
" \n",
" print(f' β
Loaded {domain}: {list(self.models[domain].keys())}')\n",
" \n",
" print(f'\\nπ Loaded models for {len(self.models)} security domains')\n",
" \n",
" def predict(self, features: dict, domain: str, use_ensemble: bool = True) -> dict:\n",
" \"\"\"\n",
" Make a real-time security prediction.\n",
" \n",
" Args:\n",
" features: Dictionary of feature values\n",
" domain: Security domain (phishing, malware, intrusion, etc.)\n",
" use_ensemble: Whether to use ensemble prediction\n",
" \n",
" Returns:\n",
" Prediction result with confidence and risk assessment\n",
" \"\"\"\n",
" if domain not in self.models:\n",
" return {'error': f'Unknown domain: {domain}', 'available_domains': list(self.models.keys())}\n",
" \n",
" try:\n",
" # Prepare features\n",
" feature_names = self.feature_names.get(domain, list(features.keys()))\n",
" X = np.zeros((1, len(feature_names)))\n",
" \n",
" for i, fname in enumerate(feature_names):\n",
" if fname in features:\n",
" X[0, i] = features[fname]\n",
" \n",
" # Scale features\n",
" if domain in self.scalers:\n",
" X_scaled = self.scalers[domain].transform(X)\n",
" else:\n",
" X_scaled = X\n",
" \n",
" # Get predictions from each model\n",
" probabilities = {}\n",
" \n",
" if 'random_forest' in self.models[domain]:\n",
" probabilities['random_forest'] = float(self.models[domain]['random_forest'].predict_proba(X_scaled)[0, 1])\n",
" \n",
" if 'xgboost' in self.models[domain]:\n",
" probabilities['xgboost'] = float(self.models[domain]['xgboost'].predict_proba(X_scaled)[0, 1])\n",
" \n",
" if 'dnn' in self.models[domain]:\n",
" probabilities['dnn'] = float(self.models[domain]['dnn'].predict(X_scaled, verbose=0)[0, 0])\n",
" \n",
" # Calculate ensemble probability\n",
" if use_ensemble and domain in self.ensemble_configs:\n",
" weights = self.ensemble_configs[domain]['weights']\n",
" prob_values = list(probabilities.values())\n",
" threat_probability = sum(w * p for w, p in zip(weights, prob_values))\n",
" else:\n",
" threat_probability = np.mean(list(probabilities.values()))\n",
" \n",
" # Determine prediction and risk level\n",
" is_threat = threat_probability > 0.5\n",
" confidence = threat_probability if is_threat else 1 - threat_probability\n",
" \n",
" if threat_probability > 0.9:\n",
" risk_level = 'CRITICAL'\n",
" elif threat_probability > 0.7:\n",
" risk_level = 'HIGH'\n",
" elif threat_probability > 0.5:\n",
" risk_level = 'MEDIUM'\n",
" elif threat_probability > 0.3:\n",
" risk_level = 'LOW'\n",
" else:\n",
" risk_level = 'MINIMAL'\n",
" \n",
" return {\n",
" 'domain': domain,\n",
" 'prediction': 'THREAT' if is_threat else 'SAFE',\n",
" 'threat_probability': round(threat_probability, 4),\n",
" 'confidence': round(confidence, 4),\n",
" 'risk_level': risk_level,\n",
" 'model_scores': probabilities,\n",
" 'timestamp': datetime.now().isoformat()\n",
" }\n",
" \n",
" except Exception as e:\n",
" return {'error': str(e), 'domain': domain}\n",
" \n",
" def analyze_url(self, url_features: dict) -> dict:\n",
" \"\"\"Specialized URL/phishing analysis\"\"\"\n",
" return self.predict(url_features, 'phishing')\n",
" \n",
" def analyze_file(self, file_features: dict) -> dict:\n",
" \"\"\"Specialized file/malware analysis\"\"\"\n",
" return self.predict(file_features, 'malware')\n",
" \n",
" def analyze_network(self, network_features: dict) -> dict:\n",
" \"\"\"Specialized network/intrusion analysis\"\"\"\n",
" return self.predict(network_features, 'intrusion')\n",
" \n",
" def analyze_request(self, request_features: dict) -> dict:\n",
" \"\"\"Specialized web request/attack analysis\"\"\"\n",
" return self.predict(request_features, 'web_attack')\n",
"\n",
"# Initialize inference engine\n",
"inference = AgenticSecurityInference()\n",
"print('\\nβ
Inference engine ready!')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6070af31",
"metadata": {},
"outputs": [],
"source": [
"# Test the inference engine with sample data\n",
"print('π§ͺ Testing Inference Engine\\n')\n",
"\n",
"# Test phishing detection\n",
"phishing_sample = {\n",
" 'url_length': 250,\n",
" 'num_dots': 8,\n",
" 'has_ip': 1,\n",
" 'has_at_symbol': 1,\n",
" 'subdomain_level': 5,\n",
" 'domain_age_days': 15,\n",
" 'has_https': 0,\n",
" 'special_char_count': 12\n",
"}\n",
"\n",
"result = inference.analyze_url(phishing_sample)\n",
"print('π Phishing Analysis Result:')\n",
"print(f' Prediction: {result.get(\"prediction\", \"N/A\")}')\n",
"print(f' Threat Probability: {result.get(\"threat_probability\", 0):.2%}')\n",
"print(f' Risk Level: {result.get(\"risk_level\", \"N/A\")}')\n",
"print(f' Confidence: {result.get(\"confidence\", 0):.2%}')\n",
"\n",
"# Test malware detection\n",
"malware_sample = {\n",
" 'file_size': 1048576,\n",
" 'entropy': 7.8,\n",
" 'pe_sections': 12,\n",
" 'imports_count': 250,\n",
" 'suspicious_api_calls': 15,\n",
" 'packed': 1\n",
"}\n",
"\n",
"result = inference.analyze_file(malware_sample)\n",
"print('\\nπ¦ Malware Analysis Result:')\n",
"print(f' Prediction: {result.get(\"prediction\", \"N/A\")}')\n",
"print(f' Threat Probability: {result.get(\"threat_probability\", 0):.2%}')\n",
"print(f' Risk Level: {result.get(\"risk_level\", \"N/A\")}')\n",
"\n",
"print('\\nβ
Inference tests complete!')"
]
},
{
"cell_type": "markdown",
"id": "2dee89a6",
"metadata": {},
"source": [
"## π Section 7: Summary and Next Steps\n",
"\n",
"### β
What We Accomplished:\n",
"\n",
"1. **π₯ Dataset Collection**\n",
" - Downloaded 15+ web security datasets\n",
" - Covered phishing, malware, intrusion, web attacks, DNS, spam\n",
" - Combined real-world and synthetic data for comprehensive training\n",
"\n",
"2. **π§ Feature Engineering**\n",
" - Domain-specific feature creation\n",
" - Entropy calculations, risk scores, behavioral features\n",
" - Optimized for real-time inference\n",
"\n",
"3. **π€ Model Training**\n",
" - Random Forest with class balancing\n",
" - XGBoost with regularization\n",
" - Deep Neural Networks with residual connections\n",
" - Weighted ensemble for maximum accuracy\n",
"\n",
"4. **π Production Deployment**\n",
" - Unified inference API\n",
" - Multi-domain threat detection\n",
" - Real-time risk assessment\n",
"\n",
"### π― Integration with Agentic AI:\n",
"\n",
"The trained models are ready to be integrated with:\n",
"- `observation_loop.py` - For real-time browser monitoring\n",
"- `action_executor.py` - For automated threat response\n",
"- `intelligence_feed.py` - For AI-explained security events\n",
"- `scan_modes.py` - For adaptive scanning with ML enhancement\n",
"\n",
"### π Output Files:\n",
"```\n",
"models/agentic_security/\n",
"βββ phishing/\n",
"β βββ random_forest.pkl\n",
"β βββ xgboost.pkl\n",
"β βββ deep_neural_network.keras\n",
"β βββ scaler.pkl\n",
"β βββ ensemble_config.pkl\n",
"βββ malware/\n",
"βββ intrusion/\n",
"βββ web_attack/\n",
"βββ training_metrics.json\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cc806c09",
"metadata": {},
"outputs": [],
"source": [
"print('π Agentic AI Security Training Complete!')\n",
"print('\\nπ Final Summary:')\n",
"print(f' Domains trained: {len(trainer.metrics)}')\n",
"print(f' Total models: {len(trainer.metrics) * 4}') # 4 models per domain\n",
"print(f' Models directory: {trainer.models_dir}')\n",
"\n",
"# Best performing models\n",
"print('\\nπ Best Performing Models (by AUC):')\n",
"for domain, models in trainer.metrics.items():\n",
" best_model = max(models.items(), key=lambda x: x[1]['auc'])\n",
" print(f' {domain}: {best_model[0]} (AUC: {best_model[1][\"auc\"]:.4f})')"
]
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|