Hack Forums
[Tutorial] Get other's ip with PHP | PHP ip logger, detailed. - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Hacks, Exploits, and Various Discussions (https://hackforums.net/forumdisplay.php?fid=45)
+--- Forum: Grayhat • Purple Team • Hybrid Security (https://hackforums.net/forumdisplay.php?fid=449)
+---- Forum: Beginner Hacking (https://hackforums.net/forumdisplay.php?fid=4)
+---- Thread: [Tutorial] Get other's ip with PHP | PHP ip logger, detailed. (/showthread.php?tid=4370408)



[Tutorial] Get other's ip with PHP | PHP ip logger, detailed. - c x y k - Jul 31, 2014

Original thread located at https://hackforums.net/showthread.php?tid=4321958, posted by @Niely.

Hello there.
I've been reading through threads that request ip loggers and such, and stumbled upon Niely's post. I read it through and thought I was able to make it a little more detailed with some extended features. This is my first post, all feedback is welcome.

Things needed:
  • Very basic knowledge about HTML and PHP, but this is not needed.
  • A webhost, such as SPAM.com (free).
  • A domain name, or ip (less likely to look legitimate), free domains available at dot.tk.
  • Basic text editor, although an IDE or such is better.

This is a very easy task, it's designed to be read through and examined, although you can just skid the post at the bottom.

The following will display a legitimate 404 Not Found page to a user, log their ip along with a timestamp in a JSON user-friendly .txt file (or .json, if you prefer that).

Getting Started

First, you need to either find an editor such as Notepad, or download an IDE such as http://brackets.io/. I won't go into details on how to install this, it's pretty self-explanatory, google is also your friend.

If you don't know how to open Notepad, you can either search for it on your computer, or open up Run (Windows Key + R), and type "Notepad.exe".

Once you've got that opened, we need to copy a very little bit of PHP code. This code will allow us to prettify the JSON.

PHP Code

<?php
    
function json_readable_encode($in, $indent = 0, $from_array = true) {
    
$_myself = __FUNCTION__;
    
$_escape = function ($str) {
      return 
preg_replace("!([\b\t\n\r\f\"\\'])!", "\\\\\\1", $str);
    };
    
$out = '';
    foreach (
$in as $key=>$value)  {
      
$out .= str_repeat("\t", $indent + 1);
      
$out .= "\"".$_escape((string)$key)."\": ";
      if (
is_object($value) || is_array($value))  {
      
$out .= "\n";
      
$out .= $_myself($value, $indent + 1);
      }
      elseif (
is_bool($value)) {
      
$out .= $value ? 'true' : 'false';
      } elseif (
is_null($value)) {
      
$out .= 'null';
      } elseif (
is_string($value)) {
      
$out .= "\"" . $_escape($value) ."\"";
      } else {
      
$out .= $value;
      }
      
$out .= ",\n";
    }
    if (!empty(
$out)) {
      
$out = substr($out, 0, -2);
    }
    
$out = str_repeat("\t", $indent) . "{\n" . $out;
    
$out .= "\n" . str_repeat("\t", $indent) . "}";
    return 
$out;
    }
?>

Now that we've done this, we need to start writing the script itself. The above is to make the logs user-friendly and pretty.

Starting to write the script

Now we've got all the easy stuff done, let's continue with some more (easy?) scripting.

First off, we need to check if the log file exists. If it doesn't, we create it. Note: You can change the file extension to .json, if you wish.

Under your current code, and before the php closing tag ("?>"), paste/write this in:

PHP Code

<?php 
clearstatcache
(); 
if(!
file_exists("log.txt")) {
     
$fp = fopen("log.txt","w");  
     
fclose($fp);    
}  
$file = file_get_contents("log.txt");
$json = json_decode($file, true);
unset(
$file);
$json[] = array(gmdate("d-m-Y H:i", time()) => $_SERVER['REMOTE_ADDR']); 
file_put_contents('log.txt', json_readable_encode($json));
unset(
$json); 

After the php closing tag ("?>"), paste/write the following:

PHP Code

<?php 
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<
html>
    <
head>
        <
title>404 Not Found</title>
    </
head>
    <
body>
        <
h1>Not Found</h1>
        <
p>The requested URL <?=$_SERVER['PHP_SELF']?> was not found on this server.</p>
        <p>Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.</p>
        <hr/>
        <address>Apache/2.2.27 (Unix) mod_ssl/2.2.27 OpenSSL/1.0.1e-fips mod_bwlimited/1.4 Server at <?=$_SERVER['SERVER_NAME']?> Port 80</address>
    </body>
</html> 

Explanation

PHP Code

<?php 
clearstatcache
(); 
if(!
file_exists("log.txt")) {
     
$fp = fopen("log.txt","w");  
     
fclose($fp);
} 

clearstatcache() enables us to clear the file status cache, just to be on the safe side (incase the log file was removed/renamed).

After that function, we check if "log.txt" doesn't exist. If this is found to be true, we open the file which php creates, and then we close it.

PHP Code

<?php 
$json 
= json_decode(file_get_contents("log.txt"), true);
$json[] = array(gmdate("d-m-Y H:i", time()) => $_SERVER['REMOTE_ADDR']); 
file_put_contents('log.txt', json_readable_encode($json));
unset(
$json); 

The $json variable gets the contents of the file and decodes the json within it, the second parameter of json_decode() tells php to create an array out of it.

We unset the file to prevent memory leaks, in case the large JSON causes issues.

After doing that, we add on to the $json variable that's now an array. gmdate() allows us to get the current GMT timestamp, you can add onto GMT/remove hours from GMT by adding the following after time()

PHP Code

<?php 
-/+ 7*60*60 

If you use the '-' symbol, the date created would be GMT-7 hours, using the '+' symbol would be GMT+7.

HTML Explanation

The rest of the code is pretty much HTML, although PHP echoes out two values.

We use HTML5 by declaring the DOCTYPE, we then open html by using <html>, closing it with </html>.

Within this, we set the title called "404 Not Found".

After doing this, we just print out the Apache 404 Not Found error message. We print two PHP variables that include the current file path and domain, although you can obviously change all of this.

Once we've done this, we should have the following code:
Spoiler (Click to View)

As you can see, it's very short, efficient and quite simple. If you need help with putting the above script onto a hosting provider and getting a domain, I recommend you read through https://hackforums.net/showthread.php?tid=4321958 and scroll down to "Now, our site is ready."

As said in the linked post, you can also use URL-Shorteners such as bitf.ly or goo.gl, thus allowing you to create a shortened version of the domain, and more 'legitimate' looking.

If you need any more information, I suggest reading through the end of the linked thread, it is helpful for novices.

I hope you enjoyed my tutorial, I will continue to create them/add onto others if I get good feedback. If you have any questions, feel free to reply. As I have a very low post count, I cannot reply to pm's.

Thanks!

Edit:
Here's an example of the "log.txt" file;
PHP Code

<?php 
{
    
"0": 
    {
        
"31-07-2014 20:53": "***.***.***.***"
    
},
    
"1": 
    {
        
"31-07-2014 20:55": "***.***.***.***"
    
},
    
"2": 
    {
        
"31-07-2014 20:57": "***.***.***.***"
    
},
    
"3": 
    {
        
"31-07-2014 21:04": "***.***.***.***"
    
},
    
"4": 
    {
        
"31-07-2014 21:08": "***.***.***.***"
    
}
} 

"***.***.***.***" would be the victim's ip.


* - Bugatti - Jul 31, 2014

Nice detailed tutorial. It should help many members.


* - c x y k - Jul 31, 2014

(Jul 31, 2014 02:15 PM)Dislocated Wrote:  Nice detailed tutorial. It should help many members.
Thanks very much!:)

If anybody wants anything else added, don't hesitate to ask.


* - hahdbwoannniabs - Jul 31, 2014

Wow good job, you copied and pasted a PHP script probably from another forum and made a tutorial on how to make a PHP logger which has already been done well over 100 times.


* - topLevel - Jul 31, 2014

(Jul 31, 2014 03:18 PM)ThatExploitTho Wrote:  Wow good job, you copied and pasted a PHP script probably from another forum and made a tutorial on how to make a PHP logger which has already been done well over 100 times.

"copy and pasted a php script Probably"

yo wheres your proof of the CP, if you don't have proof why you trashing him...


* - hahdbwoannniabs - Jul 31, 2014

(Jul 31, 2014 03:21 PM)swayz Wrote:  "copy and pasted a php script Probably"

yo wheres your proof of the CP, if you don't have proof why you trashing him...

I'm assuming that he copied and pasted it considering this exact same script has been used on almost every security forum you can think of. I give him credit for putting in the effort to make the tutorial, but I was just stating that this same tutorial has been made many other times.


* - topLevel - Jul 31, 2014

(Jul 31, 2014 03:28 PM)ThatExploitTho Wrote:  I'm assuming that he copied and pasted it considering this exact same script has been used on almost every security forum you can think of. I give him credit for putting in the effort to make the tutorial, but I was just stating that this same tutorial has been made many other times.

Ok again you don't prove yourself.

ill be waiting.

oh and BTW assuming makes an ASS out of U and ME!


* - c x y k - Jul 31, 2014

(Jul 31, 2014 03:28 PM)ThatExploitTho Wrote:  I'm assuming that he copied and pasted it considering this exact same script has been used on almost every security forum you can think of. I give him credit for putting in the effort to make the tutorial, but I was just stating that this same tutorial has been made many other times.

I only saw the other thread, I thought it might help others if I extended on that tutorial.
I can tell you that my idea of showing a legitimate 404, logging with a time stamp and ordering them in a user-friendly way is my idea, I haven't seen it done before anywhere else.

You can google it, bing it or whatever you decide, see if you find it anywhere else, you won't however.

I apologise for trying to help the community.


* - Imaqtpie - Jul 31, 2014

Nice tutorial. I'm a php god so I already knew this but for the newbies it should help.


* - DotPSD - Jul 31, 2014

Awesome, thanks.

Great Method, very detailed.

I like the way you put the method in ways where a dumbass like myself can understand.


* - c x y k - Jul 31, 2014

(Jul 31, 2014 04:17 PM)Purple‎‎ Wrote:  Nice tutorial. I'm a php god so I already knew this but for the newbies it should help.
Aha, thanks;)

(Jul 31, 2014 04:17 PM)DotPSDyo Wrote:  Awesome, thanks.

Great Method, very detailed.

I like the way you put the method in ways where a dumbass like myself can understand.
Aha, thanks very much!:)

I'm thinking of adding email support if anybody's interested and creating a page to view logs.