LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Security
User Name
Password
Linux - Security This forum is for all security related questions.
Questions, tips, system compromises, firewalls, etc. are all included here.

Notices


Reply
  Search this Thread
Old 11-21-2010, 08:58 AM   #1
YellowSnowIsBad
Member
 
Registered: Oct 2010
Posts: 49

Rep: Reputation: Disabled
IPTables - How to log?


Hi, i was looking for a way to log all incoming rules that are not part of an established connection, or are not in the INPUT ACCEPT policy. ie, if they are unsolicited connections only.
I was under the assumption that policies are 'parsed' down the table and will stop at a policy that matches. Hence why i put my logging policy at the bottom of the input chain. where am i going wrong? can someone please clarify my error?

Thank you.


Code:
# router addresses
router_mac="68:7f:74:01:dc:fe"
router_ip="192.168.2.2"
gateway_ip="192.168.2.1"

# mldonkey ports
mldonkey_tcp="27045"
mldonkey_udp="27049"

# bittorrent ports
btport_client="27051"
btport_tracker="27052"

# devices
vpndev="tun0"
ethdev="eth0"




# Flush all current rules from iptables
iptables -F

# Set default policies for INPUT, FORWARD and OUTPUT chains
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT DROP

# Set access for localhost
iptables -A INPUT -i lo -j ACCEPT
iptables -A OUTPUT -o lo -j ACCEPT

# Accept packets belonging to established and related connections
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

# START ALL RULES FROM HERE


# INPUT ACCEPT RULES
iptables -A INPUT -i "$ethdev" -s "$router_ip" -m mac --mac-source "$router_mac" -m state --state NEW -j ACCEPT
# mldonkey
iptables -A INPUT -p tcp --dport "$mldonkey_tcp" -j ACCEPT
iptables -A INPUT -p udp --dport "$mldonkey_udp" -j ACCEPT
# bittorrent
iptables -A INPUT -p tcp --dport "$btport_client" -j ACCEPT
iptables -A INPUT -p tcp --dport "$btport_tracker" -j ACCEPT


# INPUT BLOCK LOG RULES
iptables -A INPUT -j LOG --log-level 7 --log-prefix "IPTABLES:  "



# OUTPUT ACCEPT RULES
iptables -A OUTPUT -d "$router_ip" -o "$ethdev" -j ACCEPT
iptables -A OUTPUT -o ! "$vpndev" -m owner --uid-owner mldonkeyjail -j REJECT
iptables -A OUTPUT -o "$vpndev" -j ACCEPT
iptables -A OUTPUT -p tcp --dport 80 -d "$gateway_ip" -o "$ethdev" -j DROP
iptables -A OUTPUT -p udp --dport 53 -j ACCEPT
iptables -A OUTPUT -p tcp --dport 80 -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT
iptables -A OUTPUT -p tcp --dport 443 -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT


#OUTPUT DROP RULES
#iptables -A OUTPUT -o "$ethdev" -j DROP


# OUTPUT DROP LOG RULES
#iptables -A OUTPUT -o eth0 -j LOG --log-level 7 --log-prefix "IPTABLES:  "





# Save settings
iptables-save
 
Old 11-21-2010, 09:15 AM   #2
eSelix
Senior Member
 
Registered: Oct 2009
Location: Wroclaw, Poland
Distribution: Arch, Kubuntu
Posts: 1,281

Rep: Reputation: 320Reputation: 320Reputation: 320Reputation: 320
I checked this code on my PC and I got logging in file /var/log/kern.log

Check "iptables -S".

Last edited by eSelix; 11-21-2010 at 09:17 AM.
 
Old 11-21-2010, 11:09 AM   #3
YellowSnowIsBad
Member
 
Registered: Oct 2010
Posts: 49

Original Poster
Rep: Reputation: Disabled
Thanks for the reply, maybe my inquiry wasn't clear enough, my bad. It does log for me too, but it logs established connections and ports that i have specified to allow traffic. i just want to log unsolicited traffic.

i hope this is clearer. Thanks.
 
Old 11-21-2010, 11:15 AM   #4
eSelix
Senior Member
 
Registered: Oct 2009
Location: Wroclaw, Poland
Distribution: Arch, Kubuntu
Posts: 1,281

Rep: Reputation: 320Reputation: 320Reputation: 320Reputation: 320
Can you paste some examples from your log. And result of "iptables -S".

Last edited by eSelix; 11-21-2010 at 12:25 PM.
 
Old 11-21-2010, 02:16 PM   #5
unixfool
Member
 
Registered: May 2005
Location: Northern VA
Distribution: Slackware, Ubuntu, FreeBSD, OpenBSD, OS X
Posts: 782
Blog Entries: 8

Rep: Reputation: 158Reputation: 158
This may or may not help (and I'm not really strong in dealing with FW configs), but when whenever I want to log something, I always have a logging line that precedes the drop/reject line (log & drop). The same applies with traffic that is accepted that you want to log (log & accept). The same would apply at the clean-up (catch-all) rule. While it may seem repetitive to do this (it actually isn't as each rule will be unique), it helps with organization.
 
Old 11-21-2010, 03:15 PM   #6
eSelix
Senior Member
 
Registered: Oct 2009
Location: Wroclaw, Poland
Distribution: Arch, Kubuntu
Posts: 1,281

Rep: Reputation: 320Reputation: 320Reputation: 320Reputation: 320
He want to log everything what will not match any rule in iptables (what will be dropped by policy rules).
 
Old 11-21-2010, 04:42 PM   #7
YellowSnowIsBad
Member
 
Registered: Oct 2010
Posts: 49

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by eSelix View Post
He want to log everything what will not match any rule in iptables (what will be dropped by policy rules).
exactly, anything dropped should be logged.

i would post my syslog, but for some reason the daemon stopped logging a while ago. i just deleted my old logs last night. it's late right now and i am busy tommorow, but if i have time i'll post it asap.
you could take my word for it, that it is logging all traffic, inbound/outbound, dropped or accepted. I am no expert on iptables, so i wouldn't know, but shouldn't it be apparent from my rules alone?

Thanks.

$ iptables -S
( there are some rules added by blockcontrol, you can ignore them for the time being. )
Code:
-P INPUT DROP
-P FORWARD DROP
-P OUTPUT DROP
-N blockcontrol_fw
-N blockcontrol_in
-N blockcontrol_out
-A INPUT -m state --state NEW -m mark ! --mark 0x14 -j blockcontrol_in 
-A INPUT -i lo -j ACCEPT 
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT 
-A INPUT -s 192.168.2.2/32 -i eth0 -m mac --mac-source 68:7F:74:01:DC:FE -m state --state NEW -j ACCEPT 
-A INPUT -p tcp -m tcp --dport 27045 -j ACCEPT 
-A INPUT -p udp -m udp --dport 27049 -j ACCEPT 
-A INPUT -p tcp -m tcp --dport 27051 -j ACCEPT 
-A INPUT -p tcp -m tcp --dport 27052 -j ACCEPT 
-A INPUT -j LOG --log-prefix "IPTABLES:  " --log-level 7 
-A FORWARD -m state --state NEW -m mark ! --mark 0x14 -j blockcontrol_fw 
-A OUTPUT -m state --state NEW -m mark ! --mark 0x14 -j blockcontrol_out 
-A OUTPUT -o lo -j ACCEPT 
-A OUTPUT -d 192.168.2.2/32 -o eth0 -j ACCEPT 
-A OUTPUT ! -o tun0 -m owner --uid-owner mldonkeyjail -j REJECT --reject-with icmp-port-unreachable 
-A OUTPUT -o tun0 -j ACCEPT 
-A OUTPUT -d 192.168.2.1/32 -o eth0 -p tcp -m tcp --dport 80 -j DROP 
-A OUTPUT -p udp -m udp --dport 53 -j ACCEPT 
-A OUTPUT -p tcp -m tcp --dport 80 -m state --state NEW,RELATED,ESTABLISHED -j ACCEPT 
-A OUTPUT -p tcp -m tcp --dport 443 -m state --state NEW,RELATED,ESTABLISHED -j ACCEPT 
-A blockcontrol_fw -m mark --mark 0xa -j DROP 
-A blockcontrol_fw -d 208.67.220.220/32 -j RETURN 
-A blockcontrol_fw -d 208.67.222.222/32 -j RETURN 
-A blockcontrol_fw -s 192.168.2.0/24 -d 192.168.2.0/24 -j RETURN 
-A blockcontrol_fw -j NFQUEUE --queue-num 92 
-A blockcontrol_in -m mark --mark 0xa -j DROP 
-A blockcontrol_in -i lo -j RETURN 
-A blockcontrol_in -s 192.168.2.0/24 -j RETURN 
-A blockcontrol_in -s 192.168.2.2/32 -j RETURN 
-A blockcontrol_in -j NFQUEUE --queue-num 92 
-A blockcontrol_out -m mark --mark 0xa -j REJECT --reject-with icmp-port-unreachable 
-A blockcontrol_out -o lo -j RETURN 
-A blockcontrol_out -d 208.67.220.220/32 -j RETURN 
-A blockcontrol_out -d 208.67.222.222/32 -j RETURN 
-A blockcontrol_out -d 192.168.2.0/24 -j RETURN 
-A blockcontrol_out -d 192.168.2.2/32 -j RETURN 
-A blockcontrol_out -p tcp -m tcp --dport 5557 -j RETURN 
-A blockcontrol_out -p tcp -m tcp --dport 5556 -j RETURN 
-A blockcontrol_out -p tcp -m tcp --dport 5555 -j RETURN 
-A blockcontrol_out -p tcp -m tcp --dport 443 -j RETURN 
-A blockcontrol_out -p tcp -m tcp --dport 80 -j RETURN 
-A blockcontrol_out -j NFQUEUE --queue-num 92
 
Old 11-21-2010, 06:48 PM   #8
unixfool
Member
 
Registered: May 2005
Location: Northern VA
Distribution: Slackware, Ubuntu, FreeBSD, OpenBSD, OS X
Posts: 782
Blog Entries: 8

Rep: Reputation: 158Reputation: 158
Quote:
Originally Posted by eSelix View Post
He want to log everything what will not match any rule in iptables (what will be dropped by policy rules).
That doesn't change anything from what I posted above. If you want to log everything that the catch-all rule is dropping, you'll have to add a log rule before that drop rule.

I implicitly allow what traffic that needs to be allowed...everything else is blocked automatically (by the clean-up rule). In most cases, you wouldn't want to log the default drops. Best practice is to drop but not log (unless you're willing to pay the price in disk space usage). I do it because I donate my logs to SANS.

My last rules within my policy are as follows:
Code:
-A INPUT -p tcp -m tcp -i eth0 --dport 1:65535 -j LOG  --log-prefix "Clean-up Rule - BLOCKED: "
-A INPUT -p tcp -m tcp -i eth0 --dport 1:65535 -j DROP
A sample of my logs:

Code:
Nov 22 00:55:49 li7-220 kernel: Clean-up Rule - BLOCKED: IN=eth0 OUT= MAC=fe:fd:40:3e:e7:dc:00:12:f2:8f:79:08:08:00 SRC=91.213.175.90 DST=66.160.141.30 LEN=48 TOS=0x00 PREC=0x00 TTL=55 ID=19179 DF PROTO=TCP SPT=80 DPT=8005 WINDOW=65535 RES=0x00 ACK SYN URGP=0 
Nov 22 00:55:52 li7-220 kernel: Clean-up Rule - BLOCKED: IN=eth0 OUT= MAC=fe:fd:40:3e:e7:dc:00:12:f2:8f:79:08:08:00 SRC=91.213.175.90 DST=66.160.141.30 LEN=48 TOS=0x00 PREC=0x00 TTL=55 ID=57995 DF PROTO=TCP SPT=80 DPT=8005 WINDOW=65535 RES=0x00 ACK SYN URGP=0 
Nov 22 00:56:24 li7-220 kernel: Clean-up Rule - BLOCKED: IN=eth0 OUT= MAC=fe:fd:40:3e:e7:dc:00:12:f2:8f:79:08:08:00 SRC=193.137.208.208 DST=66.160.141.30 LEN=40 TOS=0x00 PREC=0x00 TTL=53 ID=0 DF PROTO=TCP SPT=6667 DPT=47513 WINDOW=0 RES=0x00 RST URGP=0 
Nov 22 00:56:54 li7-220 kernel: Clean-up Rule - BLOCKED: IN=eth0 OUT= MAC=fe:fd:40:3e:e7:dc:00:12:f2:8f:79:08:08:00 SRC=91.213.175.90 DST=66.160.141.30 LEN=52 TOS=0x00 PREC=0x00 TTL=55 ID=10652 DF PROTO=TCP SPT=80 DPT=2845 WINDOW=65535 RES=0x00 ACK SYN URGP=0 
Nov 22 00:56:57 li7-220 kernel: Clean-up Rule - BLOCKED: IN=eth0 OUT= MAC=fe:fd:40:3e:e7:dc:00:12:f2:8f:79:08:08:00 SRC=94.23.105.252 DST=66.160.141.30 LEN=44 TOS=0x00 PREC=0x00 TTL=56 ID=21765 DF PROTO=TCP SPT=80 DPT=2845 WINDOW=0 RES=0x00 ACK SYN URGP=0 
Nov 22 00:56:57 li7-220 kernel: Clean-up Rule - BLOCKED: IN=eth0 OUT= MAC=fe:fd:40:3e:e7:dc:00:12:f2:8f:79:08:08:00 SRC=91.213.175.90 DST=66.160.141.30 LEN=52 TOS=0x00 PREC=0x00 TTL=55 ID=63149 DF PROTO=TCP SPT=80 DPT=2845 WINDOW=65535 RES=0x00 ACK SYN URGP=0 
Nov 22 00:57:16 li7-220 kernel: Clean-up Rule - BLOCKED: IN=eth0 OUT= MAC=fe:fd:40:3e:e7:dc:00:12:f2:8f:79:08:08:00 SRC=91.213.175.90 DST=64.62.231.220 LEN=60 TOS=0x00 PREC=0x00 TTL=55 ID=21235 DF PROTO=TCP SPT=80 DPT=4289 WINDOW=65535 RES=0x00 ACK SYN URGP=0 
Nov 22 00:58:30 li7-220 kernel: WEBSERVER ACCEPT: IN=eth0 OUT= MAC=fe:fd:40:3e:e7:dc:00:12:f2:8f:79:08:08:00 SRC=66.249.68.136 DST=66.160.141.30 LEN=60 TOS=0x00 PREC=0x00 TTL=53 ID=50723 DF PROTO=TCP SPT=40923 DPT=80 WINDOW=5840 RES=0x00 SYN URGP=0 
Nov 22 01:02:09 li7-220 kernel: Clean-up Rule - BLOCKED: IN=eth0 OUT= MAC=fe:fd:40:3e:e7:dc:00:12:f2:8f:79:08:08:00 SRC=173.193.6.216 DST=64.62.231.220 LEN=40 TOS=0x00 PREC=0x00 TTL=111 ID=256 PROTO=TCP SPT=6000 DPT=2967 WINDOW=16384 RES=0x00 SYN URGP=0
EDIT:

I think I see the major difference between your policy and mine. I've INPUT and OUTPUT set to ACCEPT. You've them set to DENY. I know the reasons for dropping but I also know that ACCEPT works better for me with my current understanding of things (rest assured that the policy is sufficient, though).

Last edited by unixfool; 11-21-2010 at 07:57 PM.
 
Old 11-21-2010, 07:18 PM   #9
win32sux
LQ Guru
 
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870

Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
YellowSnowIsBad, can you post some sample log entries so we can see what you're seeing?
 
Old 11-22-2010, 02:11 PM   #10
eSelix
Senior Member
 
Registered: Oct 2009
Location: Wroclaw, Poland
Distribution: Arch, Kubuntu
Posts: 1,281

Rep: Reputation: 320Reputation: 320Reputation: 320Reputation: 320
@unixfool

I think the best approach in firewall is to drop well known bad packets, allow only needed packets and drop or reject every other. Your rule:
Quote:
-A INPUT -p tcp -m tcp -i eth0 --dport 1:65535 -j DROP
will not handle every unknown packet, for example: what about udp, icmp and other, dport 1:65535 is unnecesary, and what about port 0, what if we one day install new network card and it will be eth1. This is security hole. The default policy (-P) is just for handling these packets which we known nothing.
 
Old 11-22-2010, 02:52 PM   #11
unixfool
Member
 
Registered: May 2005
Location: Northern VA
Distribution: Slackware, Ubuntu, FreeBSD, OpenBSD, OS X
Posts: 782
Blog Entries: 8

Rep: Reputation: 158Reputation: 158
Quote:
Originally Posted by eSelix View Post
@unixfool

I think the best approach in firewall is to drop well known bad packets, allow only needed packets and drop or reject every other. Your rule:

will not handle every unknown packet, for example: what about udp, icmp and other, dport 1:65535 is unnecesary, and what about port 0, what if we one day install new network card and it will be eth1. This is security hole. The default policy (-P) is just for handling these packets which we known nothing.
I've italicized the important aspects of my comments.

I don't want to hijack this post, but I'm well aware (I explained already) of what the defaults allow and deny entail. Note that a default of accept isn't necessarily bad, as long as you shore up the risks. There are reasons why I set things up the way they are. I've been doing this quite a while as a profession. Managing FWs isn't my strongest skill but far from my weakest. I've been tested and tempered within the security arena...I'm not bragging. I'm just mentioning this so that you know that I'm not a neophyte at managing a firewall.

You also only saw a piece of my policy...that's not nearly enough to determine whether I'm doing right/wrong. I'm supremely comfortable with what I currently have in place. And, as you can see from the logs I provided, my logging works. I posted them as an example to the OP.

The focus should be to assist the OP's logging issue. If the default of his policy is to drop, then there's no need for a clean-up rule...I know this. The problem is that he wants logging of whatever's being dropped that there isn't an actual FW rule for. He's still going to have to place a logging rule at the very end of his input and output chains...correct me if I'm wrong in this (I didn't see anything of that in his config).

Last edited by unixfool; 11-22-2010 at 02:55 PM.
 
Old 11-22-2010, 03:06 PM   #12
eSelix
Senior Member
 
Registered: Oct 2009
Location: Wroclaw, Poland
Distribution: Arch, Kubuntu
Posts: 1,281

Rep: Reputation: 320Reputation: 320Reputation: 320Reputation: 320
Quote:
Originally Posted by unixfool View Post
Note that a default of accept isn't necessarily bad, as long as you shore up the risks.
Of course, I agree with this.


Quote:
The problem is that he wants logging of whatever's being dropped that there isn't an actual FW rule for. He's still going to have to place a logging rule at the very end of his input and output chains...correct me if I'm wrong in this (I didn't see anything of that in his config).
Maybe I do not understand correctly, my native language is not an English. He has logging rules here:
Quote:
# INPUT BLOCK LOG RULES
iptables -A INPUT -j LOG --log-level 7 --log-prefix "IPTABLES: "
 
Old 11-22-2010, 05:22 PM   #13
unixfool
Member
 
Registered: May 2005
Location: Northern VA
Distribution: Slackware, Ubuntu, FreeBSD, OpenBSD, OS X
Posts: 782
Blog Entries: 8

Rep: Reputation: 158Reputation: 158
He has it at the end of the input chain...one would think that would be enough. Something isn't right with his policy...it's usually pretty simple and basic. He still hasn't observed win32sux's (or your) request to post logs.

Last edited by unixfool; 11-22-2010 at 05:41 PM.
 
Old 11-22-2011, 09:55 PM   #14
fhleung
Member
 
Registered: Aug 2004
Distribution: Lubuntu Live OS
Posts: 432

Rep: Reputation: 30
Quote:
I checked this code on my PC and I got logging in file /var/log/kern.log
I can not find such log file?
Could someone tell me PLEASE where would iptables log go to?

Quote:
YellowSnowIsBad, can you post some sample log entries so we can see what you're seeing?
Anyone can show me the sample iptables LOG file PLEASE?

Last edited by fhleung; 11-22-2011 at 10:20 PM.
 
Old 11-23-2011, 07:26 AM   #15
eSelix
Senior Member
 
Registered: Oct 2009
Location: Wroclaw, Poland
Distribution: Arch, Kubuntu
Posts: 1,281

Rep: Reputation: 320Reputation: 320Reputation: 320Reputation: 320
Check file "/var/log/syslog" if not found logs there check every file in this directory. Logs look like this:

Code:
Nov 23 14:21:37 localhost kernel: [ 5705.672630] IN= OUT=eth0 SRC=192.168.1.1 DST=75.126.162.205 LEN=980 TOS=0x00 PREC=0x00 TTL=64 ID=57144 DF PROTO=TCP SPT=52536 DPT=80 WINDOW=48832 RES=0x00 ACK PSH URGP=0
Nov 23 14:21:37 localhost kernel: [ 5705.923623] IN= OUT=eth0 SRC=192.168.1.1 DST=75.126.162.205 LEN=40 TOS=0x00 PREC=0x00 TTL=64 ID=57145 DF PROTO=TCP SPT=52536 DPT=80 WINDOW=50358 RES=0x00 ACK URGP=0
Nov 23 14:21:44 localhost kernel: [ 5713.014774] IN= OUT=eth0 SRC=192.168.1.1 DST=75.126.162.205 LEN=980 TOS=0x00 PREC=0x00 TTL=64 ID=57146 DF PROTO=TCP SPT=52536 DPT=80 WINDOW=50358 RES=0x00 ACK PSH URGP=0
Nov 23 14:21:44 localhost kernel: [ 5713.238263] IN= OUT=eth0 SRC=192.168.1.1 DST=75.126.162.205 LEN=40 TOS=0x00 PREC=0x00 TTL=64 ID=57147 DF PROTO=TCP SPT=52536 DPT=80 WINDOW=51884 RES=0x00 ACK URGP=0
Nov 23 14:21:49 localhost kernel: [ 5717.640983] IN= OUT=eth0 SRC=192.168.1.1 DST=75.126.162.205 LEN=795 TOS=0x00 PREC=0x00 TTL=64 ID=57148 DF PROTO=TCP SPT=52536 DPT=80 WINDOW=51884 RES=0x00 ACK PSH URGP=0
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Firewall log file, how to make several different log files with IPTables? newtovanilla Linux - Newbie 5 11-28-2008 12:39 PM
a command or way to log time of iptables LOG entries? dividingbyzero Linux - Security 3 06-06-2008 01:23 AM
Deleted /var/log/messages, can't log any files-iptables chingyenccy Linux - Newbie 7 02-27-2005 04:03 PM
IPtables Log Analyzer from http://www.gege.org/iptables/ brainlego Linux - Software 0 08-11-2003 06:08 AM
iptables, changing log file from /var/log/messages acid2000 Linux - Networking 3 03-11-2003 08:38 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Security

All times are GMT -5. The time now is 05:03 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration