Open port scanner for fsockopen() function

炎欲天舞
Release: 2023-03-15 12:36:01
Original
1584 people have browsed it

Use the fsockopen() function to open the port scanner


1. Foreword

This article uses the fsockopen() function to write a port scan with a simple function device.

2. Key Technology

The port number of this example is fixed. By traversing the array, the fsockopen() function is used to connect. If the connection is successful, the port is open, otherwise The port is closed.

The core code is as follows:


foreach ($port as $key => $value) {
    echo &#39;<tr>&#39;;
    echo &#39;<td>&#39; . $key . &#39;</td>&#39;;
    echo &#39;<td>&#39; . $value . &#39;</td>&#39;;
    echo &#39;<td>&#39; . $msg[$key] . &#39;</td>&#39;;    //$errno 和 $errstr 在这里基本用不上,只是为了设置 timeout,防止请求超时
    $fp = @fsockopen($ip, $value, $errno, $errstr, 1);//如果主机(hostname)不可访问,将会抛出一个警告级别(E_WARNING)的错误提示。所有需要加@
    $result = $fp ? &#39;<span style="color:red">开启</span>&#39; : &#39;<span style="color:red">关闭</span>&#39;;
    echo &#39;<td>&#39; . $result . &#39;</td>&#39;;
    echo &#39;</tr>&#39;;
}
Copy after login

3. The code is as follows


<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>端口扫描</title>
    <style type="text/css">
        td{
            padding:10px;
            border-bottom:1px solid #eee;
        }
    </style>
</head>
<body>
    <form method="post" action=&#39;#&#39;>
        网址/ip:<input type="text" name="ip" value="<?php echo  $_POST[&#39;ip&#39;] ?? &#39;127.0.0.1&#39;?>">
        <button>扫描</button>
    </form>
    <table>
        <thead>
            <tr>
                <td>id</td>
                <td>端口号</td>
                <td>服务</td>
                <td>开启状态</td>
            </tr>
        </thead>
        <tbody>
            <?php  
                $ip = $_POST[&#39;ip&#39;] ?? &#39;127.0.0.1&#39;;
                if(ip2long($ip)){
                    $aIp = explode(&#39;.&#39;, $ip);//ip4地址使用.分隔符
                    //这里没有对 0.0.0.0 这种本机地址进行判断,只是粗略的判断ip是否合法 
                    foreach ($aIp as $key => $value) {
                        if($value < 0 || $value > 255){
                            die(&#39;地址不合法&#39;);
                        }
                    }
                }
                
                $port = array(
                    21, 
                    23, 
                    25,
                    79,
                    80, 
                    110, 
                    135, 
                    137, 
                    138, 
                    139, 
                    143, 
                    443, 
                    445, 
                    1433, 
                    3306, 

                );
                $msg = array(
                    &#39;Ftp&#39;,
                    &#39;Telnet&#39;,
                    &#39;Smtp&#39;,
                    &#39;Finger&#39;,
                    &#39;Http&#39;,
                    &#39;Pop3&#39;,
                    &#39;Location Service&#39;,
                    &#39;Netbios-NS&#39;,
                    &#39;Netbios-DGM&#39;,
                    &#39;Netbios-SSN&#39;,
                    &#39;IMAP&#39;,
                    &#39;Https&#39;,
                    &#39;Microsoft-DS&#39;,
                    &#39;MSSQL&#39;,
                    &#39;MYSQL&#39;,
                    &#39;Terminal Services&#39;
                );
                //无论使用prot还是msg循环都是可以的,因为$key是对应的,都是索引数组
                foreach ($port as $key => $value) {
                    echo &#39;<tr>&#39;;
                    echo &#39;<td>&#39; . $key . &#39;</td>&#39;;
                    echo &#39;<td>&#39; . $value . &#39;</td>&#39;;
                    echo &#39;<td>&#39; . $msg[$key] . &#39;</td>&#39;;
                    //$errno 和 $errstr 在这里基本用不上,只是为了设置 timeout,防止请求超时
                    $fp = @fsockopen($ip, $value, $errno, $errstr, 1);//如果主机(hostname)不可访问,将会抛出一个警告级别(E_WARNING)的错误提示。所有需要加@
                    $result = $fp ? &#39;<span style="color:red">开启</span>&#39; : &#39;<span style="color:red">关闭</span>&#39;;
                    echo &#39;<td>&#39; . $result . &#39;</td>&#39;;
                    echo &#39;</tr>&#39;;
                }

            ?>
        </tbody>
    </table>
</body>
</html>
Copy after login
因为偷懒,把页面和结果都写在一起了,布局就将就把。
Copy after login

4. Introduction to main functions

4.1. fsockopen

Creates a connection based on a host name and returns a resource object successfully, otherwise it fails Returns false; if the host is unavailable, a warning is thrown

The above is the detailed content of Open port scanner for fsockopen() function. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!