This time we're going to look
at a Python script.
So here on the screen you'll see the Python script.
Now again, on the exam,
they're not going to tell you it's a Python script.
They're going to expect you to be able to figure that out.
Now what are some dead giveaways
for us if this is a Python script
as opposed to something else?
Well, for one, we have variables
that don't have a dollar sign or an underscore.
So we know that it's not going to be something
like a PowerShell or a Ruby Script.
But how do we know it's Python?
Well, for me it's the print command.
None of the other ones use print.
They mostly use echo or puts, right?
And so the fact that I see print in here
tells me it's Python.
So what is this script doing anyway?
Well, let's walk through it.
The first thing is import.
It's importing in a user library called user library two.
That's just going to give us extra functions
that we can use.
We're then going to define a function.
So def get_public_ip from (request_target).
Next, we're going to use grabber,
which is going to equal urllib2.build_opener,
which is probably going to build some sort
of an opening thing, right?
It's going to grab something.
That's all we know at this point, right?
Next we have grabber.addheaders =
[('User-Agent', 'Mozilla/ 5.0)].
Hmm, what's that got to do with anything?
Well, Mozilla is a web browser, right?
And addheaders sounds like it's the header.
If you remember the way web browsers work,
they tell a website, hey,
I am Internet Explorer version six,
or I am Mozilla version 5.0.
So they know what kind of language to send back to them
and what the screen should look like.
So in this case, whatever we're trying to do here,
we're trying to impersonate the fact
that we are a Mozilla 5.0 web browser.
That's what we've learned so far.
Next, we're going to try something, try:.
This is a basically a case statement, right?
Try this, then that, then this, right?
Except we're going to use try, except, except, return.
So when I try: public_ip_address =
grabber.open(target_url).read
What do you think that does?
Even if you don't know Python,
you can kind of get an idea here
because it's pretty human readable, right?
I'm going to read something
and I'm going to use this grabber program that I'm using
to open a target website, a target URL.
Now, if I have an exception, an HTTP error,
I'm going to give the error to the screen.
Print.
There was an error trying to get your public IP address,
and that's going to be the error.
Next, if I have the error of a URL error,
it's going to say there's an error trying
to get your public IP address again,
and then I'm going to return public IP address
if I don't have either of those two errors.
So, what do you think we're doing so far?
We're trying to figure out our own IP address.
That's all we're trying to do with this script.
We're going to use public_ip = "None".
We're setting the variable to none.
Target_url =
whatever that target is that we're trying to use.
In this case, we're using ip.42.pl/raw.
And then I have public_ip = get_public_ip
from that (target_url), whatever that website is.
So we're trying to identify the public IP address
for ip.42.pl.
That's what we're trying to do in this script.
Now, if, not none, if the variable has been changed
because it was successful,
then we're going to print your public IP address
is whatever it is.
Otherwise we're going to put out your public IP address
was not found.
That's all the script does.
It's trying to determine a public IP address.
It's a lot of code for a very simple function.
Now, how else could you find your IP address?
Well, you can go to Google and say, what is my IP?
And it'll pop it up, but this is the thing
behind it that's doing it, right?
This is the Python code that's doing something like that.
And so you can see how we could read through this
and how we identified that it was Python
and what it was doing.
That's what you need to be able to understand
for the PenTest+ exam.