Sunday, January 10, 2016

How to block network traffic by country on Linux

http://xmodulo.com/block-network-traffic-by-country-linux.html

As a system admin who maintains production Linux servers, there are circumstances where you need to selectively block or allow network traffic based on geographic locations. For example, you are experiencing denial-of-service attacks mostly originating from IP addresses registered with a particular country. In other cases, you want to block SSH logins from unknown foreign countries for security reasons. Or your company has a distribution right to online videos, which allows it to legally stream to particular countries only. Or you need to prevent any local host from uploading documents to any non-US remote cloud storage due to geo-restriction company policies.
All these scenarios require an ability to set up a firewall which does country-based traffic filtering. There are a couple of ways to do that. For one, you can use TCP wrappers to set up conditional blocking for individual applications (e.g., SSH, NFS, httpd). The downside is that the application you want to protect must be built with TCP wrappers support. Besides, TCP wrappers are not universally available across different platforms (e.g., Arch Linux dropped its support). An alternative approach is to set up ipset with country-based GeoIP information and apply it to iptables rules. The latter approach is more promising as the iptables-based filtering is application-agnostic and easy to set up.
In this tutorial, I am going to present another iptables-based GeoIP filtering which is implemented with xtables-addons. For those unfamiliar with it, xtables-addons is a suite of extensions for netfilter/iptables. Included in xtables-addons is a module called xt_geoip which extends the netfilter/iptables to filter, NAT or mangle packets based on source/destination countries. For you to use xt_geoip, you don't need to recompile the kernel or iptables, but only need to build xtables-addons as modules, using the current kernel build environment (/lib/modules/`uname -r`/build). Reboot is not required either. As soon as you build and install xtables-addons, xt_geoip is immediately usable with iptables.
As for the comparison between xt_geoip and ipset, the official source mentions that xt_geoip is superior to ipset in terms of memory foot print. But in terms of matching speed, hash-based ipset might have an edge.
In the rest of the tutorial, I am going to show how to use iptables/xt_geoip to block network traffic based on its source/destination countries.

Install Xtables-addons on Linux

Here is how you can compile and install xtables-addons on various Linux platforms.
To build xtables-addons, you need to install a couple of dependent packages first.

Install Dependencies on Debian, Ubuntu or Linux Mint

$ sudo apt-get install iptables-dev xtables-addons-common libtext-csv-xs-perl pkg-config

Install Dependencies on CentOS, RHEL or Fedora

CentOS/RHEL 6 requires EPEL repository being set up first (for perl-Text-CSV_XS).
$ sudo yum install gcc-c++ make automake kernel-devel-`uname -r` wget unzip iptables-devel perl-Text-CSV_XS

Compile and Install Xtables-addons

Download the latest xtables-addons source code from the official site, and build/install it as follows.
$ wget http://downloads.sourceforge.net/project/xtables-addons/Xtables-addons/xtables-addons-2.10.tar.xz
$ tar xf xtables-addons-2.10.tar.xz
$ cd xtables-addons-2.10
$ ./configure
$ make
$ sudo make install
Note that for Red Hat based systems (CentOS, RHEL, Fedora) which have SELinux enabled by default, it is necessary to adjust SELinux policy as follows. Otherwise, SELinux will prevent iptables from loading xt_geoip module.
$ sudo chcon -vR --user=system_u /lib/modules/$(uname -r)/extra/*.ko
$ sudo chcon -vR --type=lib_t /lib64/xtables/*.so

Install GeoIP Database for Xtables-addons

The next step is to install GeoIP database which will be used by xt_geoip for IP-to-country mapping. Conveniently, the xtables-addons source package comes with two helper scripts for downloading GeoIP database from MaxMind and converting it into a binary form recognized by xt_geoip. These scripts are found in geoip folder inside the source package. Follow the instructions below to build and install GeoIP database on your system.
$ cd geoip
$ ./xt_geoip_dl
$ ./xt_geoip_build GeoIPCountryWhois.csv
$ sudo mkdir -p /usr/share/xt_geoip
$ sudo cp -r {BE,LE} /usr/share/xt_geoip
According to MaxMind, their GeoIP database is 99.8% accurate on a country-level, and the database is updated every month. To keep the locally installed GeoIP database up-to-date, you want to set up a monthly cron job to refresh the local GeoIP database as often.

Block Network Traffic Originating from or Destined to a Country

Once xt_geoip module and GeoIP database are installed, you can immediately use the geoip match options in iptables command.
$ sudo iptables -m geoip --src-cc country[,country...] --dst-cc country[,country...]
Countries you want to block are specified using two-letter ISO3166 code (e.g., US (United States), CN (China), IN (India), FR (France)).
For example, if you want to block incoming traffic from Yemen (YE) and Zambia (ZM), the following iptables command will do.
$ sudo iptables -I INPUT -m geoip --src-cc YE,ZM -j DROP
If you want to block outgoing traffic destined to China (CN), run the following command.
$ sudo iptables -A OUTPUT -m geoip --dst-cc CN -j DROP
The matching condition can also be "negated" by prepending "!" to "--src-cc" or "--dst-cc". For example:
If you want to block all incoming non-US traffic on your server, run this:
$ sudo iptables -I INPUT -m geoip ! --src-cc US -j DROP

For Firewall-cmd Users

Some distros such as CentOS/RHEL 7 or Fedora have replaced iptables with firewalld as the default firewall service. On such systems, you can use firewall-cmd to block traffic using xt_geoip similarly. The above three examples can be rewritten with firewall-cmd as follows.
$ sudo firewall-cmd --direct --add-rule ipv4 filter INPUT 0 -m geoip --src-cc YE,ZM -j DROP
$ sudo firewall-cmd --direct --add-rule ipv4 filter OUTPUT 0 -m geoip --dst-cc CN -j DROP
$ sudo firewall-cmd --direct --add-rule ipv4 filter INPUT 0 -m geoip ! --src-cc US -j DROP

Conclusion

In this tutorial, I presented iptables/xt_geoip which is an easy way to filter network packets based on their source/destination countries. This can be a useful arsenal to deploy in your firewall system if needed. As a final word of caution, I should mention that GeoIP-based traffic filtering is not a foolproof way to ban certain countries on your server. GeoIP database is by nature inaccurate/incomplete, and source/destination geography can easily be spoofed using VPN, Tor or any compromised relay hosts. Geography-based filtering can even block legitimate traffic that should not be banned. Understand this limitation before you decide to deploy it in your production environment.
Download this article as ad-free PDF (made possible by your kind donation): 
Download PDF

Tips and Tricks to Get the Most out of Your Linux WiFi

https://www.linux.com/learn/tutorials/872372-tips-and-tricks-to-get-the-most-out-of-your-linux-wifi

jack wifi a
Figure 1: Wireless settings.

Regardless of your operating system, wireless can sometimes be a headache. Either you drop a signal, your wireless connections flakes out, your connection is slow, or your wireless device winds up MIA. Either way, there are times you’ll wind up having to troubleshoot or tinker to get the most out of that connection.
Everyone using Linux knows that wireless problems aren’t limited to our favorite open source platform. As with printers, all operating systems can succumb to the woes of wireless. Fortunately, with Linux, there are plenty of ways to prevent or fix the problems.
For those that like to eke out the most power and functionality from their system, I will provide a few tips and tricks specific to wireless connectivity. Hopefully, one of these tips will be exactly what you need to get the most out of your own wireless connection (see Figure 1).
I will be demonstrating these tips using Ubuntu GNOME 15.10 and elementary OS Freya. If you’re using a different distribution, you’ll only need to make minor alterations to the command structure for this to work (such as, su’ing to root instead of using sudo).

Enable Disabled Wireless Device

You may run into this issue. You’ve done something to your desktop or laptop (such as, switch off the wireless with the machine's built-in WiFi adapter on/off switch -- which can save battery) which causes WiFi to show up as disabled in the Network manager. No matter how many times you reboot, wireless simply won’t turn on. Why this happens is the adapter gets hardblocked by rfkill.
If you open a terminal window and issue the command rfkill list and you see your wireless adapter listed as either hard or soft blocked, you’ll need to unblock it. To do this, issue the command rfkill unblock all. This should unblock your wireless adapter from rfkill and allow you to re-enable it (it might automatically re-enable without your interaction). If the wireless adapter doesn’t re-enable after this, reboot the machine and it should be fine.

Force Disable 802.11n

Even though 802.11n offers better data speeds, many older routers simply don’t support it. One way to gain an increase in speeds is to disable the 802.11n protocol on your wireless Linux machine — especially if your machine uses the iwlwifi driver (Intel wireless chips), because that particular driver does a poor job with the 802.11n protocol. To do this, you only need to disable the protocol to gain some speed. Here’s what you need to do:
  1. Open up a terminal window
  2. Find out what driver your wireless card uses with the command lshw -C network
  3. Locate the section driver= and note the name of the driver
  4. Change to super user with sudo su
  5. Issue the command echo "options DRIVER_NAME 11n_disable=1" >> /etc/modprobe.d/DRIVER_NAME.conf (Where DRIVER_NAME is the name of the driver being used)
Note: The above change is permanent. The only way to change it would be to issue the command echo "options DRIVER_NAME 11n_disable=1" >> /etc/modprobe.d/DRIVER_NAME.conf (Where DRIVER_NAME is the name of the driver being used).

Disable Power Management

Some wireless cards support power management. This feature can sometimes get in the way of the card’s connection quality (which also affects connection speeds). If your card happens to support it, you can turn off the power management feature with a simple command:
iwconfig wlp4s0 power off
The problem with the command is that, as soon as you reboot, it will reset to the default on setting. To get around this, you’ll have to create a short script that will run the command at boot. Here’s how:
Create the script (we’ll call it wifipower) with the following contents (you will substitute the name of your wireless card where mine says wlp4s0):
#!/bin/sh
/sbin/iwconfig wlp4s0 power off
Save the script and give it executable permissions with the command chmod u+x wifipower. With the permissions in place, move the file to /etc/init.d and issue the command update-rc.d wifipower defaults. Now the power management feature will turn off at boot. The only caveat to this is ensuring your card supports the feature. If it doesn’t, the power off command will report back to you that the feature isn’t supported.

Set the BSSID

Did you know that the Linux Network Manager rescans the network every two minutes? This can actually cause problems with your wireless connection. If you happen to work with your wireless in the same, familiar locations, you can set the BSSID to the MAC address of your router which will prevent Network Manager from scanning for access points on that particular wireless connection. Here’s how:
  1. Open up the Network Manager (usually found in the system tray of your desktop)
  2. Select the wireless connection you want to work with
  3. Click Edit
  4. In the Wi-Fi tab, click the drop-down associated with BSSID (Figure 2)
  5. Select the MAC address for your router (if it does not appear, you’ll have to locate it on your router and enter it manually)
  6. Click Save
    jack-wifi b
    Figure 2: Setting the BSSID for a wireless connection.

Dual-Boot Blues

If your Linux box dual boots with Windows, you may find that, after booting into Windows, your machine can no longer get an IP address. This situation is most likely caused by the fact the router thinks it already handed an IP address out to the MAC address associated with your network card.
There are a few ways around this issue. What you do will depend on how you use your machine. If you spend the vast majority of your time dual booting at home, you can simply set a static IP address for one of the operating systems. By doing this, the router will not fail to hand out a dynamically assigned IP address to the other operating system.
If you want to set the static address on the Linux side, follow these steps:
  1. Open up Network Manager
  2. Select your wireless connection from the list and click the settings icon (or Edit, depending on your desktop)
  3. Go to the IPv4 section
  4. Select Manual from the Addresses section
  5. Enter the necessary information (Figure 3)
  6. Click Apply
  7. Close Network Manager
jack-wifi c
Figure 3: Setting a static address to avoid dual-booting issues.

If you don’t want to set a static address on either side (or you cannot do so because you’re on someone else’s network), your best bet is to have Windows release the IP address before you reboot into Linux. This is done (within Windows), with the command:
ipconfig /release
Once Windows has released the IP address back to the router, it will assume the MAC address will need an IP address the next time it checks in. Reboot, and then Linux shouldn’t have any problems with wireless.
There are plenty of other wireless situations that call for other solutions, but what I’ve outlined here should go a long way to help you get the most out of your wireless connection on Linux.

Saturday, January 9, 2016

Nmap Command For Network Admins

http://kalitut.blogspot.ca/2015/12/nmap-command-for-network-admins.html

some of the most used Nmap command Linux
Every network admin knows about Nmap every one of them use it or used it.
It’s one of the best, it’s best of the best originally it is a Linux-only utility,
But it was ported to:

Windows, Solaris, BSD variants, HP-UX, OS X, IRIX, AmigaOS

When a software get ported to all those OS it’s a mark for how important that software is,
Whatever you are trying to do as a network admin or a Penetration Tester you will need to work with Nmap one day
What is Nmap ?
Nmap ("Network Mapper") is an open source tool for network exploration and security auditing.
It was designed to rapidly scan large networks, yet it works fine against single hosts.
Nmap uses raw IP packets in novel ways to determine what hosts are available on the network, what services those hosts are offering, what operating systems they are running, what type of packet filters/firewalls are in use, and dozens of other characteristics.
With Nmap you can know:
What computers are running on a local network.
What IP addresses are running on a local network.
What is the operating system of your target machine.
What ports are open on the machine that you just scanned.
Find out if the system is infected with malware or virus.
Search for unauthorized servers or network service on your network.
Find and remove computers which don’t meet the organization’s minimum level of security.

While Nmap is commonly used for security audits, many systems and network administrators, find it useful for routine tasks such as network inventory, managing service upgrade schedules, and monitoring host or service uptime.
To accomplish its goal, Nmap sends specially crafted packets to the target host and then analyzes the responses.
The software provides a number of features for probing computer networks we will try to learn some of the features together
Nmap originally written by Gordon Lyon first release in September 1997 Written in C, C++, Python, Lua

So now after knowing almost everything we need to know about Nmap we will start with the command list.

1) Nmap Scan a single host or an IP address (IPv4)
### Scan a single ip address ###
nmap 192.168.1.1
 
## Scan a host name ###
nmap exmple.com
 
## Scan a host name with more info###
nmap -v exmple.com
The output will show you some interesting open port and the MAC Address


2) Scan multiple IP address or subnet (IPv4)
nmap 192.168.1.1 192.168.1.2 192.168.1.3

## works with same subnet i.e. 192.168.1.0/24
nmap 192.168.1.1,2,3

## You can scan a range of IP address too:
nmap 192.168.1.1-20

## You can scan a range of IP address using a wildcard:
nmap 192.168.1.*

## Finally, you scan an entire subnet:
nmap 192.168.1.0/24

3) Read list of hosts/networks from a file (IPv4)
The -iL option allows you to read the list of target systems using a text file.
This is useful to scan a large number of hosts/networks.
Create a text file as follows:
Your Text file should look like this ( Test.txt )
facebook.com
Yahoo.com
192.168.1.0/24
192.168.1.1/24
10.15.23.7
localhost
Lets say the text file is in tmp
here is your commend

nmap -iL /tmp/test.txt
4) Excluding hosts/networks (IPv4)
When scanning a large number of hosts/networks you can exclude hosts from a scan:

nmap 192.168.1.0/24 --exclude 192.168.1.5
nmap 192.168.1.0/24 --exclude 192.168.1.5,192.168.1.254
OR exclude list from a file called /tmp/exclude.txt

nmap -iL /tmp/scanlist.txt --excludefile /tmp/exclude.txt
5) Turn on OS and version detection scanning script (IPv4)
The results of a scan might determine that a machine is listening on port 80, without knowing its corresponding OS and Web Server version makes the task of attempted compromise a “shot in the dark” methodology.
 NMap solve this dilemma by using OS and Version detection. The following commands:

nmap -A 192.168.1.254
nmap -v -A 192.168.1.1
nmap -A -iL /tmp/scanlist.txt
6) Check if a host/network is protected by a firewall

nmap -sA 192.168.1.254
nmap -sA exmple.com
7) Scan a host when protected by the firewall

nmap -PN 192.168.1.1
nmap -PN exmple.com
8) Scan an IPv6 host/address
The -6 option enable IPv6 scanning. The syntax is:

nmap -6 IPv6-Address-Here
nmap -6 exmple.com
nmap -6 2607:f0d0:1002:51::4
nmap -v A -6 2607:f0d0:1002:51::4
9) Scan a network and find out which servers and devices are up and running
This is known as host discovery or ping scan:

nmap -sP 192.168.1.0/24
Sample outputs:
Host 192.168.1.1 is up (0.00035s latency).
MAC Address: BC:AE:C5:C3:16:93 (Unknown)
Host 192.168.1.2 is up (0.0038s latency).
MAC Address: 74:44:01:40:57:FB (Unknown)
Host 192.168.1.5 is up.
Host nas03 (192.168.1.12) is up (0.0091s latency).
MAC Address: 00:11:32:11:15:FC (Synology Incorporated)
Nmap done: 256 IP addresses (4 hosts up) scanned in 2.80 second
10) Display the reason a port is in a particular state:

nmap --reason 192.168.1.1
nmap --reason exmple.com
11) Only show open (or possibly open) ports :

nmap --open 192.168.1.1
nmap --open exmple.com
12) Show all packets sent and received

nmap --packet-trace 192.168.1.1
nmap --packet-trace exmple.com
13) Show host interfaces and routes
This is useful for debugging (ip command or route command or netstat command like output using nmap)

nmap --iflist
Sample outputs:
nmap --iflist host interfaces and routes


14) Scan specific ports

map -p [port] hostName
## Scan port 80
nmap -p 80 192.168.1.1
 
## Scan TCP port 80
nmap -p T:80 192.168.1.1
 
## Scan UDP port 53
nmap -p U:53 192.168.1.1
 
## Scan two ports ##
nmap -p 80,443 192.168.1.1
 
## Scan port ranges ##
nmap -p 80-200 192.168.1.1
 
## Combine all options ##
nmap -p U:53,111,137,T:21-25,80,139,8080 192.168.1.1
nmap -p U:53,111,137,T:21-25,80,139,8080 server1.exampl.com
nmap -v -sU -sT -p U:53,111,137,T:21-25,80,139,8080 192.168.1.254
 
## Scan all ports with * wildcard ##
nmap -p "*" 192.168.1.1
 
## Scan top ports i.e. scan $number most common ports ##
nmap --top-ports 5 192.168.1.1
nmap --top-ports 10 192.168.1.1
Sample outputs:
nmap --top-ports


15) Scan all your devices/computers for open ports ever

nmap -T5 192.168.1.0/24
16) detect remote operating system?
You can identify a remote host apps and OS using the -O option:

nmap -O 192.168.1.1
nmap -O  --osscan-guess 192.168.1.1
nmap -v -O --osscan-guess 192.168.1.1
Sample outputs:
remote operating system

17) detect remote services (server / daemon) version numbers:

nmap -sV 192.168.1.1
Sample outputs:


18) Scan a host using TCP ACK (PA) and TCP Syn (PS) ping
If firewall is blocking standard ICMP pings, try the following host discovery methods:

nmap -PS 192.168.1.1
nmap -PS 80,21,443 192.168.1.1
nmap -PA 192.168.1.1
nmap -PA 80,21,200-512 192.168.1.1
19) Scan a host using IP protocol ping
nmap -PO 192.168.1.1
20) Scan a host using UDP ping
This scan bypasses firewalls and filters that only screen TCP:

nmap -PU 192.168.1.1
nmap -PU 2000.2001 192.168.1.1
21) Find out the most commonly used TCP ports using TCP SYN Scan

### Stealthy scan ###
nmap -sS 192.168.1.1
 
### Find out the most commonly used TCP ports using  TCP connect scan (warning: no stealth scan)
###  OS Fingerprinting ###
nmap -sT 192.168.1.1
 
### Find out the most commonly used TCP ports using TCP ACK scan
nmap -sA 192.168.1.1
 
### Find out the most commonly used TCP ports using TCP Window scan
nmap -sW 192.168.1.1
 
### Find out the most commonly used TCP ports using TCP Maimon scan
nmap -sM 192.168.1.1
22) Scan a host for UDP services (UDP scan)
Most popular services on the Internet run over the TCP protocol. DNS, SNMP, and DHCP are three of the most common UDP services. Use the following syntax to find out UDP services:

nmap -sU nas03
nmap -sU 192.168.1.1

Starting Nmap 7.01 ( https://nmap.org ) at 2015-12-15 12:27 EST
Stats: 0:05:29 elapsed; 0 hosts completed (1 up), 1 undergoing UDP Scan
UDP Scan Timing: About 32.49% done; ETC: 01:09 (0:11:26 remaining)
Interesting ports on nas03 (192.168.1.12):
Not shown: 995 closed ports
PORT     STATE         SERVICE
111/udp  open|filtered rpcbind
123/udp  open|filtered ntp
161/udp  open|filtered snmp
2049/udp open|filtered nfs
5353/udp open|filtered zeroconf
MAC Address: 00:11:32:11:15:FC (Synology Incorporated)
 
Nmap done: 1 IP address (1 host up) scanned in 1099.55 seconds

23) Scan for IP protocol
This type of scan allows you to determine which IP protocols (TCP, ICMP, IGMP, etc.) are supported by target machines:

nmap -sO 192.168.1.1
24) Scan a firewall for security weakness
The following scan types exploit a subtle loophole in the TCP and good for testing security of common attacks:
## TCP Null Scan to fool a firewall to generate a response ##
## Does not set any bits (TCP flag header is 0) ##
nmap -sN 192.168.1.254
 
## TCP Fin scan to check firewall ##
## Sets just the TCP FIN bit ##
nmap -sF 192.168.1.254
 
## TCP Xmas scan to check firewall ##
## Sets the FIN, PSH, and URG flags, lighting the packet up like a Christmas tree ##
nmap -sX 192.168.1.254

25) Scan a firewall for packets fragments:

The -f option causes the requested scan (including ping scans) to use tiny fragmented IP packets. The idea is to split up the TCP header over
several packets to make it harder for packet filters, intrusion detection systems, and other annoyances to detect what you are doing.
nmap -f 192.168.1.1
nmap -f fw2.nixcraft.net.in
nmap -f 15 fw2.nixcraft.net.in
## Set your own offset size with the --mtu option ##
nmap --mtu 32 192.168.1.1
26) Cloak a scan with decoys
The -D option it appear to the remote host that the host(s) you specify as decoys are scanning the target network too. Thus their IDS might report 5-10 port scans from unique IP addresses, but they won’t know which IP was scanning them and which were innocent decoys:
nmap -n -Ddecoy-ip1,decoy-ip2,your-own-ip,decoy-ip3,decoy-ip4 remote-host-ip
nmap -n -D192.168.1.5,10.5.1.2,172.1.2.4,3.4.2.1 192.168.1.5
27) Scan a firewall for MAC address spoofing:
### Spoof your MAC address ##
nmap --spoof-mac MAC-ADDRESS-HERE 192.168.1.1
 
### Add other options ###
nmap -v -sT -PN --spoof-mac MAC-ADDRESS-HERE 192.168.1.1
 
### Use a random MAC address ###
### The number 0, means nmap chooses a completely random MAC address ###
nmap -v -sT -PN --spoof-mac 0 192.168.1.1
28) How to save output to a text file
The syntax is:
nmap 192.168.1.1 > output.txt
nmap -oN /path/to/filename 192.168.1.1
nmap -oN output.txt 192.168.1.1

Those are the most important commend for NMAP
but those days many want thing to be more simple easy just a click and it scan , for that we have Zenmap
Zenmap is the official Nmap Security Scanner GUI. It is a multi-platform (Linux, Windows, Mac OS X, BSD, etc.) free and open source application which aims to make Nmap easy for beginners to use while providing advanced features for experienced Nmap users.
you can download it from here Link

Hope you found what you want here , leave a comment let me know what you need i will do my best to help
and keep in mind learning the commend lines is very important sometime you just have to deal with it without a GUI Scanner.