Oops I deleted my bin dir is a fun set of challenges to investigate a “rogue” process which has been deleting files on the system. It has deleted all programs from the bin directories like ps, htop, lsof. All we have left is a shell prompt…

Bash built-ins

In the challenge, the shell we are using is bash. Bash has many “built-ins” which are commands we can run without having to spawn a process and execute a binary i.e they are part of bash itself.

The most useful one right now is help which lists all the bash built-ins and their arguments. Typing help BUILTIN_NAME will give more details about that built-in command.

List of built-ins


job_spec [&]                                                                   history [-c] [-d offset] [n] or history -anrw [filename] or history -ps[1/218]
 (( expression ))                                                               if COMMANDS; then COMMANDS; [ elif COMMANDS; then COMMANDS; ]... [ else COM>
 . filename [arguments]                                                         jobs [-lnprs] [jobspec ...] or jobs -x command [args]
 :                                                                              kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [sigs>
 [ arg... ]                                                                     let arg [arg ...]
 [[ expression ]]                                                               local [option] name[=value] ...
 alias [-p] [name[=value] ... ]                                                 logout [n]
 bg [job_spec ...]                                                              mapfile [-d delim] [-n count] [-O origin] [-s count] [-t] [-u fd] [-C callb>
 bind [-lpsvPSVX] [-m keymap] [-f filename] [-q name] [-u name] [-r keyseq] [>  popd [-n] [+N | -N]
 break [n]                                                                      printf [-v var] format [arguments]
 builtin [shell-builtin [arg ...]]                                              pushd [-n] [+N | -N | dir]
 caller [expr]                                                                  pwd [-LP]
 case WORD in [PATTERN [| PATTERN]...) COMMANDS ;;]... esac                     read [-ers] [-a array] [-d delim] [-i text] [-n nchars] [-N nchars] [-p pro>
 cd [-L|[-P [-e]] [-@]] [dir]                                                   readarray [-d delim] [-n count] [-O origin] [-s count] [-t] [-u fd] [-C cal>
 command [-pVv] command [arg ...]                                               readonly [-aAf] [name[=value] ...] or readonly -p
 compgen [-abcdefgjksuv] [-o option] [-A action] [-G globpat] [-W wordlist] [>  return [n]
 complete [-abcdefgjksuv] [-pr] [-DEI] [-o option] [-A action] [-G globpat] [>  select NAME [in WORDS ... ;] do COMMANDS; done
 compopt [-o|+o option] [-DEI] [name ...]                                       set [-abefhkmnptuvxBCHP] [-o option-name] [--] [arg ...]
 continue [n]                                                                   shift [n]
 coproc [NAME] command [redirections]                                           shopt [-pqsu] [-o] [optname ...]
 declare [-aAfFgiIlnrtux] [-p] [name[=value] ...]                               source filename [arguments]
 dirs [-clpv] [+N] [-N]                                                         suspend [-f]
 disown [-h] [-ar] [jobspec ... | pid ...]                                      test [expr]
 echo [-neE] [arg ...]                                                          time [-p] pipeline
 enable [-a] [-dnps] [-f filename] [name ...]                                   times
 eval [arg ...]                                                                 trap [-lp] [[arg] signal_spec ...]
 exec [-cl] [-a name] [command [argument ...]] [redirection ...]                true
 exit [n]                                                                       type [-afptP] name [name ...]
 export [-fn] [name[=value] ...] or export -p                                   typeset [-aAfFgiIlnrtux] [-p] name[=value] ...
 false                                                                          ulimit [-SHabcdefiklmnpqrstuvxPT] [limit]
 fc [-e ename] [-lnr] [first] [last] or fc -s [pat=rep] [command]               umask [-p] [-S] [mode]
 fg [job_spec]                                                                  unalias [-a] name [name ...]
 for NAME [in WORDS ... ] ; do COMMANDS; done                                   unset [-f] [-v] [-n] [name ...]
 for (( exp1; exp2; exp3 )); do COMMANDS; done                                  until COMMANDS; do COMMANDS; done
 function name { COMMANDS ; } or name () { COMMANDS ; }                         variables - Names and meanings of some shell variables
 getopts optstring name [arg ...]                                               wait [-fn] [-p var] [id ...]
 hash [-lr] [-p pathname] [-dt] [name ...]                                      while COMMANDS; do COMMANDS; done
 help [-dms] [pattern ...]                                                      { COMMANDS ; }

Challenge 1 - print the current working directory

Solution

The `pwd` built-in shows the *p*resent *w*orking *d*irectory

Challenge 2 - List all files in the current working directory

We can not use ls as it is gone… Which built-in can help us?

Solution

echo *

This works because echo prints all its arguments to stdout. The * character is a wildcard character which makes bash perform glob expansion. This will expand the * to match all files in the directory.

Challenge 3 - Display contents of a file

No cat! We can still use echo to print stuff to stdout. How do we pass the file contents to echo?

Solution

echo "$(<file_name)"
  • <file_name redirects the file contents to stdin.
  • $(<file_name) is command substitution which means that bash executes the command in a sub-shell and substitutes the command with stdout of the command
  • "$(<file_name)" - we wrap this in double quotes to prevent bash from expanding newlines and spaces
  • Finally, we pass the whole string to echo

Challenge 4 - Print the name of the running processes

We can not use ps. /proc can be used as it is just a file system which can be read similar to how we displayed file contents earlier

Solution

for f in /proc/*/cmdline; do echo $(<$f); done
  • /proc has directories for each pid. In the pid directory for each process, there will be files for:
    • cmdline: this shows the command line arguments passed by Linux to the process
    • comm: name of the process
    • cwd: current working directory of the process i.e where it was started
    • exe: symlink to the actual location of the binary being executed by the process
    • status: More details about the process like memory usage, signal handlers, etc
    • syscall: system call number and argument registers for the system call currently being executed by the process, followed by the values of the stack pointer and program counter registers
  • /proc/*/cmdline will get cmdline file for all running processes
  • for f in /proc/*/cmdline: run a for loop with loop variable f for all the args in /proc/*/cmdline
  • echo $(<$f): echo the value in $f

This is a nice combination of a one line for loop, globbing, command substitution and input redirection.

Challenge 5 - Kill the rogue process!

Now that we know which process is causing havoc, it is time to kill it before it does more damage!

Solution

kill -9 PID
  • kill is a bash built-in. It passes a signal to the process. In this case, we need to pass the SIGKILL signal.
  • Running kill -l lists all signals with their numbers.
  • kill -NUM PID or kill -SIGNAME PID will send the signal to the process

Summary

All answers

 # List bash builtins help
 help
 # List current directory
 pwd
 # List files in pwd
 echo *
 # Read file contents
 echo "$(<file_name)"
 # Read cmdline of all running processes in proc
 for f in /proc/*/cmdline; do echo $(<$f); done
 # Kill process
 kill -9 PID