Tuesday, March 9, 2010

Turning your laptop into a wireless AP

I'm just gonna go over some simple code and tools that you can use to transform your laptop running linux into a wireless access point where wireless clients can connect to. The programs that i will be using are airmon-ng, airbase-ng, dhcpd-server and dnsmasq just to name a few. Other utilities will be used in my example here but they are mostly complementary tools that may not be deemed necessary.

First we create a virual interface using airmon-ng
#airmon-ng start wlan0 //this uses the wireless card as a prototype so to speak to create a virutal interface (mon0) that can operate in what is known as monitor mode

We then will put our new virtual interface down so we can change our mac address to something other than the original address.
# ifconfig mon0 down //pull interface down
# macchanger -m 00:00:F0:0D:00:00 mon0 //changes the mac address originally at mon0
# ifconfig mon0 up //brings the interface back up

We then will use the airbase-ng program to create yet another virtual interface that would have the ability to act as an AP. This interface operates in what is known as Master mode, where it has the ability to act as a synchronisation master for clients. I usually would use the xterm command to create a new window to execute the airbase-ng program as its output can be very useful as it will show the current clients attempting to authenticate and associate with your machine.

# xterm -bg red -bd blue -fg white -hold -geometry 96x25+0+0 -e airbase-ng -e "GoodAP" -c 6 -v mon0 & //uses mon0 interface to create a new virutual interface (at0 by default). Xterm allows the output of the airebase-ng command to be displayed in a new x-based window. Airbase-ng '-e' gives the ssid name, '-c' gives the channel number to broadcast on and -v is for more verbose output.

A new virtual interface is now available, at0. This is the interface thats gonna respond to wireless client probe requests. At this stage it needs to be configured and given and ip address.

# ifconfig at0 netmask up
# ifconfig at0 mtu 1400

Our access point with the name "GoodAP" should now be broadcasting and clients would be able see it. However we are not complete in setting up our AP. Our clients need to be able to get a IP address via dhcp server and be able to resolve dns requests via a dnsserver. You would need a dhcp.conf config file. You can find many examples on the web of simple configs.



option domain-name-servers;
default-lease-time 360;
max-lease-time 720;

subnet netmask {
option subnet-mask;
option routers;
option broadcast-address;
option domain-name-servers;

Issuing the next two commands will take care of some permission issues when running the dhcpd server. Dhcpd server is run under the dhcpd user account, which do not have write permissions for the directory /var/run. To overcome this issue, simple do the following:

touch /var/run/dhcpd.pid
chown dhcpd:dhcpd /var/run/dhcpd.pid

By issuing the above, you are now giving the server permissions to have the relevant access to its PID file that it attempts to create and write to.

# chown dhcpd {dhcpd.conf,dhcpd.leases}
# chgrp dhcpd {dhcpd.conf,dhcpd.leases}
Remember, you may have to do the same for the parent folder as well from which the script or command is being ran

Start the dhcpd service:
# dhcpd3 -cf dhcpd.conf -lf dhcpd.leases -f at0
Note: You may get some errors relating to permissions and writing to the lease file. Simply change the user and group ownership of your leases file. You may also have to change the permissions of the directory as well. I made a directory specifically for my config and lease files and had to change the ownership permissions to the file and directory for everything to work fine.

Since the dhcpd.conf file sets the clients up to use our ip as the DNS server, we can set up a simple DNS server to handle the requests. I used dnsmasq (apt-get install dnsmasq). It works straight off a fresh install with no configuration. It uses the its local/etc/resolv.conf to forward the requests to. So basically, it listens on port 53 and forwards the request to the servers listed in /etc/resolve.conf. It may also cache these lookups as well.

Start the simple dns cacheing server.
# dnsmasq restart

You would need to set your kernel to forward mode to forward all packets not destined for it
# echo 1 > /proc/sys/net/ipv4/ip_forward

The last thing we really need to do here is to set IP masqerading. It allows the synchronization between two networks with different IP address, like a NAT router.

# iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

With this setup, airbase-ng will set up the AP interface for you, its your duty to start the necessary dhcp and dns servers to automate client setups. Forwarding withing the kernel is crucial so the clients wont be succeptable to denial of service by the kernel and an all important ip masquerade command for synchronization between the wireless nic and ethernet nic on the laptop.

The above is a basic overview of how this is done and may require an above average understanding of linux and servers. These are the basic commands that should get you up and going, although on your machine, there may be some things that need tweaking. Remember, everymachine is different and what might work for me may not work for you without little modifications on your system. Its very important to know what you are doing and what to expect from these tools as this knowledge would prove to be very valuable when you have to troubleshoot problems.

No comments:

Post a Comment