Hello everybody and welcome to part 2 of this tutorial. And now let's go ahead and
save the file. So press ctrl o to write the output
to the file. It says fire file named write, you can actually modify it here. So
you can type in some letters, or whatever you want, you can change the name here, or
you can even change the extension if you want. But, there is no
need for the time being. I'm just gonna go ahead and save it as test. Just
press Enter, there you go. It says wrote one line. Control X to exit, there we go.
Clear the screen, and now I'm going to use the cat command to get a listing of this
particular file, which is far more reasonable than the previous one which
you couldn't read. The cat command, by the way, it was functioning properly, but it
was mostly a binary file. Therefore, it gave you a listing, but you
couldn't really read anything from it. So let's just go ahead and type in cat test,
and there we go. It says some random text goes here. That is the text that we have
typed into the test file, and you can cat it like
this. These things are very good especially with files that you want to
make sure, you do not want to change them in any way, and you don't
want to make any accidental changes, or anything of a kind, and you want to
quickly see what is located within that file. There is another way of doing this,
basically it's less, and let's go ahead and navigate over /etc/apt/, ls,
and here we have plenty of files to work with. So let's just take sources.list
as an example here. So if I do cat sources.list, I'm gonna get
everything that is within that file as before, and it's gonna be listed in my
terminal here to stay, it's not gonna go away, anywhere. But there is
another command which I can use, let me just go ahead and clear the screen, it is
called less. And if I type in less sources.list, I'm gonna be prompted
with a new tab where I can actually view things here, not new
terminal tab but a new workspace within the terminal itself, and when
I press Q it's gonna exit and nothing will be displayed here. So very nice in
terms of being neat and organized, and so on, and so forth.
So less and cat, very nice things, but you can use, for example, cat sources.list
and then the command that I have previously mentioned is grep. Grep means,
basically, you pull something, you grab something, from something else.
Now here's what I mean by it. You type in cat sources.list and you type in a
pipe. This symbol here it's called a pipe, which basically states whatever the
output of this is, pipe it to whatever command comes here. So I'm
just gonna type in grep, and let's say I want to type in src. Press Enter,
excellent! So it will only print lines that have src in them. Linux is
case sensitive so when you are greping it's gonna check whether
the case of the letters, whether it's lowercase or not. But, you
can give it an ignore case. You can type it in like this, -i means it ignores case.
Oops, my bad -i to ignore the case. You're
gonna get the same output because there are no different things within this file,
but you get you get the general idea. So this is how you would use cat with grep
in order to pull things out of a file. Very important! This is a huge part of
the Linux terminal, filtering through text files, primarily because we're gonna
be doing large network scans, and we will want to create files from which we will
be able to pool useful information, which we will later in turn pass to other
tools to do something with. Anyway, let me just go ahead and clear the screen for
the time being. You also have echo. So, echo, and I give it the quotation marks
open. Let's say, "I AM ALIVE." Close quotation marks, and it will echo
these words. It literally is an echo. You type something into it and it echoes
them here. Let's just go back to the home directory now, get a listing, and use
exactly the same command echo "I AM ALIVE." By the way, you can scroll through the
previous commands by using arrow keys. So, using the up arrow key
and down arrow key you can scroll back through previous commands, and you don't
need to retype them. So I am alive, and I'm going to insert this, so I'm going to use a
greater than sign, and type in test. So this will echo "I AM Alive" into test. And
if I do less, I'm gonna use cat, test, you see it has replaced the contents of
the test, which was some random text goes here, with I AM ALIVE.
Very nice command to have. You can change variable names, variable values with echo,
and so on, and so forth. We will use this a bit more as we progress through the
course, and I don't really want to get in depth here primarily because later on
when we have clear examples that directly relate to what we are doing at
the moment, which is basically pen testing, then you will see more advanced
usage of pretty much all of these commands, especially grep. We have
touch. Touch is a quick way to create files. So, for example, I can do touch, and
I can say file1, file2, file 3. Press ENTER, LS, and you see it has immediately
created three files. So touch is a very quick method...oops.
It went a bit over my head there. Anyway, touch is a very quick way of creating any
amount of files that you wish, and you can specify folder paths, you can create
this file in home, this one in var, this point in VAR log, and so on, and so forth.
Anyway, down below you have mkdir. So, if I go ahead and type in mkdir, it's
make directory. So you are making some sort of a directory. Let's say the name
of this directory will be placeToBe. If I get LS, there we go. It states
that this file is actually a directory, which is a placeToBe.
I can even navigate to it and say
placeToBe ls, there is nothing in it, but you get the idea. You can make directories in
such a fashion. Now chown, if I type chown, it allows you to change the
ownership of a particular file. Now since we only have a single user here which is
root, there isn't really any point in doing so unless we create some other new
users. But, we're not gonna do that as we don't need them. But just to show you how
this would work, for example, if the owner of a file was not root, and if
you wanted to change it to root, he would do the following. So, chown, and then you
would type the username, colon, the user group. So the user group and the
user name are usually the same on your home PCs unless you're in some bigger
server, or something like that, and then you just specify the name of
the file. I don't know you can type in test, there we go. So it
will effectively change the ownership of file test to the username root, who is from
the user group root. And if I do LS -la, you can see here it states who the
owners are and which groups actually own the files as well. Clear the
screen. There is another more used command,
it's called chmod, and this command allows you to change file permissions. So,
this is something that you will need to use on a daily basis and quite often,
primarily because, well you see if I have an executable in Linux. For example,
let's just go ahead and edit, well actually we don't need to edit it, we can
type in echo "echo, and not like that, hello," close quotation marks, and I
to output this to test, and I want to move test to test.sh. Sh is basically
one of the scripts that's a
bash script for Linux that in such a way you can automate tasks. We will deal with
it in greater depth as we proceed through the tutorial. You will need to be
acquainted with bash scripting definitely, but bash scripting is very
similar to the terminal itself, so pretty much all the commands that you use in the
terminal you can use embarrass scripting as well. Basically, just combining a bunch
of them in a file. And now I want this to be my executable, but you see the way
to to start executables is ./, as I have stated previously, that's a
command to start any executable, and if I type in test and I press tab it doesn't
give me a list of possibilities. Why? Surely now test.sh is an
executable, but no. You see it doesn't have a permission to be an executable
file. You see here the difference, just take a look at this file, and take a
look at this file. This one is green, this one is not. And now look at their
permissions. So, you see it has a notation that is executable for all groups,
users, etc., while this one up here, test.sh, does not have such permissions.
You need to change that. And the way you change it is chmod, and you will
type in +x. So if you want the file to be writable you type in
+w, if you want it to be executable +x, if you want to be able to
read the file +r, very simple. There is also a way of doing this with numbers,
so you can type in 755, don't try remembering all those
modes. Whatever you need you basically go on
the net and check, it can be quite complicated, but these three you need to
know. It's +w, +r, and +x, and also 777 which is a global mode. This is
the mode that is not recommended for actual usage, but you can use chmod
to actually test or troubleshoot things. For example, if you want to be absolutely
sure that certain actions are not being prevented due to file permissions, you
change the mode of those files to 777 which is the global mode that anybody
can do anything with the file, and if you still have an error message you know
that it is absolutely not related to anything in regards to permissions. So
those four things you do need to know. So let me just go ahead and type in
+x, type in test.sh,
press ENTER, and if I do ls -la once again, you will see that test.sh
is now executable. Let me just run it, and there you go. If I run this it's
gonna echo hello onto my terminal screen. Now there's a bit
more to go, not much more left, there is just one
more command that I would really like to show to you, and that can be a very
dangerous command indeed, it can mess you up in ways you can't even begin
to imagine yet, that is the command rm. So RM is basically remove, and once you
remove things with this command it's next to impossible to recover pretty
much anything. So if I type in rm test.sh it's gonna remove it, and it's no
longer going to be there. But if I go ahead and navigate over to placeToBe
and, I don't know, touch test, gonna make a new file there, and if I go ahead
and say rm placeToBe it's gonna say RM cannot remove placeToBe because it is
a directory. This is a fail-safe of the RM. So you wouldn't delete a full
directory because you wouldn't be able to recover it, and that would be really
bad. What you can do is type in rm -f, and then placeToBe, cannot remove
directory. Oops, okay, no problems. You see you get stuck and I'm deliberately gonna
leave this part in this tutorial just to
troubleshoot it. So we type in --help, and here you have recursive, remove
directories and their contents recursively. So that's a very
nice option. So let's just go ahead and do that because this will go into the
folder, and remove everything within the folder, and the folder itself, hopefully.
So let's just go ahead and type in rm -r placeToBe, press Enter, there you go.
It has deleted all of of it, and there we go it is no longer there. The -f function, - f
that I have used, is forced. It will not ask you any questions, it will delete the
folder or file which can be also very dangerous. When you're using RM
you always want to be asked questions. In any case, I have, as I said, I have
deliberately left this in order to show you that even if you get stuck, don't
worry about it. I get stuck all the time, and if you don't know what to type in,
what argument to pass, just type in --help and take a look at what you can
actually type, read a little bit, and then use the options. Try it out,
sometimes it's gonna work, sometimes it won't, but more often than not you will
be able to figure it out from the help menu. If you can't figure it out from the
help menu just type in man rm and you can read pretty much everything there is
to know about this command here, and if you really can't perform the task, if
you can't find anything useful here, then forums are your next best bet. But trust
me, you will succeed in doing it. In any case, this was a brief
introduction to some of the basic commands that we will use. Please make
sure that you know what each one of these commands does, at least its basic
functions, and then later on we will get in-depth and do more advanced stuff, and
use more advanced commands by combining them, and so on. In any case, I bid you all
farewell, and I hope to see you in the next tutorial.