- Random Access Memory
- Posts
- Bizarre Bash
Bizarre Bash
![main img](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/14c6645d-f625-4315-8bae-a4da257cb3fc/f6f859e2-2902-4605-9a1e-00806478b8ff_2240x1260.png)
init
Hi Y’ALL, this blog post covers the analysis of weird things in bash that is posted on twitter by Alex Chapman. The image below shows the tweet. By creating files with weird names that have a newline in the filename and creating a tar archive on these files and then executing using ./ (bash method) will be executed as a shell script. The irony here is bash will execute any files that have newlin characters in the starting bytes and we will see it in a while.
![tweet](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/f330c666-35ac-4c5b-a438-170e2d49d539/831a5352-8139-40a4-8406-252be4fbe3ec_1800x912.jpeg)
Normal Flow
First of all, lets us try taring normal files instead of files with weird names.
![cmd img](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/24d38f2d-d438-4573-bf84-c7226fd3811d/59d9fa4f-03b0-4985-8af6-50a2f9605d48_1312x632.png)
we use touch to create new empty files such as file1 and file2. we archive the files in tar format. To note, tar is not used for compression until it is used with gzip which will end in .tar.gz. And then, chmod is used to give execution permission and then we execute it by using ./. Bash tells us a sweet message ‘exec file format error’. Of course, we know that tar files are not executables. But bash rules the execution because it is the one that forks and exec the file and creates a child process(new process) and so bash will be the parent. Since bash is the parent of the commands that are run in a bash terminal, we can use strace to see the workflow on how bash executes ./runme.
Now we will get the PID of the bash terminal from ps aux and then use strace to trace syscalls made by the process
![strace img](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/ac9bfa99-0096-4ad1-aabf-ecf40cf00509/a61e3f02-84e2-40cb-830c-72b89e080234_1802x708.png)
![strace-1 img](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/44b69db1-50c1-49bc-b708-0aa0a9d40476/655b6f86-c60e-451a-b560-d37bf72a8f50_2048x1786.png)
Now we will run the tar file and analyze strace output. I have cut-shorted the strace output such that it contains only important information. As we see from the above image, it clones to create a new child process and executes the file runme by calling the exec syscall and it fails. The reason it fails is that the file should be of ELF format to get executed. The error message is returned from the bash, write syscall is called and finally, it prints the error. Now it is all good, but we will try now the weird workflow.
Weird Flow
Okay, now we insert newline characters in starting bytes of the runme file. We just see hexdump and modify the starting bytes to include a newline using radare2.
![weird flow img](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/55a2e60d-8c5c-4f57-bb28-35155473d31f/0f6e6288-adfb-4ffc-a800-68df6812f491_1734x2532.png)
As we see from the above image it ran as a shell script. we will see the strace output to understand it better.
![weird strace output](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/e20235aa-7f85-4ceb-a569-ca9ce611b2aa/735aa532-f657-46df-846b-adb2060e9f65_2048x3090.png)
From the strace output, we can deduce that the bash will create a child process and executes runme using execve syscall, it failed as it is not a ELF format file. But since there is a newline, bash tries to execute it as a shell script. It tries to access /usr/bin/bash and it searches the file file1 (first file’s filename in runme tar). since it can’t find the file1 bin, it shows the message command not found. If we give a valid command as a filename with a newline in it, then it will be executed as a shell script.
exit
Now we will add a valid command with a newline in the filename and execute it. To include a newline in the filename, we use the bash newline feature that is $’\n’. When bash sees this $’\n’ it will be replaced by the newline. You can try it using echo $'\n'.
![final img](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/f90b8df7-e63b-41e5-bea2-06bdc4f97211/948d56d8-d58d-4a9d-9757-bfed7b35712f_2048x1340.png)
In tar, filenames of the file included will be in the starting bytes and if we have a newline in starting bytes, bash will execute its magic by executing the tar file as a shell script and it is not only the tar file can execute, any file can be executed if it contains newline in starting bytes. From the above image, the newline is added in the filename by using $’\n’ and when tared, the filenames will be in the starting bytes 690a(0a is the newline) and finally, bash thinks that it is a shell script and executes. Another way of telling the bash to execute as a shell script is by having the text #!/usr/bin/bash in the starting line, this way of telling the bash to execute is a normal way but newline character was new to me, If it was new to you, share this to others.
Will see ya in the next blog until then happy learning!!.bizz