Project 3
#include <iostream>
#include <fstream>
#include <ctime>
#include <chrono>
#include <thread>
#include <string>
#include <sstream>
using namespace std;
using namespace std::chrono;
const string logFileName = "can_log.txt";
// Function to log CAN messages
void logCanMessages() {
ofstream logFile(logFileName, ios::app);
// Simulated CAN message logging (replace with actual logging code)
for (int i = 0; i < 10; ++i) {
logFile << "CAN Message " << i << endl;
this_thread::sleep_for(milliseconds(100)); // Simulate CAN message frequency
}
logFile.close();
}
// Function to perform Full Bus DoS attack
void fullBusDosAttack(int duration) {
cout << "Executing Full Bus DoS attack for " << duration << " seconds..." << endl;
this_thread::sleep_for(seconds(duration));
cout << "Full Bus DoS attack finished." << endl;
}
// Function to perform Partial DoS attack
void partialDosAttack(int priority, int duration) {
cout << "Executing Partial DoS attack at priority " << priority << " for " << duration << " seconds..." << endl;
this_thread::sleep_for(seconds(duration));
cout << "Partial DoS attack finished." << endl;
}
// Function to perform Message Replay attack
void messageReplayAttack(int bufferSize, int fuseCapture, int fuseReplay) {
cout << "Capturing CAN messages for " << bufferSize << " seconds..." << endl;
this_thread::sleep_for(seconds(bufferSize));
// Simulated message capture (replace with actual capture code)
ifstream logFile(logFileName);
string line;
while (getline(logFile, line)) {
cout << "Captured CAN message: " << line << endl;
}
logFile.close();
cout << "Waiting for " << fuseReplay << " seconds before replaying captured messages..." << endl;
this_thread::sleep_for(seconds(fuseReplay));
// Simulated message replay (replace with actual replay code)
cout << "Replaying captured messages..." << endl;
cout << "Message Replay attack finished." << endl;
}
// Function to create directory with descriptive name
string createAttackDirectory(string attackType) {
auto now = system_clock::now();
auto time = system_clock::to_time_t(now);
stringstream ss;
ss << attackType << " attack - " << put_time(localtime(&time), "%m-%d-%Y at %H-%M-%S");
string directoryName = ss.str();
cout << "Creating directory: " << directoryName << endl;
mkdir(directoryName.c_str(), 0777);
return directoryName;
}
int main() {
int choice;
int duration;
int priority;
int bufferSize;
int fuseCapture;
int fuseReplay;
// Prompt user to select attack type
cout << "Select attack type:" << endl;
cout << "1. Full Bus DoS" << endl;
cout << "2. Partial DoS" << endl;
cout << "3. Message Replay" << endl;
cout << "Enter choice: ";
cin >> choice;
switch (choice) {
case 1:
// Prompt user for attack duration
cout << "Enter attack duration (seconds): ";
cin >> duration;
// Perform Full Bus DoS attack
fullBusDosAttack(duration);
break;
case 2:
// Prompt user for attack priority and duration
cout << "Enter priority for Partial DoS attack: ";
cin >> priority;
cout << "Enter attack duration (seconds): ";
cin >> duration;
// Perform Partial DoS attack
partialDosAttack(priority, duration);
break;
case 3:
// Prompt user for buffer size, fuse capture, and fuse replay
cout << "Enter buffer size (seconds): ";
cin >> bufferSize;
cout << "Enter fuse for capture (seconds): ";
cin >> fuseCapture;
cout << "Enter fuse for replay (seconds): ";
cin >> fuseReplay;
// Perform Message Replay attack
messageReplayAttack(bufferSize, fuseCapture, fuseReplay);
break;
default:
cout << "Invalid choice." << endl;
return 1;
}
// Log CAN messages from the start of the program until it finishes
logCanMessages();
return 0;
}