40 #define XFF_CHAIN_MINLEN 7
42 #define XFF_CHAIN_MAXLEN 256
44 #define XFF_DEFAULT "X-Forwarded-For"
53 static int ParseXFFString(
char *input,
char *output,
int output_size)
55 size_t len = strlen(input);
59 if (input[0] ==
'[') {
60 char *end = strchr(input,
']');
64 if (end != input+(
len - 1)) {
86 if (d == 3 && c == 1) {
88 char *x = strchr(input,
':');
101 if (inet_pton(AF_INET, input, ip) == 1 ||
102 inet_pton(AF_INET6, input, ip) == 1)
104 strlcpy(output, input, output_size);
117 char *dstbuf,
int dstbuflen)
122 uint64_t total_txs = 0;
123 uint8_t *p_xff = NULL;
125 htp_state = (
HtpState *)FlowGetAppState(f);
127 if (htp_state == NULL) {
128 SCLogDebug(
"no http state, XFF IP cannot be retrieved");
133 if (tx_id >= total_txs)
138 SCLogDebug(
"tx is NULL, XFF cannot be retrieved");
142 htp_header_t *h_xff = NULL;
143 if (tx->request_headers != NULL) {
144 h_xff = htp_table_get_c(tx->request_headers, xff_cfg->
header);
150 memcpy(xff_chain, bstr_ptr(h_xff->value), bstr_len(h_xff->value));
151 xff_chain[bstr_len(h_xff->value)]=0;
155 p_xff =
memrchr(xff_chain,
' ', bstr_len(h_xff->value));
164 p_xff = memchr(xff_chain,
',', bstr_len(h_xff->value));
170 return ParseXFFString((
char *)p_xff, dstbuf, dstbuflen);
184 uint64_t total_txs = 0;
186 htp_state = (
HtpState *)FlowGetAppState(f);
187 if (htp_state == NULL) {
188 SCLogDebug(
"no http state, XFF IP cannot be retrieved");
193 for (; tx_id < total_txs; tx_id++) {
217 if (xff_mode != NULL && strcasecmp(xff_mode,
"overwrite") == 0) {
220 if (xff_mode == NULL) {
221 SCLogWarning(
"The XFF mode hasn't been defined, falling back to extra-data mode");
223 else if (strcasecmp(xff_mode,
"extra-data") != 0) {
225 "The XFF mode %s is invalid, falling back to extra-data mode", xff_mode);
232 if (xff_deployment != NULL && strcasecmp(xff_deployment,
"forward") == 0) {
235 if (xff_deployment == NULL) {
236 SCLogWarning(
"The XFF deployment hasn't been defined, falling back to reverse "
239 else if (strcasecmp(xff_deployment,
"reverse") != 0) {
240 SCLogWarning(
"The XFF mode %s is invalid, falling back to reverse proxy deployment",
248 if (xff_header != NULL) {
249 result->
header = (
char *) xff_header;
262 static int XFFTest01(
void) {
263 char input[] =
"1.2.3.4:5678";
265 int r = ParseXFFString(input, output,
sizeof(output));
266 FAIL_IF_NOT(r == 1 && strcmp(output,
"1.2.3.4") == 0);
270 static int XFFTest02(
void) {
271 char input[] =
"[12::34]:1234";
273 int r = ParseXFFString(input, output,
sizeof(output));
274 FAIL_IF_NOT(r == 1 && strcmp(output,
"12::34") == 0);
278 static int XFFTest03(
void) {
279 char input[] =
"[2a03:2880:1010:3f02:face:b00c:0:2]:80";
281 int r = ParseXFFString(input, output,
sizeof(output));
282 FAIL_IF_NOT(r == 1 && strcmp(output,
"2a03:2880:1010:3f02:face:b00c:0:2") == 0);
286 static int XFFTest04(
void) {
287 char input[] =
"[2a03:2880:1010:3f02:face:b00c:0:2]";
289 int r = ParseXFFString(input, output,
sizeof(output));
290 FAIL_IF_NOT(r == 1 && strcmp(output,
"2a03:2880:1010:3f02:face:b00c:0:2") == 0);
294 static int XFFTest05(
void) {
295 char input[] =
"[::ffff:1.2.3.4]:1234";
297 int r = ParseXFFString(input, output,
sizeof(output));
298 FAIL_IF_NOT(r == 1 && strcmp(output,
"::ffff:1.2.3.4") == 0);
302 static int XFFTest06(
void) {
303 char input[] =
"12::34";
305 int r = ParseXFFString(input, output,
sizeof(output));
306 FAIL_IF_NOT(r == 1 && strcmp(output,
"12::34") == 0);
310 static int XFFTest07(
void) {
311 char input[] =
"1.2.3.4";
313 int r = ParseXFFString(input, output,
sizeof(output));
314 FAIL_IF_NOT(r == 1 && strcmp(output,
"1.2.3.4") == 0);
318 static int XFFTest08(
void) {
319 char input[] =
"[1.2.3.4:1234";
321 int r = ParseXFFString(input, output,
sizeof(output));
326 static int XFFTest09(
void) {
327 char input[] =
"999.999.999.999:1234";
329 int r = ParseXFFString(input, output,
sizeof(output));