08 Apr 2010 @ 9:16 AM 

Just for reference this is what i use to connect to nfs drives on my server

via script, GalacticAC is defined in /etc/hosts with the ip of my server

#! /bin/bash 
# script to mount my files on webserver
sudo mount -t nfs GalacticAC:/mnt/sdb1/ /home/michael/server/sdb1

and via fstab (will mount automatically on boot)

GalacticAC:/mnt/sdb1/ /home/michael/server/sdb1 nfs defaults 0 0
Posted By: admin
Last Edit: 08 Apr 2010 @ 09:16 AM

EmailPermalinkComments (0)
Tags
Categories: Linux, Networking
 06 Oct 2009 @ 10:55 AM 

I’m so dumb i was still using samba. If your using linux you really dont need samba ! You can set it up using nfs and have a easier more secure method. Its like the file was on your own machine.

This is what i used to do to mount my samba shares in ubuntu.

#!/bin/bash
#pass=$1
echo -n "Password: "
stty -echo
read password
#script to connect to samba drive on server.
smbmount //ip/dir /home/michael/server/ #-o username=zonemikel,password=$password
#example smbmount //192.168.1.200/WebFiles /home/michael/webserver/ -o username=zonemikel,password=$password
echo "connected"
read -p "Press any key to unmount drives. " -n1 -s
smbumount /home/michael/server/
smbumount /home/michael/webserver/
echo "unmounted"

I had to have it un mount or my pc would not shut down right … anyway thats the old way this is the new

sudo mount -t nfs serverName:/mnt/files /home/michael/server

No passwords, it allows only ip addresses from the network to connect. Since I’m using a direct connection from the server to this subnetwork there is no other place for someone to patch in and use that ip, so its relativly secure. If I were using it over a large network i think anyone could change their ip to a “trusted” (/etc/hosts.allow) ip and easily gain acess to your files, but on a home network such as mine with a trusted and un trusted subnetwork this is ok.

Here is the original Link to the instructions
This text below (the remainder of this article) is NOT my writing ! I just want to keep a copy here in case they move their site or something

While most things about Ubuntu work pretty much the way you want/expect the first time, one exception is sharing folders between two computers on the same LAN (”Local Area Network” — in this case, multiple computers in the same house sharing an internet connection via a router). Surprisingly, it’s easier/more intuitive to do this between an Ubuntu computer and a Windows computer! I wish Ubuntu would make a more intuitive, graphical way for non-technical users to do this — though I certainly have fun tinkering with these things and learning more about networking (or what-have-you) as I go.

Through some command-line magic, I’ve gotten my laptop to be able to access the music folder on my desktop — right now I’m playing music on my laptop that’s actually stored on my desktop’s much bigger hard drive. Sweet! How did I manage this?

Say you have two computers, both running Linux (in this case they’re both running Ubuntu and I can’t vouch that this will work on any other distro, but I suspect it would) — the server, which has the folder you want to share on its physical media, and the client, which will connect to the server and access its data.

On the client:
1. In a Terminal window, run

sudo apt-get install nfs-common

— this installs the software you’ll need.
2. Run

ifconfig

to find your IP on the local network; it should look something like

inet addr:192.168.1.101

(If you see more than one instance of “inet addr” in the output of ifconfig, choose the address that doesn’t begin with 127.)

On the server:
3. In a Terminal window, run

sudo apt-get install nfs-kernel-server

4. Edit the

/etc/hosts

file and add a line that looks like this:

neuron 192.168.1.101

where “neuron” is replaced with the hostname or a nickname for your client (in this case, “neuron” is the name of my laptop) and “192.168.1.101″ is replaced with the IP you found in step 2.
5. Test this — in a Terminal, run

ping -c 1 neuron

(or whatever name you used) and see if you get a response. If you get a response like “unknown host”, something is wrong — re-check your work from the previous steps (and check that the two computers are really on the same network!). If you get a response like “… 64 bytes from neuron… 1 packets transmitted, 1 received … ” then everything is hunky-dory so far and you are ready to move on!
6. Edit the

/etc/hosts.allow

file and add a line that looks like this:

ALL: 192.168.1.101

(again, use the IP that you found in Step 2).
7. Edit the

/etc/exports

file and add a line that looks like this:

/home/music 192.168.1.101(rw,sync,subtree_check,no_root_squash)

where again 192… is replaced with the IP from step 2, and “/home/music” is replaced with the full path to the folder you want to share. Note that for some reason it is important that the parenthesized arguments don’t have a space between them.
8. Run

sudo /etc/init.d/nfs-kernel-server restart

9. Run

ifconfig

and get the IP of your server, the same way that you found it for the client.

Back on the client!
10. Edit

/etc/hosts

and add a line like

192.168.1.103 boffin

where “192.168.1.103″ is replaced with the IP of your server (from step 9) and “boffin” is replaced with the hostname/nickname of your server (”boffin” is the name of my desktop).
11.

ping -c 1 boffin

to check that this worked, just like in Step 5.
12. Make a mountpoint for your shared directory — in my case, I used

mkdir /media/boffin-music

13. Mount the shared directory at the mountpoint, like this:

sudo mount -t nfs boffin:/home/music /media/boffin-music

replacing “boffin” with your server’s name, “/home/music” with the location on the server of your shared folder, and “/media/boffin-music” with the mountpoint you created in step 12.
14. Now you should be able to browse to the mountpoint and see your shared files, and open them, and use them. Awesome!

Posted By: admin
Last Edit: 06 Oct 2009 @ 01:41 PM

EmailPermalinkComments (0)
Tags
Categories: Linux, Networking
 23 May 2009 @ 1:05 PM 

This is a continuation of articles “using iptables on your linux router“  and “how to setup your slackware linux box as a router“. This is the script that im pretty much finished with and how it works.

Hardware Setup

This is a basic diagram of my network setup. Crossover cables are denoted with ‘C’. This does not represent the actual spacing between items at all, for instance PC 2 is about 100ft away from the hub, the hub is right on top of the server.

Eth1 Network (192.168.10.0)
Very Un-trusted !
Only allow ports 80, and 22
PC1 can be allowed samba ports also

Eth2 Network(192.168.1.0)
Very trusted
All Packets are passed directly through

network-setup

Firewall Script Flow Chart

This is a flow chart that explains how the firewall handles packets.

firewall-flow-chart

After making all this i realized that everything goes differently, packets go to see if its pc3 then to see if eth1 then they go to the firewall table to see if established/related, that is of course if they made it that far. If they were eth1 then they would have gone to net two. So please realize this above diagram is not exactly like my script, but very similar.

For simplicity I don’t include the “firewall” table which just accepts all local loopback and then goes to the trusted table. Really I should eliminate the “firewall” table, feel free to. The “NETTWO” table is dedicated to the unsecure network, It only allows port 80, 22 and PC1. PC1 is also pretty restricted. The table “NETTWO” hooks into the wifi router, which I am VERY weary of, I do further security on that router, its just a old Linksys wireless router. Its actually the router that I replaced my linux box with.

Anyway without further a due here is the actual script.

#!/bin/bash
 
RETVAL=0
external_int="eth0"
external_ip="`ifconfig $external_int | grep 'inet addr' | awk '{print $2}' | sed -e 's/.*://'`"
 
# start the firewall
start() {
 
	# set all default behaviour to accept
	iptables -P INPUT ACCEPT
	iptables -P OUTPUT ACCEPT
	iptables -P FORWARD ACCEPT
 
	# create new chains for firewall and trusted filtering
	iptables -N FIREWALL
	iptables -N TRUSTED
	iptables -N NETTWO # temp? tables for subnetwork of wifi, luis and garage computer
 
	# Log chain
	iptables -N LOG_DROP
	iptables -A LOG_DROP -j LOG --log-prefix '[IPTABLES DROP] : '
	iptables -A LOG_DROP -j DROP
 
	# allow established and related incoming connetions only
	iptables -A FIREWALL -i eth0 -m state --state ESTABLISHED,RELATED -j ACCEPT
	# Allow self communication
	iptables -A FIREWALL -i lo -j ACCEPT
	iptables -A FIREWALL -o lo -j ACCEPT
	# send all package to the trusted chain
	iptables -A FIREWALL -j TRUSTED
	# drop all other packets
	iptables -A FIREWALL -j LOG_DROP
 
	############################
	# Prerouting, to speed up things
	############################
 
	# give the ubuntu pc a green light to do whatever, compleatly trusted ! (to firewall only)
	# ubuntu pc is connected to eth2 and has static ip of 192.168.1.11
	iptables -A INPUT   -j ACCEPT -p all -s 192.168.1.11 -i eth2
	iptables -A OUTPUT  -j ACCEPT -p all -d 192.168.1.11 -o eth2
	# for the subnetwork that has luis and wifi we send it to its own table
	iptables -A INPUT -i eth1 -j NETTWO
	# send all input packets to the firewall chain
	iptables -A INPUT -j FIREWALL
 
	############################
	# Begin Forward Table and Inet sharing
	############################
 
	# forward all traffic coming from eth1 to eth 0 ( make all eth1's stuff go to the net)
	iptables -A FORWARD -i eth1 -o eth0 -j ACCEPT
	# forward only established or related connections from eth0 to eth1 ( so only stuff a local lanputer has started)
	iptables -A FORWARD -i eth0 -o eth1 -m state --state ESTABLISHED,RELATED -j ACCEPT
	# same as above but for eth2 (gigabit connection)
	iptables -A FORWARD -i eth2 -o eth0 -j ACCEPT
	iptables -A FORWARD -i eth0 -o eth2 -m state --state ESTABLISHED,RELATED -j ACCEPT
	# forward all sof2mp test stuff to this computer (take all connections to server)
	iptables -A FORWARD -i eth0 -o eth2 -p udp -m udp --dport 20100:20112 -j ACCEPT
	# broadcast the sof2 mp test server
	iptables -A FORWARD -i eth0 -o eth2 -p tcp -m tcp --dport 20100:20112 -j ACCEPT
	# any packets that get to this point are logged then dropped
	iptables -A FORWARD -j LOG_DROP
	# masquerade the ip's so they look like they are "all from one" machine
	iptables -A POSTROUTING -t nat -o eth0 -j MASQUERADE
	# turn forwarding on "tell the kernel"
	echo 1 > /proc/sys/net/ipv4/ip_forward
 
	############################
	# network two routing table, for subnetwork with luis and wifi
	# only port 80 and 22 are allowed, the rest are logged/dropped
	############################
 
	iptables -A NETTWO -p tcp -m tcp --dport 80 -j ACCEPT
	iptables -A NETTWO -p tcp -m tcp --dport 22 -j ACCEPT
	#iptables -A NETTWO -m mac --mac-source "pc2's mac" -p tcp -m tcp --dport 137:139 -j ACCEPT
	#iptables -A NETTWO -m mac --mac-source "pc2's mac" -j ACCEPT
	iptables -A NETTWO -j LOG --log-prefix '[NETTWO DROP] : '
	iptables -A NETTWO -j DROP
 
	############################
	# Begin Trusted Table
	############################
 
	# Allow http, the eth+ defines "all" ethernet devices
	iptables -A TRUSTED -i eth+ -p tcp -m tcp --dport 80 -j ACCEPT
 
	# Allow http, the eth+ defines "all" ethernet devices
	iptables -A TRUSTED -i eth1 -p tcp -m tcp --dport 80 -j ACCEPT
 
	# Allow https
	iptables -A TRUSTED -o eth0 -p udp -m udp --dport 443 -j ACCEPT
	iptables -A TRUSTED -o eth0 -p tcp -m tcp --dport 443 -j ACCEPT
 
	# Allow SSH
	iptables -A TRUSTED -i eth+ -p tcp -m tcp --dport 22 -j ACCEPT
 
	# sof II test
	iptables -A TRUSTED -i eth0 -p tcp -m tcp --dport 20100 -j ACCEPT
	iptables -A TRUSTED -i eth0 -p udp -m udp --dport 20100 -j ACCEPT
 
	# Nexuiz
	iptables -A TRUSTED -i eth0 -p tcp -m tcp --dport 26000 -j ACCEPT
	iptables -A TRUSTED -i eth0 -p udp -m udp --dport 26000 -j ACCEPT
 
	# teamspeak
	iptables -A TRUSTED -i eth0 -p tcp -m tcp --dport 8767  -j ACCEPT
	iptables -A TRUSTED -i eth0 -p udp -m udp --dport 8767 -j ACCEPT
 
	# allow samba server for print and files to local nics only
	iptables -A TRUSTED -i eth1 -p tcp -m tcp --dport 137:139 -j ACCEPT
	iptables -A TRUSTED -i eth2 -p tcp -m tcp --dport 137:139 -j ACCEPT
	iptables -A TRUSTED -i eth1 -p tcp -m tcp --dport 445 -j ACCEPT
	iptables -A TRUSTED -i eth2 -p tcp -m tcp --dport 445 -j ACCEPT
 
	# log and drop all packets going through the trusted chain that make it this far
	iptables -A TRUSTED -j LOG_DROP
 
	# end message
	echo "- Iptables Script ran, security enabled"
 
	RETVAL=0
}
# open everything and forward everything
dumb() {
	# set all default behaviour to accept
	iptables -P INPUT ACCEPT
	iptables -P OUTPUT ACCEPT
	iptables -P FORWARD ACCEPT
	# now forward everything
	# forward all traffic coming from eth2to eth 0 (all ubuntu's computer goes to net)
	iptables -A FORWARD -i eth1 -o eth2 -j ACCEPT
	# forward all traffic from eth0 to eth2, all traffic from net to ubuntu computer
	iptables -A FORWARD -i eth2 -o eth1 -j ACCEPT
	# same as above but for eth1 (local lan slow)
	iptables -A FORWARD -i eth2 -o eth0 -j ACCEPT
	iptables -A FORWARD -i eth0 -o eth2 -j ACCEPT
 
	iptables -t nat -A PREROUTING -p udp -i eth0 -d $external_ip --dport 20100:20112 -j DNAT --to-destination 192.168.1.11
	iptables -t nat -A PREROUTING -p tcp -i eth0 -d $external_ip --dport 20100:20112 -j DNAT --to-destination 192.168.1.11
 
	# masquerade the ip's so they look like they are "all from one" machine
	iptables -A POSTROUTING -t nat -o eth0 -j MASQUERADE
	# turn forwarding on "tell the kernel"
	echo 1 > /proc/sys/net/ipv4/ip_forward
	echo "+ everything accepted, everything forwarded";
}
 
# stop the firewall (everything is open !)
stop() {
	# set the default policy
	iptables -P INPUT ACCEPT	# accept all incoming packets
	iptables -P FORWARD ACCEPT	# forward all packets to other puters (flood)
	iptables -P OUTPUT ACCEPT	# accept all outgoing packets and send them
 
	# set default policy for the NAT table
	iptables -t nat -P PREROUTING ACCEPT
	iptables -t nat -P POSTROUTING ACCEPT
	iptables -t nat -P OUTPUT ACCEPT
 
	# delete all old rules
	iptables -F 			# flush
	iptables -t nat -F		# flust nat table
 
	# delete all chains
	iptables -X
	iptables -t nat -X
 
	echo "+ All tables flushed"
	RETVAL=0
}
lockdown() {
	echo "locking down everything"
	iptables -t nat -P PREROUTING DROP
	iptables -t nat -P POSTROUTING DROP
	iptables -t nat -P OUTPUT DROP
	iptables -t nat -P PREROUTING DROP
	iptables -t nat -P POSTROUTING DROP
	iptables -t nat -P OUTPUT DROP
	echo "itz locked down"
}
# case statement to call the correct function, exept status which is in the case
case $1 in
   start)
     start
     ;;
   dumb)
	 stop
     dumb
     ;;
   stop)
     stop
     ;;
   restart)
     stop
     start
     ;;
   status)
     iptables -L
     iptables -t nat -L
     RETVAL=0
     ;;
   lockdown)
	stop
	lockdown
	;;
   *)
echo "Usage: Firewall (start|stop|restart|status)"
esac
 
exit

Thats pretty much it. I’m still kinda dumb when it comes to the forwarding part, so be careful of that part. Also when i allow pc2 to connect to samba ports i use its mac addres so replace “pc2 mac” with the mac address without quotes. Like this 00:00:00:00:00

Im dropping packets like there is no tommorrow. I’ve found that pc1 has something on it that is always sending packets to weird ports/ip’s they are all getting blocked.

Please comment if you know how i can improve this, or can point out any errors.

Another note, if you have email coming out from your server be sure to enable port 25, or whatever port you use. I just did that to mine.

Posted By: admin
Last Edit: 23 May 2009 @ 01:05 PM

EmailPermalinkComments (9)
Tags
 05 May 2009 @ 2:17 PM 

This goes along with my other article on how to setup a router/gateway in slackware. After you have setup that router you will need to be able to open up ports. I found small quick fixes for this, but i didnt really make anything that i understood till a few days later and lots of reading. That is why i’m totally rewriting this article.

The best way to think of iptables/chains is kinda like chains (go figure) the packets go from link to link until they reach their destination or are dropped/rejected. One chain link can have several links atached to it so the packet could go out several different ways, lenghts of chain can be short or long.

Iptables in two paragraphs

So just for a example lets say we have a router that just accepts http or ssh traffic (which mine does). The chain would start at the first link, it would say “is it http?” if no, next link “is it ssh” if no next link. If there is no other link the “default policy” will take effect. The default policy can be drop or accept. So if we had a policy of accept the packet would get accepted if it was not a http or ssh packet, if we had a default drop policy it would get dropped. To avoid confusion we would have another link that would “drop” the packet if its not what we want.

There are 3 main sections of chain that you can mess with, INPUT, OUTPUT and FORWARD. The input chain/table is for packets coming in, the output for packets going out and the forward for packets getting forwarded. Really hard huh ? We are mostly concerned with packets coming in, the packets we send out should not be limited in most cases, and forwarding is its own thing.

An Example

The most basic configuration of my server allows only two ports open, I believe this to be secure, or at least better than the dozen that were open before I did this. The two ports that are open are ssh (22) and http (80). This way i can remote admin my server and allow my webserver to go through (which is on the same machine).

So that all has to do with packets coming in, lets add links to the INPUT table

# (link 1) Allow http, the eth+ defines "all" ethernet devices
iptables -A INPUT -i eth+ -p tcp -m tcp --dport 80 -j ACCEPT
# (link 2) Allow SSH
iptables -A INPUT -i eth+ -p tcp -m tcp --dport 22 -j ACCEPT
# (link 3) throw everything else away
iptables -A INPUT -j DROP

The comments are preceded by # so they are just saying whats going on. Basically we added three links “-A” for “append”. Ill leave the rest of the syntax up to you to figure out, but this is the main syntax that is used in most of the statements for iptables, these are very common lines.

The first link checks all eth nics (eg. eth+) and if they are passing port 80 data (http) we Accept it. Same goes for link two, except we are using port 22. Then link three just drops everything, this makes it like “stealth” mode, they cant figure out if its closed or open. Our server returns no response on “drop” if you want to report the port as closed use “REJECT”.

You should note that ORDER MATTERS, if we were to put the drop link in first it would drop all packets first. So add all the ports you want to allow before you put your Drop portion.

Another Example

I’ve just done this to my home network. Since i have wifi and my bro-in law hooked up to my subnetwork 192.168.10.0 im very weary of that connection. I’m almost positive that my bro in law has spyware or something that is trying to communicate with god knows what at all hours of the day. I know this by looking at the log files.

This subnetwork is hooked up to eth1, so simple enough only traffic on port 80 is allowed on that interface. To accomplish this i send all the traffic from that port to its own chain. The chain is very short it just allows port 80 and logs/drops everything else.

    iptables -N NETTWO  # create table
    iptables -A NETTWO -p tcp -m tcp --dport 80 -j ACCEPT    # is it port 80 ?
    iptables -A NETTWO -j LOG --log-prefix '[NETTWO DROP] : '   #if no log/drop
    iptables -A NETTWO -j DROP

and then add it as one of the first lines of your input table for eth1. So if anything is coming to/from eth1 we send it to this table. That way if you have another table with more complicated checking for tons of ports it does not have to check all of them before dumping the stuff on odd ports.

#first line of my input table, if eth1 jmp nettwo
 
iptables -i eth1 -j NETTWO

I know this works well because i was editing the script in windows over the samba share on the wifi. When i clicked back over to copy my code for the page it was froze because i closed the samba ports.

Also this provides a nice way to see all the stuff being dropped by differnet parts of the network, because looking through the log files is very difficult.

Links to other links of chain

A great way of doing this is to have a list/links/chain that you make yourself, then you can add that like a link into the input chain. For instance I made a “TRUSTED” chain/table and append all the ports i want to accept into it. Then my “input” table first sends the packet to the “trusted” table and then drops it. But, first i also do a little preliminary checking and send it to another table called firewall. So its INPUT -> FIREWALL -> TRUSTED -> DROP

# create two chains, one for fw other is trusted (for clean script?)
iptables -N FIREWALL
iptables -N TRUSTED
# allow established and related incoming connections only
iptables -A FIREWALL -i eth0 -m state --state ESTABLISHED,RELATED -j ACCEPT
# Allow self communication
iptables -A FIREWALL -i lo -j ACCEPT
iptables -A FIREWALL -o lo -j ACCEPT
# send all package to the trusted chain
iptables -A FIREWALL -j TRUSTED
# drop all other packets
iptables -A FIREWALL -j DROP

The -N makes a new table, then i use state matching “-m” to see if its a established or related packet, then i automatically accept local loopback, from there i append the trusted chain then append the drop link. So, basically instead of having one long chain we have one chain link that connects to several chains, but they are all pretty short. I’m sure this helps speed up things a lot. Like for instance if you know 90% of your packets are going to one machine you can filter them out in your first link before your server even starts to think about it. Thats what the “established, related” does, I’ve we’ve already established a link to a webpage or game server there is no need to check every single packet after that, its the “new” connections that we want to worry about.

All the stuff we want to keep out we append to the firewall table before the appending of the “trusted” table. This way we see if its somone we dont like, if so we drop them if not we see if its somone we do like if so let them pass if not drop them. Pretty simple stuff, but it takes a while to get used to the syntax and such.

There is no one script for everyone

This stuff is unique to every machine, if you dont wanna learn iptables get firestarter, firewallbuilder, guarddog or any of the other gui tools out there. I only tried one, and it was just as complicated as learning this, except when you learn iptables it allows you to do everything you want. But there is no copy/paste script that you can use (that i know of) belive me i tried a lot of them, they didnt work for me and i had no idea what i was doing. If you want good security thats in your hands read the material and practice with your iptables. You can use webpages like this one to see which ports you have open, close them open them and such and make sure you have iptables down.

I got most of this information from a really good tutorial i found, you might wanna check it out. http://ubuntuforums.org/showthread.php?t=159661&highlight=iptables

I barley feel comfortable with my iptables now, and i can write it from scratch. Until you get to the point where you KNOW what is going on and can write your iptables syntax, I would not feel secure.

Checking IPTABLES -L (use “iptables -L -v” instead)

This was confusing me forever, you type “iptables -L” and it shows you the rules but all the source and destination are anywhere anywhere. So, it looks like everything is going trough. What we need is a better way of seeing whats going on. If you want the best way to see whats going on try tcpdump, but for our purposes the command “iptables -L -v” works great. That “-v” part makes it print out alot more detail. The most useful part of that command is that it shows you how many packets it has sent to each part, so you can easily test ports and see where everything is going. For example, check it then go do something on that port and then check it again, if the number of packets through that port has not changed something is wrong, or right :)

Other Stuff

There is of course much more to it than this, there is the “forwarding” part. How do you want to handle the forwarding of your internet access to the machines on your local lan. That part is still pretty fuzzy for me, the main thing im wondering is do i need to filter out stuff for the forwarding part or is that already handled by the input part. Im pretty sure the input table filters stuff before it even gets to your forwarding table, but you might want to check up on that. Anyway this is what i do to forward to all my other machines, straight simple,

# enable port forwarding
iptables -A FORWARD -j ACCEPT
# hide computers behind the firewall
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
echo 1 > /proc/sys/net/ipv4/ip_forward

I was messing with that for a while trying to get it to just forward stuff on port 80 but i think the filtering stuff does not work the same in the forward table. I recommend having a basic firewall on the other systems on your lan to ensure safety.

Script ?

The best thing about all this is you can make it into one big script. I made mine into a executable in my /etc/rc.d/ folder i can access it like most of the other executables in that folder with ./firewall start or ./firewall restart and such. For instance when I know ill be playing games or hosting a server i can have special functions inside the script just for that like ./firewall gamenight or something. You could even use this to turn on/off interenet connections like if you have kids or something and you want your inet to turn off at a certain time you can just call the script ./firewall stop or ./firewall start at certain times. The possiblilites are endless, as they should be with linux. Here is the core of my bash script so you can make your own

# case statement to call the correct function, exept status which is in the case
case $1 in
   start)
     start
     ;;
   stop)
     stop
     ;;
   restart)
     stop
     start
     ;;
   status)
     iptables -L
     iptables -t nat -L
     RETVAL=0
     ;;
   lockdown)
	stop
	lockdown
	;;
   *)
echo "Usage: Firewall (start|stop|restart|status)"
esac
 
exit

Then above that you just need to make your fucntions like this is my “stop” function, you should note it calls the stop function before it resets. This pretty much opens my firewall up and enables forwarding. It illustrates the use of functions in bash, this should be above the code i have above, just like in C you cant call functions unless they are above the thing that is calling them.

# stop the firewall (everything is open !)
stop() {
	echo "Remvoing all Iptables rules: "
	echo "Everything on the network will be open !!!"
 
	# set the default policy
	iptables -P INPUT ACCEPT	# accept all incoming packets
	iptables -P FORWARD ACCEPT	# forward all packets to other puters (flood)
	iptables -P OUTPUT ACCEPT	# accept all outgoing packets and send them
 
	# set default policy for the NAT table
	iptables -t nat -P PREROUTING ACCEPT
	iptables -t nat -P POSTROUTING ACCEPT
	iptables -t nat -P OUTPUT ACCEPT
 
	# delete all old rules
	iptables -F 			# flush
	iptables -t nat -F		# flust nat table
 
	# delete all chains
	iptables -X
	iptables -t nat -X
 
	# setup port forwarding
	echo "Enabling forwarding from eth0 to everyone"
	iptables --table nat --append POSTROUTING --out-interface eth0 -j MASQUERADE
	echo "All tables flushed, all connections accepted/forwarded"
	RETVAL=0
}

If your interested in more of that look at this tutorial, almost everything in this article was adapted from that tutorial and the advanced one linked to on that page.

Helpful links

http://www.speedguide.net/analyzer.php? (Analize your internet connection)

https://www.grc.com/x/ne.dll?bh0bkyd2 (tests what ports are open on your computer)

http://www.speedtest.net/ ( a fast internet speed tester)

http://linux.die.net/man/8/iptables (excellent quick reference, when you already know how)

Posted By: admin
Last Edit: 05 May 2009 @ 02:17 PM

EmailPermalinkComments (1)
Tags
 03 May 2009 @ 1:46 PM 

Slackware Router/Nat/Fileserver/Webhost etc.

Get rid of your router and use a linux webserver instead. My server has always been up but it was just a webhosting server before, with some files on it. I’m using a old version of slackware on my server and xp or ubuntu on my clients.

The diagram explains a lot but my main goals were

  • Use gigabit Ethernet without having to buy a router (pc to pc)
  • Give the slackware box more control over my internet ip and such
  • Separate low speed and high speed networks
  • Learn stuff

(all cables are straight through except the one that says crossover)

Stuff you will need !

If you are setting up a gateway as I am you will need your isp’s gateway and your isp’s dns servers. You can find this info on your current router by looking in the status page. If worst comes to worse just call your isp and ask them. You need this info and cannot make a nat/gateway without it. If your not worried about internet then don’t worry about this.

To find your isp’s dns servers just connect your linux box and when your modem assigns it a ip it will edit the /etc/resolv.conf and add your dns’s. To get your ISP gateway the only way i know is plug in your router and look at the status page. You can also do all this by plugging your cable modem directly into a windows pc then when it connects to your modem go to the connections->status and the dns’s will be in there along with the gateway. Write all this info somewhere for future reference.

If you are connecting “like devices” such as pc to pc or hub to hub you will need a crossover cable. You can easily make a adapter for your current cables. See “further reading” for links on how crossover cables are setup.

Setting up the Server

Ip Forwarding and Masquerading

This stuff is way beyond me. In a nutshell what I gather we are doing is setting eth0 to forward all packets to eth1 and eth2, and eth1 and eth2’s packets are forwarded out to the net. I may be wrong about this though. The full tutorial (and where I got all my info for this part) was at http://www.yolinux.com/TUTORIALS/LinuxTutorialIptablesNetworkGateway.html , it is a very good tutorial, read the whole thing. Since I had two Ethernet cards I had a extra line but aside from that the iptables is the same one on that site for the dsl/cable router.

How to do it ?

Setting up your iptables script

  1. Log into your slackware server as root
  2. create a file in your /etc/rc.d/ folder called rc.iptables like this “vi /etc/rc.d/rc.iptables” or you can use gedit or kedit if your in a gui.
  3. Add these lines to it, in my config eth0 goes to the internet and eth1 and eth2 goes to the local network that I am providing with internet. Add the below script and save it.

    # Delete and flush. Default table is "filter". Others like "nat" must be explicitly stated.
    iptables --flush
    iptables --table nat --flush
    iptables --delete-chain
    iptables --table nat --delete-chain
     
    # Set up IP FORWARDing and Masquerading
    iptables --table nat --append POSTROUTING --out-interface eth0 -j MASQUERADE
    iptables --append FORWARD --in-interface eth2 -j ACCEPT
    iptables --append FORWARD --in-interface eth1 -j ACCEPT
    echo 1 > /proc/sys/net/ipv4/ip_forward
  4. Now make it executable “chmod +x /etc/rc.d/rc.iptables”
    NOTE: that should make it run every time on startup; however, mine never does I have to run it manually every time I restart my server. I don’t mind though, your server should not restart often. You can run it by going to the rc.d directory and typing “./rc.iptables”

Nic card configuration on the server

Most of the configuration for your nic cards is handled by the file /etc/rc.d/rc.inet1.conf this file holds the config settings that get used when you run your /etc/rc.d/rc.inet1 executable. I just happened to stumble on it when I was doing all this, lucky I did ! The last part where I define “gateway” is the gateway provided by my isp, its not some other computer on my local network (obviously from the format) you will need to get this value, you cant use my config without it.

Setting up your /etc/rc.d/rc.inet1.conf

  1. Edit your /etc/rc.d/rc.inet1.conf
  2. This is what mine looks like edit yours accordingly
    # /etc/rc.d/rc.inet1.conf
    #
    # This file contains the configuration settings for network interfaces.
    # If USE_DHCP[interface] is set to "yes", this overrides any other settings.
    # If you don't have an interface, leave the settings null ("").
     
    # You can configure network interfaces other than eth0,eth1... by setting
    # IFNAME[interface] to the interface's name. If IFNAME[interface] is unset
    # or empty, it is assumed you're configuring eth.
     
    # Several other parameters are available, the end of this file contains a
    # comprehensive set of examples.
     
    # =============================================================================
     
    # Config information for eth0:(this is the 3com card 30)
    IPADDR[0]=""
    NETMASK[0]=""
    USE_DHCP[0]="yes"
    DHCP_HOSTNAME[0]=""
     
    # Config information for eth1: (this is the built in card i think
    IPADDR[1]="192.168.10.1"
    NETMASK[1]="255.255.255.0"
    USE_DHCP[1]=""
    DHCP_HOSTNAME[1]=""
     
    # Config information for eth2: (this is the gigabit card 01)
    IPADDR[2]="192.168.1.200"
    NETMASK[2]="255.255.255.0"
    USE_DHCP[2]=""
    DHCP_HOSTNAME[2]=""
     
    # Config information for eth3:
    IPADDR[3]=""
    NETMASK[3]=""
    USE_DHCP[3]=""
    DHCP_HOSTNAME[3]=""
     
    # Default gateway IP address:
    GATEWAY="98.195.216.1"
     
    # Change this to "yes" for debugging output to stdout.  Unfortunately,
    # /sbin/hotplug seems to disable stdout so you'll only see debugging output
    # when rc.inet1 is called directly.
    DEBUG_ETH_UP="yes"
     
    ## Example config information for wlan0.  Uncomment the lines you need and fill
    ## in your info.  (You may not need all of these for your wireless network)
    #IFNAME[4]="wlan0"
    #IPADDR[4]=""
    #NETMASK[4]=""
    #USE_DHCP[4]="yes"
    #DHCP_HOSTNAME[4]="icculus-wireless"
    #DHCP_KEEPRESOLV[4]="yes"
    #DHCP_KEEPNTP[4]="yes"
    #DHCP_KEEPGW[4]="yes"
    #DHCP_IPADDR[4]=""
    #WLAN_ESSID[4]=BARRIER05
    #WLAN_MODE[4]=Managed
    ##WLAN_RATE[4]="54M auto"
    ##WLAN_CHANNEL[4]="auto"
    ##WLAN_KEY[4]="D5AD1F04ACF048EC2D0B1C80C7"
    ##WLAN_IWPRIV[4]="AuthMode=WPAPSK EncrypType=TKIP WPAPSK=7B1ABEEB5D197741923ED26727569C365E31212096A0EAFAD563B268BAD01CAF TxRate=0"
    #WLAN_WPA[4]="wpa_supplicant"
    #WLAN_WPADRIVER[4]="ndiswrapper"
     
    ## Some examples of additional network parameters that you can use.
    ## Config information for wlan0:
    #IFNAME[4]="wlan0"              # Use a different interface name nstead of
                                    # the default 'eth4'
    #HWADDR[4]="00:01:23:45:67:89"  # Overrule the card's hardware MAC address
    #MTU[4]=""                      # The default MTU is 1500, but you might need
                                    # 1360 when you use NAT'ed IPSec traffic.
    #DHCP_KEEPRESOLV[4]="yes"       # If you dont want /etc/resolv.conf overwritten
    #DHCP_KEEPNTP[4]="yes"          # If you don't want ntp.conf overwritten
    #DHCP_KEEPGW[4]="yes"           # If you don't want the DHCP server to change
                                    # your default gateway
    #DHCP_IPADDR[4]=""              # Request a specific IP address from the DHCP
                                    # server
    #WLAN_ESSID[4]=DARKSTAR         # Here, you can override _any_ parameter
                                    # defined in rc.wireless.conf, by prepending
                                    # 'WLAN_' to the parameter's name. Useful for
                                    # those with multiple wireless interfaces.
    #WLAN_IWPRIV[4]="AuthMode=WPAPSK EncrypType=TKIP WPAPSK=thekey TxRate=0"
                                    # Some drivers require a private ioctl to be
                                    # set through the iwpriv command. If more than
                                    # one is required, you can place them in the
                                    # IWPRIV parameter (space-separated, see the
                                    # example).

    So, in my configuration remember that eth0 is for the internet the other two are for local lan, the third one is the default.

    The gateway from your ISP

    The most IMPORTANT thing, that isn’t really straightforward is the “gateway” that ip address is the gateway provided by my ISP. The only way I could find that value was to plug in my old router, go to the status and get it. If you do plug in your old router to get this value get the DNS servers also, If you can just print the whole page your router gives you its gold.

    Restarting everything

    At this point we just want to restart all the network stuff, so type in these commands in your /etc/rc.d/

    cd /etc/rc.d/
    ./rc.inet1 restart
    ./rc.inet2 restart
    ./rc.inetd restart
    ./rc.iptables

    Not sure if they are all needed, but restarting them does not hurt. Also if you have a webserver restart that with “./rc.httpd restart”

    If your running a web server

    If you had a web server running before you started, you should note now that a lot of ip’s have changed. If you had specified ip’s in your virtualhost settings in httpd.conf go back and change them all to *. This way its name based virtual hosts and the ip’s don’t matter. When I first did this all I would get from my webserver was a directory listing, of course I got everything else working before I was testing my webserver though !

    Testing at this point

    Well, hopefully we setup the server as a nat. There is not much you can test at this point. Since I’m not running a dhcp server when I start up my computers that are connected to my server they will ask server for a ip and the server wont give it to them, at that point everything stops. Sorry, we need to go setup at least one computer before we can test. In hindsight a dhcp server on the linux box is much more convient, it makes it so the computers can just automatically get their ip’s and you would be done at this point. But, I didnt do it this way, its not that much harder to setup your nic configs on the client machines, but later I’ll write a article when I setup dhcp on my server.
    You should be able to ping places like yahoo.com from your server at this point, if your server does not have internet access nothing connected to it will either. Type in ifconfig and make sure everything looks alright, make sure eth0 has a internet ip.

    Quick setup on a windows machine

    Im using windows XP, not that unspeakable garbage that came after it. If your setting up a router you should be pretty familiar with this stuff. You basically need to setup the settings so your pc knows its static ip, gateway, and DNS servers. These will be the instructions for a windows machine on the subnetwork 192.168.10.x, it’s the 100Mbs network.

    1. Go to your network connection
    2. Right click the network connection you are connecting to the server and click properties
    3. Click the “internet protocol tcp/ip” and click properties
    4. Give it a static ip for its network, eg. If your nic on the server is 192.168.X.X set this ones static ip to 192.168.X.X+1
      NOTE: the third octal in the ip address is the network, so if your setting up two different lans like me then you need to make it the same as the nic you are connecting it to. So in my case I connected the normal computers to 192.168.10.1 and I gave them ip’s like 192.168.10.1
    5. Set subnet mask 255.255.255.0
    6. Set gateway to the ip of the nic that you are connecting to on the server eg. 192.168.10.1
    7. Set the DNS’s to the DNS ip addresses your ISP uses, I got these values from my router. You need to put something in here or you wont be able to browse webpages.
      NOTE: if you can access shares on server and ping the server and ip addresses on the internet but you cannot ping places like yahoo.com or google.com your problem is step 7, your either using the wrong DNS or you haven’t set it.

    You should be able to connect to your server and the internet through your server now. If not try restarting the computer, if you still cant try and ping the server, make sure your connected to the right nic.

    Connecting a linux machine

    Im using ubuntu on the machine im connecting. You might be using another distro. The setup is pretty straightforward. We need to do what we did in windows, set ip, set gateway, set dns’s. Ill give the instructions for the Ubuntu machine connected to the 192.168.1.x network.

    1. Edit /etc/network/interfaces this is how I set mine (setup ip’s and gateway)
      auto lo
      iface lo inet loopback
       
      #our inet interface to the server
      auto eth0
      iface eth0 inet static
      address 192.168.1.11
      gateway 192.168.1.200
      netmask 255.255.255.0
      network 192.168.1.0
      broadcast 192.168.1.255
    2. Edit /etc/ resolv.conf this isn’t generated by network manager J (setup dns’s)
      # Generated by NetworkManager
      nameserver 68.87.85.98
    3. Restart your network “sudo /etc/init.d/networking restart”

    Finished

    That’s it. Now, hopefully everything works, if not you will have to use sound, logical reasoning to narrow down the problems. Every network is different so its kinda hard for people to help you with really detailed stuff like this. Once you get to this point, you are the last hope for your network, there are no heroes out there that can save you.

    Conclusions

    This gives you much greater control over your network. Now I have two subnetworks that can’t see each other, but can see the internet, and the server. This way you can setup your wifi to be completely separate from the rest of your network, but allow wifi users to connect to the internet through it.

    A note on crossovers

    If you are connecting a computer to a computer like I am you will need a crossover cable, I’m not a very patient person so I made one. I also ordered a “crossover converter” from ebay for a few bucks. Its not very hard to make a “converter” or to just rewire your cable a little. Pretty much you need to swap two pairs of wires, so all together from the eight pins you just mess with four.

    I did something like this http://www.instructables.com/id/Crossover-Dongle/ but not as pretty J

    Ill just use that till my other converter shows up, so far its working ok. I actually cut a old cable and took the connector off of a old 3com nic.

    How much speed did I get from my gigabit Ethernet ?

    One of the main goals of all this was to allow two computers to share gigabit Ethernet without having to buy a router. So, how much of a boost did I get.

    I tested my gigabit connection and I tested my other sub network connection between two 100Mbs. My tests were quick and dirty, I did all the conversions myself and just looked at the time to see how long it would take for it to transmit a ~700meg file.

    Below are all my calculations. What I found was that I’m only getting about 17% of what I should from my gigabit Ethernet which is about 169Mbs. For the 100Mbs I’m only getting about 65% of what I should which is about 65Mbs. So even though my throughput is horrible im still going almost three times as fast. I’m pretty sure the low bandwidth is from a bottleneck on the server pci bus speed or my Ethernet cable. Ill troubleshoot it later.

    transfer of file: size 716,808 KB or 716808000 bytes or 5734464000 bits

    @gigabit, time ~34seconds; speed 168,660,705.88 or about 169Mbs ; eff=17%

    @100Mbs, time ~88seconds; speed 65,164,363.63 so about 65Mbs ; eff=65%

    Equation I derived Speed=size in bytes / time in seconds

    What’s Next ?

    Well, the next thing I need to tackle is a better understanding of iptables. I always need to route ports and such for playing games. Like we play nexiuz a lot and I need to be able to forward port 26000 to the machine im playing on. Also I plan on reusing my old router to give me better wifi signal upstairs and outside.

    Further reading

    http://www.makeitsimple.com/how-to/dyi_crossover.htm (how crossovers work)

    http://www.yolinux.com/TUTORIALS/LinuxTutorialIptablesNetworkGateway.html

    http://www.yolinux.com/TUTORIALS/LinuxTutorialNetworking.html (good tutorial)

    http://www.slackware.com/~alien/efg/ (generates iptables config, I didn’t use but might later)

Posted By: Michael
Last Edit: 03 May 2009 @ 01:46 PM

EmailPermalinkComments (12)
Tags
Categories: Linux, Networking
Change Theme...
  • Users » 5
  • Posts/Pages » 71
  • Comments » 62
Change Theme...
  • VoidVoid « Default
  • LifeLife
  • EarthEarth
  • WindWind
  • WaterWater
  • FireFire
  • LightLight

Contact Me



    No Child Pages.

Front



    No Child Pages.