Day 6 OSCP | Bash Tricks, Redirection and Piping

Reading Time: ( Word Count: )

December 11, 2020
Nextdoorsec-course

Let me show you some tricks on the Bash or Z shell that will help you with productivity.

You have probably come to the idea that Bash maintains a history of the commands you type, by default it keeps 1000 lines of code. You can view it on the terminal settings, under the “Behavior” tab, it should say “History size (in lines)”.

Now let’s get to the practicality. After we’ve typed plenty of commands, let’s explore the “history” command.

  ┌──(kali㉿kali)-[~]      └─$ history 1 echo $$ 2 exit 3 echo $globalvar 4 env 5 clear 6 sudo traceroute -I nextdoorsec.com 7 sudo traceroute -T nextdoorsec.com 8 sudo traceroute -U nextdoorsec.com ...

 

You can see that each command is numbered, so rather than retyping them, we can use the history expansion facility to rerun them easily by making use of the number.

Type the “!” character followed by the line number to execute the command.

  ┌──(kali㉿kali)-[~]      └─$ !3 echo $globalvar Global Var

 

To rerun the last executed command type “!!“.

  ┌──(kali㉿kali)-[~]      └─$ !! echo $globalvar Global Var

 

By default, the command history is saved to the .bash_history file in the user home directory. Two environment variables control the history size: HISTSIZE and HISTFILESIZE.

HISTSIZE controls the number of commands stored in memory for the current session and HISTFILESIZE configures how many commands are kept in the history file. These variables can be edited according to our needs and saved to the Bash configuration file (.bashrc) that we will explore later.

Another useful trick is pressing CTRL + R, just like searching for a word on a page with CTRL + F, but this time in our history. It’s called reverse-i-search facility. The first result will be the most recent command containing the letter/word you’ve entered.

  ┌──(kali㉿kali)-[~]    (reverse-i-search)`h': history

 

What are redirection and piping?

 

STDIN, STDOUT and STDERR

Every program run from the command line has three data streams connected to it that serve as communication channels with the external environment. These streams are defined as follows:

 Stream Name  Description
 0 Standard Input (STDIN)  Data fed into the program
 1 Standard Output (STDOUT)  Output from the program (defaults to terminal)
 2 Standard Error (STDERR)  Error messages (defaults to terminal)

 

To say it the simplest way: redirection is used to redirect the Standard Output, Standard Input or Standard Error to either a file or stream, for example, ls > output.txt. (Note – if output.txt exists, it’ll be overwritten and it’s non-reversible)
Normally it would show/print the result/output to the terminal, but now we’ve redirected the Standard Output to the output.txt file, using the > operator.

  ┌──(kali㉿kali)-[~]      └─$ ls > output.txt
  ┌──(kali㉿kali)-[~]      └─$ cat output.txt Desktop Documents Downloads Music notes output.txt Pictures Public Templates Videos

 

We can also use redirection the “other way”. To send data the “other way”, which is from a file, we use the < operator. In the example below, we redirect the “wc” command’s Standard Input with data originating directly from the file we generated in the previous example. “wc -m” counts characters in the file.

  ┌──(kali㉿kali)-[~]      └─$ wc -m < output.txt 84

 

Let’s redirect the Standard Error while using the file descriptor numbers (the red numbers on the table above) on our command line. We’ll redirect the error message to a file, by sticking the stream number 2 to the “>” operator.

  ┌──(kali㉿kali)-[~]      └─$ cat file.txt cat: file.txt: No such file or directory
  ┌──(kali㉿kali)-[~]      └─$ cat file.txt 2> error.txt
  ┌──(kali㉿kali)-[~]      └─$ cat error.txt cat: file.txt: No such file or directory

Piping

Pipes ( | ) are used to pass the output to another program or utility’s input. The pipe takes everything written to standard output from cat error.txt and sends it to the standard input of wc -m which then will print out to the standard output unless otherwise specified.

  ┌──(kali㉿kali)-[~]      └─$ cat error.txt cat: err.txt: No such file or directory
  ┌──(kali㉿kali)-[~]      └─$ cat error.txt | wc -m 41
  ┌──(kali㉿kali)-[~]      └─$ cat error.txt | wc -m > characters.txt
  ┌──(kali㉿kali)-[~]      └─$ cat characters.txt 41
Aydan Arabadzha

Aydan Arabadzha

Author

Aydan, a cybersecurity ace and AI visionary, thrives on the frontlines of offensive security. His passion birthed NextdoorSec, a groundbreaking cybersecurity firm. A relentless pioneer, Aydan is persistently pushing boundaries, shaping the future of the digital world one byte at a time.

Other interesting articles

Automated vs Manual Penetration Testing

Automated vs Manual Penetration Testing

Pentesting is largely divided into two methodologies: Automated vs Manual Penetration Testing. Both have ...
8 Steps in Penetration Testing You Should Know

8 Steps in Penetration Testing You Should Know

Mastering the art of penetration testing has become a critical ability for security experts to combat cyber ...
Spear Phishing vs Whaling: What is the Difference

Spear Phishing vs Whaling: What is the Difference

Spear phishing is a particularly devious type of phishing assault in which the individual targeted plays a ...
1 Comment
  1. null

    Appreciate your content!

    4
    Reply

Submit a Comment

Your email address will not be published. Required fields are marked *