tar
Tar command with useful usage flags with their exlanation and use cases.
This guide will walk you through different flags, options of tar commands, where you might need them in active engagement.
Basic Commands
- Creates a new archive
1
tar -cvf backup.tar /var/www
- c β create
- v β verbose
- f β filename
What it does?
Recursively archives files/directories.π΄ Pentesting Use Cases:
π¦ Exfiltrating data cleanly:1
tar -czf loot.tar.gz /etc /var/www
π¦ Packing reverse shell toolkit before upload
π¦ Bundling post-exploitation findings.π‘ On compromised systems, tar is almost always installed β reliable for staging data.
- Extract an archive
1
tar -xvf backup.tar
- x β extract
- v β verbose
- f β filename
What it does?
Extract archive contents.π΄Pentesting Use Cases:
π¦ Deploying uploaded tools
π¦ Unpacking privesc scripts
π¦ Extracting backups found on system
- List Archive Content
1
tar -tvf backup.tar
- t β view
- v β verbose
- f β filename
What it does?
List contents.π΄ Pentesting Use Cases:
π¦ Before extracting unknown archive:1
tar -tf suspicious.tar
π¦ Prevents accidental file overwrite
π¦ Detects malicious archive entries like:1
../../etc/passwd
- Append Files
1
tar -rf backup.tar newfile.txt
What it does?
Add files to end of archive.β οΈ Cannot be used on compressed archives.
π΄ Pentesting Use Cases:
π¦ Add new loot incrementally without rebuilding archive. *** - Update (Append only if newer)
1
tar -uvf backup.tar file.txt
What it does?
Adds file only if newer than archived version.
It DOES NOT replace old copy β it appends a new version.
Archive may contain multiple versions.β οΈ Cannot be used on compressed archives.
π΄ Pentesting Use Cases:
π¦ Incremental backups during long engagement
π¦ Preserving timeline of file changes
Step 2: Create a System User for the Binary
- Create a system user with no login access:
1
sudo useradd -s /usr/sbin/nologin -r -M cloudflared
- What is a system user? A system user is a user account created for running system processes or services, rather than for interactive login by human users.
- What is
nologinaccess? Usingnologinfor the shell means the user cannot log in interactively, which enhances security by preventing potential misuse of the account. - Explanation of
-s,-r, and-M:-s /usr/sbin/nologin: Sets the userβs shell tonologin, preventing interactive logins.-r: Creates a system account, typically with a UID lower than 1000.-M: Prevents the creation of a home directory for the user.
Step 3: Configure the Binary
- Create and edit the configuration file:
1
sudo nano /etc/default/cloudflaredExample configuration:
1 2
# Commandline args for cloudflared, using Cloudflare DNS CLOUDFLARED_OPTS=--port 5053 --upstream https://1.1.1.1/dns-query --upstream https://1.0.0.1/dns-query
- What is
/etc/defaultdirectory for? The/etc/defaultdirectory is used to store configuration files for various system services. These files typically define environment variables and command-line options.
- What is
- Set the appropriate permissions for the configuration file and the binary:
1 2
sudo chown cloudflared:cloudflared /etc/default/cloudflared sudo chown cloudflared:cloudflared /usr/local/bin/cloudflared
- Why set owner and group to
cloudflared? Changing the ownership ensures that only thecloudflareduser has the necessary permissions to read and execute the binary, enhancing security.
- Why set owner and group to
Step 4: Create a Systemd Service
- Create and edit the systemd service file:
- What is
/etc/systemd/system/directory for? This directory is used to store service unit files that define systemd services. These files control how services are started, stopped, and managed on the system.
1
sudo nano /etc/systemd/system/cloudflared.serviceService file configuration:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
[Unit] Description=cloudflared DNS over HTTPS proxy After=syslog.target network-online.target [Service] Type=simple User=cloudflared EnvironmentFile=/etc/default/cloudflared ExecStart=/usr/local/bin/cloudflared proxy-dns $CLOUDFLARED_OPTS Restart=on-failure RestartSec=10 KillMode=process [Install] WantedBy=multi-user.target
Explanation of the directives:
- [Unit]
- Description: Describes the service.
- After: Specifies the service dependencies. The service will start after the listed targets.
- [Service]
- Type: Defines the service type.
simplemeans the service will be considered started right after theExecStartcommand is executed. - User: The user under which the service will run.
- EnvironmentFile: Specifies the file containing environment variables.
- ExecStart: The command to start the service.
- Restart: Defines the restart policy.
on-failurerestarts the service if it fails. - RestartSec: The time to wait before restarting the service.
- KillMode: How the serviceβs processes are killed.
- Type: Defines the service type.
- [Install]
- WantedBy: Specifies the target under which the service should be started.
multi-user.targetmeans the service will start in multi-user mode (default for most servers).
- WantedBy: Specifies the target under which the service should be started.
- What is
Step 5: Enable and Start the Service
Enable the service to start on boot:
1
sudo systemctl enable cloudflared
Start the service:
1
sudo systemctl start cloudflaredCheck the status of the service:
1
sudo systemctl status cloudflared
Additional Information
- What is
multi-user.target?multi-user.targetis a systemd target that signifies the system is in multi-user mode. Itβs similar to the traditional runlevel 3, where multiple users can log in.
This guide provides a comprehensive example of how to install a binary, create a system user, configure the binary, and set up a systemd service to manage the binary. Adjust the specific paths, user names, and configuration options as needed for your particular use case.
