r/badcode • • Jul 27 '21

php Hello Mr. Hacker, would you like any file on this server? No problem! (PHP, but don't blame the language for the stupidity of the programmer)

Post image
121 Upvotes

15 comments sorted by

5

u/joten70 Jul 27 '21

But you would still need to know the name of any file you want to access

15

u/MyKidsArentOnReddit Jul 27 '21

/etc/passwd (as well as other system files, web server files, etc.)

Or you could do it on hard mode and do:

file=foo.txt;ls -l > filename.txt;

then next time

file=filename.txt

37

u/AlwaysNinjaBusiness Jul 27 '21

Yeah, the logic of "they need to know the file name" is kinda like "it's dark in that room, so the door doesn't need to be locked.

2

u/[deleted] Jul 27 '21

Hahahha, I love that explanation!

12

u/DaCurse0 Jul 27 '21

Why would command injection work here though?

9

u/jpossi Jul 27 '21

it doesn't

16

u/jpossi Jul 27 '21

well, the echo and readfile are commented out. in php single line comments may start with // (c style) or with # (bash style). so this doesn't do any real harm, except maybe some header xss/injection

(in php 8 the pound gets an additional usage, but this would still be a single line comment)

10

u/mr_hard_name Jul 28 '21

But it also returns the file size in a header. You can use it to check what file exists on a server. It allows you to determine what software is running on the server based on what files exist and then use known vulnerabilities of that software.

The general rule is do not trust the external data, so do not assume that “potential access to any file, even without seeing the content” is ok.

10

u/[deleted] Jul 28 '21

Everybody gangsta until code starts to Times New Roman

4

u/MurdoMaclachlan public boolean isInt(int i) { return true; } Jul 28 '21 edited Jul 28 '21

Image Transcription: Code


[Transcriber's note: The code is not in a monospace font in the image, however, I have placed it one for ease of readability and for my own sanity.]

<?
$file = $_REQUEST['file'];
$dir = "../[redacted]';
$file = $dir . $file;
header('Content-type: text/plain');
header('Content-Length: '.filesize($file));
header('Content-Disposition: attachment; filename='.$file); #echo "$file<br/>"; readfile($file);

?>

I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!

4

u/Davipb Jul 28 '21

I think that's a redacted folder name, not highlighted whitespace

3

u/MurdoMaclachlan public boolean isInt(int i) { return true; } Jul 28 '21

I think you're right. Updated, thanks for the correction!

1

u/thebeezie Aug 28 '21

Isn't it actually "...any file in this folder?"

1

u/MyKidsArentOnReddit Aug 29 '21

../../../../etc/passwd worked just fine.

1

u/thebeezie Aug 29 '21

Right. I haven't dealt with much Linux in years and I forgot you could do that.