-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
XForwardedForChecker.ts
281 lines (240 loc) · 9.95 KB
/
XForwardedForChecker.ts
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import { IIpExtractionResult, MAX_CONFIDENCE } from '../../baseScenarios/index.js';
import { APITypes } from 'crowdsec-client';
import { AddressObject, createDebugger } from '../../utils.js';
import { IScenarioOptions } from '../IScenarioOptions.js';
import { IncomingMessage } from 'http';
import { IncomingHttpHeaders } from 'http2';
import { CheckerScenario } from '../../baseScenarios/CheckerScenario.js';
import { LRUCache } from 'lru-cache';
import * as crypto from 'crypto';
export interface IXForwardedForOptions {
/**
* ips/cidr of allowed reverse proxies
*/
trustedProxies?: Array<string>;
alertOnNotTrustedIps?: boolean;
}
declare module '../IScenarioOptions.js' {
interface IScenarioOptions {
'x-forwarded-for'?: IXForwardedForOptions;
}
}
export interface IExtractedIP {
ip: string;
address: AddressObject;
trustedProxy?: boolean;
valid?: boolean;
}
const debug = createDebugger('XForwardedForChecker');
export class XForwardedForChecker extends CheckerScenario {
static readonly scenarioName = 'thib3113/x-forwarded-for';
static readonly scenarioVersion = '0.0.1';
private reverseProxiesRange: Array<AddressObject>;
private readonly currentOptions: IXForwardedForOptions;
private extractIpCache: LRUCache<string, Array<IExtractedIP>, unknown>;
constructor(options?: IScenarioOptions) {
debug('construct');
super(options);
const currentOptions = options?.['x-forwarded-for'];
this.currentOptions = {
alertOnNotTrustedIps: true,
...currentOptions
};
this.reverseProxiesRange = (currentOptions?.trustedProxies ?? []).map((cidr) => this.getAddressObjectWithCache(cidr));
this.extractIpCache = new LRUCache<string, Array<IExtractedIP>>({
max: this.ipObjectCache.max
});
}
private generateAlert(
alert: Omit<APITypes.Alert, 'scenario_version' | 'scenario_hash' | 'capacity' | 'leakspeed' | 'simulated' | 'events' | 'source'> &
Partial<Pick<APITypes.Alert, 'events' | 'capacity' | 'leakspeed' | 'simulated' | 'source'>>,
ip: string
): APITypes.Alert {
return {
capacity: 0,
simulated: false,
events: [],
leakspeed: '0s',
...alert,
remediation: true,
source: {
...alert.source,
scope: 'ip',
ip,
value: ip
},
scenario_hash: '',
scenario_version: XForwardedForChecker.scenarioVersion
};
}
protected _check(ip: AddressObject, req: IncomingMessage): Array<APITypes.Alert> | APITypes.Alert | undefined {
const localDebug = debug.extend(`check(${ip.address})`);
localDebug('start()');
const { alertOnNotTrustedIps } = this.currentOptions;
const date = new Date().toISOString();
const alerts: Array<APITypes.Alert> = [];
const ipResult = this.extractIps(req);
const ipStr = ip.addressMinusSuffix ?? '';
const untrustedProxyIndex = ipResult.findIndex((i) => !i.trustedProxy);
if (untrustedProxyIndex < 0) {
localDebug('current ip not found in XForwardedFor header');
return [];
}
if (untrustedProxyIndex + 1 < ipResult.length) {
localDebug('untrusted "proxy" pass header, send alert ? %o', alertOnNotTrustedIps);
if (alertOnNotTrustedIps) {
const scenarioName = `${XForwardedForChecker.scenarioName}/untrusted-proxy`;
const headers = this.getXForwardedForHeader(req.headers);
//create alert
alerts.push(
this.generateAlert(
{
scenario: scenarioName,
created_at: date,
events: [
{
meta: [
{
key: 'http_forwarded_for',
value: headers[0]
},
{
key: 'http_forwarded_for_parsed',
value: ipResult.map(({ ip }) => ip).join(', ')
},
{
key: 'source_ip',
value: ipStr
},
{
key: 'timestamp',
value: date
}
],
timestamp: date
}
],
events_count: 1,
message: `Ip ${ipStr} performed '${scenarioName}' (1 event) at ${date}`,
start_at: date,
stop_at: date
},
ipStr
)
);
}
}
return alerts;
}
private extractIpsFromHeader(xForwardedHeaders: string | Array<string>): Array<IExtractedIP> {
return (
(Array.isArray(xForwardedHeaders) ? xForwardedHeaders : [xForwardedHeaders])
//flat multiple headers
.flat()
//split x-forwarded-for
.map((header) => (header ?? '').replace(/\s*/g, '').split(','))
//flat all ips
.flat()
//try to parse ips
.map((header) => {
const returnIP = {
ip: header
} as IExtractedIP;
try {
returnIP.address = this.getAddressObjectWithCache(header);
} catch (e) {
debug(`fail to read ip "${header}"`);
}
return returnIP;
})
.reverse()
);
}
private getXForwardedForHeader(headers: IncomingHttpHeaders): Array<string> {
const localDebug = debug.extend('getXForwardedForHeader');
localDebug('start');
const headerName = 'X-Forwarded-For'.toLowerCase();
const headersFound = Object.entries(headers)
.filter(([key, value]) => key.toLowerCase() === headerName && value)
.map(([_, value]) => value)
.flat() as Array<string>;
debug('find %d header matching %s', headersFound.length, headerName);
return headersFound;
}
private extractIps(req: IncomingMessage): Array<IExtractedIP> {
const localDebug = debug.extend('extractIps');
localDebug('start');
const { remoteAddress } = req.socket;
const remoteAddressIPResult = {
ip: remoteAddress,
address: this.getAddressObjectWithCache(remoteAddress),
valid: true
} as IExtractedIP;
let firstUntrustedIpFound = false;
const xForwardedForHeaderValues = this.getXForwardedForHeader(req.headers);
localDebug(`X-Forwarded-For extracted (${xForwardedForHeaderValues.length}) : "${xForwardedForHeaderValues.join('", "')}"`);
//use cache if this xForwardedFor is always known
const cacheKey = xForwardedForHeaderValues.map((h) => crypto.createHash('sha256').update(h).digest('hex')).join('|');
const cache = this.extractIpCache.get(cacheKey);
if (cache) {
return [...cache];
}
const result = [remoteAddressIPResult, ...this.extractIpsFromHeader(this.getXForwardedForHeader(req.headers))].map((ipResult) => {
localDebug('test if %o is a reverse proxy', ipResult.ip);
ipResult.valid = true;
ipResult.trustedProxy = false;
if (!ipResult.address) {
ipResult.valid = false;
return ipResult;
}
if (
!firstUntrustedIpFound &&
this.reverseProxiesRange.some((address) => {
const res = ipResult.address.isInSubnet(address);
localDebug(`%o is in subnet %o ? %o`, ipResult.ip, address.address, res);
return res;
})
) {
localDebug('%o is a reverse proxy', ipResult.address.addressMinusSuffix);
return {
...ipResult,
trustedProxy: true
};
}
firstUntrustedIpFound = true;
localDebug('%o is not a reverse proxy', ipResult.ip);
return ipResult;
});
this.extractIpCache.set(cacheKey, result);
return [...result];
}
public extractIp = (req: IncomingMessage): IIpExtractionResult | undefined => {
const localDebug = debug.extend('extractIp');
localDebug('start');
const ips = this.extractIps(req);
let lastProxy: IExtractedIP | undefined;
const lastIp: IExtractedIP | undefined = ips.find((value, index, obj) => {
if (value.trustedProxy) {
return false;
}
lastProxy = obj[index - 1];
return !value?.trustedProxy;
});
if (!lastIp?.valid) {
debug('fail to extract ip');
const previousIp = lastProxy;
if (previousIp && previousIp.valid && previousIp.trustedProxy) {
return {
confidence: 1,
ip: previousIp.ip
};
}
return undefined;
}
debug('found ip %o from %s', lastIp.ip, ips[0].ip);
return {
ip: lastIp.ip,
confidence: MAX_CONFIDENCE
};
};
}