← All posts

Field notes / Security

When sudo Becomes Root: Understanding apt-get Privilege Escalation

Privilege escalation on Linux often starts with a deceptively simple question:

What am I allowed to run as another user?

On many systems, the answer comes from sudo.

Administrators use sudo to delegate privileged actions without giving users unrestricted root access.

That is the intended model:

Normal user
    ↓
Approved command
    ↓
sudo
    ↓
Privileged operation

The security problem appears when the approved command can do more than the administrator realized.

A program may appear harmless because its normal purpose is legitimate:

Package management
File viewing
Text editing
Archiving
Service management

But if that program can execute another command, launch a shell, invoke an editor, load plugins, or otherwise escape its intended workflow, then the delegated permission may effectively become:

Run arbitrary commands as root

That is the core idea behind many sudo privilege-escalation paths.

apt-get is a good example because it looks like a package-management command.

Under the right sudo configuration, however, its extensibility can become much more powerful.

What sudo Actually Does

sudo allows an authorized user to execute commands as another user, commonly root.

For example:

sudo apt-get update

Conceptually:

User
  ↓
sudo policy check
  ↓
Allowed?
  ↓
Run apt-get as root

The important detail is this:

sudo does not grant privilege to the idea of package management. It grants privilege to a specific executable and whatever behavior that executable permits.

That distinction matters.

The administrator may think:

This user can update packages.

The operating system sees:

This user can execute /usr/bin/apt-get as root.

If apt-get can be influenced into executing additional commands, those commands may inherit the same privilege.

The First Step: Inspect sudo Permissions

A common enumeration step is:

sudo -l

This lists commands the current user may run through sudo.

An output might contain something resembling:

User student may run the following commands:
    (root) NOPASSWD: /usr/bin/apt-get

That means the user can execute:

sudo /usr/bin/apt-get ...

as root without entering a password.

At first glance, that may appear restrictive.

The user cannot directly run:

sudo /bin/bash

But the real question is not:

Can I run bash with sudo?

It is:

Can the permitted program cause another command to run?

That is a much broader question.

Think in Capabilities, Not Command Names

A common mistake when reviewing sudoers is to reason from the program name.

For example:

apt-get = package manager
less    = file viewer
vim     = editor
tar     = archive utility
find    = filesystem search

Those descriptions are accurate.

They are not complete.

Security analysis should instead ask what capabilities the executable provides.

For example:

Can it execute external programs?
Can it spawn a shell?
Can it invoke an editor?
Can it load libraries?
Can it execute hooks?
Can it write arbitrary files?
Can it modify privileged configuration?
Can it run scripts?

A seemingly ordinary command can become dangerous when executed with elevated privileges.

Why apt-get Is Interesting

apt-get is part of Debian-family package-management tooling.

Its normal purpose includes operations such as:

apt-get update
apt-get install package
apt-get upgrade

Those actions legitimately require significant system privileges.

Package management may need to:

Write system files
Modify installed software
Run package scripts
Update configuration
Interact with privileged directories

Because package-management workflows are powerful and extensible, delegating unrestricted apt-get access through sudo deserves careful scrutiny.

The attack surface is not just:

Can the user install packages?

It is:

What execution paths become available when this program runs as root?

Privilege Escalation Through Command Execution

One dangerous pattern occurs when a privileged program provides some mechanism for invoking another command.

Conceptually:

User
  ↓
sudo apt-get
  ↓
apt-get feature / option
  ↓
External command
  ↓
Runs with inherited root privilege

At that point, the security boundary has effectively disappeared.

The user was not granted:

root shell

directly.

But they were granted a program capable of producing the equivalent result.

That is why sudo policy must account for the full behavior of the permitted executable.

The Shell Escape Problem

Many Unix programs were designed for interactive use.

Some provide shell escapes.

An editor might allow:

Run shell command

A pager might allow:

Open interactive shell

A debugger might allow:

Execute process

A package tool might provide:

Hooks
Pre- or post-operation commands
Configuration-driven execution

Normally, those capabilities are features.

Under sudo, they become security-relevant.

The process tree may look like:

user shell
    ↓
sudo
    ↓
apt-get running as root
    ↓
child command
    ↓
root privileges inherited

The weakness lies in the delegation model, not necessarily in the application itself.

This Is Usually a Misconfiguration

It is important to classify this correctly.

If apt-get behaves as documented and supports functionality that can invoke another process, that is not automatically an apt-get vulnerability.

The security flaw is typically:

Unsafe sudo configuration

The program has been given more privilege than the security policy intended.

That distinction matters when reporting.

The finding should not claim:

apt-get is vulnerable.

A more accurate description is:

The current user is permitted to execute apt-get with elevated privileges in a way that allows privilege escalation.

The difference is important.

The vulnerable component is the privilege delegation.

NOPASSWD Makes Exploitation Easier, But Is Not the Root Cause

A sudoers rule may include:

NOPASSWD:

For example:

(root) NOPASSWD: /usr/bin/apt-get

This means the user does not need to authenticate again before invoking the permitted command.

That removes friction.

But NOPASSWD is not necessarily what makes the configuration dangerous.

Even a rule requiring a password can still grant excessive privilege if the user is legitimately allowed to authenticate and execute the command.

Compare:

(root) /usr/bin/apt-get

and:

(root) NOPASSWD: /usr/bin/apt-get

Both can be dangerous if unrestricted apt-get execution leads to arbitrary command execution.

NOPASSWD affects how easily the privilege is invoked.

The core issue is what the delegated program can do.

Command Arguments Matter

sudoers rules can sometimes restrict not only the executable but also how it is invoked.

This matters because:

/usr/bin/apt-get update

is much narrower than:

/usr/bin/apt-get

with arbitrary arguments.

Conceptually:

Broad rule:
sudo apt-get [ATTACKER-CONTROLLED ARGUMENTS]

Narrow rule:
sudo apt-get update

The broader the allowed argument space, the more capabilities the user may be able to reach.

This principle applies far beyond apt-get.

When reviewing sudoers, ask:

Is the executable restricted?
Are arguments restricted?
Can environment variables influence behavior?
Can configuration files influence behavior?
Can referenced files be controlled by the user?

A safe-looking command may become unsafe because of attacker-controlled parameters.

Environment Variables Can Matter

Some privileged programs read environment variables that affect execution.

For that reason, sudo normally sanitizes parts of the environment.

But configuration can change that behavior.

A privilege-escalation review should consider:

Which environment variables survive sudo?
Can the user control program configuration?
Can library loading be influenced?
Can paths be manipulated?

For example, an unsafe PATH configuration can create situations where a privileged program invokes the wrong executable.

Conceptually:

Privileged program runs:
helper

Expected:
 /usr/bin/helper

Attacker influences PATH:
 /tmp/helper

If the privileged process executes the attacker-controlled binary, privilege can cross the boundary.

This is not specific to apt-get.

It is a general privileged-execution principle.

Package Managers Are Privileged by Nature

Package managers deserve additional attention because their normal job already includes modifying sensitive parts of the system.

A package installation may legitimately involve:

Writing into /usr
Writing into /etc
Creating users
Registering services
Running maintainer scripts
Changing permissions

That means even without a direct shell escape, unrestricted package-management privileges can be highly dangerous.

For example, if a user can install arbitrary attacker-controlled packages as root, those packages may contain installation scripts executed with elevated privileges.

Conceptually:

Attacker-controlled package
        ↓
sudo package installation
        ↓
Package script
        ↓
Runs as root

Again, the administrator may think they delegated:

Package installation

But package installation itself is already a privileged code-execution mechanism.

Least Privilege Means Capability-Level Analysis

The principle of least privilege is often summarized as:

Give users only the permissions they need.

That sounds straightforward.

The hard part is defining what a permission actually enables.

Suppose a support technician needs to refresh package metadata.

A rule like:

sudo apt-get

may grant much more than necessary.

The required capability might only be:

Refresh package index

The delegated permission should match that requirement as closely as possible.

Conceptually:

Business requirement
        ↓
Exact required operation
        ↓
Narrow sudo rule

rather than:

Business requirement
        ↓
Powerful general-purpose executable

The latter often creates privilege-escalation opportunities.

Root Is Not the Only Escalation Target

Privilege escalation is often discussed as:

user → root

But sudo can delegate execution as other users too.

For example:

(user2) /some/command

This may permit movement from:

low-privilege account
        ↓
service account

or:

application user
        ↓
deployment user

That intermediate identity may provide access to:

Secrets
SSH keys
Source repositories
Configuration
Services
Automation credentials

Privilege escalation should therefore be understood as crossing an authorization boundary, not merely reaching UID 0.

GTFOBins Is a Reference, Not Magic

During Linux privilege-escalation testing, resources such as GTFOBins are useful because they document legitimate Unix binaries whose functionality can be abused when unusual privileges are assigned.

The important part is understanding what the entry is telling you.

Do not reduce the process to:

sudo -l
   ↓
Search command name
   ↓
Copy command
   ↓
Root

Instead ask:

What capability is being abused?

Why does it inherit privilege?

What security assumption failed?

Would restricting arguments prevent it?

Would removing sudo permission prevent it?

That understanding transfers to programs you have never seen before.

Memorizing one command does not.

The Better Testing Workflow

When sudo -l reveals an allowed binary, a disciplined workflow looks like:

1. Identify the exact sudo rule.
2. Determine which user the command runs as.
3. Determine whether a password is required.
4. Determine whether arguments are restricted.
5. Review the permitted executable's capabilities.
6. Identify shell escapes, hooks, file writes, subprocess execution, or configuration loading.
7. Test the minimum required behavior.
8. Confirm the effective privilege.

The key transition is:

Allowed executable
       ↓
Allowed capability
       ↓
Privilege boundary

That is where the actual analysis happens.

Avoid Unnecessary Destructive Actions

Privilege-escalation testing should prove the security boundary with minimal impact.

Once you demonstrate:

uid=0(root)

or equivalent elevated execution, the finding is usually established.

There is generally no need to:

Change system passwords
Install persistent users
Disable services
Modify security controls
Delete files
Establish persistence

unless the assessment explicitly requires additional validation.

The goal is to prove privilege escalation, not to remodel the server while you are there.

Confirm the Result

Once an elevated execution path is reached, confirm the resulting identity.

For example:

id

may show:

uid=0(root) gid=0(root) groups=0(root)

or:

whoami

may return:

root

That establishes the privilege boundary crossed.

A strong assessment records:

Original user
Permitted sudo command
Mechanism used
Resulting user
Security impact

Why Root Access Is So Significant

Root is the traditional superuser on Linux.

A root process may typically access or modify nearly every part of the host.

Depending on the system, that can include:

Application data
System configuration
Credentials
Password hashes
SSH keys
Logs
Services
Other users' files
Installed software
Network configuration

Root may also provide opportunities to:

Create users
Modify sudoers
Install persistence
Read application secrets
Access service credentials
Tamper with logs
Control running services

That is why a local privilege-escalation finding can transform a limited application compromise into full host compromise.

The Attack Chain Matters

Consider an application vulnerability that gives an attacker a shell as:

www-data

The initial compromise may be constrained.

Conceptually:

Web vulnerability
      ↓
www-data shell
      ↓
Limited permissions

Then enumeration reveals:

www-data may run apt-get as root

Now the chain becomes:

Web vulnerability
      ↓
www-data
      ↓
sudo misconfiguration
      ↓
root

The privilege-escalation flaw magnifies the impact of the original vulnerability.

This demonstrates why attack chains matter.

Individual findings do not exist in isolation.

A Low-Privilege Shell Is Often Only the Beginning

Obtaining a shell during a penetration test is not necessarily the end of exploitation.

It changes the questions.

Before the shell:

How do I reach the host?

After the shell:

Who am I?

What can I access?

What groups am I in?

What sudo permissions exist?

What services are running?

What credentials are stored locally?

Privilege escalation is fundamentally about understanding the local trust model.

The system already has multiple security boundaries.

The attacker is looking for one that was delegated incorrectly.

sudoers Is Security Policy

The /etc/sudoers configuration and related files are not merely convenience settings.

They define authorization.

Conceptually:

Identity
   ↓
sudoers policy
   ↓
Allowed privileged actions

A rule such as:

user ALL=(root) NOPASSWD: /usr/bin/apt-get

is therefore a security decision.

It says:

I trust this user to exercise every capability reachable through this apt-get invocation as root.

That wording sounds much more serious than:

They can run apt-get.

But technically, that is the trust being granted.

Restricting Executables May Still Not Be Enough

Sometimes administrators create wrapper scripts.

For example:

/usr/local/bin/update-packages

and allow:

sudo /usr/local/bin/update-packages

This can be safer than delegating a broad utility directly.

But the wrapper itself must be reviewed.

Ask:

Can the user modify the wrapper?
Does it invoke commands without absolute paths?
Does it accept unsafe user input?
Does it reference user-writable files?
Does it load user-controlled configuration?

A wrapper script is only safer if it actually reduces the capability surface.

Otherwise, it simply moves the vulnerability.

Writable Scripts Create Another Escalation Path

Suppose sudoers allows:

sudo /usr/local/bin/maintenance.sh

and the user can edit:

/usr/local/bin/maintenance.sh

The security model collapses.

Conceptually:

User modifies script
      ↓
sudo executes script as root
      ↓
User-controlled code runs as root

This illustrates another broad rule:

A privileged executable is only as trustworthy as every file that influences its execution.

Permissions on scripts, configuration files, plugins, libraries, and dependencies all matter.

Path Resolution Matters

Consider a privileged script containing:

backup-tool

instead of:

/usr/local/bin/backup-tool

If the execution environment allows the user to influence command resolution, an attacker may be able to substitute another executable.

Conceptually:

Privileged script
      ↓
Search PATH for backup-tool
      ↓
Attacker-controlled directory first
      ↓
Attacker executable runs as root

This is one reason privileged scripts should use predictable environments and absolute command paths where appropriate.

Again, the theme is capability analysis.

The initial sudo rule may look safe.

Everything downstream matters.

Reporting the Finding

A strong report should explain the privilege boundary clearly.

For example:

Low-privileged user
        ↓
sudo permission for /usr/bin/apt-get
        ↓
Permitted program provides command-execution capability
        ↓
Arbitrary command executed with root privileges
        ↓
Full host compromise

Document:

Affected user
sudoers permission
Executable path
Relevant argument restrictions
Privilege-escalation mechanism
Resulting identity
Demonstrated impact

Avoid framing the issue as:

GTFOBins says this command gives root.

That is not the vulnerability.

The vulnerability is the unsafe authorization configuration.

Remediation

The strongest fix is to remove unnecessary privileged delegation.

Remove Unneeded sudo Access

If the user does not require privileged apt-get access, remove the rule.

Restrict the Exact Operation

If a specific package-management task is required, delegate only the narrowest necessary action.

Avoid unrestricted arbitrary arguments where possible.

Prefer Purpose-Built Privileged Operations

A narrowly designed root-owned wrapper can sometimes safely expose one required operation.

The wrapper must itself:

Use fixed commands
Use absolute paths
Reject untrusted arguments
Avoid user-controlled configuration
Remain non-writable by delegated users

Review All sudoers Entries

Search for powerful binaries capable of:

Shell execution
Subprocess execution
Arbitrary file writes
Editor invocation
Plugin loading
Script execution
Configuration injection

Review File Permissions

Ensure users cannot modify:

Privileged scripts
Referenced configuration
Executed binaries
Libraries
Service definitions

Apply Least Privilege

Delegate the required business operation, not a broadly capable program.

Monitor Privileged Execution

Log and review unexpected sudo usage, especially privileged tools rarely required during normal operations.

Why Blocking Shells Is Not Enough

An administrator might attempt to solve privilege escalation by preventing:

/bin/bash
/bin/sh

from being executed with sudo.

That is useful.

It is not sufficient.

A user may still reach command execution indirectly through:

Editors
Interpreters
Package managers
Debuggers
Backup utilities
File-processing tools
Service managers

The real security objective is not:

Prevent sudo bash.

It is:

Prevent unauthorized arbitrary privileged execution.

That is a capability-level problem.

The Bigger AppSec Lesson

Privilege escalation through sudo is often taught as a collection of commands:

Run sudo -l.

Find a binary.

Look it up.

Get root.

That workflow can solve a lab.

It does not explain the vulnerability.

The deeper lesson is about delegated trust.

An administrator intends:

User may perform Task X.

The system implements:

User may execute Program Y with elevated privilege.

Those two statements are only equivalent if Program Y can do nothing beyond Task X.

That assumption is frequently wrong.

Programs are complicated.

They invoke other processes.

They read configuration.

They launch editors.

They load plugins.

They execute hooks.

They write files.

They interpret arguments.

They inherit environments.

When elevated privilege is attached to that complexity, unintended capabilities can cross the security boundary.

That is why the important question is not:

What command can I run with sudo?

The better question is:

What capabilities become mine because this command runs with sudo?

In the case of an unsafe apt-get rule, the administrator may believe they delegated package management.

What they may actually have delegated is root.

And Linux will enforce exactly the permission that was configured, not the permission someone meant to configure.

← Back to the blog