Results 1 to 10 of 10

Thread: Iptables log every connection

  1. #1
    Join Date
    Nov 2008
    Beans
    15

    Iptables log every connection

    Is there any posible way to log connections i mean time, src address, src port, destination address, destination port with ip tables?

    Thankyou.

  2. #2
    Join Date
    Aug 2008
    Location
    Brazil
    Beans
    12,497
    Distro
    Ubuntu Studio 12.04 Precise Pangolin

    Re: Iptables log every connection

    Here is an example to log inbound tcp connections:

    Code:
    iptables -A INPUT -p tcp -j LOG --log-prefix ' INPUT TCP ' --log-level 4
    You should put it in the top of the chain to log all incoming tcp traffic.

  3. #3
    Join Date
    Aug 2008
    Location
    Brazil
    Beans
    12,497
    Distro
    Ubuntu Studio 12.04 Precise Pangolin

    Re: Iptables log every connection

    BTW, I think the easiest way to log all the stuff is to create a new chain:

    Code:
    iptables -N INBOUND
    Then instead of using ACCEPT, redirect all traffic that should be accepted to the INBOUND chain. For example:

    Code:
    iptables -A INPUT -i eth0 -p tcp -m state --state ESTABLISHED,RELATED -j INBOUND
    Then log and accept every connection on the INBOUND chain:

    Code:
    iptables -A INBOUND -p tcp -j LOG --log-prefix ' INBOUND TCP ' --log-level 4
    iptables -A INBOUND -p tcp -j ACCEPT

  4. #4
    Join Date
    Nov 2007
    Location
    London, England
    Beans
    6,255
    Distro
    Xubuntu 17.10 Artful Aardvark

    Re: Iptables log every connection

    I guess you only want to log the initial connection rather than every packet. So I would suggest accepting packets on established connections without logging. Something like this:
    Code:
    # Create a chain that logs new connections:
    iptables -N LOGNEW
    iptables -A LOGNEW -j LOG --log-prefix ' INBOUND TCP ' --log-level 4
    iptables -A LOGNEW -j ACCEPT
    # Accept packets on existing connections without any fuss:
    iptables -A INPUT -p tcp -m state --state ESTABLISHED,RELATED -j ACCEPT
    # Log incoming packets on new conections:
    iptables -A INPUT -p tcp -j LOGNEW

  5. #5
    Join Date
    Jul 2007
    Beans
    305
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: Iptables log every connection

    Code:
    iptables -I INPUT -m state --state NEW -j LOG --log-prefix "New Connection: "
    iptables -I OUTPUT -m state --state NEW -j LOG --log-prefix "New Connection: "
    iplist

    "Specialization is for Insects", R. Heinlein

  6. #6
    Join Date
    Dec 2008
    Location
    Littleton, Colorado USA
    Beans
    350
    Distro
    Ubuntu 7.10 Gutsy Gibbon

    Re: Iptables log every connection

    I don't think NEW works for UDP packets. I think all packets will be logged in the UDP case. UDP are stateless. I checked my iptables script and it has UDP packets with a NEW flag. I'm not sure if the ESTABLISHED path even works for UDP. All UDP packets may go through the NEW path.

    ICMP packets are generally stateless too.
    Last edited by lensman3; May 14th, 2009 at 05:20 AM.

  7. #7
    Join Date
    Jul 2007
    Beans
    305
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: Iptables log every connection

    Quote Originally Posted by lensman3 View Post
    I don't think NEW works for UDP packets.
    Wrong. The state module uses a logical connection layer/abstraction.


    Code:
    [ 2134.566659] New Connection: IN= OUT=wlan0 SRC=192.168.178.229 DST=192.168.178.21 LEN=60 TOS=0x00 PREC=0x00 TTL=64 ID=65094 DF PROTO=UDP SPT=55717 DPT=53 LEN=40
    iplist

    "Specialization is for Insects", R. Heinlein

  8. #8
    Join Date
    Nov 2008
    Beans
    3

    Re: Iptables log every connection

    uljanow: you da man!

  9. #9
    Join Date
    Oct 2007
    Location
    Madison, Wisconsin
    Beans
    90
    Distro
    Ubuntu 12.10 Quantal Quetzal

    Re: Iptables log every connection

    Quote Originally Posted by uljanow View Post
    Code:
    iptables -I INPUT -m state --state NEW -j LOG --log-prefix "New Connection: "
    iptables -I OUTPUT -m state --state NEW -j LOG --log-prefix "New Connection: "
    where do these logs go? i tried the command but can't find the log

  10. #10
    Join Date
    Oct 2007
    Location
    Madison, Wisconsin
    Beans
    90
    Distro
    Ubuntu 12.10 Quantal Quetzal

    Re: Iptables log every connection

    Quote Originally Posted by spezticle View Post
    where do these logs go? i tried the command but can't find the log
    nevermind i figured out where the log is... uh.. now how do i turn it off my log is going to eat my hdd up in a matter of hours.

    edit:
    i'll figure it out later, i just shutdown the pc for now. i'm going to bed deal with it in the morning.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •