Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pandas as pd
- import sys
- import os
- # Define the attack classification mapping
- attack_mapping = {
- # DDoS Attacks
- 'DDoS-ICMP_Flood': 'DDoS Attacks',
- 'DDoS-UDP_Flood': 'DDoS Attacks',
- 'DDoS-TCP_Flood': 'DDoS Attacks',
- 'DDoS-RSTFINFlood': 'DDoS Attacks',
- 'DDoS-PSHACK_Flood': 'DDoS Attacks',
- 'DDoS-SYN_Flood': 'DDoS Attacks',
- 'DDoS-SynonymousIP_Flood': 'DDoS Attacks',
- 'DDoS-ICMP_Fragmentation': 'DDoS Attacks',
- 'DDoS-UDP_Fragmentation': 'DDoS Attacks',
- 'DDoS-ACK_Fragmentation': 'DDoS Attacks',
- 'DDoS-HTTP_Flood': 'DDoS Attacks',
- 'DDoS-SlowLoris': 'DDoS Attacks',
- # DoS Attacks
- 'DoS-UDP_Flood': 'DoS Attacks',
- 'DoS-TCP_Flood': 'DoS Attacks',
- 'DoS-SYN_Flood': 'DoS Attacks',
- 'DoS-HTTP_Flood': 'DoS Attacks',
- # Mirai Botnet Attacks
- 'Mirai-greeth_flood': 'Mirai Botnet Attacks',
- 'Mirai-udpplain': 'Mirai Botnet Attacks',
- 'Mirai-greip_flood': 'Mirai Botnet Attacks',
- # Reconnaissance
- 'Recon-HostDiscovery': 'Reconnaissance',
- 'Recon-OSScan': 'Reconnaissance',
- 'Recon-PortScan': 'Reconnaissance',
- 'Recon-PingSweep': 'Reconnaissance',
- # Web Application Attacks
- 'XSS': 'Web Application Attacks',
- 'SqlInjection': 'Web Application Attacks',
- 'CommandInjection': 'Web Application Attacks',
- 'DictionaryBruteForce': 'Web Application Attacks',
- 'Uploading_Attack': 'Web Application Attacks',
- # Man-in-the-Middle Attacks
- 'MITM-ArpSpoofing': 'Man-in-the-Middle Attacks',
- 'DNS_Spoofing': 'Man-in-the-Middle Attacks',
- # Malware
- 'BrowserHijacking': 'Malware',
- 'Backdoor_Malware': 'Malware',
- # Vulnerability Assessment
- 'VulnerabilityScan': 'Vulnerability Assessment',
- # Benign Traffic
- 'BenignTraffic': 'Benign Traffic'
- }
- def reclassify_attacks(csv_path):
- try:
- # Read CSV file
- df = pd.read_csv(csv_path)
- # Get the name of the last column
- last_column = df.columns[-1]
- # Replace values in the last column using the mapping
- df[last_column] = df[last_column].map(attack_mapping)
- # Create output filename
- base_filename = os.path.splitext(csv_path)[0]
- output_filename = f"{base_filename}_reclassified.csv"
- # Save the modified dataset
- df.to_csv(output_filename, index=False)
- print(f"Successfully reclassified attacks. Saved to: {output_filename}")
- # Print summary of new classifications
- print("\nSummary of reclassified attacks:")
- print(df[last_column].value_counts())
- except Exception as e:
- print(f"Error processing file: {e}")
- sys.exit(1)
- def main():
- if len(sys.argv) != 2:
- print("Usage: python3 script.py <path_to_csv_file>")
- sys.exit(1)
- csv_path = sys.argv[1]
- if not os.path.exists(csv_path):
- print(f"Error: File {csv_path} does not exist")
- sys.exit(1)
- reclassify_attacks(csv_path)
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement