Introduction
In this post, I’m building on the foundational Linux knowledge we’ve already covered, moving into some of the more intermediate concepts that are absolutely essential for anyone serious about penetration testing. We’ll be exploring how to manipulate data, control how and when commands are executed, and manage file permissions and ownership.
Skills Learned
- Linux
- Penetration Testing
- OSCP
Mastering Input and Output
First up, I want to talk about how we can control the flow of information in the Linux command line. This is done with what we call input/output (I/O) operators.
I start with the redirection operator (>
). This little symbol is incredibly powerful. It allows you to take the output of a command and send it to a file. For example, if you want to save the output of the echo
command to a text file, you can do so easily. But be careful! The >
operator will overwrite the file if it already exists.
If you want to add new information to a file without erasing what’s already there, you’ll want to use the append operator (>>
). This works just like the redirection operator, but it adds the new output to the end of the file.
Taking Control of Command Execution
Next, I’ll show you how to string commands together and control how they run.
The double ampersand (&&
) is a conditional operator. It tells the system to run the second command only if the first one was successful. This is great for creating simple scripts where you want to make sure one step completes without errors before moving on to the next.
On the other hand, the semicolon (;
) is a sequential operator. It will run multiple commands one after the other, regardless of whether the previous command was successful or not.
And for those times when you want to run a command in the background so you can continue working in the shell, you can use the single ampersand (&
). This is especially useful for long-running processes that would otherwise tie up your terminal.
Understanding the Environment
Now, let’s talk about environment variables. These are special variables that are part of the system’s environment and can be accessed by any command. I’ll show you how to view the contents of an environment variable using the $
operator, like with the $PATH
variable, which tells the system where to look for executable files.
I’ll also demonstrate how to create or modify environment variables using the export
command. And I’ll point out the useful $USER
variable, which stores the name of the currently logged-in user.
The Power of the Pipe
One of the most powerful features of the Linux command line is the pipe (|
). The pipe allows you to take the output of one command and use it as the input for another. This lets you chain commands together to perform complex tasks. For example, you can use the ps
command to list all running processes and then pipe that output to the grep
command to filter for a specific process.
Managing File Permissions and Ownership
Security is paramount in penetration testing, so it’s crucial to understand how to manage file permissions and ownership.
I’ll walk you through the chmod
command, which allows you to change the permissions of a file or directory. I’ll explain the numerical permission system (e.g., 666
for read and write access for the owner, group, and everyone else) and how it relates to the three permission slots: owner, group, and others.
I’ll also cover the chown
command, which is used to change the owner and/or group of a file or directory. And for those times when you need to apply these changes to all the files and subdirectories within a directory, I’ll show you how to use the -R
(recursive) switch.
Creating Links and Finding Files
Next, I’ll introduce you to symbolic links. The ln
command can be used to create a link to a file, which is like a shortcut. When you use the -s
switch, you create a symbolic link, which is a reference to the original file. Any changes you make to the linked file will be reflected in the original.
Finally, I’ll show you how to use the find
command, a powerful tool for searching for files based on a variety of criteria, such as name, type, permissions, or owner. I’ll also show you a neat trick for redirecting error messages to /dev/null
to keep your output clean.
Technical Commands Used
Throughout this session, I use a number of commands to demonstrate these concepts. Here’s a list of the key commands I cover:
- I/O Redirection:
echo [text] > [filename]
echo [text] >> [filename]
cat [filename]
- Command Execution:
command1 && command2
command1 ; command2
command &
- Environment Variables:
echo $[variable_name]
export [variable_name]=[value]
- Piping:
command1 | command2
ps aux | grep apache
cat [filename] | cut -d'[delimiter]' -f[field_number]
- File Permissions and Ownership:
chmod [numeric_permission] [filename]
chown [owner]:[group] [filename]
chown -R [owner]:[group] [directory]
- Symbolic Linking:
ln [source_file] [destination_file]
ln -s [source_file] [destination_file]
- Searching for Files:
find [path] -type [file_type] -name "[filename]"
find [path] -user [username]
find [path] -group [groupname]
find [path] 2>/dev/null