nmap command to scan TCP/UDP ports

Nmap, short for Network Mapper, emerges as a command-line tool capable of scanning networks by sending packets and analyzing the responses. It’s particularly adept at identifying open ports and services running on a target system.

Scanning TCP Ports

Nmap’s TCP port scanning is robust. For instance, scanning ports 1 to 100 on a target:

nmap -p 1-100 <target>

To focus on specific ports, say 80, 443, and 8080:

nmap -p 80,443,8080 <target>

Or a comprehensive scan across all TCP ports (1 to 65535):

nmap -p- <target>

Scanning UDP Ports

UDP port scanning differs due to the protocol’s connectionless nature. Scanning UDP ports 1 to 100:

nmap -sU -p 1-100 <target>

For specific UDP ports, e.g., 53 and 161:

nmap -sU -p 53,161 <target>

Scanning Both TCP & UDP ports

nmap -sU -sT -p 53 <target>

or

`nmap -sUT -p 53 <target>`

Quick way to add swap – Ubuntu

 

One of the quickest way to avoid out of memory errors is by adding some swap space. Swap is an area on hard drive where operating system can temporally store data that it can no longer hold in RAM.

Use following way to add additional swap space in Ubuntu:

First check about swap space with free command:

free -m

Lets say if we need 10GB of swap, for this first create a 10GB file with following command:

sudo fallocate -l 10G /swapfile

View details about created swapfile:

ls -hl /swapfile

Output:

-rw-r–r– 1 root root 10G May 19 06:54 /swapfile

Change permissions of swapfile:

sudo chmod 600 /swapfile

Set swap space with following command:

sudo mkswap /swapfile

Check for new swap space:

sudo swapon -s

Output:

Filename                Type        Size    Used    Priority
/swapfile                               file        10485756    2700    -2

Make swap file permanent by adding it in /etc/fstab

/swapfile none swap sw 0 0

Lighttpd both http and https

 

We can configure both http & https on lighttpd webserver.

Use following configuration to serve both http & https requests:

server.document-root = "/var/www"
server.upload-dirs = ( "/var/cache/lighttpd/uploads" )
accesslog.filename = "/var/log/lighttpd/access.log"
server.errorlog = "/var/log/lighttpd/error.log"
server.pid-file = "/var/run/lighttpd.pid"
server.username = "www-data"
server.groupname = "www-data"
server.port = 80

#Configuration for https
$SERVER["socket"] == ":443" {
  ssl.engine = "enable"
  ssl.pemfile = "/etc/lighttpd/certs/www.example.com.pem"
}

In above configuration http://www.example.com.pem file should contain both the private key and the certificate.

After updating lighttpd.conf file restart lighttpd:

$ service lighttpd restart

After restarting both http & https should work for your host.

iptables start on boot – Ubuntu/Debian

 

By default Ubuntu/Debian OS wont load iptable on boot.

So to load iptables on boot use follow procedure:

Save iptable conf with iptables-save command:

iptables-save > /etc/iptables.conf

Next, open file with name /etc/network/if-up.d/iptables and add following content:

#!/bin/sh
iptables-restore < /etc/iptables.conf

Change permission file /etc/network/if-up.d/iptables as executable with following command:

chmod +x /etc/network/if-up.d/iptables

Now, iptables will restored on boot.

Whenever you change iptable rules don’t forgot to update iptables.conf file. Use following command to update iptables.conf file:

iptables-save > /etc/iptables.conf

Python – ImportError: No module named memcache

While trying to connect memcached using python I got following error:

ImportError: No module named memcache

Since I imported memcache & it’s not available in my server I got the error message saying that “No module named memcache”.

To resolve this issue we need to install python-memcache module.

Use following command to install python-memcache:

$ pip install python-memcache

If  your OS is Ubuntu/Debian following command also works for you:

$ apt-get install python-memcache

After installing python-memcache we can seamlessly connect to memcached.

fatal error: Python.h: No such file or directory

While trying to install some python module I got following exception:

fatal error: Python.h: No such file or directory

Reason for exception:

If you haven’t properly installed the header files and static libraries for python dev this issue may occur.

To resolve this issue install python-dev module by using following command:

$ sudo apt-get install python-dev

Linux – Ping multiple hosts using fping

 

fping is a ping like program which uses the Internet Control Message Protocol (ICMP) echo request to determine if a host is up.

With fping we can ping multiple hosts at a time. Use following command to ping multiple hosts:

$ fping -f IPsFiles.txt 

where in IPsFiles.txt I have multiple hots.

Below are some other examples of fping:

To ping a host/IP:

$ fping 192.168.2.2

Output:

192.168.2.2 is alive

$ fping 192.168.2.22

Output:

192.168.1.22 is unreachable

To ping range of IP’s:

$ fping -g 192.168.2.0/24

To list only unreachable hosts/IP’s

$ fping -q -u -f IPsFiles.txt

where -q is for Quiet mode and -u is to list only unreachable targets.

To list only reachable hots/IP’s

$ fping -q -a -f IPsFiles.txt

where -a is to list only alive hots.

ssh disable warning messages – Linux/Ubuntu

To disable ssh warning message we need to add LogLevel to quiet in $HOME/.ssh/config file.

LogLevel=quiet

We can also diable warning messages with ssh command as shown below:

$ ssh -o LogLevel=quiet 192.168.1.10

With following example we can also disable Host Key Checking, set known hosts file to null and disable logging:

$ ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o LogLevel=quiet 192.168.1.20

ssh remove offending key – Linux/Ubuntu

 

ssh-keygen command is used to remove offending key entries from .ssh/known_hosts

Syntax to remove offending key of a host:

$ ssh-keygen -R hostName/IP_Address

Example 1:

$ ssh-keygen -R test.example.com

Above example will remove ssh key associated with hostname test.example.com

Example 2:

$ ssh-keygen -R 192.168.1.10

This example will remove ssh key associated with IP Address 192.168.1.10

How to clear swap space – Ubuntu/Linux

 

To clear swap space in Ubuntu/Linux we need to use swapoff command as shown below:

$ swapoff -a

Above command will turn of swap completely.

Before running swapoff command you need to have enough free RAM available, since swapoff command will move all swap data to RAM.

If you wont have enough free RAM this command may make your system unstable.

Again to turn on swap you need to use swapon command ash shown below:

$ swapon -a

You need to have root/sudo permissions to run above commands.