PHP ile MAC<->IP<->HostName dönüşümü yapmak

PHP uygulamasının yalnızca belli bilgisayarlar tarafından kullanılmasını sağlamak için MAC (ethernet adresi) adresi kontrolü yapabilirsiniz. Benzer kontrol IP adresi ve Hostname kontrolü ile de gerçekleştirilebilir. Ancak güvenlik açısından kritik bir uygulamaya erişim sağlatıyorsanız söz konusu kontrollerin tamamen yetersiz olduğunu bilmek gerekir.Çünkü MAC, IP ve Hostname gibi adresler kullanıcı tarafından kolayca değiştirilebilir. Ancak bu kontrolleri kullanıcıyı rahatsız etmeden mevcut kimlik doğrulama prosedürüne  ekleyebilirsiniz. Ayrıca MAC adresi sadece yerel ağlarda geçerli olan bir parametredir, internet ortamında var olmadığını ve kontrol edemeyeceğinizi hatırlamalısınız.

Linux sunucularda  IP adresinden MAC adresini getirmek için:

<?php

/*
 $id: ip2mac.php, v 0.1 2006/02/13 23:32 Nightwalker <[email protected]> Exp $

 Relased on BSD License

 Copyright (c) 2007, Nightwalker
 All rights reserved.

 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions are met:

		* Redistributions of source code must retain the above copyright notice,
		this list of conditions and the following disclaimer.
		* Redistributions in binary form must reproduce the above copyright notice,
		this list of conditions and the following disclaimer in the documentation
		and/or other materials provided with the distribution.
		* Neither the name of the <ORGANIZATION> nor the names of its contributors
		may be used to endorse or promote products derived from this software
		without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.

 Usage example:

		ip2mac( "192.168.4.128", "eth1" );
*/

		function ip2mac( $ip, $ifc )
		{
				# path to arping.
				$arping = "/sbin/arping";

				# arguments for arping.
				$args   = " -c 1 -I {$ifc} {$ip} -f";

				exec( $arping . $args . " | grep -o ..:..:..:..:..:..", $response );

				return $response[0];
		}

?>

IP adresinden Hostname’i getirmek için;

function getip() {
		 if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown"))
		 $ip = getenv("HTTP_CLIENT_IP");
	  
		 else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown"))
		 $ip = getenv("HTTP_X_FORWARDED_FOR");
	  
		 else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown"))
		 $ip = getenv("REMOTE_ADDR");
	  
		 else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown"))
		 $ip = $_SERVER['REMOTE_ADDR'];
	  
		 else
		 $ip = "unknown";
	  
		 return(trim($ip));
	} //getip

	function nslookup($ip) {
		// execute nslookup command
		exec('nslookup '.$ip, $op);
		// php is running on windows machine
		if (substr(php_uname(), 0, 7) == "Windows") {
			return substr($op[3], 6);
		}
		else {
			// on linux nslookup returns 2 diffrent line depending on
			// ip or hostname given for nslookup
			if (strpos($op[4], 'name = ') > 0)
				return substr($op[4], strpos($op[4], 'name =') + 7, -1);
			else
				return substr($op[4], strpos($op[4], 'Name:') + 6);
		}
	}

	// example function call to get hostname of user ip:
	$ComputerName = nslookup( getip() );

IP’den MAC adresini getirmek için:

$ipAddress=$_SERVER['REMOTE_ADDR'];
$macAddr=false;

#run the external command, break output into lines
$arp=`arp -a $ipAddress`;
$lines=explode("\n", $arp);

#look for the output line describing our IP address
foreach($lines as $line)
{
$cols=preg_split('/\s+/', trim($line));
if ($col[0]==$ipAddress)
{
$macAddr=$col[1];
}
}

Yazar: Özgür Koca

Yazar - Tankado.com

Bir cevap yazın

E-posta hesabınız yayımlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir

This site uses Akismet to reduce spam. Learn how your comment data is processed.