-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinjector.py
61 lines (50 loc) · 1.73 KB
/
injector.py
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
import time
import random
import subprocess
import requests as r
from utils import save_logs
SLEEP_TIME = 1
PROBABILITY_INJECTION = 10 # 10% of probabilities to inject NUMBER_OF_PORTS anomalies each SLEEP_TIME seconds
NUMBER_OF_PORTS = 30
INJECTION_TIME_SLEEP = 5
INJECT_OPTIONS = [
"-sT",
"-sS",
"-sF",
"-sN",
"-sX"
]
def should_inject() -> bool:
"""
Determines if an injection should happen based on PROBABILITY_INJECTION.
"""
random_number = random.uniform(0, 100)
return random_number < PROBABILITY_INJECTION
def choose_random_port():
return random.randint(1, 65_535)
def run_nmap(IP_ADDRESS, OPTION):
subprocess.call(["nmap", IP_ADDRESS , "-p", f'0-{NUMBER_OF_PORTS}', OPTION], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell=False)
def run_injector(IP_ADDRESS='127.0.0.1'):
while True:
injected = False
message = ""
if should_inject():
option = random.choices(INJECT_OPTIONS)[0]
message = f"[INJECTOR] Running NMAP on first {NUMBER_OF_PORTS} ports -> Injector will sleep for {INJECTION_TIME_SLEEP} with attack mode {option}"
injected = True
run_nmap(IP_ADDRESS, option)
else:
# normal traffic
PORT = choose_random_port()
url = f"http://{IP_ADDRESS}:{PORT}/"
message = f"[INJECTOR] Normal traffic request on {url}"
try:
r.get(url, timeout=1)
except Exception as ex:
pass # ignore if port is closed
print(message)
save_logs((message + "\n",))
if injected:
time.sleep(INJECTION_TIME_SLEEP)
time.sleep(SLEEP_TIME)
continue