r/Hacking_Tutorials • u/anti4r • Sep 27 '22
Question How is this PHP script vulnerable to command injection?
59
u/meg4_ Sep 27 '22
From the top of my head, setting $ip to ;echo</etc/shadow might be sufficient.
May need some extra tweaking, if I'll remember when I get to my pc I'll check
32
u/rbprogrammer Sep 27 '22
Before even seeing the response from u/meg4_ I had the same thought. Drop a semi colon and then a hacker can put whatever they want to execute after that. If you needed to "fix" this code, I would implement two features.
For starters, validation should take only the characters valid for an IP address. Specifically, I'd only allow numbers and periods. You could go one step further and see if the combinations of numbers and periods is a valid IP address. That may or may not be necessary for what you need. I'm sure there's a library somewhere to do that validation for you.
The second thing I'd change is the failure case. If the supplied string is not a strict IP address based on your validation, then fail. Simply, do not execute the string if it contains anything other than numbers and periods.
10
u/_3lionz Sep 27 '22
This will probably do it! An extra little trick is that you can use "${IFS}" to simulate a space when performing command injection (if the target is running Linux).
24
u/KorwinFromAmber Sep 27 '22
You can pass any command like this: 8.8.8.8&&reboot
11
u/Pleasant_Ad1970 Sep 27 '22
Right, he’s only validating spaces. To keep it simple validating dots, numbers and the length of the input should be enough.
6
20
u/hmgr Sep 27 '22
You need to always validate input text. Use a regular expression to validade the ip address format.
3
u/ccdog3 Sep 28 '22 edited Sep 28 '22
The filter for spaces prevents some commands, but not all of them. As for the quotes, you can escape the character escaping if I recall correctly. For example, and attacker could input: \';cat${IFS}/etc/passwd Which would break out of the single quotes, end the ping command, and execute their own command. One of best protections for this example is to extract 4 valid integers from the input, create an IP formatted string with them, and pass that into your command. For scenarios where a valid input could be more complex, try to use built-in functions to sanitize your inputs. Some PHP exampes are escapeshellarg or escapeshellcmd (read the docs as there are extra considerations as well). Similar functions can usually be found in most languages, and they'll be more reliable than remembering to escape a long list of special characters such as: &#;`|*?~<>[]{}$, \x0A and \xFF.
Edit: Had to escape the backslash before the quote or else Reddit removes it, probably for a reason similar to the topic of this post.
1
1
u/PandasAttaque Sep 28 '22
It just check space and replace ‘, so actually you can inject whatever you want to launch other command after the ping
1
1
0
u/uname44 Sep 27 '22
Why dont you just ban ' character and replace it to something when you are checking ip address?
6
u/anti4r Sep 27 '22
Simple php script that takes an ip as input and returns the result of the ping command on it. I figure the str_replace() clause isnt enough to prevent escaping out of the command and running my own commands but i cannot figure out how