只能将str(而不是“超时”)连接到str

 收藏

Working on converting old Python 2 banner grab script to Python 3. Had some moderate success thus far working through simple syntax discrepancies, but really hitting a wall and Google provides zero answers. The error is can only concatenate str (not "timeout") to str. I cannot find this exact error nearly anywhere, and I honestly am not sure what it is referencing.. I am new to the Python world. Below is the code, with the general idea it will reference a text document with certain banners, and based on the open ports found on my Metasploit 3 VM, it will return those it deems vulnerable. You call this by python script.py within a shell on the machine. (if that helps)

import os
import sys
import socket

def retBanner(ip, port):
    try:
        socket.setdefaulttimeout(2)
        s = socket.socket()
        s.connect((ip, port))
        banner = s.recv(1024)
        return banner
    except Exception as e:
        return (e)


def checkFile(filename):
    try:
        if not os.path.isfile(filename):
            raise Exception("File doesn't exist")
            return -1
        if not os.access(filename, os.R_OK):
            raise Exception("Permission Denied")
        f = open(filename, 'r')
        return f
    except Exception as e:
        print (e)
        return -1


def checkVulns(banner, filename):
    print ("[+] Reading Vulnerable Banner list from: ") + filename
    f = checkFile(filename)
    for line in f.readlines():
        print ("[+] Checking for banner: " + line.strip('\n'))
        if line.strip('\n') in banner:
            print ("[+] Server is Vulnerable: ") + banner.strip('\n')
        else:
            print ('[-] Server is not vulnerable')
    return


def main():
    portList = [21, 22, 25, 80, 110, 443]
    if (len(sys.argv) == 2):
        filename = sys.argv[1]
    else:
        filename = "vuln_banners.txt"
    for x in range(1, 255):
        ip = '192.168.95.' + str(x)
        for port in portList:
            banner = retBanner(ip, port)
            if banner:
                print (('[+] ') + ip + ': ' + banner)
                if (checkVulns(banner, filename) == -1):
                    return


if __name__ == '__main__':
    main()

当然,我敢肯定我还没有找到所有问题,但是除了异常差异,打印语句差异和其他较小的语法差异之外,这就是让我感到高兴的地方。希望这是唯一的问题。先感谢您。

回复
  • Read about Official documents

    有关套接字超时的说明   套接字对象可以处于以下三种模式之一:阻止,非阻止或超时。默认情况下,总是在阻塞模式下创建套接字,但是可以通过调用setdefaulttimeout()来更改套接字。      ·在超时模式下,如果无法在为套接字指定的超时内完成操作(它们会引发超时异常)或系统返回错误,则操作将失败。

    In your code,When your request time out,it will return a TimeOut Exception,And you return it.Now you can change the return value when time out.

    def retBanner(ip, port):
        try:
            socket.setdefaulttimeout(2)
            s = socket.socket()
            s.connect((ip, port))
            banner = s.recv(1024)
            return banner
        except OSError: # Timeout Exception is a subclass of OSError.
            return "TimeOut" # Return a string,not a exception.You can divided it into many exceptions,there is not only Timeout exception.
        except Exception as e:
            print(e.args)