python任务一

填充文档这个事情呢

应该是我理解的填充文档吧???

就是只用填空就好了吧?

不管了

(摊手)

##端口扫描

通过Python的网络连接来开发一个基础的端口扫描器,我们的设计思路是使用socket一遍又一遍的去连接ip与端口的组合的新值,为了方面我们能够快速的完成它,首先需要了解一下的概念,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连接。

先import这socket模块并且调用connect()函数去连接指定的IP地址与端口。它就会建立一个TCP连接(SYN/SYN-ACK/ACK)并且我们再通过send()函数给服务器发送一个真实的数据,然后使用recv()打印出响应的内容。

import socket

s = socket.socket()

s.connect((‘127.0.0.1s’, 22))

s.send(‘Primal Security \n’)

17

banner = s.recv(1024)

print banner

OpenSSH

对于不能打开的连接: >>>

s.connect((‘127.0.0.1’, 23))

Traceback (most recent call last):

File “”, line 1, in ?

File “”, line 1, in connect

socket.error: (111, ‘Connection refused’)

使用"try/except"循环来处理错误: >>>

try:


s.connect((‘127.0.0.1’, 23))

… except: pass

端口号,我们使用数组来存储,然后遍历这一个数组: >>>

ports = [22, 445, 80, 443,
3389]

for port in ports:


print port

22

445

80

443

3389

如果我们想一次性扫描多台主机,可以使用一个for循环嵌套。最外层的是主机的ip,然后里面的for循环是端口。

hosts = [‘127.0.0.1’,
‘192.168.1.5’, ‘10.0.0.1’]

ports = [22, 445, 80, 443,
3389]

for host in hosts:


for port in ports:


try:


print “[+] Connecting to “+host+”:”+str(port)


s.connect((host, port))


s.send(‘Primal Security \n’)


banner = s.recv(1024)


if banner:


print "[+] Port “+str(port)+” open: "+banner


s.close()


except:pass

[+] Connecting to 127.0.0.1:22

[+] Port 22 open: OpenSSH

[+] Connecting to 127.0.0.1:445

[+] Connecting to 127.0.0.1:80

[+] Connecting to 127.0.0.1:443

[+] Connecting to 127.0.0.1:3389

[+] Connecting to 192.168.1.5:22

[+] Connecting to 192.168.1.5:445

[+] Connecting to 192.168.1.5:80

[+] Connecting to 192.168.1.5:443

[+] Connecting to 192.168.1.5:3389

[+] Connecting to 10.0.0.1:22

[+] Connecting to 10.0.0.1:445

[+] Connecting to 10.0.0.1:80

[+] Connecting to 10.0.0.1:443

[+] Connecting to 10.0.0.1:3389

使用"dir(socket)"来了解更多,当然还有’help()’.

做得不好也别打我啊

python任务一