Wednesday, September 30, 2009

How to Change MAC Address

Changing MAC address of a machine is called spoofing a MAC address or faking a MAC address. In linux, you can change MAC address of your machine. This is how it is done.

First find the physical MAC address of your machine by running the following command :

$ ifconfig -a | grep HWaddr
eth0 Link encap:Ethernet HWaddr 00:1f:f3:cc:c2:f9

The hexadecimal numbers in blue denote my machine's MAC address. Yours will be different.

Next, login as root in Linux and enter the following commands -



# ifconfig eth0 down
# ifconfig eth0 hw ether 00:11:22:33:44:55
# ifconfig eth0 up
# ifconfig eth0 | grep HWaddr


Note above that I have changed the MAC address to a different number highlighted in blue. 00:11:22:33:44:55 is the new MAC address I have provided for my Linux machine. You can choose any 48 bits hexadecimal address as your MAC address.

Thursday, September 10, 2009

Disabling Macbook Pro Touchpad

We may want to disable Macbook Pro (or any other Laptop) Touchpad, such cases are:
(i) We have connected a USB mouse, so we do not want to use touchpad for now.
(ii) While typing, there is no use of touchpad, if touchpad is too sensitive, the cursor keeps on jumping due to slight touch of palm or finger.

I have written a script to disable/enable touchpad (which is using synaptics driver).





#!/bin/bash

## Disable touchpad if USB Mouse is attached

SYNAPTICS=`which synclient`

if [[ "$SYNAPTICS" == "" ]]
then
echo "$0: please install synaptics touchpad driver."
echo "Also make sure that 'Option \"SHMConfig\" \"on\"'"
echo " is added in Touchpad device Section in /etc/X11/xorg.conf"
exit
fi

USB_mouse_present=`grep -ic "usb.*mouse" /proc/bus/input/devices`
# if no USB Mouse; enable touchpad
if [ $USB_mouse_present -eq 0 ]
then
$SYNAPTICS TouchpadOff=0
else
$SYNAPTICS TouchpadOff=1
fi

# if any parameter [on|off] is given, override previous command
if [ $# -ge 1 ]
then
if [ "$1" = "on" ]
then
$SYNAPTICS TouchpadOff=0
else
$SYNAPTICS TouchpadOff=1
fi
fi

exit 0



Sample Run:
Turn on touchpad

$ ./touchpad.sh on

Turn off touchpad

$ ./touchpad.sh off

If we have plugged USB mouse, then just give following command

$ ./touchpad.sh

On removing USB mouse, give following, the touchpad will be enabled automatically. :)

$ ./touchpad.sh


PS: The configuration for synaptics driver can be referred from here or here.

Friday, September 4, 2009

IP Masquerade and Network Address Translation (NAT)

If we want to connect multiple computers to the Internet using single public IP Address, Masquerading (A form of NATing) helps us.

NAT describes the process of modifying the network addresses contained with datagram headers while they are in transit. IP masquerade is the name given to one type of network address translation that allows all of the hosts on a private network to use the Internet at the price of a single IP address.

IP masquerading allows you to use a private (reserved) IP network address on your LAN and have your Linux-based router perform some clever, real-time translation of IP addresses and ports. When it receives a datagram from a computer on the LAN, it takes note of the type of datagram it is, “TCP,” “UDP,” “ICMP,” etc., and modifies the datagram so that it looks like it was generated by the router machine itself (and remembers that it has done so). It then transmits the datagram onto the Internet with its single connected IP address. When the destination host receives this datagram, it believes the datagram has come from the routing host and sends any reply datagrams back to that address. When the Linux masquerade router receives a datagram from its Internet connection, it looks in its table of established masqueraded connections to see if this datagram actually belongs to a computer on the LAN, and if it does, it reverses the modification it did on the forward path and transmits the datagram to the LAN computer.

I have written a shell script, which converts a Linux box into a router. The script is written as:




#!/bin/bash

## Output interface: connected to Internet
out_iface=ppp0

## Run as root always
user_id=`whoami`

if [[ "$user_id" != "root" ]]
then
echo "$0: please run this script as root user."
exit
fi

## Checking existance of iptables
IPTABLES=`which iptables`

if [[ "$IPTABLES" == "" ]]
then
echo "$0: please install iptables."
exit
fi

if [ $# -ge 1 ]
then
case "$1" in
status)
$IPTABLES -t nat -L
exit 0
;;
stop)
## Disabling Packet forwarding in kernel
echo 0 > /proc/sys/net/ipv4/ip_forward
echo "Flushing NAT MASQUERADE Entries"
$IPTABLES -t nat -F
exit 0
;;
restart)
$0 stop
if [ $# -ge 2 ]
then
$0 start $2
else
$0 start
fi
;;
start)
if [ $# -ge 2 ]
then
out_iface=$2
fi
## Enabling Packet forwarding in kernel
echo 1 > /proc/sys/net/ipv4/ip_forward

## Enabling NAT Masquerade, if not enabled
if [ -z "`$IPTABLES -t nat -L | grep MASQUERADE`" ]
then

$IPTABLES -t nat -A POSTROUTING -o $out_iface -j MASQUERADE
fi
;;
*)
echo "USAGE: $0 <start|status|restart|stop> [internet_interface]"
exit 1
;;
esac
else
echo "USAGE: $0 <start|status|restart|stop> [internet_interface]"
exit 1
fi

exit 0



Sample Runs:

$ ./NAT_Masquerade.sh
./NAT_Masquerade.sh: please run this script as root user.
$ sudo ./NAT_Masquerade.sh
USAGE: ./NAT_Masquerade.sh [internet_interface]
$
Here, internet_interface is the interface which is connected to internet.
By default, ppp0 (Dial up) interface is taken.

$ sudo ./NAT_Masquerade.sh status
Chain PREROUTING (policy ACCEPT)
target prot opt source destination

Chain POSTROUTING (policy ACCEPT)
target prot opt source destination

Chain OUTPUT (policy ACCEPT)
target prot opt source destination
$
Since Masquerade is not yet applied, Chain POSTROUTING rule is empty.

Applying IP Masquerade to internet_interface eth0.
$ sudo ./NAT_Masquerade.sh start eth0
$ sudo ./NAT_Masquerade.sh status
Chain PREROUTING (policy ACCEPT)
target prot opt source destination

Chain POSTROUTING (policy ACCEPT)
target prot opt source destination
MASQUERADE all -- anywhere anywhere

Chain OUTPUT (policy ACCEPT)
target prot opt source destination
$ sudo ./NAT_Masquerade.sh stop
Flushing NAT MASQUERADE Entries
$