Python_Script - ports scan

退一步自然优雅,
忍一时何等清闲;
让三分依然自在,
耐片刻快活神仙。

英文原文出处

0x0 环境

python3
Windows

0x1 前言

下文主要是使用 python 进行网络连接,同时实现一个简单的端口扫描。

0x2 实验

① 利用 for 循环实现端口遍历

>>> for port in range(1000,1024):
...     print("[+] The port is: " + str(port))
...
[+] The port is: 1000
[+] The port is: 1001
[+] The port is: 1002
[+] The port is: 1003
[+] The port is: 1004
[+] The port is: 1005
[+] The port is: 1006
[+] The port is: 1007
[+] The port is: 1008
[+] The port is: 1009
[+] The port is: 1010
[+] The port is: 1011
[+] The port is: 1012
[+] The port is: 1013
[+] The port is: 1014
[+] The port is: 1015
[+] The port is: 1016
[+] The port is: 1017
[+] The port is: 1018
[+] The port is: 1019
[+] The port is: 1020
[+] The port is: 1021
[+] The port is: 1022
[+] The port is: 1023
>>>

② socket 模块实现 22 端口探测

1. 开启 ssh 服务

在 Windows 下 ssh 开启/关闭的命令如下(以管理员身份运行):

net start sshd # 开启 ssh 服务
net stop sshd # 关闭 ssh 服务

2. python 源码

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import socket

s = socket.socket()
string = 'zhutou'
string = string.encode() # str 转换为 bytes

s.connect(('127.0.0.1',22))
s.send(string)
banner = s.recv(1024)
print(banner.decode())

3. 结果

在这里插入图片描述

4. 分析

在上面的 python 源码中调用了 socket 模块并用 connect() 函数连接本地环回接口和 ssh 服务对应端口号 22,这样就会建立一个相应的 TCP 连接(SYN/SYN-ACK/ACK),用 send() 函数发送数据并用 recv() 函数接收 TCP 套接字的数据(字符串)。下面我们将端口号改成 23(这是一个没有开启的端口),看一看响应的结果:
在这里插入图片描述
这里由于本地的 23 端口没有开放,所以无法进行 TCP 连接,而在端口扫描过程中难免会遇到某些端口没有开放,这个时候就可以使用 try ... except ... 进行绕过错误,继续扫描接下来的端口。

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import socket

s = socket.socket()
string = 'zhutou'
string = string.encode() # str 转换为 bytes

try:
    s.connect(('127.0.0.1',23))
    s.send(string)
    banner = s.recv(1024)
    print(banner.decode())
except:
    pass

在这里插入图片描述
PS_1: 脚本的名字和脚本内使用的模块的名字不能重复,不然 python 解释器会将当前脚本当成模块导入引起错误(例如:在某脚本中导入 socket 模块 import socket,然后脚本的名字又自定义为 socket.py ,而解释器又只认名字不按实际情况,所以出错 )
PS_2 变量的名字不能和函数的名字重复,不然解释器会将变量当成函数。(例如:在某脚本中定义了 str 变量,之后又调用了 str() 函数,由此混淆解释器导致错误)

③ for 循环进行端口探测

1. python 源码

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import socket
import sys

s = socket.socket()
string = 'zhutou'
string = string.encode() # str 转换为 bytes

for port in range(20,26):
    try:
        print("[+] Attempting to connect to 127.0.0.1: " + str(port))
        s.connect(('127.0.0.1',port))
        s.send(string)
        banner = s.recv(1024)
        banner = banner.decode()

        if banner:
            print("[+] Port " + str(port) + " open: " + banner)
        s.close()
    except:
        pass

2. 结果

在这里插入图片描述

④ 指定端口扫描

1. python 源码

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import socket
import sys

s = socket.socket()
string = 'zhutou'
string = string.encode() # str 转换为 bytes
ports = [22, 80, 443, 3306]

for port in ports:
    try:
        print("[+] Attempting to connect to 127.0.0.1: " + str(port))
        s.connect(('127.0.0.1',port))
        s.send(string)
        banner = s.recv(1024)
        banner = banner.decode()

        if banner:
            print("[+] Port " + str(port) + " open: " + banner)
        s.close()
    except:
        pass

2. 结果

在这里插入图片描述

⑤ 结合 sys 模块扫描指定端口

1. python 源码

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import socket
import sys

s = socket.socket()
string = 'zhutou'
string = string.encode() # str 转换为 bytes

for port in range(len(sys.argv)):
    port +=1
    try:
        print("[+] Attempting to connect to 127.0.0.1:" + sys.argv[port])
        s.connect(('127.0.0.1',int(sys.argv[port])))
        s.send(string)
        banner = s.recv(1024)
        banner = banner.decode()
        if banner:
            print("[+] Port " + sys.argv[port] + 'open: ' + banner)
        s.close()
    except:
        pass

2. 结果

PS > python socket_connect.py 22 80 443 3306
[+] Attempting to connect to 127.0.0.1:22
[+] Port 22open: SSH-2.0-OpenSSH_for_Windows_7.7

[+] Attempting to connect to 127.0.0.1:80
[+] Attempting to connect to 127.0.0.1:443
[+] Attempting to connect to 127.0.0.1:3306

⑥ 多主机端口扫描

1. python 源码

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import socket
import sys

s = socket.socket()
string = 'zhutou'
string = string.encode() # str 转换为 bytes

hosts = ['127.0.0.1','192.168.11.143','10.10.16.128']
ports = [22, 80, 443, 3306]

for host in hosts:
    for port in ports:
        try:
            print("[+] Attempting to connect to " + str(host) + ':' + str(port))
            s.connect((host,port))
            s.send(string)
            banner = s.recv(1024)
            banner = banner.decode()

            if banner:
                print("[+] Port " + str(port) + " open: " + banner)
            s.close()
        except:
            pass

2. 结果

在这里插入图片描述

                                                                                                                                                                     猪头
                                                                                                                                                                  2020.3.27
发布了51 篇原创文章 · 获赞 4 · 访问量 2739

猜你喜欢

转载自blog.csdn.net/LZHPIG/article/details/105151089