Python Web Hacking: PHP Command Injection | Natas: OverTheWire (Level 9)

John Hammond · Intermediate ·🔐 Cybersecurity ·8y ago

Key Takeaways

Exploits PHP command injection vulnerability on Natas: OverTheWire (Level 9)

Full Transcript

what's up guys John Hammond here coming back at you with another video for the natus war game from over the wire so we just got the password for level 9 and we've got a script that will get the webpage content for us so we don't have to use a web browser and this one looks like it's saying alright natus 9 here's a form you can find words containing and inputs just regular text variable looks like with the variable name needle and it looks like it'll search for us the output is displayed in pre formatted text and we could probably see that in the web browser if we really wanted to let's do that just so you get an idea of what this level looks like natus 9 and thatis 9 and you can see there is another option here to view source code so we can check that out index source HTML that was the name of the file and we can do that in our sublime text stuff if we really wanted to we could decode that if we wanted to or we can just view it in the web browser so it'll handle that a little bit nicely ok so looks like PHP code the notes here with variables with a dollar sign and all key is apparently set to whatever we post to whatever is requested once we communicate with the server and if key is not an empty string it will pass through hmm what is that looks like a function pass through I'll check that out and it's oh it looks like it might be running a command here grep so like we've kind of done in the command line with the bash shell grep whatever we pass to it nice key in dictionary text dictionary text must be a word that's trying to are dictionary that it's looking through what is the PHP pass-through function is it literally just gonna run a command for us yes it will totally just execute commands and display output okay so we could potentially have remote code execution here and we can totally get in the way of it because we're just passing in raw arguments to this thing so if we wanted to find words that say like ten words containing a p-- okay all of these things in dictionary dot txt Apple obviously right what's etc so we can put more in here like how about a period so grep will use the regular expressions here and that will return a period from period will match everything just like regular expressions will match any character so it returned the entire dictionary text file but since we're supplying this as an argument that you can see went through without any like quotes or surrounding it to specify that that as a single argument we could bleed into other arguments here and specify other files that we want to read or change up how this command works let's take a look and let's do that in our shell here or are our sublime text script needle can be app to see if we get responses let's run this we don't want to go to index source anymore we want to be posting it to the actual page and we're getting all of this output so can we return anything with the period huh okay I must be getting some strange characters out so let's go a let's just app huh maybe some maybe some strange characters are in this dictionary that Python does not want to render for us we can change the encoding here coding equals utf-8 that's the magic magic Python syntax to suddenly be okay with Unicode characters will that work for us no okay man that's super annoying let's do this with the verb browser then I suppose at least let's figure out our attack let's say we can actually return anything and if we get a space in between this we can actually ask for another file that we want to look through I think right let's go to natus pass and we want natus 10 right search looks like it returns from the file that things matched in but the naty's password file should get a result it should have matched at some point let's check out what's control f4 natus 9 that is 10 no luck huh was it in etc is it just gonna display anything for us that's not dictionary dot txt it doesn't look like there's any other notion of natus no so maybe that didn't work okay well we still have command execution right can we do period or like anything from nowhere yep nothing nothing from nowhere and use a comment to end the and what we're in and the rest of the line here so it doesn't put in that dictionary dot txt for us but we use the semicolons to note a new command right ok cool so we've got command injection just like that so now we should just be able to cat natus pass natus 10 is that not the is that not the right location a little wire what the heck over the wire log over the wild org what am i doing maddis etc natus web pass oh I'm a fool so the old attack probably would have worked just fine Matis web paths natus 10 yeah oh jeez it totally would have worked just fine except I'm an idiot I'm sorry I didn't even realize that path guys I thought it was the same syntax as the other war games well hey okay that's done for us right there's the attack we can return anything and let's just get something that we know is in here actually no we can just say anything and then we can still use a comment to not include the not include the dictionary text file natus web Pass natus 10 and use the hash tag here so we won't return the rest of the responses from the dictionary text file we commented out the rest of that command here so now we've got our password loaded or displayed out to us and let's use our regular expressions here to carve it out just like that we get our password and we are good to go so natus 9 is done we can put this in the script for natus 10 and just like that we are ready to move on so I hope you guys hope that made sense a little bit of command injection because that that code was just being passed through two real command like a bath shell and executing it and we can just kind of wrap around the arguments or and do some unique things with it and we can load content from other files because that's the way grep works and we can use other bash syntax stuff like comments or other commands with the semicolon to do other interesting things so cool hack cool vulnerability there and that's the real thing command injection is for sure a real problem in web security stuff so okay now they filter on certain certain characters huh let's get start on that in the next video guys thank you guys for watching hope you're enjoying these

Original Description

If you would like to support me, please like, comment & subscribe, and check me out on Patreon: https://patreon.com/johnhammond010 E-mail: johnhammond010@gmail.com PayPal: http://paypal.me/johnhammond010 GitHub: https://github.com/JohnHammond Site: http://www.johnhammond.org Twitter: https://twitter.com/_johnhammond
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from John Hammond · John Hammond · 0 of 60

← Previous Next →
1 Code Commentaries? PHP to JavaScript in Bash and PHP!
Code Commentaries? PHP to JavaScript in Bash and PHP!
John Hammond
2 Tutorials? MySQL connection with PHP and Bash!
Tutorials? MySQL connection with PHP and Bash!
John Hammond
3 Variable Naming in Python! Happy Birthday, Linux! Nokia N900!
Variable Naming in Python! Happy Birthday, Linux! Nokia N900!
John Hammond
4 JavaScript Splits The URL!
JavaScript Splits The URL!
John Hammond
5 HTML Tables in Python!
HTML Tables in Python!
John Hammond
6 HTML, Net Shares, GML!
HTML, Net Shares, GML!
John Hammond
7 Python 08 Programming Style and Comments
Python 08 Programming Style and Comments
John Hammond
8 Python 26 Object Oriented Programming
Python 26 Object Oriented Programming
John Hammond
9 75 Python Tutorials, Out Now!
75 Python Tutorials, Out Now!
John Hammond
10 Batch 14 Mathematical Expressions
Batch 14 Mathematical Expressions
John Hammond
11 Batch 85 Array Append
Batch 85 Array Append
John Hammond
12 Batch 86 Array Count
Batch 86 Array Count
John Hammond
13 Batch 87 Array Index
Batch 87 Array Index
John Hammond
14 Batch 88 Array Insert
Batch 88 Array Insert
John Hammond
15 Batch 89 Array Remove
Batch 89 Array Remove
John Hammond
16 Batch 90 Array Reverse
Batch 90 Array Reverse
John Hammond
17 Python [colorama] 00 Installing on Linux
Python [colorama] 00 Installing on Linux
John Hammond
18 Python [colorama] 09 Cursor Position
Python [colorama] 09 Cursor Position
John Hammond
19 Python [hashlib] 02 Algorithms
Python [hashlib] 02 Algorithms
John Hammond
20 Python 00 Installing IDLE on Linux
Python 00 Installing IDLE on Linux
John Hammond
21 Python [pygame] 11 Rectangular Collision Detection
Python [pygame] 11 Rectangular Collision Detection
John Hammond
22 Python [pygame] 12 Platforming Rectangular Collision Resolution
Python [pygame] 12 Platforming Rectangular Collision Resolution
John Hammond
23 Python [XML-RPC] 01 Research
Python [XML-RPC] 01 Research
John Hammond
24 Python [pyenchant] 03 Personal Word Lists
Python [pyenchant] 03 Personal Word Lists
John Hammond
25 FancyURLopener Authentication and User-Agent [urllib] 03
FancyURLopener Authentication and User-Agent [urllib] 03
John Hammond
26 Python 04: PEP8 Coding
Python 04: PEP8 Coding
John Hammond
27 Python Challenge! 17 COOKIES
Python Challenge! 17 COOKIES
John Hammond
28 Google CTF 2016: Ernst Echidna
Google CTF 2016: Ernst Echidna
John Hammond
29 Google CTF 2016: Spotted Quoll
Google CTF 2016: Spotted Quoll
John Hammond
30 Google CTF 2016: Can you Repo It?
Google CTF 2016: Can you Repo It?
John Hammond
31 Google CTF 2016: No Big Deal
Google CTF 2016: No Big Deal
John Hammond
32 Google CTF 2016: In Recorded Conversation
Google CTF 2016: In Recorded Conversation
John Hammond
33 Homemade CTF Challenge: 01 "Orchestra"
Homemade CTF Challenge: 01 "Orchestra"
John Hammond
34 Homemade CTF Challenge: 02 "Bae's Base"
Homemade CTF Challenge: 02 "Bae's Base"
John Hammond
35 Homemade CTF Challenge: 03 "Web Hunt"
Homemade CTF Challenge: 03 "Web Hunt"
John Hammond
36 Homemade CTF Challenge: 04 "UPX"
Homemade CTF Challenge: 04 "UPX"
John Hammond
37 Homemade CTF Challenge: 05 "The Assumption Song"
Homemade CTF Challenge: 05 "The Assumption Song"
John Hammond
38 Homemade CTF Challenge: 06 "A Brisk Stroll"
Homemade CTF Challenge: 06 "A Brisk Stroll"
John Hammond
39 Homemade CTF Challenge: 06 "I lost my password!"
Homemade CTF Challenge: 06 "I lost my password!"
John Hammond
40 web25 :: Mr. Robot : EKOPARTY CTF 2016
web25 :: Mr. Robot : EKOPARTY CTF 2016
John Hammond
41 web50 : RFC 7230 :: EKOPARTY CTF 2016
web50 : RFC 7230 :: EKOPARTY CTF 2016
John Hammond
42 misc50 : Hidden inside EKO :: EKOPARTY CTF 2016
misc50 : Hidden inside EKO :: EKOPARTY CTF 2016
John Hammond
43 Hack The Vote 2016 CTF: Sander's Fan Club [web100]
Hack The Vote 2016 CTF: Sander's Fan Club [web100]
John Hammond
44 Hack The Vote 2016 CTF Warpspeed [forensics150]
Hack The Vote 2016 CTF Warpspeed [forensics150]
John Hammond
45 Juniors CTF 2016 :: Black Suprematic Square
Juniors CTF 2016 :: Black Suprematic Square
John Hammond
46 Juniors CTF 2016 :: Six Strange Tales
Juniors CTF 2016 :: Six Strange Tales
John Hammond
47 Juniors CTF 2016 :: Lost Code
Juniors CTF 2016 :: Lost Code
John Hammond
48 Juniors CTF 2016 :: Here Goes!
Juniors CTF 2016 :: Here Goes!
John Hammond
49 Juniors CTF 2016 :: Southern Cross
Juniors CTF 2016 :: Southern Cross
John Hammond
50 Juniors CTF 2016 :: Clone Attack
Juniors CTF 2016 :: Clone Attack
John Hammond
51 Juniors CTF 2016 :: Dirty Repo
Juniors CTF 2016 :: Dirty Repo
John Hammond
52 Juniors CTF 2016 :: Hackers Blog
Juniors CTF 2016 :: Hackers Blog
John Hammond
53 Juniors CTF 2016 :: Voting!!!
Juniors CTF 2016 :: Voting!!!
John Hammond
54 Juniors CTF 2016 :: The Good, The Bad and The Junkman
Juniors CTF 2016 :: The Good, The Bad and The Junkman
John Hammond
55 Juniors CTF 2016 :: Stop Thief!
Juniors CTF 2016 :: Stop Thief!
John Hammond
56 Juniors CTF 2016 :: ROFL
Juniors CTF 2016 :: ROFL
John Hammond
57 Juniors CTF 2016 :: Restriced Area
Juniors CTF 2016 :: Restriced Area
John Hammond
58 Juniors CTF 2016 :: Oh SSH!
Juniors CTF 2016 :: Oh SSH!
John Hammond
59 HackCon CTF 2017 TRIVIA and BONUS Challenges
HackCon CTF 2017 TRIVIA and BONUS Challenges
John Hammond
60 HackCon CTF 2017 "Bacche" Challenges
HackCon CTF 2017 "Bacche" Challenges
John Hammond

Related AI Lessons

Security Belongs on the Blueprint
Integrate security into building design to mitigate physical and cyber risks
Medium · Cybersecurity
# A 4-Line HTML File Stole the Admin’s Secret — Intigriti LeakyJar CTF Writeup
Learn how a 4-line HTML file exploited a CSRF vulnerability to steal an admin's secret in the Intigriti LeakyJar CTF challenge
Medium · Cybersecurity
The Digital Gateway to Arabic Cybersecurity
Learn about the importance of language-specific cybersecurity solutions, particularly for Arabic-speaking regions, and how they can enhance digital security
Medium · Cybersecurity
Cybersecurity vs Cloud Computing – Which Career Will Dominate 2026? ☁️
Learn which IT career, cybersecurity or cloud computing, will dominate in 2026 and why it matters for your career choices
Medium · Cybersecurity
Up next
You Think Your Card Declined by Mistake? It Might Be a 2026 Scam
Tolulope Michael
Watch →