-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy path_session.py
More file actions
250 lines (203 loc) · 10.6 KB
/
_session.py
File metadata and controls
250 lines (203 loc) · 10.6 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import json
import hashlib
import time
from datetime import datetime, timedelta
from pathlib import Path
from typing import Dict, List, Optional, Tuple
from _log import Log
class SessionManager:
"""
Manages persistent sessions for MikrotikAPI-BF v2.1.
Similar to John The Ripper's session management.
"""
def __init__(self, sessions_dir: str = "sessions"):
self.sessions_dir = Path(sessions_dir)
self.sessions_dir.mkdir(parents=True, exist_ok=True)
self.log = Log(verbose=True, verbose_all=False)
def _generate_session_id(self, target: str, services: List[str], wordlist_hash: str) -> str:
"""Generate a unique session ID based on target, services, and wordlist."""
session_data = f"{target}:{':'.join(sorted(services))}:{wordlist_hash}"
return hashlib.md5(session_data.encode()).hexdigest()[:12]
def _get_wordlist_hash(self, wordlist: List[Tuple[str, str]]) -> str:
"""Generate hash of wordlist for session identification."""
wordlist_str = "|".join([f"{u}:{p}" for u, p in wordlist])
return hashlib.md5(wordlist_str.encode()).hexdigest()[:8]
def create_session(self, target: str, services: List[str], wordlist: List[Tuple[str, str]],
config: Dict) -> str:
"""Create a new session."""
wordlist_hash = self._get_wordlist_hash(wordlist)
session_id = self._generate_session_id(target, services, wordlist_hash)
session_data = {
'session_id': session_id,
'target': target,
'services': services,
'wordlist_hash': wordlist_hash,
'total_combinations': len(wordlist),
'tested_combinations': 0,
'successful_credentials': [],
'failed_combinations': [],
'current_progress': 0.0,
'start_time': datetime.now().isoformat(),
'last_update': datetime.now().isoformat(),
'config': config,
'status': 'running',
'estimated_completion': None,
'average_time_per_attempt': None
}
session_file = self.sessions_dir / f"{session_id}.json"
with open(session_file, 'w', encoding='utf-8') as f:
json.dump(session_data, f, indent=2, ensure_ascii=False)
self.log.info(f"[SESSION] Created session: {session_id}")
return session_id
def load_session(self, session_id: str) -> Optional[Dict]:
"""Load an existing session."""
session_file = self.sessions_dir / f"{session_id}.json"
if not session_file.exists():
return None
try:
with open(session_file, 'r', encoding='utf-8') as f:
return json.load(f)
except Exception as e:
self.log.error(f"[SESSION] Failed to load session {session_id}: {e}")
return None
def find_existing_session(self, target: str, services: List[str],
wordlist: List[Tuple[str, str]]) -> Optional[Dict]:
"""Find existing session for the same target, services, and wordlist."""
wordlist_hash = self._get_wordlist_hash(wordlist)
for session_file in self.sessions_dir.glob("*.json"):
try:
with open(session_file, 'r', encoding='utf-8') as f:
session_data = json.load(f)
if (session_data.get('target') == target and
set(session_data.get('services', [])) == set(services) and
session_data.get('wordlist_hash') == wordlist_hash):
return session_data
except Exception:
continue
return None
def update_session(self, session_id: str, tested_count: int, successful_creds: List[Dict],
failed_combinations: List[Tuple[str, str]], current_combination: Tuple[str, str] = None):
"""Update session progress."""
session_file = self.sessions_dir / f"{session_id}.json"
if not session_file.exists():
return
try:
with open(session_file, 'r', encoding='utf-8') as f:
session_data = json.load(f)
# Update progress
session_data['tested_combinations'] = tested_count
session_data['successful_credentials'] = successful_creds
session_data['failed_combinations'] = failed_combinations
session_data['current_progress'] = (tested_count / session_data['total_combinations']) * 100
session_data['last_update'] = datetime.now().isoformat()
# Calculate average time per attempt
if tested_count > 0:
start_time = datetime.fromisoformat(session_data['start_time'])
elapsed_time = (datetime.now() - start_time).total_seconds()
session_data['average_time_per_attempt'] = elapsed_time / tested_count
# Estimate completion time
remaining_attempts = session_data['total_combinations'] - tested_count
if session_data['average_time_per_attempt']:
estimated_remaining = remaining_attempts * session_data['average_time_per_attempt']
session_data['estimated_completion'] = (datetime.now() + timedelta(seconds=estimated_remaining)).isoformat()
# Update current combination being tested
if current_combination:
session_data['current_combination'] = f"{current_combination[0]}:{current_combination[1]}"
with open(session_file, 'w', encoding='utf-8') as f:
json.dump(session_data, f, indent=2, ensure_ascii=False)
except Exception as e:
self.log.error(f"[SESSION] Failed to update session {session_id}: {e}")
def complete_session(self, session_id: str, successful_creds: List[Dict], final_status: str = "completed"):
"""Mark session as completed."""
session_file = self.sessions_dir / f"{session_id}.json"
if not session_file.exists():
return
try:
with open(session_file, 'r', encoding='utf-8') as f:
session_data = json.load(f)
session_data['status'] = final_status
session_data['successful_credentials'] = successful_creds
session_data['end_time'] = datetime.now().isoformat()
session_data['last_update'] = datetime.now().isoformat()
with open(session_file, 'w', encoding='utf-8') as f:
json.dump(session_data, f, indent=2, ensure_ascii=False)
self.log.info(f"[SESSION] Completed session: {session_id}")
except Exception as e:
self.log.error(f"[SESSION] Failed to complete session {session_id}: {e}")
def get_session_stats(self, session_id: str) -> Optional[Dict]:
"""Get session statistics."""
session_data = self.load_session(session_id)
if not session_data:
return None
stats = {
'session_id': session_id,
'target': session_data.get('target'),
'status': session_data.get('status'),
'progress': session_data.get('current_progress', 0.0),
'tested': session_data.get('tested_combinations', 0),
'total': session_data.get('total_combinations', 0),
'successful': len(session_data.get('successful_credentials', [])),
'average_time': session_data.get('average_time_per_attempt'),
'estimated_completion': session_data.get('estimated_completion'),
'start_time': session_data.get('start_time'),
'last_update': session_data.get('last_update')
}
return stats
def format_time_estimate(self, session_data: Dict) -> str:
"""Format time estimate for display."""
if not session_data.get('estimated_completion'):
return "Calculating..."
try:
estimated_time = datetime.fromisoformat(session_data['estimated_completion'])
remaining = estimated_time - datetime.now()
if remaining.total_seconds() < 0:
return "Overdue"
hours, remainder = divmod(int(remaining.total_seconds()), 3600)
minutes, seconds = divmod(remainder, 60)
if hours > 0:
return f"{hours}h {minutes}m {seconds}s"
elif minutes > 0:
return f"{minutes}m {seconds}s"
else:
return f"{seconds}s"
except Exception:
return "Unknown"
def should_resume(self, session_data: Dict) -> bool:
"""Determine if session should be resumed."""
if session_data.get('status') == 'completed':
return False
# Resume if less than 100% complete and not too old (24 hours)
if session_data.get('current_progress', 0) < 100:
last_update = datetime.fromisoformat(session_data.get('last_update', session_data.get('start_time')))
if (datetime.now() - last_update).total_seconds() < 86400: # 24 hours
return True
return False
def cleanup_old_sessions(self, days: int = 7):
"""Clean up sessions older than specified days."""
cutoff_time = datetime.now() - timedelta(days=days)
cleaned = 0
for session_file in self.sessions_dir.glob("*.json"):
try:
with open(session_file, 'r', encoding='utf-8') as f:
session_data = json.load(f)
last_update = datetime.fromisoformat(session_data.get('last_update', session_data.get('start_time')))
if last_update < cutoff_time:
session_file.unlink()
cleaned += 1
except Exception:
continue
if cleaned > 0:
self.log.info(f"[SESSION] Cleaned up {cleaned} old sessions")
def list_sessions(self) -> List[Dict]:
"""List all available sessions."""
sessions = []
for session_file in self.sessions_dir.glob("*.json"):
try:
with open(session_file, 'r', encoding='utf-8') as f:
session_data = json.load(f)
sessions.append(session_data)
except Exception:
continue
return sorted(sessions, key=lambda x: x.get('last_update', x.get('start_time', '')), reverse=True)