File size: 42,405 Bytes
2fb680d |
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 |
import json
import os
from typing import List, Dict, Any, Optional, Generator
from dataclasses import dataclass
from enum import Enum
import hashlib
import logging
from datetime import datetime
import chromadb
from chromadb.config import Settings
from sentence_transformers import SentenceTransformer
from huggingface_hub import hf_hub_download
# Import the base LLM handler
from llm_handler import CybersecurityLLM
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# ================================================
# Security Knowledge Definitions
# ================================================
class SecurityTopic(Enum):
PHISHING = "phishing"
PASSWORDS = "passwords"
MALWARE = "malware"
SOCIAL_ENGINEERING = "social_engineering"
DATA_PROTECTION = "data_protection"
NETWORK_SECURITY = "network_security"
INCIDENT_RESPONSE = "incident_response"
PHYSICAL_SECURITY = "physical_security"
MOBILE_SECURITY = "mobile_security"
CLOUD_SECURITY = "cloud_security"
COMPLIANCE = "compliance"
EMAIL_SECURITY = "email_security"
RANSOMWARE = "ransomware"
ZERO_TRUST = "zero_trust"
SUPPLY_CHAIN = "supply_chain"
@dataclass
class SecurityKnowledge:
topic: SecurityTopic
title: str
content: str
keywords: List[str]
severity: str # low, medium, high, critical
last_updated: str = ""
def to_dict(self) -> Dict[str, Any]:
return {
"topic": self.topic.value,
"title": self.title,
"content": self.content,
"keywords": json.dumps(self.keywords), # Serialize list to JSON string
"severity": self.severity,
"last_updated": self.last_updated or datetime.now().isoformat()
}
# ================================================
# Main Knowledge Base Class
# ================================================
class CybersecurityKnowledgeBase:
def __init__(self,
persist_directory: str = "./knowledge_db",
embedding_model: str = "all-MiniLM-L6-v2"):
"""
Initialize knowledge base with vector database
Args:
persist_directory: Directory to persist ChromaDB
embedding_model: Sentence transformer model for embeddings
"""
logger.info(f"Initializing knowledge base at {persist_directory}")
# Create directory if it doesn't exist
os.makedirs(persist_directory, exist_ok=True)
# Initialize ChromaDB with persistence
self.client = chromadb.PersistentClient(
path=persist_directory,
settings=Settings(
anonymized_telemetry=False,
allow_reset=True
)
)
# Create or get collection
try:
self.collection = self.client.get_collection("cybersecurity_knowledge")
logger.info(f"Loaded existing collection with {self.collection.count()} documents")
except:
self.collection = self.client.create_collection(
name="cybersecurity_knowledge",
metadata={"description": "Cybersecurity best practices and knowledge"}
)
logger.info("Created new knowledge collection")
# Initialize embedder
logger.info(f"Loading embedding model: {embedding_model}")
self.embedder = SentenceTransformer(embedding_model)
# Load core knowledge if collection is empty
if self.collection.count() == 0:
logger.info("Loading core cybersecurity knowledge...")
self._load_core_knowledge()
# Track statistics
self.stats = {
"total_documents": self.collection.count(),
"queries_processed": 0,
"last_updated": datetime.now().isoformat()
}
def _load_core_knowledge(self):
"""Load comprehensive cybersecurity knowledge"""
knowledge_items = [
# Phishing and Email Security
SecurityKnowledge(
topic=SecurityTopic.PHISHING,
title="Comprehensive Phishing Detection Guide",
content="""
IDENTIFYING PHISHING EMAILS - Complete Guide:
Red Flags to Watch For:
• Generic greetings: "Dear Customer" instead of your actual name
• Urgency tactics: "Act now or your account will be closed!"
• Grammar/spelling errors: Professional companies proofread their emails
• Mismatched sender: Display name doesn't match email address
• Suspicious links: Hover to see if URL matches claimed sender
• Unexpected attachments: Especially .zip, .exe, .scr, .vbs files
• Requests for sensitive info: Legitimate companies don't ask for passwords via email
• Too good to be true: "You've won $1 million!"
• Emotional manipulation: Fear, greed, curiosity, sympathy
How to Verify Suspicious Emails:
1. Check sender's email address carefully (not just display name)
2. Hover over links WITHOUT clicking to preview destination
3. Look for HTTPS and correct domain in links
4. Contact company directly through official channels (not email links)
5. Check for personalization - legitimate emails often include account numbers
6. Verify with IT security team when in doubt
What to Do If You Receive Phishing:
1. Don't click links or download attachments
2. Don't reply or provide any information
3. Report to IT security immediately
4. Forward to anti-phishing team if available
5. Delete the email after reporting
6. Warn colleagues if it's widespread
If You Clicked a Phishing Link:
1. Disconnect from network immediately
2. Change passwords from a different device
3. Report to IT security IMMEDIATELY
4. Run antivirus scan
5. Monitor accounts for suspicious activity
6. Enable MFA on all accounts if not already done
""",
keywords=["phishing", "email", "scam", "suspicious", "link", "attachment", "spear phishing", "whaling",
"BEC"],
severity="critical"
),
# Password Security
SecurityKnowledge(
topic=SecurityTopic.PASSWORDS,
title="Password Security Best Practices",
content="""
CREATING STRONG PASSWORDS:
Requirements for Strong Passwords:
• Minimum 12-16 characters (longer is better)
• Mix of uppercase and lowercase letters
• Include numbers and special characters (!@#$%^&*)
• Avoid dictionary words and personal information
• Unique for every account - never reuse passwords
• Consider passphrases: 'Coffee@7Makes$Me!Happy2024'
• Avoid patterns: Password1, Password2, etc.
• Don't use keyboard patterns: qwerty, 123456
Password Management Best Practices:
• Use a reputable password manager (Bitwarden, 1Password, LastPass)
• Enable two-factor authentication (2FA) everywhere possible
• Use authenticator apps over SMS when possible
• Never share passwords via email, chat, or phone
• Change passwords immediately if breach suspected
• Don't write passwords on sticky notes
• Use different passwords for work and personal accounts
• Consider using hardware keys for critical accounts
Multi-Factor Authentication (MFA):
• Something you know (password)
• Something you have (phone, token)
• Something you are (biometric)
Password Manager Benefits:
• Generate random, unique passwords
• Securely store all passwords
• Auto-fill credentials safely
• Sync across devices
• Alert you to breaches
• Share passwords securely when needed
Common Password Mistakes:
• Using personal information (birthdate, pet names)
• Reusing passwords across sites
• Sharing passwords with others
• Using simple substitutions (P@ssw0rd)
• Not updating default passwords
• Ignoring breach notifications
""",
keywords=["password", "authentication", "2FA", "MFA", "login", "credentials", "passphrase",
"password manager"],
severity="critical"
),
# Malware Prevention
SecurityKnowledge(
topic=SecurityTopic.MALWARE,
title="Malware Prevention and Response",
content="""
MALWARE PREVENTION STRATEGIES:
Prevention Best Practices:
• Keep OS and all software updated with latest patches
• Use reputable antivirus with real-time protection
• Enable Windows Defender or equivalent
• Download software only from official sources
• Verify digital signatures on downloads
• Scan USB drives before opening files
• Disable macros in Office documents from unknown sources
• Use application sandboxing when possible
• Regular backups following 3-2-1 rule
• Keep UAC (User Account Control) enabled
Types of Malware:
• Viruses: Self-replicating, attaches to files
• Worms: Self-spreading through networks
• Trojans: Disguised as legitimate software
• Ransomware: Encrypts files for ransom
• Spyware: Steals information secretly
• Adware: Displays unwanted advertisements
• Rootkits: Hides presence from system
• Keyloggers: Records keystrokes
• Cryptominers: Uses resources to mine cryptocurrency
Warning Signs of Infection:
• Computer running unusually slow
• Frequent crashes or blue screens
• Programs starting automatically
• Browser homepage changed
• New toolbars or extensions
• Excessive pop-ups
• Files encrypted with ransom note
• Unusual network activity
• Disabled security software
• Missing or modified files
If Infected - Immediate Steps:
1. Disconnect from all networks (WiFi, Ethernet)
2. Enter Safe Mode if possible
3. Run full antivirus scan
4. Use additional malware removal tools (Malwarebytes)
5. Check for system restore points
6. Contact IT security team immediately
7. Change all passwords from clean device
8. Monitor financial accounts
9. Consider complete system reinstall for severe infections
""",
keywords=["malware", "virus", "ransomware", "trojan", "antivirus", "infection", "worm", "spyware"],
severity="critical"
),
# Social Engineering
SecurityKnowledge(
topic=SecurityTopic.SOCIAL_ENGINEERING,
title="Social Engineering Defense Strategies",
content="""
DEFENDING AGAINST SOCIAL ENGINEERING:
Common Social Engineering Tactics:
• Pretexting: Creating fake scenarios to steal information
• Baiting: Offering something enticing (USB drives, downloads)
• Quid pro quo: Offering service for information
• Tailgating: Following into secure areas
• Vishing: Voice phishing via phone
• Smishing: SMS/text message phishing
• Watering hole: Compromising frequently visited websites
• Dumpster diving: Searching trash for information
• Shoulder surfing: Looking over shoulder for passwords
Red Flags to Recognize:
• Unsolicited contact asking for information
• Urgency without verification
• Requests to bypass normal procedures
• Appeals to authority without proof
• Offers that seem too good to be true
• Requests for passwords or sensitive data
• Emotional manipulation (fear, greed, sympathy)
• Name dropping without context
• Resistance to verification
Defense Strategies:
• Always verify identity before sharing information
• Use callback numbers from official sources
• Be suspicious of unsolicited contacts
• Never give passwords over phone/email
• Question unusual requests, even from "colleagues"
• Report suspicious behavior immediately
• Trust but verify - confirm through separate channel
• Be aware of information you share publicly
• Secure physical documents and screens
• Educate family about work-related scams
Verification Techniques:
• Call back on known number
• Check employee directory
• Verify with manager
• Ask for employee ID
• Request email confirmation
• Check digital signatures
• Verify through IT security
""",
keywords=["social engineering", "pretexting", "vishing", "smishing", "manipulation", "tailgating",
"phishing"],
severity="high"
),
# Network Security
SecurityKnowledge(
topic=SecurityTopic.NETWORK_SECURITY,
title="Network and WiFi Security Guide",
content="""
NETWORK SECURITY BEST PRACTICES:
Home WiFi Security:
• Change default router admin credentials immediately
• Use WPA3 encryption (WPA2 minimum)
• Create strong WiFi password (20+ characters)
• Change default network name (SSID)
• Disable WPS (WiFi Protected Setup)
• Keep router firmware updated monthly
• Use guest network for visitors and IoT devices
• Disable remote management unless necessary
• Turn off SSID broadcast if practical
• Use MAC address filtering for added security
• Position router centrally to minimize external signal
• Regular reboot router (monthly)
Public WiFi Safety:
• Avoid accessing sensitive accounts
• Always use VPN for all connections
• Verify network name with venue staff
• Turn off automatic WiFi connection
• Forget network after use
• Never accept certificate warnings
• Disable file sharing
• Use cellular data for sensitive tasks
• Keep firewall enabled
• Use HTTPS websites only
VPN Best Practices:
• Use company-approved VPN only
• Connect before accessing any resources
• Keep VPN client updated
• Report connection issues immediately
• Don't use free/public VPN services
• Verify VPN is active before working
Network Hygiene:
• Regular network scans for unknown devices
• Monitor bandwidth usage
• Check for unauthorized access points
• Secure all network equipment physically
• Document network configuration
• Regular security audits
""",
keywords=["wifi", "network", "router", "VPN", "encryption", "WPA3", "public wifi", "wireless"],
severity="high"
),
# Incident Response
SecurityKnowledge(
topic=SecurityTopic.INCIDENT_RESPONSE,
title="Security Incident Response Procedures",
content="""
SECURITY INCIDENT RESPONSE GUIDE:
IMMEDIATE RESPONSE STEPS:
1. STOP - Don't try to fix it yourself
2. DISCONNECT - Unplug network cable or disable WiFi
3. DOCUMENT - Write down:
- What happened
- When it occurred
- What you were doing
- Error messages
- Unusual behavior observed
4. REPORT - Contact IT security immediately
5. PRESERVE - Don't delete anything, take screenshots
6. WAIT - For IT security instructions
Types of Incidents Requiring Immediate Reporting:
• Clicked suspicious link or attachment
• Entered credentials on suspicious site
• Lost device with company data
• Suspicious computer behavior
• Unauthorized access attempts
• Data breach or leak discovered
• Ransomware infection
• Physical security breach
• Stolen credentials
• Suspicious phone calls asking for info
Information to Provide:
• Your name and contact information
• Time and date of incident
• Affected systems/accounts
• Description of what happened
• Actions taken so far
• Any error messages (exact wording)
• Screenshots if possible
• Anyone else who might be affected
DO NOT:
• Try to fix it yourself
• Delete or modify evidence
• Inform unauthorized people
• Post about it on social media
• Continue using affected systems
• Pay ransoms
Contact Information:
IT Security Hotline: [Organization specific]
Email: security@[organization]
After hours: [Emergency contact]
""",
keywords=["incident", "breach", "response", "report", "emergency", "compromise", "security incident"],
severity="critical"
),
# Data Protection
SecurityKnowledge(
topic=SecurityTopic.DATA_PROTECTION,
title="Data Protection and Privacy Guide",
content="""
DATA PROTECTION BEST PRACTICES:
Data Classification:
• Public: Can be freely shared
• Internal: Within organization only
• Confidential: Specific authorized individuals
• Restricted: Highest sensitivity, strict controls
Handling Sensitive Data:
• Encrypt files before sharing externally
• Use approved file sharing platforms only
• Never use personal email for work data
• Implement clean desk policy
• Lock computer when stepping away (Win+L or Cmd+Ctrl+Q)
• Use privacy screens in public spaces
• Shred physical documents with sensitive info
• Secure disposal of electronic media
• Don't discuss sensitive info in public
• Be aware of smart speakers/devices
Encryption Best Practices:
• Use full disk encryption (BitLocker, FileVault)
• Encrypt removable media
• Use encrypted communication channels
• Encrypt email with sensitive data
• Password protect sensitive documents
• Use enterprise encryption tools
• Store encryption keys securely
Data Backup Practices:
• Follow 3-2-1 rule:
- 3 copies of important data
- 2 different storage media
- 1 offsite backup
• Test restore procedures regularly
• Encrypt backup drives
• Store backups securely
• Automate where possible
• Document what's backed up
• Verify backup integrity
Privacy Considerations:
• Minimize data collection
• Only share need-to-know basis
• Regular data audits
• Respect retention policies
• Secure data destruction
• GDPR/CCPA compliance
""",
keywords=["data", "encryption", "backup", "confidential", "sensitive", "GDPR", "privacy",
"classification"],
severity="high"
),
# Mobile Security
SecurityKnowledge(
topic=SecurityTopic.MOBILE_SECURITY,
title="Mobile Device Security Guidelines",
content="""
MOBILE DEVICE SECURITY:
Device Security Settings:
• Enable screen lock (PIN, password, biometric)
• Set auto-lock to 1-2 minutes
• Keep OS and apps updated automatically
• Download apps only from official stores
• Review app permissions carefully
• Enable remote wipe capability
• Use Find My Device features
• Encrypt device storage
• Disable Bluetooth when not needed
• Turn off WiFi auto-connect
• Disable Siri/Assistant on lock screen
BYOD (Bring Your Own Device) Security:
• Separate work and personal data
• Use MDM if required by company
• Install company security apps
• Follow company mobile policy
• Report lost/stolen immediately
• Don't jailbreak/root devices
• Use company VPN for work
• Regular security updates
Mobile Threats:
• Malicious apps
• Unsecured WiFi
• SMiShing (SMS phishing)
• Bluetooth attacks
• Physical theft
• Shoulder surfing
• Juice jacking (USB charging)
• SIM swapping
Safe Mobile Practices:
• Avoid public WiFi for sensitive tasks
• Use VPN when on public networks
• Don't click links in text messages
• Be cautious with QR codes
• Use official app stores only
• Keep personal info private
• Regular app permission audits
• Backup device regularly
• Use mobile antivirus
• Avoid charging at public USB ports
""",
keywords=["mobile", "smartphone", "tablet", "BYOD", "iOS", "Android", "app security", "MDM"],
severity="medium"
),
# Ransomware Specific
SecurityKnowledge(
topic=SecurityTopic.RANSOMWARE,
title="Ransomware Prevention and Response",
content="""
RANSOMWARE PROTECTION GUIDE:
Prevention Strategies:
• Regular automated backups (tested restores)
• Keep all software patched and updated
• Email filtering and sandboxing
• Disable macros by default
• User training on phishing
• Network segmentation
• Principle of least privilege
• Application whitelisting
• Endpoint detection and response (EDR)
If Ransomware Strikes:
1. Immediately disconnect from network
2. Power off if actively encrypting
3. Report to IT security immediately
4. Do NOT pay ransom
5. Preserve evidence for investigation
6. Check for decryption tools
7. Restore from clean backups
8. Rebuild affected systems
9. Investigate root cause
10. Implement lessons learned
Warning Signs:
• Files with strange extensions
• Cannot open documents
• Ransom notes in folders
• Slow computer performance
• Renamed files
• Wallpaper changed to ransom message
Recovery Planning:
• Maintain offline backups
• Test restore procedures
• Document critical systems
• Incident response plan
• Communication plan
• Legal/law enforcement contacts
""",
keywords=["ransomware", "encryption", "ransom", "backup", "recovery", "bitcoin", "crypto"],
severity="critical"
),
# Cloud Security
SecurityKnowledge(
topic=SecurityTopic.CLOUD_SECURITY,
title="Cloud Services Security",
content="""
CLOUD SECURITY BEST PRACTICES:
Account Security:
• Use strong, unique passwords
• Enable MFA on all cloud accounts
• Regular access reviews
• Monitor for unusual activity
• Use SSO where available
• Secure API keys and tokens
Data Protection in Cloud:
• Understand shared responsibility model
• Encrypt data at rest and in transit
• Use cloud provider encryption
• Control data residency
• Regular security audits
• Implement DLP policies
Safe Cloud Usage:
• Only use approved cloud services
• Read terms of service
• Understand data ownership
• Configure privacy settings
• Regular permission reviews
• Monitor shared links
• Set expiration on shares
• Audit access logs
Common Cloud Risks:
• Misconfigured storage buckets
• Excessive permissions
• Shadow IT
• Account takeover
• Data leakage
• Compliance violations
• Insider threats
• API vulnerabilities
""",
keywords=["cloud", "SaaS", "AWS", "Azure", "Google Cloud", "OneDrive", "Dropbox", "Office 365"],
severity="high"
)
]
# Add all knowledge items to vector database
batch_size = 10
for i in range(0, len(knowledge_items), batch_size):
batch = knowledge_items[i:i + batch_size]
embeddings = []
documents = []
metadatas = []
ids = []
for item in batch:
# Generate embedding
embedding = self.embedder.encode(item.content).tolist()
embeddings.append(embedding)
# Prepare document
documents.append(item.content)
# Prepare metadata
metadatas.append(item.to_dict())
# Generate unique ID
doc_id = hashlib.md5(
f"{item.topic.value}_{item.title}_{len(item.content)}".encode()
).hexdigest()
ids.append(doc_id)
# Add batch to collection
self.collection.add(
embeddings=embeddings,
documents=documents,
metadatas=metadatas,
ids=ids
)
logger.info(f"Added batch {i // batch_size + 1} of knowledge items")
logger.info(f"Successfully loaded {len(knowledge_items)} knowledge items")
def search(self,
query: str,
k: int = 3,
filter_topic: Optional[str] = None,
min_severity: Optional[str] = None) -> List[Dict[str, Any]]:
"""
Search for relevant security information
Args:
query: User's question
k: Number of results to return
filter_topic: Optional topic filter
min_severity: Minimum severity level filter
Returns:
List of relevant documents with metadata
"""
self.stats["queries_processed"] += 1
# Generate query embedding
query_embedding = self.embedder.encode(query).tolist()
# Build filter
where_filter = {}
if filter_topic:
where_filter["topic"] = filter_topic
if min_severity:
severity_levels = ["low", "medium", "high", "critical"]
min_index = severity_levels.index(min_severity)
valid_severities = severity_levels[min_index:]
where_filter["severity"] = {"$in": valid_severities}
# Search with or without filter
if where_filter:
results = self.collection.query(
query_embeddings=[query_embedding],
n_results=k,
where=where_filter
)
else:
results = self.collection.query(
query_embeddings=[query_embedding],
n_results=k
)
# Format results
formatted_results = []
if results['documents'] and results['documents'][0]:
for doc, metadata, distance in zip(
results['documents'][0],
results['metadatas'][0],
results['distances'][0]
):
formatted_results.append({
'content': doc,
'topic': metadata.get('topic', 'unknown'),
'title': metadata.get('title', 'Untitled'),
'severity': metadata.get('severity', 'medium'),
'keywords': json.loads(metadata.get('keywords', '[]')), # Deserialize JSON string back to list
'relevance_score': 1 - (distance / 2) # Convert distance to similarity
})
return formatted_results
def add_custom_knowledge(self,
content: str,
topic: str,
title: str,
keywords: List[str],
severity: str = "medium") -> bool:
"""
Add custom security knowledge to the database
Args:
content: Knowledge content
topic: Topic category
title: Title of the knowledge
keywords: Related keywords
severity: Severity level
Returns:
Success status
"""
try:
# Generate embedding
embedding = self.embedder.encode(content).tolist()
# Generate unique ID
doc_id = hashlib.md5(
f"{topic}_{title}_{len(content)}_{datetime.now().isoformat()}".encode()
).hexdigest()
# Add to collection
self.collection.add(
embeddings=[embedding],
documents=[content],
metadatas=[{
"topic": topic,
"title": title,
"keywords": json.dumps(keywords), # Serialize list to JSON string
"severity": severity,
"last_updated": datetime.now().isoformat(),
"custom": True
}],
ids=[doc_id]
)
self.stats["total_documents"] = self.collection.count()
logger.info(f"Added custom knowledge: {title}")
return True
except Exception as e:
logger.error(f"Failed to add custom knowledge: {e}")
return False
def get_statistics(self) -> Dict[str, Any]:
"""Get knowledge base statistics"""
self.stats["total_documents"] = self.collection.count()
self.stats["last_accessed"] = datetime.now().isoformat()
# Get topic distribution
all_metadata = self.collection.get()['metadatas']
topic_counts = {}
severity_counts = {}
for metadata in all_metadata:
topic = metadata.get('topic', 'unknown')
severity = metadata.get('severity', 'unknown')
topic_counts[topic] = topic_counts.get(topic, 0) + 1
severity_counts[severity] = severity_counts.get(severity, 0) + 1
self.stats["topic_distribution"] = topic_counts
self.stats["severity_distribution"] = severity_counts
return self.stats
def export_knowledge(self, output_file: str = "knowledge_export.json") -> bool:
"""Export all knowledge to JSON file"""
try:
all_data = self.collection.get()
export_data = {
"exported_at": datetime.now().isoformat(),
"total_documents": len(all_data['ids']),
"documents": []
}
for doc, metadata, doc_id in zip(
all_data['documents'],
all_data['metadatas'],
all_data['ids']
):
export_data["documents"].append({
"id": doc_id,
"content": doc,
"metadata": metadata
})
with open(output_file, 'w') as f:
json.dump(export_data, f, indent=2)
logger.info(f"Exported knowledge to {output_file}")
return True
except Exception as e:
logger.error(f"Failed to export knowledge: {e}")
return False
# ================================================
# RAG-Enhanced LLM Class
# ================================================
class RAGCybersecurityLLM(CybersecurityLLM):
def __init__(self,
repo_id: str = "daskalos-apps/phi4-cybersec-Q4_K_M",
filename: str = "phi4-mini-instruct-Q4_K_M.gguf",
local_dir: str = "./models",
knowledge_dir: str = "./knowledge_db",
force_download: bool = False):
"""
Initialize LLM with RAG capabilities
Args:
repo_id: Hugging Face repository ID
filename: Model filename
local_dir: Local cache directory
knowledge_dir: Knowledge base directory
force_download: Force model re-download
"""
# Initialize base LLM
super().__init__(repo_id, filename, local_dir, force_download)
# Initialize knowledge base
logger.info("Initializing RAG knowledge base...")
self.knowledge_base = CybersecurityKnowledgeBase(persist_directory=knowledge_dir)
# Enhanced system prompt for RAG
self.rag_prompt_template = """<|system|>
{system}
You have access to a comprehensive cybersecurity knowledge base. Use the provided context to give accurate, detailed answers. If the context doesn't contain relevant information, use your general knowledge but indicate when you're doing so.
<|end|>
<|user|>
Context from knowledge base:
{context}
User Question: {user}
<|end|>
<|assistant|>"""
def generate_with_rag(self,
prompt: str,
max_tokens: int = 512,
use_rag: bool = True,
k_documents: int = 3,
min_relevance: float = 0.5) -> Dict[str, Any]:
"""
Generate response with RAG enhancement
Args:
prompt: User's question
max_tokens: Maximum response length
use_rag: Whether to use RAG
k_documents: Number of documents to retrieve
min_relevance: Minimum relevance threshold
Returns:
Response with metadata and sources
"""
context = None
sources = []
if use_rag:
# Search knowledge base
logger.info(f"Searching knowledge base for: {prompt[:50]}...")
relevant_docs = self.knowledge_base.search(prompt, k=k_documents)
# Filter by relevance
relevant_docs = [
doc for doc in relevant_docs
if doc.get('relevance_score', 0) >= min_relevance
]
if relevant_docs:
# Build context from relevant documents
context_parts = []
for i, doc in enumerate(relevant_docs, 1):
context_parts.append(
f"[Source {i}: {doc['title']} - Severity: {doc['severity']}]\n"
f"{doc['content'][:1000]}..." # Limit context length
)
sources.append({
"title": doc['title'],
"topic": doc['topic'],
"severity": doc['severity'],
"relevance": doc['relevance_score']
})
context = "\n\n".join(context_parts)
logger.info(f"Found {len(relevant_docs)} relevant documents")
else:
logger.info("No highly relevant documents found")
# Generate response
if context and use_rag:
# Use RAG prompt template
full_prompt = self.rag_prompt_template.format(
system=self.system_prompt,
context=context,
user=prompt
)
else:
# Use standard prompt template
full_prompt = self.format_prompt(prompt)
try:
response = self.llm(
full_prompt,
max_tokens=max_tokens,
temperature=0.7,
top_p=0.95,
top_k=40,
repeat_penalty=1.1,
stop=self.stop_tokens,
echo=False
)
text = response['choices'][0]['text'].strip()
return {
"response": text,
"tokens_used": response['usage']['total_tokens'],
"model": self.model_info['repo_id'],
"sources": sources,
"rag_used": use_rag and bool(context)
}
except Exception as e:
logger.error(f"Generation error: {e}")
return {
"response": "I apologize, but I encountered an error. Please try rephrasing your question.",
"error": str(e),
"sources": [],
"rag_used": False
}
def generate_stream_with_rag(self,
prompt: str,
max_tokens: int = 512,
use_rag: bool = True,
k_documents: int = 3) -> Generator:
"""Stream response with RAG enhancement"""
# Get context if using RAG
context = None
if use_rag:
relevant_docs = self.knowledge_base.search(prompt, k=k_documents)
if relevant_docs:
context_parts = [f"{doc['title']}: {doc['content'][:500]}" for doc in relevant_docs]
context = "\n\n".join(context_parts)
# Generate prompt
if context:
full_prompt = self.rag_prompt_template.format(
system=self.system_prompt,
context=context,
user=prompt
)
else:
full_prompt = self.format_prompt(prompt)
# Stream response
stream = self.llm(
full_prompt,
max_tokens=max_tokens,
temperature=0.7,
top_p=0.95,
top_k=40,
repeat_penalty=1.1,
stop=self.stop_tokens,
echo=False,
stream=True
)
for output in stream:
token = output['choices'][0].get('text', '')
if token:
yield token
def add_knowledge(self, content: str, topic: str, title: str, keywords: List[str]) -> bool:
"""Add new knowledge to the RAG system"""
return self.knowledge_base.add_custom_knowledge(
content=content,
topic=topic,
title=title,
keywords=keywords
)
def get_knowledge_stats(self) -> Dict[str, Any]:
"""Get knowledge base statistics"""
return self.knowledge_base.get_statistics()
|