Contents

Bandit Level 7 to 8: Finding a Needle with grep

Searching for the word 'millionth' in a massive text file. A practical introduction to the powerful grep command.

Introduction

We have successfully used the find command to locate files based on their metadata. Now, for Bandit Level 7 to 8, our focus shifts from finding the file to finding specific data inside a file.

This level introduces grep, one of the most fundamental and widely used text-processing utilities in the Linux command-line world. 🕵️

The Challenge: Level 7 Goal

The goal for this level is described as follows:

The password for the next level is stored in the file data.txt next to the word millionth.

We have a large file and a keyword. Our mission is to extract the line containing that keyword.


Step-by-Step Walkthrough

Let’s use the right tool for the job to solve this efficiently.

Step 1: Log into bandit7

Use the password from the server-wide search in the previous level to SSH into the bandit7 user.

ssh [email protected] -p 2220

Step 2: Examine the Target File

First, let’s see what we’re dealing with. Use ls -lh to list the file and its size in a human-readable format.

ls -lh

You will see the file data.txt and notice that it’s quite large.

-rw-r----- 1 bandit8 bandit7 9.5M Oct 15 10:00 data.txt

Trying to open this file with cat or a text editor would flood your terminal with millions of lines, making it impossible to find the password manually.

Step 3: The Solution - Using grep

The grep command (which stands for Global Regular Expression Print) is designed for exactly this task. It scans a file line by line and prints only the lines that contain a matching pattern.

The basic syntax is: grep "pattern" filename

In our case, the pattern is the word “millionth” and the filename is data.txt.

Step 4: Execute the Command

Run the grep command with our specific pattern and file.

grep "millionth" data.txt

Instead of printing the whole file, grep will instantly filter it and show you only the single line that contains our keyword.

millionth dfwvzFQi4mU0wfNbFOe9RoWskMLg7eEc

The password for bandit8 is the string of text right next to the word “millionth”.


Key Concepts Learned

  • grep Command: This is the main takeaway. grep is the go-to tool for searching for text patterns inside files. You will use it constantly in system administration and cybersecurity.
  • Text Processing: This level highlights the importance of tools that can efficiently process large volumes of text data without having to read it all manually.
  • Pattern Matching: grep finds lines that match a given pattern. While we used a simple word, grep can also handle complex patterns called regular expressions, making it incredibly powerful.

Conclusion

Fantastic work! You’ve successfully filtered a massive dataset to extract a single piece of critical information. The grep command is an essential part of any command-line user’s toolkit.

Copy the password you found and get ready for Level 8!