Skip to contentSkip to conversation

Logging SNAT'ed connections

DISCUSSION Jan Kasprzak 4 participants 13 messages Jun 8 – Jun 12

lore
Share & tools
lore
#0 lore raw

Hi all,

how do you log SNAT'ed connections for further forensic purposes, when somebody complains about your public IP address and you want to be able to find a local user behind NAT?

I used to run something like

conntrack --event --buffer-size 1600000 -o id,timestamp --src-nat >> $LOGFILE

which did what I want. But now I tried to run the same on AlmaLinux9 host, and it seems that --src-nat filter does not get applied - it shows even connections which are not SNAT'ed at all, because they are for my own IP address:

[1780936563.343940]	    [NEW] icmp     1 30 src=128.1.227.61 dst=xx.yy.zz.13 type=8 code=0 id=8724 [UNREPLIED] src=xx.yy.zz.13 dst=128.1.227.61 type=0 code=0 id=8724 id=2525400839
[1780936563.343994]	 [UPDATE] icmp     1 30 src=128.1.227.61 dst=xx.yy.zz.13 type=8 code=0 id=8724 src=xx.yy.zz.13 dst=128.1.227.61 type=0 code=0 id=8724 id=2525400839
[1780936565.281121]	    [NEW] icmp     1 30 src=128.1.227.61 dst=xx.yy.zz.11 type=8 code=0 id=8724 [UNREPLIED] src=xx.yy.zz.11 dst=128.1.227.61 type=0 code=0 id=8724 id=2574615308
[1780936565.281167]	 [UPDATE] icmp     1 30 src=128.1.227.61 dst=xx.yy.zz.11 type=8 code=0 id=8724 src=xx.yy.zz.11 dst=128.1.227.61 type=0 code=0 id=8724 id=2574615308

Here xx.yy.zz.11 and .13 are my public IP addresses. Why do these connections show up in the conntrack --event --src-nat output? Thanks!

-Yenya

Signature · 4 lines
| Jan "Yenya" Kasprzak <kas at {fi.muni.cz - work | yenya.net - private}> |
| https://www.fi.muni.cz/~kas/                        GPG: 4096R/A45477D5 |
    I don't like Python; its lack of inline, anonymous, multi-statement
    functions makes me sad.                                --Eric Wastl
#1 lore raw
Quoted 25 lines · Jan Kasprzak

Hi all,

how do you log SNAT'ed connections for further forensic purposes, when somebody complains about your public IP address and you want to be able to find a local user behind NAT?

I used to run something like

conntrack --event --buffer-size 1600000 -o id,timestamp --src-nat >> $LOGFILE

which did what I want. But now I tried to run the same on AlmaLinux9 host, and it seems that --src-nat filter does not get applied - it shows even connections which are not SNAT'ed at all, because they are for my own IP address:

[1780936563.343940]	    [NEW] icmp     1 30 src=128.1.227.61 
dst=xx.yy.zz.13 type=8 code=0 id=8724 [UNREPLIED] src=xx.yy.zz.13 
dst=128.1.227.61 type=0 code=0 id=8724 id=2525400839
[1780936563.343994]	 [UPDATE] icmp     1 30 src=128.1.227.61 
dst=xx.yy.zz.13 type=8 code=0 id=8724 src=xx.yy.zz.13 dst=128.1.227.61 
type=0 code=0 id=8724 id=2525400839
[1780936565.281121]	    [NEW] icmp     1 30 src=128.1.227.61 
dst=xx.yy.zz.11 type=8 code=0 id=8724 [UNREPLIED] src=xx.yy.zz.11 
dst=128.1.227.61 type=0 code=0 id=8724 id=2574615308
[1780936565.281167]	 [UPDATE] icmp     1 30 src=128.1.227.61 
Quoted 5 lines

dst=xx.yy.zz.11 type=8 code=0 id=8724 src=xx.yy.zz.11 dst=128.1.227.61 type=0 code=0 id=8724 id=2574615308

Here xx.yy.zz.11 and .13 are my public IP addresses. Why do these connections show up in the conntrack --event --src-nat output? Thanks!

Unfortunately, it appears to be a bug affecting ICMP. Possibly here:

https://git.netfilter.org/libnetfilter_conntrack/tree/src/conntrack/objopt.c?id=7e5397b9167bdd7597be809b8f088ff333e1ad31#n189

For now, I would suggest filtering with something else.

conntrack -E -o id,timestamp |
perl -ne 'print if /\bsrc=(\S+).*\bdst=(\S+)/ && $1 ne $2'

Signature · 1 line
Kerin Millar
#2 lore raw

Hi, Kerin,

Quoted 8 lines · Kerin Millar

Unfortunately, it appears to be a bug affecting ICMP. Possibly here:

https://git.netfilter.org/libnetfilter_conntrack/tree/src/conntrack/objopt.c?id=7e5397b9167bdd7597be809b8f088ff333e1ad31#n189

For now, I would suggest filtering with something else.

conntrack -E -o id,timestamp |
perl -ne 'print if /\bsrc=(\S+).*\bdst=(\S+)/ && $1 ne $2'

Oh, I did not expect it to be a bug.

Anyway, you probably mean src= in both cases. Using backreferences, I did it this way:

perl -nE 'print if /\bsrc=(\S+)\s.*\bsrc=(?!\1\s)/'

Thanks!

-Yenya

Signature · 4 lines
| Jan "Yenya" Kasprzak <kas at {fi.muni.cz - work | yenya.net - private}> |
| https://www.fi.muni.cz/~kas/                        GPG: 4096R/A45477D5 |
    I don't like Python; its lack of inline, anonymous, multi-statement
    functions makes me sad.                                --Eric Wastl
#3 lore raw
Quoted 12 lines · Jan Kasprzak

Hi, Kerin,

Quoted 8 lines · Kerin Millar

Unfortunately, it appears to be a bug affecting ICMP. Possibly here:

https://git.netfilter.org/libnetfilter_conntrack/tree/src/conntrack/objopt.c?id=7e5397b9167bdd7597be809b8f088ff333e1ad31#n189

For now, I would suggest filtering with something else.

conntrack -E -o id,timestamp |
perl -ne 'print if /\bsrc=(\S+).*\bdst=(\S+)/ && $1 ne $2'

Quoted 6 lines

Oh, I did not expect it to be a bug.

Anyway, you probably mean src= in both cases. Using backreferences, I did it this way:

perl -nE 'print if /\bsrc=(\S+)\s.*\bsrc=(?!\1\s)/'

The idea I had in mind was to compare orig.src against reply.dst. Where orig.src != reply.dst, NAT applies. Your approach compares orig.src to reply.src. Those will differ for most flows, whether they were subjected to NAT or not.

I would tend not to use backreferences in that way because it becomes quite easy to make a mistake.

$ printf 'x=123.45 y=123.45\n' | perl -nE 'say "matched: $1" if /x=(\S+).*\by=(?!\1\s)/'
matched: 123.4

--
Kerin Millar

#4 lore raw
Quoted 13 lines · Kerin Millar
Quoted 12 lines · Jan Kasprzak

Hi, Kerin,

Quoted 8 lines · Kerin Millar

Unfortunately, it appears to be a bug affecting ICMP. Possibly here:

https://git.netfilter.org/libnetfilter_conntrack/tree/src/conntrack/objopt.c?id=7e5397b9167bdd7597be809b8f088ff333e1ad31#n189

For now, I would suggest filtering with something else.

conntrack -E -o id,timestamp |
perl -ne 'print if /\bsrc=(\S+).*\bdst=(\S+)/ && $1 ne $2'

Quoted 8 lines
Quoted 6 lines

Oh, I did not expect it to be a bug.

Anyway, you probably mean src= in both cases. Using backreferences, I did it this way:

perl -nE 'print if /\bsrc=(\S+)\s.*\bsrc=(?!\1\s)/'

The idea I had in mind was to compare orig.src against reply.dst. Where orig.src != reply.dst, NAT applies. Your approach compares orig.src to reply.src. Those will differ for most flows, whether they were subjected to NAT or not.

OK, understood. Thanks for clarification.

Quoted 4 lines

I would tend not to use backreferences in that way because it becomes quite easy to make a mistake.

$ printf 'x=123.45 y=123.45\n' | perl -nE 'say "matched: $1" if /x=(\S+).*\by=(?!\1\s)/'
matched: 123.4

This is why I had \s after the first group: (\S+)\s.* instead of (\S+).* But yes, if we are trying to match the first src= against the second dst=, we would hav to avoid .* to match the first dst= instead. Something like this:

perl -nE 'print if /\bsrc=(\S+)\s.*\bdst=.*\bdst=(?!\1\s)/'

-Yenya

Signature · 4 lines
| Jan "Yenya" Kasprzak <kas at {fi.muni.cz - work | yenya.net - private}> |
| https://www.fi.muni.cz/~kas/                        GPG: 4096R/A45477D5 |
    I don't like Python; its lack of inline, anonymous, multi-statement
    functions makes me sad.                                --Eric Wastl
#5 lore raw
In reply to Kerin Millar
Quoted 17 lines · Kerin Millar
Quoted 16 lines · Jan Kasprzak

Hi, Kerin,

Quoted 8 lines · Kerin Millar

Unfortunately, it appears to be a bug affecting ICMP. Possibly here:

https://git.netfilter.org/libnetfilter_conntrack/tree/src/conntrack/objopt.c?id=7e5397b9167bdd7597be809b8f088ff333e1ad31#n189

For now, I would suggest filtering with something else.

conntrack -E -o id,timestamp |
perl -ne 'print if /\bsrc=(\S+).*\bdst=(\S+)/ && $1 ne $2'

Oh, I did not expect it to be a bug.

Anyway, you probably mean src= in both cases. Using backreferences, I did it this way:

Quoted 7 lines
Quoted 1 line

perl -nE 'print if /\bsrc=(\S+)\s.*\bsrc=(?!\1\s)/'

The idea I had in mind was to compare orig.src against reply.dst. Where orig.src != reply.dst, NAT applies. Your approach compares orig.src to reply.src. Those will differ for most flows, whether they were subjected to NAT or not.

I would tend not to use backreferences in that way because it becomes quite easy to make a mistake.

$ printf 'x=123.45 y=123.45\n' | perl -nE 'say "matched: $1" if /x=(\S+).*\by=(?!\1\s)/'
matched: 123.4

Exactly, great care needs to be taken with back references; also given 'x=123.4 y=123.45', the \1 will match the '123.4' portion of 'y=123.45', making it seems like they're equal when they're not, so getting it done properly going that route will be far more complex.

Instead, here ...

$ for s in 'x=123.45 y=123.45'  'x=123.4 y=123.45'  'x=123.45 y=123.4' ; do
  printf "%s\n" "$s" | perl -nE 'say "x $1 does not match y $2" if m{
      \b x=(\S+)  .*  \b y=(\S+)
    }x && $1 ne $2;'
done
x 123.4 does not match y 123.45
x 123.45 does not match y 123.4

...is a cleaner way if one wants a more readable version.

Signature · 1 line
gfish
#6 lore raw
In reply to Kerin Millar
Quoted 29 lines · Kerin Millar
Quoted 28 lines · Jan Kasprzak

Hi all,

how do you log SNAT'ed connections for further forensic purposes, when somebody complains about your public IP address and you want to be able to find a local user behind NAT?

I used to run something like

conntrack --event --buffer-size 1600000 -o id,timestamp --src-nat >> $LOGFILE

which did what I want. But now I tried to run the same on AlmaLinux9 host, and it seems that --src-nat filter does not get applied - it shows even connections which are not SNAT'ed at all, because they are for my own IP address:

[1780936563.343940]	    [NEW] icmp     1 30 src=128.1.227.61 
dst=xx.yy.zz.13 type=8 code=0 id=8724 [UNREPLIED] src=xx.yy.zz.13 
dst=128.1.227.61 type=0 code=0 id=8724 id=2525400839
[1780936563.343994]	 [UPDATE] icmp     1 30 src=128.1.227.61 
dst=xx.yy.zz.13 type=8 code=0 id=8724 src=xx.yy.zz.13 dst=128.1.227.61 
type=0 code=0 id=8724 id=2525400839
[1780936565.281121]	    [NEW] icmp     1 30 src=128.1.227.61 
dst=xx.yy.zz.11 type=8 code=0 id=8724 [UNREPLIED] src=xx.yy.zz.11 
dst=128.1.227.61 type=0 code=0 id=8724 id=2574615308
[1780936565.281167]	 [UPDATE] icmp     1 30 src=128.1.227.61 

dst=xx.yy.zz.11 type=8 code=0 id=8724 src=xx.yy.zz.11 dst=128.1.227.61 type=0 code=0 id=8724 id=2574615308

Quoted 6 lines
Quoted 2 lines

Here xx.yy.zz.11 and .13 are my public IP addresses. Why do these connections show up in the conntrack --event --src-nat output? Thanks!

Unfortunately, it appears to be a bug affecting ICMP. Possibly here:

https://git.netfilter.org/libnetfilter_conntrack/tree/src/conntrack/objopt.c?id=7e5397b9167bdd7597be809b8f088ff333e1ad31#n189

Maybe then this helps?

https://patchwork.ozlabs.org/project/netfilter-devel/patch/[email protected]/

#7 lore raw
Quoted 33 lines · Pablo Neira Ayuso
Quoted 32 lines · Kerin Millar
Quoted 30 lines · Jan Kasprzak

Hi all,

how do you log SNAT'ed connections for further forensic purposes, when somebody complains about your public IP address and you want to be able to find a local user behind NAT?

I used to run something like

conntrack --event --buffer-size 1600000 -o id,timestamp --src-nat >> $LOGFILE

which did what I want. But now I tried to run the same on AlmaLinux9 host, and it seems that --src-nat filter does not get applied - it shows even connections which are not SNAT'ed at all, because they are for my own IP address:

[1780936563.343940]	    [NEW] icmp     1 30 src=128.1.227.61 
dst=xx.yy.zz.13 type=8 code=0 id=8724 [UNREPLIED] src=xx.yy.zz.13 
dst=128.1.227.61 type=0 code=0 id=8724 id=2525400839
[1780936563.343994]	 [UPDATE] icmp     1 30 src=128.1.227.61 
dst=xx.yy.zz.13 type=8 code=0 id=8724 src=xx.yy.zz.13 dst=128.1.227.61 
type=0 code=0 id=8724 id=2525400839
[1780936565.281121]	    [NEW] icmp     1 30 src=128.1.227.61 
dst=xx.yy.zz.11 type=8 code=0 id=8724 [UNREPLIED] src=xx.yy.zz.11 
dst=128.1.227.61 type=0 code=0 id=8724 id=2574615308
[1780936565.281167]	 [UPDATE] icmp     1 30 src=128.1.227.61 

dst=xx.yy.zz.11 type=8 code=0 id=8724 src=xx.yy.zz.11 dst=128.1.227.61 type=0 code=0 id=8724 id=2574615308

Here xx.yy.zz.11 and .13 are my public IP addresses. Why do these connections show up in the conntrack --event --src-nat output? Thanks!

Quoted 7 lines
Quoted 3 lines

Unfortunately, it appears to be a bug affecting ICMP. Possibly here:

https://git.netfilter.org/libnetfilter_conntrack/tree/src/conntrack/objopt.c?id=7e5397b9167bdd7597be809b8f088ff333e1ad31#n189

Maybe then this helps?

https://patchwork.ozlabs.org/project/netfilter-devel/patch/[email protected]/

I have not yet tried it but shouldn't the helper function be checking against the protonum field? Also, the referenced commit appears unrelated to the fix. I think it should refer to b4c3a23c884c.

--
Kerin Millar

#8 lore raw
Quoted 36 lines · Kerin Millar
Quoted 35 lines · Pablo Neira Ayuso
Quoted 34 lines · Kerin Millar
Quoted 25 lines · Jan Kasprzak

Hi all,

how do you log SNAT'ed connections for further forensic purposes, when somebody complains about your public IP address and you want to be able to find a local user behind NAT?

I used to run something like

conntrack --event --buffer-size 1600000 -o id,timestamp --src-nat >> $LOGFILE

which did what I want. But now I tried to run the same on AlmaLinux9 host, and it seems that --src-nat filter does not get applied - it shows even connections which are not SNAT'ed at all, because they are for my own IP address:

[1780936563.343940]	    [NEW] icmp     1 30 src=128.1.227.61 
dst=xx.yy.zz.13 type=8 code=0 id=8724 [UNREPLIED] src=xx.yy.zz.13 
dst=128.1.227.61 type=0 code=0 id=8724 id=2525400839
[1780936563.343994]	 [UPDATE] icmp     1 30 src=128.1.227.61 
dst=xx.yy.zz.13 type=8 code=0 id=8724 src=xx.yy.zz.13 dst=128.1.227.61 
type=0 code=0 id=8724 id=2525400839
[1780936565.281121]	    [NEW] icmp     1 30 src=128.1.227.61 
dst=xx.yy.zz.11 type=8 code=0 id=8724 [UNREPLIED] src=xx.yy.zz.11 
dst=128.1.227.61 type=0 code=0 id=8724 id=2574615308
[1780936565.281167]	 [UPDATE] icmp     1 30 src=128.1.227.61 
Quoted 5 lines

dst=xx.yy.zz.11 type=8 code=0 id=8724 src=xx.yy.zz.11 dst=128.1.227.61 type=0 code=0 id=8724 id=2574615308

Here xx.yy.zz.11 and .13 are my public IP addresses. Why do these connections show up in the conntrack --event --src-nat output? Thanks!

Unfortunately, it appears to be a bug affecting ICMP. Possibly here:

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

https://git.netfilter.org/libnetfilter_conntrack/tree/src/conntrack/objopt.c?id=7e5397b9167bdd7597be809b8f088ff333e1ad31#n189

Maybe then this helps?

https://patchwork.ozlabs.org/project/netfilter-devel/patch/[email protected]/

I have not yet tried it but shouldn't the helper function be checking against the protonum field? Also, the referenced commit appears unrelated to the fix. I think it should refer to b4c3a23c884c.

Thanks for reviewing, v2 is here:

https://patchwork.ozlabs.org/project/netfilter-devel/patch/[email protected]/

#9 lore raw
Quoted 41 lines · Pablo Neira Ayuso
Quoted 40 lines · Kerin Millar
Quoted 39 lines · Pablo Neira Ayuso
Quoted 29 lines · Kerin Millar
Quoted 28 lines · Jan Kasprzak

Hi all,

how do you log SNAT'ed connections for further forensic purposes, when somebody complains about your public IP address and you want to be able to find a local user behind NAT?

I used to run something like

conntrack --event --buffer-size 1600000 -o id,timestamp --src-nat >> $LOGFILE

which did what I want. But now I tried to run the same on AlmaLinux9 host, and it seems that --src-nat filter does not get applied - it shows even connections which are not SNAT'ed at all, because they are for my own IP address:

[1780936563.343940]	    [NEW] icmp     1 30 src=128.1.227.61 
dst=xx.yy.zz.13 type=8 code=0 id=8724 [UNREPLIED] src=xx.yy.zz.13 
dst=128.1.227.61 type=0 code=0 id=8724 id=2525400839
[1780936563.343994]	 [UPDATE] icmp     1 30 src=128.1.227.61 
dst=xx.yy.zz.13 type=8 code=0 id=8724 src=xx.yy.zz.13 dst=128.1.227.61 
type=0 code=0 id=8724 id=2525400839
[1780936565.281121]	    [NEW] icmp     1 30 src=128.1.227.61 
dst=xx.yy.zz.11 type=8 code=0 id=8724 [UNREPLIED] src=xx.yy.zz.11 
dst=128.1.227.61 type=0 code=0 id=8724 id=2574615308
[1780936565.281167]	 [UPDATE] icmp     1 30 src=128.1.227.61 

dst=xx.yy.zz.11 type=8 code=0 id=8724 src=xx.yy.zz.11 dst=128.1.227.61 type=0 code=0 id=8724 id=2574615308

Quoted 6 lines
Quoted 2 lines

Here xx.yy.zz.11 and .13 are my public IP addresses. Why do these connections show up in the conntrack --event --src-nat output? Thanks!

Unfortunately, it appears to be a bug affecting ICMP. Possibly here:

https://git.netfilter.org/libnetfilter_conntrack/tree/src/conntrack/objopt.c?id=7e5397b9167bdd7597be809b8f088ff333e1ad31#n189

Maybe then this helps?

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

https://patchwork.ozlabs.org/project/netfilter-devel/patch/[email protected]/

I have not yet tried it but shouldn't the helper function be checking against the protonum field? Also, the referenced commit appears unrelated to the fix. I think it should refer to b4c3a23c884c.

Thanks for reviewing, v2 is here:

https://patchwork.ozlabs.org/project/netfilter-devel/patch/[email protected]/

Thanks. I'll try v2 out here.

--
Kerin Millar

#12 lore raw
In reply to Jan Kasprzak
Quoted 15 lines · Jan Kasprzak

Hi all,

how do you log SNAT'ed connections for further forensic purposes, when somebody complains about your public IP address and you want to be able to find a local user behind NAT?

I used to run something like

conntrack --event --buffer-size 1600000 -o id,timestamp --src-nat >> $LOGFILE

which did what I want. But now I tried to run the same on AlmaLinux9 host, and it seems that --src-nat filter does not get applied - it shows even connections which are not SNAT'ed at all, because they are for my own IP address:

Quoted 7 lines
[1780936563.343940]	    [NEW] icmp     1 30 src=128.1.227.61 dst=xx.yy.zz.13 type=8 code=0 id=8724 [UNREPLIED] src=xx.yy.zz.13 dst=128.1.227.61 type=0 code=0 id=8724 id=2525400839
[1780936563.343994]	 [UPDATE] icmp     1 30 src=128.1.227.61 dst=xx.yy.zz.13 type=8 code=0 id=8724 src=xx.yy.zz.13 dst=128.1.227.61 type=0 code=0 id=8724 id=2525400839
[1780936565.281121]	    [NEW] icmp     1 30 src=128.1.227.61 dst=xx.yy.zz.11 type=8 code=0 id=8724 [UNREPLIED] src=xx.yy.zz.11 dst=128.1.227.61 type=0 code=0 id=8724 id=2574615308
[1780936565.281167]	 [UPDATE] icmp     1 30 src=128.1.227.61 dst=xx.yy.zz.11 type=8 code=0 id=8724 src=xx.yy.zz.11 dst=128.1.227.61 type=0 code=0 id=8724 id=2574615308

Here xx.yy.zz.11 and .13 are my public IP addresses. Why do these connections show up in the conntrack --event --src-nat output? Thanks!

Could you check if this fixes the issue for you?

https://patchwork.ozlabs.org/project/netfilter-devel/patch/[email protected]/

Thanks.