Day 6 OSCP | Bash Tricks, Redirection and Piping

Reading Time: ( Word Count: )

December 11, 2020

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 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

Other interesting articles

How Many Types of Penetration Testing are There?

How Many Types of Penetration Testing are There?

A penetration test is a deliberate attack on a software or hardware system with the goal of exposing security ...
5 Types of Social Engineering Attacks and How to Prevent Them

5 Types of Social Engineering Attacks and How to Prevent Them

The term "social engineering" refers to a wide range of malevolent behaviours carried out through human ...
What are Some Risks and Benefits of Performing Penetration Testing?

What are Some Risks and Benefits of Performing Penetration Testing?

In the world of cyber security, various methods and tests are conducted to ensure the security barrier of a ...
How to Hire the Best IT Security Consultant: 4 Simple Tips

How to Hire the Best IT Security Consultant: 4 Simple Tips

Whether you run an online business or an offline store, your business needs to be secure. You might be surprised, ...
1 Comment
  1. Appreciate your content!

    4
    Reply

Submit a Comment

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

error: Alert: Content is protected !!