-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReadMsgs.c
More file actions
233 lines (186 loc) · 7.1 KB
/
Copy pathReadMsgs.c
File metadata and controls
233 lines (186 loc) · 7.1 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
/*
This source file is part of the AutoBlock project.
Copyright (C) 2026 Dennis Hawkins
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
If you use this code in any way, I would love to hear from you. My email
address is: dennis@galliform.com
*/
// This module handles reading the asterisk log file.
#include "AutoBlock.h"
/**
* Parses a single Asterisk log line to extract authentication failure metadata.
*
* @param line [Input] The raw log line text - must be null terminated.
* @param out_id [Output] Buffer to copy the extracted <sip:[id]@...> string. Must be >= 32 bytee.
* @param out_fail_ip [Output] Buffer to copy the extracted target "failed for" IP string. Must be >= 32 bytes.
* @return A bitmask tracking all components found. Returns 0 if "NOTICE" is missing.
*/
// out_id must be at least 32 chars.
DWORD parse_asterisk_log_line(char *line, char *out_id, char *out_fail_ip)
{
DWORD mask = 0;
const char *request_ptr;
const char *sip_start;
const char *host_start;
const char *at_sign;
const char *sip_end;
const char *failed_for_ptr;
const char *ip_start_quote;
const char *ip_end_quote;
const char *port_colon;
const char *ip_end_boundary;
size_t host_len;
size_t span_count;
size_t ip_len, id_len;
// Initialize outputs
if (!line || !out_id || !out_fail_ip) return(0); // parameters null
out_id[0] = '\0'; // Must be >= 32 bytes long.
out_fail_ip[0] = '\0'; // Must be >= 32 bytes long.
// Check for "NOTICE"
if (strstr(line, "NOTICE") == NULL)
{
return 0; // Return zero immediately if NOTICE is missing
}
mask |= BIT_NOTICE;
// Locate "Request" and verify method types
request_ptr = strstr(line, "Request");
if (!request_ptr) return mask; // These aren't the droid's we're looking for.
if (strstr(request_ptr, "'REGISTER'") != NULL)
{
mask |= BIT_REGISTER;
}
else if (strstr(request_ptr, "'INVITE'") != NULL)
{
mask |= BIT_INVITE;
}
else if (strstr(request_ptr, "'OPTIONS'") != NULL)
{
mask |= BIT_OPTIONS;
}
// Extract information from the "<sip:" wrapper
sip_start = strstr(request_ptr, "<sip:");
if (!sip_start) return mask;
// Jump past "<sip:"
sip_start += 5;
at_sign = strchr(sip_start, '@');
sip_end = strchr(sip_start, '>');
if (at_sign && sip_end && at_sign < sip_end)
{
// Copy the ID string safely
id_len = MIN(at_sign - sip_start, 31);
strncpy(out_id, sip_start, id_len);
out_id[id_len] = '\0';
// Check if ID is alpha or digits
if (strspn(out_id, "0123456789") == id_len)
mask |= BIT_ID_IS_NUM;
else if (id_len)
mask |= BIT_ID_IS_ALPHA;
// Determine if target host is an IP or Domain URL
host_start = at_sign + 1;
host_len = sip_end - host_start;
// Scan the host segment to see if it only contains dotted IPv4 characters
span_count = strspn(host_start, "0123456789.");
mask |= ((span_count == host_len) ? BIT_ADDR_IS_IP : BIT_ADDR_IS_NAME);
}
// Check for "failed for" and extract trailing attacking IP address
failed_for_ptr = strstr(sip_end ? sip_end : request_ptr, "failed for");
if (failed_for_ptr)
{
mask |= BIT_FAILED_FOR;
// Find the single-quoted string containing the attacker's IP:Port
ip_start_quote = strchr(failed_for_ptr, '\'');
if (ip_start_quote)
{
ip_start_quote++; // step past opening quote
ip_end_quote = strchr(ip_start_quote, '\'');
if (ip_end_quote)
{
// Determine length without taking the port (isolate up to the ':')
port_colon = strchr(ip_start_quote, ':');
ip_end_boundary = (port_colon && port_colon < ip_end_quote) ? port_colon : ip_end_quote;
ip_len = MIN(ip_end_boundary - ip_start_quote, 31);
strncpy(out_fail_ip, ip_start_quote, ip_len);
out_fail_ip[ip_len] = '\0';
}
}
}
// Check for terminal authentication signatures
if (strstr(line, "Failed to authenticate") != NULL)
{
mask |= BIT_FAILED_TO_AUTHENTICATE;
}
if (strstr(line, "No matching endpoint found") != NULL)
{
mask |= BIT_NO_MATCHING_ENDPOINT;
}
return mask;
}
// Read and validate a line.
// Return an IP address as a DWORD if IP is a hacker.
// Return zero if a hacker IP was not found.
DWORD ValidateHacker(char *line, DWORD *flags)
{
DWORD mask, ext;
char idBuf[64];
char ipBuf[64];
int isHacker = false;
IPTYPE ip;
idBuf[0] = ipBuf[0] = '\0';
mask = parse_asterisk_log_line(line, idBuf, ipBuf);
*flags = mask;
if (!mask) return(0); // Not a NOTICE
// Check if we process this request type
ext = mask & G_RequestTypes;
if (ext == 0) return(0); // We don't process this notice
// Is the asterisk server specified by domain name
if (mask & BIT_ADDR_IS_NAME && G_AllowNamedServer)
{
if (G_AllowNamedServer > 0)
G_AllowNamedServer--; // decrement until false
return(0); // they know us
}
if (!(mask & BIT_FAILED_TO_AUTHENTICATE) && !(mask & BIT_NO_MATCHING_ENDPOINT) )
return(0); // Unrecognized error
// Check to see if a numerical id is allowed
if (mask & BIT_ID_IS_NUM)
{
ext = atoi(idBuf);
isHacker = SearchID(ext); // Search G_AllowedNumericalIds
// isHacker is TRUE if numerical ID not allowed
}
if ((mask & BIT_ADDR_IS_IP) && (mask & BIT_FAILED_FOR))
isHacker = true;
if (isHacker && ipBuf[0]) // return the hacker's IP address
{
Str2Ip(ipBuf, &ip);
if (ip.bits == 32) return(ip.IP);
}
return(0); // Not enough evidence
}
// Format a DWORD into standard dotted-decimal ASCII format
// Return pointer to static string. Not thread safe!
// Pass 0 for bits if no block
char *IP2Str(IPTYPE ip)
{
static char IpStr[64];
DWORD ipval = ip.IP;
BYTE octet1 = (BYTE)((ipval >> 24) & 0xFF);
BYTE octet2 = (BYTE)((ipval >> 16) & 0xFF);
BYTE octet3 = (BYTE)((ipval >> 8) & 0xFF);
BYTE octet4 = (BYTE)(ipval & 0xFF);
if (ip.bits) // cidr format
sprintf(IpStr, "%d.%d.%d.%d/%d", octet1, octet2, octet3, octet4, ip.bits);
else // single IP format
sprintf(IpStr, "%d.%d.%d.%d", octet1, octet2, octet3, octet4);
return(IpStr);
}