forked from PierreGode/Ragnar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathattack_logger.py
More file actions
224 lines (192 loc) · 7.04 KB
/
attack_logger.py
File metadata and controls
224 lines (192 loc) · 7.04 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
"""
attack_logger.py - Utility for logging attack outputs to the /api/attack endpoint
This module provides a simple interface for attack actions to log their outputs
to a centralized attack log system accessible via the web API.
"""
import json
import requests
from datetime import datetime
from logger import Logger
import logging
# Configure logger
logger = Logger(name="attack_logger.py", level=logging.DEBUG)
class AttackLogger:
"""
Utility class for logging attack outputs to the centralized attack log system.
"""
def __init__(self, api_base_url="http://localhost:8000"):
"""
Initialize the attack logger.
Args:
api_base_url: Base URL of the Flask API (default: http://localhost:8000)
"""
self.api_base_url = api_base_url.rstrip('/')
self.endpoint = f"{self.api_base_url}/api/attack"
def log_attack(self, attack_type, target_ip, target_port='', status='unknown',
message='', details=None):
"""
Log an attack output to the centralized system.
Args:
attack_type: Type of attack (e.g., 'SSHBruteforce', 'FTPBruteforce', 'SQLConnector')
target_ip: IP address of the target
target_port: Port number (optional)
status: Attack status - 'success', 'failed', or 'timeout'
message: Human-readable message describing the attack result
details: Dictionary containing additional attack details (credentials, files, etc.)
Returns:
bool: True if logged successfully, False otherwise
"""
try:
# Prepare log data
log_data = {
'attack_type': attack_type,
'target_ip': target_ip,
'target_port': str(target_port) if target_port else '',
'status': status,
'message': message,
'details': details if details else {},
'timestamp': datetime.now().strftime('%Y-%m-%d %H:%M:%S')
}
# Send POST request to API endpoint
response = requests.post(
self.endpoint,
json=log_data,
timeout=5
)
if response.status_code == 201:
logger.debug(f"Attack logged successfully: {attack_type} on {target_ip}")
return True
else:
logger.warning(f"Failed to log attack: HTTP {response.status_code}")
return False
except requests.exceptions.Timeout:
logger.warning(f"Timeout while logging attack for {target_ip}")
return False
except requests.exceptions.ConnectionError:
logger.warning(f"Connection error while logging attack - API may not be running")
return False
except Exception as e:
logger.error(f"Error logging attack: {e}")
return False
def log_success(self, attack_type, target_ip, target_port='', message='', **details):
"""
Log a successful attack.
Args:
attack_type: Type of attack
target_ip: Target IP address
target_port: Target port (optional)
message: Success message
**details: Additional details as keyword arguments
"""
return self.log_attack(
attack_type=attack_type,
target_ip=target_ip,
target_port=target_port,
status='success',
message=message,
details=details
)
def log_failure(self, attack_type, target_ip, target_port='', message='', **details):
"""
Log a failed attack.
Args:
attack_type: Type of attack
target_ip: Target IP address
target_port: Target port (optional)
message: Failure message
**details: Additional details as keyword arguments
"""
return self.log_attack(
attack_type=attack_type,
target_ip=target_ip,
target_port=target_port,
status='failed',
message=message,
details=details
)
def log_timeout(self, attack_type, target_ip, target_port='', message='', **details):
"""
Log an attack timeout.
Args:
attack_type: Type of attack
target_ip: Target IP address
target_port: Target port (optional)
message: Timeout message
**details: Additional details as keyword arguments
"""
return self.log_attack(
attack_type=attack_type,
target_ip=target_ip,
target_port=target_port,
status='timeout',
message=message,
details=details
)
def get_attack_logs(self, ip=None, attack_type=None, status=None, limit=100, days=7):
"""
Retrieve attack logs with optional filtering.
Args:
ip: Filter by target IP (optional)
attack_type: Filter by attack type (optional)
status: Filter by status (optional)
limit: Maximum number of logs to retrieve
days: Number of days to look back
Returns:
dict: Response containing attack logs and statistics
"""
try:
params = {
'limit': limit,
'days': days
}
if ip:
params['ip'] = ip
if attack_type:
params['type'] = attack_type
if status:
params['status'] = status
response = requests.get(
self.endpoint,
params=params,
timeout=10
)
if response.status_code == 200:
return response.json()
else:
logger.warning(f"Failed to retrieve attack logs: HTTP {response.status_code}")
return None
except Exception as e:
logger.error(f"Error retrieving attack logs: {e}")
return None
# Example usage in attack actions:
"""
# At the top of your attack action file (e.g., ssh_connector.py):
from attack_logger import AttackLogger
# In your class __init__:
self.attack_logger = AttackLogger()
# When an attack succeeds:
self.attack_logger.log_success(
attack_type='SSHBruteforce',
target_ip=ip,
target_port=port,
message=f'Successfully authenticated as {username}',
username=username,
password=password,
attempts=attempt_count
)
# When an attack fails:
self.attack_logger.log_failure(
attack_type='SSHBruteforce',
target_ip=ip,
target_port=port,
message='All credential combinations failed',
attempts_made=total_attempts
)
# When an attack times out:
self.attack_logger.log_timeout(
attack_type='SSHBruteforce',
target_ip=ip,
target_port=port,
message='Connection timed out after 300 seconds'
)
"""