Home>Article>Backend Development> fsockopen() function creates a port scanner

fsockopen() function creates a port scanner

大家讲道理
大家讲道理 Original
2017-08-19 09:30:03 1842browse


1. Preface

This article uses the fsockopen() function to write a port scanner with simple functions.

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 ''; echo '' . $key . ''; echo '' . $value . ''; echo '' . $msg[$key] . ''; //$errno 和 $errstr 在这里基本用不上,只是为了设置 timeout,防止请求超时 $fp = @fsockopen($ip, $value, $errno, $errstr, 1);//如果主机(hostname)不可访问,将会抛出一个警告级别(E_WARNING)的错误提示。所有需要加@ $result = $fp ? '开启' : '关闭'; echo '' . $result . ''; echo ''; }

3. The code is as follows

##

    端口扫描  
网址/ip:
$value) { if($value < 0 || $value > 255){ die('地址不合法'); } } } $port = array( 21, 23, 25, 79, 80, 110, 135, 137, 138, 139, 143, 443, 445, 1433, 3306, 3389 ); $msg = array( 'Ftp', 'Telnet', 'Smtp', 'Finger', 'Http', 'Pop3', 'Location Service', 'Netbios-NS', 'Netbios-DGM', 'Netbios-SSN', 'IMAP', 'Https', 'Microsoft-DS', 'MSSQL', 'MYSQL', 'Terminal Services' ); //无论使用prot还是msg循环都是可以的,因为$key是对应的,都是索引数组 foreach ($port as $key => $value) { echo ''; echo ''; echo ''; echo ''; //$errno 和 $errstr 在这里基本用不上,只是为了设置 timeout,防止请求超时 $fp = @fsockopen($ip, $value, $errno, $errstr, 1);//如果主机(hostname)不可访问,将会抛出一个警告级别(E_WARNING)的错误提示。所有需要加@ $result = $fp ? '开启' : '关闭'; echo ''; echo ''; } ?>
id 端口号 服务 开启状态
' . $key . '' . $value . '' . $msg[$key] . '' . $result . '
4. Introduction to the main functions

4.1, fsockopen

Creates a connection based on a host name, returns a resource object successfully, returns false on failure; if the host is unavailable, a warning is thrown

For details, refer to: http:// php.net/manual/en/function.fsockopen.php

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

Statement:
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