DNS 침투를 따뜻하게 지켜보는 명령줄 도구

16420 단어 server네트워크PHP
서버 이전이나 했을 때에, 어느 정도까지 DNS 정보가 갱신되고 있는지 알고 싶을 때에 사용합니다.

2012-04-04 추가:

댓글에서 geekpage는이 도구의 문제를 지적했습니다. 코멘트를 참조하신 후, 각자의 수정하는 되어 상황에 대응해 주세요m(_ _)m 지적해 주신 geekpage씨 감사합니다!



dns-penetration-monitor.php
#!/usr/bin/env php
<?php

//$monitor = new DNSPenetrationMonitor(ドメイン, 新しいIP);
$monitor = new DNSPenetrationMonitor('suin.asia', '59.106.173.151');
$monitor->monitor();

class DNSPenetrationMonitor
{
    protected static $providers = array(
        '202.234.232.6'     => 'OCN',
        '221.113.139.250'   => 'OCN',
        '210.196.3.183'     => 'auone',
        '210.141.112.163'   => 'auone',
        '202.248.37.74'     => '@nifty',
        '202.248.20.133'    => '@nifty',
        '202.238.95.24'     => 'So-net',
        '202.238.95.26'     => 'So-net',
        '143.90.130.165'    => 'odn',
        '143.90.130.39'     => 'odn',
        '8.8.8.8'           => 'google',
        'dns3.sakura.ad.jp' => 'sakura',
        'exnsa.plala.or.jp' => 'plala',
        '198.153.192.1'     => 'Norton',
        '198.153.194.1'     => 'Norton',
        '208.67.222.222'    => 'OpenDNS',
        '208.67.220.220'    => 'OpenDNS',
        '4.2.2.1'           => 'GTEIDNS',
        '4.2.2.2'           => 'GTEIDNS',
        '156.154.70.1'      => 'Dnsadvantage',
        '156.154.71.1'      => 'Dnsadvantage',
    );

    protected $hostName   = '';
    protected $newAddress = '';
    protected $intervalSeconds = 5;
    protected $providerStringLength = 0;

    /**
     * @param string $hostName
     * @param string $newAddress
     * @param int $intervalSeconds
     */
    public function __construct($hostName, $newAddress, $intervalSeconds = 5)
    {
        $this->hostName = $hostName;
        $this->newAddress = $newAddress;
        $this->intervalSeconds = $intervalSeconds;
        $this->providerStringLength = $this->_calculateProviderStringLength();
    }

    /**
     * @return void
     */
    public function monitor()
    {
        $this->_print("Start monitor: {$this->hostName}");
        $this->_print("To quit: Ctrl + C");

        while ( true )
        {
            $this->lookup();
            $this->_print("Sleep {$this->intervalSeconds} seconds...");
            sleep($this->intervalSeconds);
        }
    }

    /**
     * @return void
     */
    public function lookup()
    {
        foreach ( self::$providers as $providerAddress => $providerName )
        {
            $currentAddress = $this->_getAddress($this->hostName, $providerAddress);
            $this->_printLookupResult($currentAddress, $providerAddress);
        }
    }

    protected function _calculateProviderStringLength()
    {
        $maxLength = 0;

        foreach ( self::$providers as $address => $name )
        {
            $length = strlen($address.$name);

            if ( $maxLength < $length )
            {
                $maxLength = $length;
            }
        }

        return $maxLength;
    }

    protected function _isNewAddress($currentAddress)
    {
        return ( strpos($currentAddress, $this->newAddress) !== false );
    }

    protected function _getAddress($hostName, $nameServerName)
    {
        $command = sprintf('nslookup %s %s', $hostName, $nameServerName);
        $output  = array();
        exec($command, $output);
        $output = array_filter($output);
        return array_pop($output);
    }

    protected function _getStatusString($currentAddress)
    {
        if ( $this->_isNewAddress($currentAddress) )
        {
            return  "\033[32mNEW\033[0m";
        }

        return "\033[31mOLD\033[0m";
    }

    protected function _printLookupResult($currentAddress, $providerAddress)
    {
        $now = date('Y-m-d H:i:s');
        $status = $this->_getStatusString($currentAddress);
        $providerName = self::$providers[$providerAddress];
        $providerString = str_pad("$providerName($providerAddress)", $this->providerStringLength + 2, ' ', STR_PAD_RIGHT);
        $message = "[$now] $providerString $status $currentAddress";
        $this->_print($message);
    }

    protected function _print($message)
    {
        file_put_contents('php://stdout', $message.PHP_EOL);
    }
}

좋은 웹페이지 즐겨찾기