-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathattack.py
More file actions
156 lines (128 loc) Β· 5.26 KB
/
attack.py
File metadata and controls
156 lines (128 loc) Β· 5.26 KB
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
#!/usr/bin/env python3
"""
BotCarbon β DDoS Attack Simulator
==================================
Simulates malicious bot traffic against a target server for demo purposes.
Shows the carbon impact of unmitigated cyber attacks.
Usage:
python attack.py # Default: localhost:3000
python attack.py --target URL # Custom target
python attack.py --intensity high # Low / Medium / High intensity
"""
import argparse
import asyncio
import random
import time
import sys
from datetime import datetime
# ANSI colors for terminal output
RED = "\033[91m"
GREEN = "\033[92m"
YELLOW = "\033[93m"
CYAN = "\033[96m"
DIM = "\033[2m"
BOLD = "\033[1m"
RESET = "\033[0m"
# Attack config
ATTACK_TYPES = [
("DDoS Flood", "GET", "/"),
("SQL Injection", "POST", "/api/login"),
("XSS Probe", "GET", "/search?q=<script>alert(1)</script>"),
("Brute Force", "POST", "/api/login"),
("Bot Scraping", "GET", "/api/users"),
("Credential Stuffing", "POST", "/api/auth"),
("API Abuse", "GET", "/api/v1/tokens"),
("Path Traversal", "GET", "/../../../etc/passwd"),
]
INTENSITY = {
"low": {"rps": 50, "concurrent": 5, "label": "Reconnaissance"},
"medium": {"rps": 200, "concurrent": 20, "label": "Sustained Attack"},
"high": {"rps": 800, "concurrent": 50, "label": "Full DDoS Flood"},
}
# Carbon constants (same as frontend)
KWH_PER_REQUEST = 0.0003
CO2_PER_KWH = 0.4 # kg
def fake_ip():
return f"{random.randint(1, 223)}.{random.randint(0, 255)}.{random.randint(0, 255)}.{random.randint(0, 255)}"
def print_banner():
print(f"""
{RED}{BOLD}ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β π΄ BotCarbon Attack Simulator π΄ β
β For demonstration purposes only. β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ{RESET}
""")
def print_stats(total, elapsed, intensity_label):
kwh_wasted = total * KWH_PER_REQUEST
co2_kg = kwh_wasted * CO2_PER_KWH
rps = total / max(elapsed, 0.001)
sys.stdout.write(f"\r{BOLD}[{YELLOW}{intensity_label}{RESET}{BOLD}]{RESET} "
f"Requests: {RED}{total:,}{RESET} | "
f"RPS: {RED}{rps:.0f}{RESET} | "
f"kWh wasted: {RED}{kwh_wasted:.2f}{RESET} | "
f"COβ: {RED}{co2_kg * 1000:.1f}g{RESET} | "
f"Elapsed: {DIM}{elapsed:.1f}s{RESET} ")
sys.stdout.flush()
async def simulate_attack(target, intensity_key):
config = INTENSITY[intensity_key]
print_banner()
print(f"{CYAN}Target:{RESET} {target}")
print(f"{CYAN}Intensity:{RESET} {config['label']} ({config['rps']} req/s)")
print(f"{CYAN}Started:{RESET} {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
print(f"{DIM}{'β' * 56}{RESET}")
print()
total_requests = 0
start_time = time.time()
try:
while True:
batch_size = random.randint(
config["rps"] // 4,
config["rps"]
)
for _ in range(batch_size):
attack = random.choice(ATTACK_TYPES)
ip = fake_ip()
total_requests += 1
# Print individual attacks occasionally
if random.random() < 0.02:
print(f"\n {RED}βΈ{RESET} {DIM}{ip:>15}{RESET} "
f"{YELLOW}{attack[1]:>4}{RESET} "
f"{attack[2][:40]:<40} "
f"{RED}{attack[0]}{RESET}")
elapsed = time.time() - start_time
print_stats(total_requests, elapsed, config["label"])
await asyncio.sleep(1)
except KeyboardInterrupt:
elapsed = time.time() - start_time
kwh_total = total_requests * KWH_PER_REQUEST
co2_total = kwh_total * CO2_PER_KWH
print(f"\n\n{DIM}{'β' * 56}{RESET}")
print(f"\n{BOLD}π Attack Summary{RESET}")
print(f" Total requests: {RED}{total_requests:,}{RESET}")
print(f" Duration: {elapsed:.1f}s")
print(f" Avg RPS: {total_requests / max(elapsed, 1):.0f}")
print(f" Energy wasted: {RED}{kwh_total:.2f} kWh{RESET}")
print(f" COβ emitted: {RED}{co2_total * 1000:.1f}g ({co2_total:.4f} kg){RESET}")
print()
print(f" {GREEN}π‘ If routed through Cloudflare Edge Shield:{RESET}")
print(f" {GREEN}~{kwh_total * 0.95:.2f} kWh saved{RESET}")
print(f" {GREEN}~{co2_total * 0.95 * 1000:.1f}g COβ prevented{RESET}")
print()
def main():
parser = argparse.ArgumentParser(
description="BotCarbon DDoS Attack Simulator (Demo Only)"
)
parser.add_argument(
"--target",
default="http://localhost:3000",
help="Target URL (default: http://localhost:3000)"
)
parser.add_argument(
"--intensity",
choices=["low", "medium", "high"],
default="high",
help="Attack intensity (default: high)"
)
args = parser.parse_args()
asyncio.run(simulate_attack(args.target, args.intensity))
if __name__ == "__main__":
main()