Название | Kali Linux Penetration Testing Bible |
---|---|
Автор произведения | Gus Khawaja |
Жанр | Зарубежная компьютерная литература |
Серия | |
Издательство | Зарубежная компьютерная литература |
Год выпуска | 0 |
isbn | 9781119719076 |
To list the files and subfolders inside any directory, use the ls
command to get the job done (I use it a lot to get simpler output). But sometimes, the ls
command by itself is not enough, so you may need to add a couple of options to get better output clarity. The first option that you can use is the ‐a
command (all contents including hidden files), and the second option is the ‐l
command (formatted list):
root@kali:~# ls Desktop Documents Downloads Music Pictures Public Templates Videos root@kali:~# ls -la total 144 drwx------ 14 root root 4096 Sep 22 10:24 . drwxr-xr-x 19 root root 36864 Jul 27 15:41 .. -rw------- 1 root root 155 Sep 22 10:23 .bash_history -rw-r--r-- 1 root root 570 Jul 18 17:08 .bashrc drwx------ 6 root root 4096 Sep 22 11:21 .cache drwxr-xr-x 8 root root 4096 Sep 22 10:22 .config drwxr-xr-x 2 root root 4096 Sep 22 10:21 Desktop -rw-r--r-- 1 root root 55 Sep 22 10:21 .dmrc drwxr-xr-x 2 root root 4096 Sep 22 10:21 Documents drwxr-xr-x 2 root root 4096 Sep 22 10:21 Downloads -rw-r--r-- 1 root root 11656 Jul 27 13:22 .face lrwxrwxrwx 1 root root 11 Jul 27 13:22 .face.icon -> /root/.face drwx------ 3 root root 4096 Sep 22 10:24 .gnupg -rw------- 1 root root 306 Sep 22 10:24 .ICEauthority drwxr-xr-x 3 root root 4096 Sep 22 10:21 .local drwxr-xr-x 2 root root 4096 Sep 22 10:21 Music drwxr-xr-x 2 root root 4096 Sep 22 10:21 Pictures -rw-r--r-- 1 root root 148 Jul 18 17:08 .profile drwxr-xr-x 2 root root 4096 Sep 22 10:21 Public drwxr-xr-x 2 root root 4096 Sep 22 10:21 Templates drwxr-xr-x 2 root root 4096 Sep 22 10:21 Videos -rw------- 1 root root 98 Sep 22 10:24 .Xauthority -rw------- 1 root root 5961 Sep 22 10:24 .xsession-errors -rw------- 1 root root 6590 Sep 22 10:23 .xsession-errors.old root@kali:~#
Take note that filenames that start with a dot character before their names mean that they are hidden (e.g., .bash_history
). Also, at the far left before the permissions, the letter d
means it's a directory and not a file. Finally, you can list another directory's contents differently than the current one by specifying the path of the destination folder:
$ls -la [destination directory path]
Permissions
For the permissions, the same principle applies to a file or a directory. To simplify it, the permissions are divided into three categories:
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