Hey everybody.
Welcome to the first video in the module we're actually going to go over some code now what I want to
first start this video off with is we need to do we need to have a compiler for assembly.
So what we're going to be using is something called Nassim now to uninstall Nazism.
You're going to do a pseudo apt get install them.
Right.
So the password now that'll go ahead and install it.
Now I have already installed it.
So that's fine.
And the latest version is fine.
So when you run this on yours you will see it actually get installed.
Perfect.
Now we've got our compiler.
Again make sure because we're on Linux you close out this terminal session and reopen it.
Otherwise you won't be able to see you won't be able to find Nasim now it's clear correctly.
So what we've got in our current directory is a file called Main dot ESM.
Now that ESM is a common file extension for assembly code.
However it does not mean that we are going to need it.
You can use any extension you want here just for us to quickly identify what file we're looking at now
this over on the right is just a simple text editor in an Ubuntu it's called g edit which is just I
just did added opened and then I picked main ESM and now we've got the file.
So the first thing we want to do is understand how comments work in assembly.
Now that's a semicolon and then we can do author Red Team nation.
So comments are semicolons not pound signs and not slashes.
So make sure you understand that because all of the example code that's going to be about each video
is going to have a bunch of comments along with it so you can understand and follow what the code is
doing.
Now it's important before we get to this point that if you have any issues or misunderstandings about
what registers are and what flags are.
I need to make sure that you go back to that video and re watch registers and flags because you're going
to be working with them at an intimate level.
When we get through the assembly course.
So if you need it if you need a second go back and re watch the videos do a little bit more google searching
make sure you understand what they are and what they're commonly used for and then things we made a
little bit more clear if you're still uneasy when we go through this video.
So there's a general structure to an assembly file the first thing you're going to want to do is tell
the compiler Nazism where the entry point of the application is.
So this is done with the global keyword.
Now what we're gonna do is we're going to call this underscore start.
Now this can be underscore Maine or Maine or whatever you want it to call.
In this case I'm just going to start.
So this tells us that when we use the word start underscore start that is where the starting of the
application is.
So we give ourselves some space and we'll do underscore start and then move down to the next line.
Now this is our common structure.
So right now if we ran if we went and built this it would do nothing.
However this is our common structure we're going to use this structure throughout the entire course.
So get familiar with what this is doing.
Again global underscore start tells us that the entry point to the application is at the start symbol
which is this and then anything under start is what gets run immediately.
Now the first thing we're gonna do is we're going to introduce you to the move command which is an OVA.
What this does is this takes we'll do a quick example here.
Move X explorer.
Now what the move does is it takes this data on the right hand side of it and moves that into the register.
That's on the left.
So in this case a X now E A X is a special register as we've talked about before but it's also the one
when you initialize a system call whatever value is an E X X is what is going to be register looked
at on the second table and then it's going to open up and it's going to launch that Cisco.
So a quick look at the Cisco table.
If we do a history graph with X so this is gonna be the file location on mine it should be the same
for years so go in and copy it if you need to.
So if we look in here really quickly we see that four is what we're using here is four right.
Which means we can write to the screen right to a file write to anything we want.
So that's how this is corresponding.
Now keep in mind we're providing this in hex so 0 x 4 this number 4 just happens to be 0 x 4.
When you start getting past 16 because it's based sixteen when you're getting into 17 18 19 it's not
0 x just whatever the numbers you're not going to put 0 x 20 because that's not going to be this this
call.
So make sure you grab a decimal to hex calculator you can google for it and that and then you'll be
able to calculate what that digit is supposed to be because this is the digit in decimal but it needs
to be provided in hex.
However you could just put four here if you want to Nazism we'll understand that but get familiar with
converting things to hex right away so that it's not as confusing.
So that's what this is called table.
So we'll go ahead and close out of this
right.
So move we've moving four into a X which will set the value for into the E X register which tells us
that this this call is right so we'll go here right here.
So this is sets this call value for K right.
Right.
Now we've got this call now the next thing we're going to want to do is pass in the file descriptor.
Now this part is optional because by default if you left it out it is going to provide a standard output
as the default file descriptor.
So just as a quick thing here if we man to right.
So this is how you access the man pages for a system command it's Man 2 And then the.
This is.
So if you look under right we can see the file descriptor and then we're going to take in the value
or the text that we want to print.
And then the size of the print the size of the text.
So in this case the first thing we're doing is passing this is the first parameter is file descriptor
so we can leave that blank or we can set it to 1.
So we'll set it to 1 just show you that it's there.
So the way this calls work is that E X holds this is called number then E B X holds the first parameter
which is here an E C X holds the next one e d x holds the next one and then anything after X you have
to push onto the stack which we're not going to have to go over in this particular case.
However what just understand that when you get past ADX you're gonna be You're gonna want to push it
to the register.
So what we'll do is we'll move X we'll move 0 x 1 and x which is our system out file descriptor which
is the standard one that we've been using before.
Next we're going to want to do is move strip move our string or our text into the register.
Now it's important to understand here is that this is easy X right.
And we're going to passion string.
Now I'm not going to go over what string is right now we're going to do that and a little bit.
Just note that this is a variable.
So keep that in mind this is a variable so string is a variable.
Next thing we're going to do is move into X is again what's left is R value r r r length of the string.
So we'll do string L for string length.
Now what we've got here is we've set zero x 4 into X for the right size call which we have over here.
Then we're setting the file descriptor which is 0 x 1 which is standard out then we're moving in the
string which is essentially a memory location for the text that we want to print.
And then we're telling it how how many bytes are we going to print because what it's going to do is
it gonna take that memory address and then print each character one at a time until it hits the length
that we've sent it.
Now if you send it more it's going to try to print into the next memory address which could be useless
garbage data.
So it's important that these match now what we're gonna want to do is give ourselves in space and we
need to set up a variable.
So we're gonna set up a string now following this as D.B. which is defined byte.
We'll have a whole video on this in the in the future.
Just keep.
Just leave that alone for now.
Which is defining a bite.
Now we're going to pass it are strength.
So we'll do Red Team activation and then comma and then 0 x 0 0 0.
Now 0 0 a if you convert that to decimal is going to is going to be on the ascii table a carriage return.
So that is essentially a new line.
That's all we're doing here.
It's just a quick way for us to bump things to the next line.
Now we've got our string which is our defined byte which is our text with a new line at the end.
What we're going to want to do is then we want our string out.
So there is a short way to do this.
And if you do string l equals dollar sign.
Dash.
String now this is a special case is it.
It's just a quick way for us to turn a value or the length of a string based on a variable.
So I'll go ahead and try to explain this part.
What we're gonna do is so this the dollar sign is referencing the end of the string.
So in this case the 0 x then it's subtracting the start of the string which starts right before.
So these are memory locations.
So what that's doing is it's setting the difference of the memory locations or the offset which gives
us the amount of bytes that separate.
So that's why this works.
If that's not as if you don't quite understand that that's fine we'll go over it in a little bit more
detail later through the course but that's that's the way it works.
So what we've done so far is we've again moved 0 4 which is this is called value for right.
And we've passed in the file descriptor that we've passed in our string that we want to set.
And we've sent in the length.
Now the only thing left to do is use an interrupt.
So I.A. 0 x 18 interrupt 80 basically tells us execute whatever system call is in x.
Now when that happens it's going to take all of these parameters and then it's going to run.
Now there's one more thing we're going to want to do.
Because technically this will print it to the screen.
We're done.
So that's how you print to the screen it's a little bit more complicated than just saying print as we
did with Python.
However your understanding the lower levels and how to interface with system calls directly.
So what we want to do next is use the exit as this call so we won't actually exit the application without
any errors.
So if we come over to our man page and we do man to exit we can see that it takes just a status which
means exiting 0 is fine exiting 1 means there was an error.
So what we'll do is we're gonna move in the X we're gonna move 0 1 0 x 1 so 1 if we looked at that table
is the exit 1 exit Cisco then we're going to move x or x I'm sorry we're gonna move now this is gonna
be whatever return value we want.
So in this case we'll just do 0 0 so we'll end up no issues and then again we have our interrupt.
So and your X 80.
So this is going to do is we're going to put 0 4 for the right system call into x and x we're going
to pass in the file descriptor which is 1 for standard input or standard output then easy X we're going
to pass in the the memory address of the strength and then an E D X we're passing in the memory address
to the length of the string interrupt eighty which executes the system call then we're moving one into
X for the exit Siskel and then we're moving 0 into X for the status that we want to do and then interrupt
eighty for the Siskel so to compile this we need to run two separate commands right.
So we need Nassim dash f yeah.
And then the name of the file which in this case is main dot RSA
symbol string not defined
Oh yeah.
Or redefining it.
So we need to add the L to the end here.
Sorry about that so what we've done is we've generated our object file or not.
Then we need to do our linking process so we need to link all of the required libraries in this case.
There is none so it'll just try to build the executable.
So linking is L.D. dash M now we're gonna have to tell it since this is a 64 bit OS we're gonna have
to tell it to link as a 32 bit.
So we'll do e f underscore I 386 dash 0 4 output so we'll just name that as main and then we need main
O for object file now when that's done we should just remain main right here.
Now if we do a file on Main it'll tell us it isn't e l f binary perfect.
So if we run that dot slash main we should get right to make it perfect.
So that's how you print a string and then exit the application.
So what we can do is we can go here let's just do this for shits and giggles.
So what we'll do is we'll recompile some
rebuild it.
So as you can see we print out the tax Hillary gets a segment fault because it wasn't meant to exit
that way.
So the operating system had to actually exit this application so that's why you're always going to want
that that extra bit at the end and in all of our examples our structure is going to look like this then
it's going to have our exit code at the bottom and then whatever data we want down here and then whatever
code we want up here.
So this is how you make hello world.
So just to print something out in assembly it's a little bit to take in so take a step through the process
download the the source code above the video look through the comments it provides good documentation
on what each step is doing and then make sure you play around with this in the next feature videos we're
going to be going over something called GDP which is the debugger in Ubuntu and in most Linux operating
systems we're gonna do a little bit of a crash course on just enough to need to know to view the registers
and to view the flags for what we're gonna be doing.
So go ahead play around with this make sure you understand what we're doing here and if you have any
questions feel free to run over the Facebook group.