In this demonstration,
I'm going to walk you through this Python code
and make a few modifications to the code
for enhanced functionality.
As mentioned in the previous lesson,
this code is used to simply retrieve your public IP address
by basically going to this target URL identified here
and then displaying the public IP address
with these functions down here.
So next, I'm going to go ahead and switch tabs
and I'm going to run this script,
but the first thing that we need to do
is we need to ensure that it has executable functions,
so if you're following along,
make sure that this particular script
has executable functions, so we could do the list
l.
We have the Python_IP_retrieval.py,
that's the name of the actual script.
Okay, so if we look here, we see
that we have executable permissions, okay
on all spaces,
so we're good to go there.
Once we've identified that we have executable rights,
we can simply do ./
then we'll do python_IP_retrieval.py.
All right, perfect.
So we have a little warning here
for the HTTPS request being made to the host.
It's not verifying the certificate. We removed that here.
We disable the certificate requirements,
just for this example,
because we're just running into a few errors with that,
but the script did what it was supposed to do.
This is my public IP address.
All right. Now, next, we're going to modify this script
and change the functionality by creating a function to fetch
and display HTTP headers from a given URL.
Now, remember, HTTP headers will provide information
about the server and the response,
so we typically going to do this in our reconnaissance phase
of our penetration test.
So the first thing we're going do is rename this function.
Instead of 'get_public_ip,'
we're going to name it 'get_http_headers.'
Scroll down here.
Next, we're going to remove all instances
of this public IP address here,
'cause we don't need that anymore. All right.
We're going to change that to http_headers,
so let's do that first.
Okay, so I'll just cut that.
And so what this is basically doing
is using this particular variable,
'cause this is going to get the results
of this executed function, this HTTP request,
and we're going to take the headers attribute
from the http.request object and input it there.
So we're going to get the headers attribute
and call that with the response variable.
All right, so now what I'm going to simply do
is just go through and change every single one of these.
And this is going to be 'http_headers.'
All right, so for the second time,
I want to simply copy and paste a lot of this right here.
I'm going to remove this.
I'm going to paste it in with the correct code.
So, all I did was simply remove the bottom portion,
which was just displaying information
based off the IP address, so we don't need that,
so we take that out, and now,
we're going to change our target URL.
Before, it was focusing on the one
to give us a public IP address.
Now, we're just going to simply use example.com,
because we want to retrieve the header
from that particular website, okay?
So we're going to insert the response
into the variable 'http_headers'
once we call this 'get_http_headers,'
which is the function that we define up here.
Okay?
And we're going to return the HTTP header
based off the requested target,
so we're going to input this target URL, request target,
and we're going to get the header,
and it's going to be stored in the 'http_headers' variable.
So here, we're going to check to see
if this 'http_headers' variable is empty or not.
So here, anytime we have an error,
it's going to switch it to none,
but once we actually retrieve the header,
this will no longer be empty,
and so pretty much, we're evaluating if it's empty or not.
So pretty much if it's not empty, that means it was good,
and we're going to print this information here.
Okay?
And if it's, of course none, then we're going to print
that we could not retrieve the HTTP headers.
All right? So let's save this.
We're just going to exit,
and we're going to change the name to Python retrieval.
We're just going to say one for modification purposes.
All right?
Yes, perfect. Let's clear the screen.
So first, let's verify the permissions of that current file.
Okay, we see here that we do not have
any executable permissions, so let's change that.
All right, so it looks like that went through correctly.
All right, so now for the grand finale.
We're going to run the Python script.
Okay, perfect.
We see here we're able to successfully grab the header
from example.com, which is also known as banner grabbing.
To sum things up, we covered how to modify
an existing Python script from retrieving our IP address
to retrieving HTTP headers, also known as banner grabbing.
I hope you enjoyed this demonstration,
and I'll see you in the next one.