Kali Linux Penetration Testing Bible. Gus Khawaja
Чтение книги онлайн.
Читать онлайн книгу Kali Linux Penetration Testing Bible - Gus Khawaja страница 15
Read ( r ): 4
Write ( w ): 2
Execute ( x ): 1
The permissions template applies the following pattern:
[User:r/w/x] [group:r/w/x] [everyone:r/w/x]
Figure 1.6 Kali Linux – Files and Folders Commands
Let's look at a practical example. Lat's say you created a simple shell script that prints “test” (using the echo
command) and that you wanted display its permissions (take note that this example uses the root user inside the terminal window):
root@kali:~# echo 'echo test'> test.sh root@kali:~# ls -la | grep 'test.sh' -rw-r--r-- 1 root root 10 Sep 22 11:25 test.sh root@kali:~#
From the previous output results, we can see the following:
For the root user, you can read and write because of rw at the beginning.
For the root group, they can only read this file.
For everyone else on the system, they can only read as well.
Let's say you want to execute this file, since you're the one who created it and you're the master root. Do you think you'll be able to do it (according to the previous permissions for the root user)?
root@kali:~# ./test.sh bash: ./test.sh: Permission denied
TIP
The dot in the previous example means the current directory.
Indeed, the root has no permission to execute it, right? To change the permissions of the previous file based on the formula ( r
=4, w
=2, and x
=1), use this:
User:4+2+1=7; Group:4+2+1=7; Everyone:4
Then, use the chmod
command to get the job done (this time, you should be able to execute the shell script):
$chmod [permissions numbers] [file name] root@kali:~# chmod 774 test.sh root@kali:~# ls -la | grep 'test.sh' -rwxrwxr-- 1 root root 10 Sep 22 11:25 test.sh root@kali:~# ./test.sh test root@kali:~#
There is another shortcut for this, which allows the execution of a file instead of calculating the numbers of each. We just need to add +x
to the chmod
command (but be careful because when you execute this one, you will be giving the execution permission to everyone as well):
$chmod +x [file name] root@kali:~# chmod +x test.sh root@kali:~# ls -la | grep 'test.sh' -rwxrwxr-x 1 root root 10 Sep 22 11:25 test.sh
Manipulating Files in Kali
To simply create an empty file in Linux, you can use the touch
command:
$touch [new file]
To insert text quickly into a file, you can use the echo
command. Later in this chapter, you will learn how to edit text files with a text editor:
$echo 'text to add'> [file name]
To know a file type in a Linux system, you must use the file
command:
$file [file name]
Let's assemble all the commands together in the terminal window:
root@kali:~# touch test.txt root@kali:~# echo test> test.txt root@kali:~# file test.txt test.txt: ASCII text
To copy a file in Kali, you must use the cp
command to get the job done:
$ cp [source file path] [destination file path] root@kali:~# cp test.txt /home/kali root@kali:~# ls /home/kali Desktop Downloads Music Public test.sh Videos Documents ls_file.txt Pictures Templates test.txt
To move a file that is equivalent to cut in Windows OS, you must use the mv
command:
$mv [source file path] [destination file path] root@kali:~# mv test.txt Documents/ root@kali:~# ls Documents/ test.txt
To delete the file that we just copied earlier in the kali
home directory, use the rm
command:
$rm [file path – that you want to delete] root@kali:~# rm /home/kali/test.txt
To rename the previous file, we use the same mv
command that we used to move a file:
$mv [original file name] [new file name] root@kali:~/Documents# mv test.txt hello.txt root@kali:~/Documents# ls hello.txt
Searching for Files
There are multiple ways to search for files in Kali; the three common ones are the locate
, find
, and which
commands.
You can use the locate
command to locate a file that you're looking for quickly. You need to know that the locate
command stores its data in a database, so when you search, you will find your results faster.
First, you will need to update the database for the locate
command using the updatedb
command:
$updatedb
Now, we can start searching using the locate
command:
$locate [file name] root@kali:/# locate test.sh /home/kali/test.sh /usr/share/doc/socat/examples/readline-test.sh /usr/share/doc/socat/examples/test.sh
You can use the ‐n
switch for the locate
command to filter out the number of output results. This option is handy if you know that the results will be enormous:
$locate -n [i] [search file criteria] root@kali:/# locate *.conf -n 3 /etc/adduser.conf /etc/ca-certificates.conf /etc/debconf.conf
TIP
Use the grep
command to get more granular results.
To find an application path, use the which
command. This command will use the $PATH
environment variable to find the results that you're looking for. As an example, to find where Python is installed, you can do the following:
$which [application name] root@kali:/# which python /usr/bin/python
It's important to understand that a Linux system will use $PATH
to execute binaries. If you run it in the terminal window, it will display all the directories where you should save your programs/scripts (if you want to execute them without specifying