Score:1

IPTables log connection pid on OUTPUT chain

vn flag

I'm trying to find pid of a socket using iptables OUTPUT chain log, or even better adding it into the log.

My current iptable rule:

sudo iptables -A OUTPUT -j LOG --log-prefix='[PID]' --log-level 7 --log-uid

I'm a bit frustrated since I know that iptables' owner module can filter items by pid (using -m owner --owner-pid flag) which means that the info is there, but I can't log it.

I know that it impossible to do with INPUT chain since iptables is a kernel process, but for OUTPUT chain it should be possible.

any idea? or even of how to cross some log data in order to get PID of OUTPUT chain connections?

A.B avatar
cl flag
A.B
https://meta.stackexchange.com/questions/64068/is-cross-posting-a-question-on-multiple-stack-exchange-sites-permitted-if-the-qu ( https://unix.stackexchange.com/questions/688185/iptables-log-connection-pid-on-output-chain )
Score:0
fr flag

So, there currently is no way for IPtables to filter packets based on PID. But you can do it based on UID or GID:

owner match options:
[!] --uid-owner userid[-userid]      Match local UID
[!] --gid-owner groupid[-groupid]    Match local GID
[!] --socket-exists                  Match if socket exists
    --suppl-groups                   Also match supplementary groups set with --gid-owner

You could add a new user and then run the application as the newly created user:

sudo -u user application

If you have existing users, for example, postfix, that already have user accounts, you could do this:

First, find the user's UID:

[root@mail ~]# cat /etc/passwd | grep postfix
postfix:x:89:89::/var/spool/postfix:/sbin/nologin

Second, add this iptables rule. Pay attention to where you want it in your OUTPUT chain:

/usr/sbin/iptables -A OUTPUT -m owner --uid-owner 89 -j LOG --log-prefix "POSTFIX: "

And then all packets from user postfix will be logged.

Or Yaacov avatar
vn flag
looks like the --cmd-owner option was removed in kernel >= 2.6.15. (unfix-able)
fr flag
Yes, you are right. I just edited the post.
Or Yaacov avatar
vn flag
Thank you again Cameron, but still this is not what I was looking for.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.