- [Instructor] Welcome to the next video of section two,
Automating Interactive Input.
In the previous video, we have learned about spell checking
and dictionary manipulation.
In this video, we'll see how to deal with commands
that read input interactively.
Automating interactive input
for command line utilities are extremely useful
for writing automation tools or testing tools.
There will be many situations when we deal
with commands that read input interactively.
An example of executing command
and supplying the interactive input is as follows.
Creating utilities that can automate the acceptance
of inputs are useful to supply input to local commands,
as well as for remote applications.
Let's see how to automate them.
Think about the sequence of an interactive input.
From the previous code, we can formulate the steps
of the sequence as follows.
Converting the preceding steps
one, Return, hello, and Return by observing the characters
are actually typed into the keyboard,
we can formulate the following string.
The \n character is sent when we press Return.
By appending the \n characters, we get the actual string
that is passed to standard in, standard input.
Hence, by sending the equivalent string
for the characters typed by the user,
we can automate the passing of input
in the interactive processes.
Let's see how it works.
Let's write a script that reads input interactively
and uses this script for automation examples.
Let's automate the sending of input
to the command as follows.
Thus, crafting input with \n works.
We have used echo minus e to produce the input sequence,
where e signals to echo to interpret escape sequences.
If the input is large, we can use an input file
and redirection operator to supply input.
You can also manually craft the input file
without the echo commands by hand typing.
For example,
this redirects interactive input data from a file.
If you're a reverse engineer,
you may have played with buffer overflow exploits.
To exploit them, we need to redirect a shellcode,
such as \xeb\x1a\x5e\x31\xc0\x88\x46,
which is written in hex.
These characters cannot be typed directly
through the keyboard, as keys for these characters
are not present in the keyboard.
Therefore, we should use...
This will redirect the shellcode to a vulnerable executable.
We've described a method to automate interactive
input programs by redirecting expected input text
through stdin, standard input.
We're sending the input
without checking the input the program asks for.
We're also expecting the program
to ask for input in a specific static order.
If the program asks for input randomly
or in a changing order,
or sometimes certain inputs are never asked for,
the aforementioned method fails.
It will send the wrong inputs
to different input prompts by the program.
In order to handle a dynamic input supply
and provide input by checking the input requirements
by the program on runtime,
we have a great utility called Expect.
The Expect command supplies the correct input
for the correct input prompt by the program.
Trailing from the previous section,
let's see how to use Expect.
Automation of interactive input
can also be done using other methods.
Expect scripting is another method for automation.
Let's go through it.
Automating with Expect.
Expect does not come, by default,
with most of the common Linux distributions.
You have to install the Expect package manually
using your package manager.
Expect expects for a particular input prompt
and sends data by checking messages in the input prompt.
Run it as follows.
The hash spawn parameter specifies
which commands are to be automated.
The hash expect parameter provides the expected message.
Hash send is the message to be sent.
Hash expect eof defines the end of the command interaction.
Awesome, we've successfully seen how
to automate interactive input.
In the next video, we'll learn about making commands quicker
by running in parallel processes.