Live data from Hacker News

Pi-hole Remote Code Execution

natedotred.wordpress.com

11–20 of 46 posts

Re: Pi-hole Remote Code Execution

#11

> exec("sudo ... " . $user_input_string ...) Wow, this is always a mistake and a huge one. exec() is dangerous, exec calling with sudo more so, and should never be used in conjunction with unprivileged user input like this. Granted a weak attempt was made to sanitize the user string, but so weak one might wonder if it is Underhanded Code at play here. The big problem with this sort of issue is that it indicates that…

[deleted]

Re: Pi-hole Remote Code Execution

#12
The crazy thing is that there is a correct escaping available: `escapeshellarg()`. With correct escaping, you don't even need to sanitize the user input here - the target command will parse the MAC address itself and report any errors.

The existing code is analogous to building SQL queries using string concatenation and forgetting the mysqli_real_escape_string() call. Really the solution is to use a parameterized interface (e.g. by calling pcntl_fork() + pcntl_exec(), that accepts an array of arguments instead of a string command-line).

Pcntl doesn't seem to have a wrapper for the posix_spawn syscall.

Re: Pi-hole Remote Code Execution

#13
post #10

The dev trusted a regexp to 'validate' user input for a _privileged_ command execution, a function which fails at validating a constant-sized, colon-separated sequence of hex numbers in a string, everything about the input screams structure, and yet it was still half-assed! Although this requires you to authenticate at the web portal, so 'some' sort of trust is necessary to gain this level of access. I believe they e…

Hmm, wonder if this is the type of stuff a static code analysis would pick up like (veracode)

[deleted]

Re: Pi-hole Remote Code Execution

#14

The root of this issue seems to be a regexp function (preg_match()) in PHP coupled with an exec() of a variable that was not properly screened, coupled with an 'sudo' inside of the exec(), e.g.: exec(" sudo pihole -a addstaticdhcp ".$mac." ".$ip." ".$hostname); and/or exec(" sudo pihole -a removestaticdhcp ".$mac); So three places to audit: 1) Regexp's and related complex high-level functions; 2) Calls to exec() 3) U…

The problem is that php expects a string that is then passed to sh -c, which is then parsed by a shell.

Instead php should have an interface that accepts:

    exec(["sudo", "pihole", "-a", "addstaticdhcp", $mac, $ip, $hostname]);
without any shell trickery.

If there's a sudo in this specific like doesn't matter, your reverse shell is going to run as a regular user and after that it only matters low locked down the sudoers configuration is.

Re: Pi-hole Remote Code Execution

#15
post #10

The dev trusted a regexp to 'validate' user input for a _privileged_ command execution, a function which fails at validating a constant-sized, colon-separated sequence of hex numbers in a string, everything about the input screams structure, and yet it was still half-assed! Although this requires you to authenticate at the web portal, so 'some' sort of trust is necessary to gain this level of access. I believe they e…

Hmm, wonder if this is the type of stuff a static code analysis would pick up like (veracode)

Likely would. The `validMAC` function doesn’t just fail silently if the regex fails, which would get past naive signature detections. Oh and feeding user input directly into `exec`…

Re: Pi-hole Remote Code Execution

#16
post #12

The crazy thing is that there is a correct escaping available: `escapeshellarg()`. With correct escaping, you don't even need to sanitize the user input here - the target command will parse the MAC address itself and report any errors. The existing code is analogous to building SQL queries using string concatenation and forgetting the mysqli_real_escape_string() call. Really the solution is to use a parameterized int…

escapeshellarg is an ugly hack, php is passing everything through `sh -c` for no reason.

The pcntl_exec function is the only one that has a reasonable interface, but is way too low level.

php should simply accept this as a valid function call:

    exec(["sudo", "pihole", "-a", "addstaticdhcp", $mac, $ip, $hostname]);

Re: Pi-hole Remote Code Execution

#17

> exec("sudo ... " . $user_input_string ...) Wow, this is always a mistake and a huge one. exec() is dangerous, exec calling with sudo more so, and should never be used in conjunction with unprivileged user input like this. Granted a weak attempt was made to sanitize the user string, but so weak one might wonder if it is Underhanded Code at play here. The big problem with this sort of issue is that it indicates that…

So find the rest and fix them

Re: Pi-hole Remote Code Execution

#18
post #8
post #5

Does this pihole admin interface perform any kind of CSRF protection, or can this be exploites by any random website as long as the victim has a browser tab with a valid session?

Requires a POST that would hit CORS pretty hard

dns rebinding could make it work, thats not the question

Re: Pi-hole Remote Code Execution

#20
post #10

The dev trusted a regexp to 'validate' user input for a _privileged_ command execution, a function which fails at validating a constant-sized, colon-separated sequence of hex numbers in a string, everything about the input screams structure, and yet it was still half-assed! Although this requires you to authenticate at the web portal, so 'some' sort of trust is necessary to gain this level of access. I believe they e…

Hmm, wonder if this is the type of stuff a static code analysis would pick up like (veracode)

[deleted]
Post reply on HN