Ready to take Python coding to a new level? Explore our Python Code Generator. The perfect tool to get your code up and running in no time. Start now!
In this quick guide, you’ll learn how to create your own grep tool using Python. grep
is a command-line tool used for searching text patterns within files and streams. grep
stands for "global regular expression print." The importance of this tool cannot be overemphasized because it is very handy and saves us a lot of stress and time while bug hunting or pen-testing.
grep
allows users to quickly and efficiently search through files, logs, and outputs of other commands to identify patterns of interest.
Let’s say you’re bug-hunting or pen-testing, and you discover a file that seems pretty interesting like a phpinfo.php
file. If you’re familiar with PHP or web development, you know some interesting parameters to search for. Instead of manually checking the file as it could be really large, you could simply use your grep tool and search for certain parameters like SECRET_KEY
. Doing this will save you a lot of time and effort.
Let’s see how to do it. First, we install colorama
for colored output:
$ pip install colorama
As usual, open a Python file name it meaningfully like grep_python.py
and follow along.
We start the code by importing the necessary libraries and initializing colorama
:
# Import the necessary libraries.
import re, sys
from colorama import init, Fore
# Initialize colorama.
init()
re
is for regular expressions, sys
for system-specific functionality, and colorama
for colored terminal output in Python.
Next, we create a simple function to extract patterns from a given file.
# Grep function.
def grep(pattern, filename):
try:
found_match = False
with open(filename, 'r') as file:
for line in file:
if re.search(pattern, line):
# Print matching lines in green.
print(Fore.GREEN + line.strip() + "\n") # We are including new lines to enhance readability.
found_match = True
if not found_match:
# Print message in red if no content is found.
print(Fore.RED + f"No content found matching the pattern '{pattern}'.")
except FileNotFoundError:
# Print error message in red if the file is not found.
print(Fore.RED + f"File '{filename}' not found.")
This grep()
function takes a regular expression pattern and a filename as input, searches for lines in the specified file that match the pattern, and prints them in green. If no matches are found, it prints a message in red indicating no content was found. If the specified file is not found, it prints an error message in red.
It’s that simple.
Finally, we make sure the user input from the CLI is as it is supposed to be and call the function:
if len(sys.argv) != 3:
# Print usage message in red if the number of arguments is incorrect.
print(Fore.RED + "Usage: python grep_python.py <pattern> <filename>")
sys.exit(1)
pattern = sys.argv[1]
filename = sys.argv[2]
grep(pattern, filename)
That’s it! Now, let’s run our code:
$ python grep_python.py <pattern> <filename>
Note: This phpinfo.php
file was obtained from one of PortSwigger's Labs. You can test your web security skills there. The scenario is that I was lucky to come across the phpinfo.php
file and using our cool grep tool by searching SECRET_KEY
, I obtained the secret key, saving a lot of time and effort that could be channeled to finding other bugs.
Learn also: How to Organize Files by Extension in Python.
I hope you enjoyed this one. Till next time!
Ready for more? Dive deeper into coding with our AI-powered Code Explainer. Don't miss it!
View Full Code Assist My Coding
Got a coding query or need some guidance before you comment? Check out this Python Code Assistant for expert advice and handy tips. It's like having a coding tutor right in your fingertips!