This is the 10th post of a series for teaching cyber-security in a coding-club. Read [part 9].
Pipe and count
Disk Usage
In our mission we may have to inspect large files. For example creating a file full of possible passwords to try. if you had a huge file called passwords_file and did:
catpasswords_file.txt
it might spend a long time printing the contents to the shell. However there is a way to know how big a file is in advance. Find your shell (or Terminal) and type:
The column after group has numbers: 30, 16, 14, 20, 28, 1704, etc ...
Those are the sizes of the files in bytes. If you want it in a more "Human" readable form (with kilobytes k, Megabytes M or Gigabytes G) you can instead type:
ls -lh
Note the letter h which means human, as in easier to read. But what if we have entered a server and don't know where the large files are. We need to find that Megabyte file which is full of secret data! We can then use:
du-h
Which stands for Disk Usage displayed in human format.
Try it out! You should have gotten a long list of all the folders (or directories) in this computer with all their sizes.
Once you find the largest directory you could go inside to find more information. But for now, let's learn one of the most powerful tools of the shell: PIPES!
Pipes
Using the output of a command
So far we have been typing commands like ls or du or cat and reading the output in the shell. But what if I want to do something with it?
for example, after running du -h we saw something like this:
What if I need to know how many folders and sub-folders I have? Well, I could count it by hand, or I could ask a counting program to do it for me. This counting program is called wc:
Command: wc
Definition:
The wc command or word count command counts how many words, lines and characters are in a file. If you use the option wc -l it will only display the number of lines.
If you list your files with ls you should see a file called README.md. Run the command:
wcREADME.md
The output should look like this:
22 233 1704 README.md
Which means that the "README.md" file has 22 lines, 233 words and 1704 bytes. Ok. Fine. But what does this have to do with pipes?
Well, I said the pipe command is very powerful. Let's see how.
Command: <input> | <output>
Definition:
The | command or pipe command takes the output of what is left of it and passes it as input to whatever is on the right of it. The symbol you need to type is a vertical line (often found above the "\" symbol).
So my original question was to find out how many sub-directories I have. I printed du -h which showed me a really long list of all the sub-directories. But who has time for counting them all??? So here's what I want to do:
Take the output of the command du -h and pipe it to the counting command. Try it out!
du-h|wc-l
Now instead of looking at the mess that comes out of du -h we can see straight away that there are an exact number of lines in the output, and therefore that same number of directories.
Real hackers will use pipes all the time. for example:
ps-e|grepsshd|wc-l
which means:
ps -e: list all the processes in this computer
|: pipe the output of ps to the program grep
grep sshd: grep looks for the word sshd, and prints it as many times as it's found
|: pipe the output of grep into wc
wc -l Count the lines
In a single line we can do: Computer! Please count how many secure connections are being made to this computer through sshd right now
But how can we use pipes for security and passwords? More in [Part 11].