September 03, 2017

Method of executing commands without any PATH in Linux

There are some commands that can be executed without any PATH.

Look at this image.


I removed all PATH. The commands loosing PATH can be executed by commands alone. But the "echo" was not. It is strange.

So, I made a Python code to find commands like that. Perhaps there are more commands,

import subprocess
import os

beforePATH = subprocess.check_output("echo $PATH", shell=True)
beforePATH = beforePATH.decode()
os.environ["PATH"] = "nonodummydummy"

subprocess.check_output("/bin/ls /bin 1> /tmp/lsList1.txt", shell=True)
subprocess.check_output("/bin/ls /usr/bin 1> /tmp/lsList2.txt", shell=True)

fullString = ""
with open("/tmp/executedRst.sh", "w") as rst:
        with open("/tmp/lsList1.txt", "r") as f:
                aLineList = f.readlines()
                
                for i in range(0, len(aLineList)):
                        aData = aLineList[i].split("\n")
                        fullString = fullString + aData[0] + ";\n"
                
        with open("/tmp/lsList2.txt", "r") as f1:
                aLineList = f1.readlines()
                
                for i in range(0, len(aLineList)):
                        aData = aLineList[i].split("\n")
                        fullString = fullString + aData[0] + ";\n"
                
        rst.write("#!/bin/bash")
        rst.write("\n")
        rst.write(fullString)
        
subprocess.check_output("/bin/chmod 755 /tmp/executedRst.sh", shell=True)
result = str(subprocess.check_output("/tmp/executedRst.sh", shell=True))
print(result)
subprocess.check_output("/bin/rm -rf /tmp/lsList2.txt", shell=True)
subprocess.check_output("/bin/rm -rf /tmp/lsList1.txt", shell=True)
subprocess.check_output("/bin/rm -rf /tmp/executedRst.sh", shell=True)
os.environ["PATH"] = beforePATH

Place this anywhere on your Linux and exectes it(=python3 [filename]).

Then, some commands can be seen. in my linux, It was "echo", "kill", "printf", "pwd". and I also luckily found commands "cd", "export", "set". These are not in /usr/bin or /bin.

Later, I found that these are all shell-builtin commands. These are not related to PATH.


The all shell-builtin commands are here(Link)