LabManual-2
LabManual-2
Sanjay K. Sahay
Dept. of CSIS, BITS Pilani, K.K. Birla Goa Campus
0.1 Theory
As networks grow larger and more complex, administrators and power users require more ad-
vanced tools for secure remote administration, file transfers, network scanning, automated re-
quests, and firewall configuration. This chapter explores a variety of powerful commands that
address these needs. However, some of these commands (especially those that modify firewall
rules or alter system connectivity) can disrupt your network or introduce security vulnerabili-
ties if misused. Therefore, while we encourage experimentation for learning, always proceed
with caution and only on systems or networks where you have proper authorization.
Below are the advanced commands we will cover in detail:
• wget
• curl
• nc (netcat)
• ssh
• nmap
• iptables
We will highlight benign commands that only gather information and pose minimal risk, as
well as risky commands that can change configurations or break connectivity.
• Safe / Benign: Downloading or uploading files when you already have SSH access is
typically safe, as long as you have the correct permissions on the remote server.
• Risky: Overwriting critical configuration files on the server or transferring sensitive data
without verifying the remote host’s authenticity can lead to security or stability issues.
1
Usage and Examples:
• scp localfile.txt user@host:/home/user/ Copies localfile.txt to
/home/user/ on the remote host. Prompted for user’s password or private key
passphrase if necessary.
• scp -r mydir user@host:/home/user/ Recursively copies all files and sub-
directories within mydir to the remote host.
• sftp user@host Opens an interactive SFTP session. You can navigate the remote
filesystem using cd or ls, then upload (put) or download (get) files.
• sftp -i /.ssh/mykey_rsa user@host Uses a specified private key (mykey_rsa)
instead of a password. Ensure you keep private keys secure and never share them.
Additional Tips:
• scp and sftp rely on SSH, so any issues with SSH configuration (e.g., firewall rules or
missing keys) will affect these commands.
• For automated transfers (like cron jobs), use SSH key-based authentication to avoid stor-
ing passwords in scripts.
0.1.2 wget
Purpose: wget is a non-interactive command-line utility for retrieving files over HTTP,
HTTPS, and FTP. It’s ideal for scripting, mirroring websites, and performing unattended down-
loads.
Key Features:
• Non-interactive: Continues even if you log out.
• Recursive Download: Mirrors entire directories or websites.
• Resume Option: Automatically resumes partial downloads if the server supports it.
Common Options:
• -O <filename>: Save the downloaded file under a specific name instead of the de-
fault.
• -r: Recursive downloading of linked pages or files.
• -k: Convert links in downloaded HTML pages for local offline viewing.
• -c: Resume an interrupted download (if the server supports partial content requests).
2
Examples:
0.1.3 curl
Purpose: curl is an extremely versatile data transfer tool supporting HTTP(S), FTP(S),
SCP, SFTP, and more. It’s commonly used for REST API testing, retrieving files, or debugging
web servers.
• Safe / Benign: Basic GET requests, retrieving public URLs, or performing read-only
API requests are generally safe.
• Risky: Posting sensitive data to the wrong endpoint or ignoring SSL certificate checks
(-k) can lead to security risks.
Common Options:
• -X <method>: Specify the HTTP method (GET, POST, PUT, DELETE, etc.).
Examples:
3
0.1.4 nc (netcat)
Purpose: nc, also known as netcat, is called the “Swiss army knife of networking.” It can
open TCP/UDP connections, listen on ports, transfer files, and even perform simple port scans.
• Risky: Port scanning hosts without permission, creating backdoors, or transferring criti-
cal files in unsecured environments.
Common Usage:
0.1.5 ssh
Purpose: ssh (Secure Shell) enables secure remote logins, command execution, and tunnel-
ing. It replaces older, insecure protocols like telnet.
• Safe / Benign: Read-only activities on a remote system you are authorized to access.
4
Key Features:
• Encrypted Connections: Prevents eavesdropping.
• Tunneling/Port Forwarding: Forward local or remote ports through the SSH tunnel
securely.
Examples:
• ssh user@host Prompt for password (or key passphrase) to log into host.
0.1.6 nmap
Purpose: nmap is a powerful network scanner used to discover hosts, open ports, running
services, operating system details, and potential vulnerabilities.
• Risky: Scanning networks without permission is often illegal or against usage policies.
High-intensity scans can appear hostile to intrusion detection systems.
• -sT (Connect Scan): Uses the OS’s connect call. Easier to detect in logs.
• -sV (Version Detection): Attempts to identify service versions (e.g., Apache 2.4.29).
• -A: Enables several advanced features including OS detection, version detection, and
default script scanning.
Examples:
• nmap -sS <target_host> Performs a stealthy SYN scan of <target_host>.
• nmap -A -T4 <target_host> Comprehensive scan (-A) with faster timing (-T4)
on a reliable network.
5
0.1.7 iptables
Purpose: iptables configures the Linux netfilter firewall, allowing granular control of
inbound and outbound packets. It is extremely powerful but can also break network access if
misconfigured.
• Risky: Changing default policies to DROP, removing essential ACCEPT rules, or incor-
rectly forwarding ports can immediately lock you out of the system.
Key Concepts:
• Tables:
Safe Viewing:
sudo iptables -L -v
sudo iptables -t nat -L -v
sudo iptables -S
Example Administration:
By first adding an ACCEPT rule for SSH (dport 22) and then setting the default INPUT
policy to DROP, only explicitly allowed ports remain open. Use caution: a single error can
block remote administration.
6
0.2 Lab Experiments: Advanced Commands
Below are a series of advanced experiments to deepen your understanding. Each experiment
specifies its Objective, the Expected Outcome, and step-by-step Instructions. Always verify
you have permission before scanning, transferring files, or modifying firewall rules on any
network or system.
Confirm you do not need a password unless your key has a passphrase.
• Expected Outcome:
– You successfully transfer files to/from the remote server.
– scp and sftp both use SSH, but sftp offers an interactive interface while
scp is non-interactive (better for scripts).
– Key-based authentication simplifies automated transfers, avoiding stored pass-
words.
7
(b) Viewing HTTP Headers with curl:
curl -I https://www.example.com
Observe the HTTP status code (200 OK, etc.), server type, and any other head-
ers (e.g., Date, Content-Length).
(c) REST API Request: If you have a local or test API endpoint, try:
curl -X POST \
-d ’{"name":"Alice"}’ \
-H "Content-Type: application/json" \
https://api.example.com/users
• Tools: nc (netcat)
• Objective: Use nc to create an ad-hoc server (listener) on one machine and connect
from another, transferring files or messages in real time. This demonstrates basic
TCP usage and nc’s flexibility.
• Instructions:
(a) Set Up a Listener (Machine A):
nc -l -p 5000 > received.txt
Now Machine A is listening on TCP port 5000 and redirects all incoming data
into received.txt.
(b) Send Data (Machine B):
cat data.txt | nc <MachineA_IP> 5000
8
• Tools: nmap
• Objective: Investigate open ports and running services on a test machine or lab
environment. Understand how to interpret scan results.
• Instructions:
(a) Basic SYN Scan:
nmap -sS <target_host>
Attempts to identify the service versions (e.g., SSH 7.9p1, Apache 2.4) and
guess the OS (e.g., Linux kernel 5.x).
(c) Nmap Scripting Engine (Optional):
nmap --script=http-title -p 80 <target_host>
• Tools: iptables
• Objective: Carefully add a rule to allow SSH or HTTP inbound, then remove it.
Observe how changes affect connectivity. Perform this only on a test machine or
virtual environment to avoid lockouts.
• Instructions:
(a) View Existing Rules (Safe):
sudo iptables -L -v
Check which ports or services are currently allowed or blocked. Note the de-
fault policy (ACCEPT/DROP).
(b) Add an Allow Rule (Optional):
sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT
9
This explicitly allows inbound SSH connections on port 22.
(c) Verify SSH Connectivity: From another machine, attempt:
ssh user@<test_machine_ip>
Assuming the rule you added is the first in the chain. Verify it’s gone by listing
rules again.
(e) Optional Default Policy Change:
sudo iptables -P INPUT DROP
This sets all inbound traffic to DROP by default. You should ensure critical
ports (like SSH) have ACCEPT rules first, or you risk locking yourself out.
• Expected Outcome:
– You see how a new rule appears in the INPUT chain using sudo iptables
-L.
– SSH remains accessible due to the ACCEPT rule. Removing that rule or setting
the default policy to DROP could block SSH unless properly configured.
– You gain practical experience managing basic firewall rules, understanding
how changes can immediately impact connectivity.
• Warning: Improper iptables configuration can break network access. Always
have a direct console or backup method to revert changes if you become locked out.
Note: Remember that many of these advanced commands can significantly impact your system
or network if misused. Always test in a safe, controlled environment, maintain proper backups
of critical configurations, and only scan or modify settings on networks you are authorized to
access. Practicing good documentation and change management will help you avoid accidental
outages or security breaches.
10