【问题标题】:Python CGI script to scan remote HTTP Client用于扫描远程 HTTP 客户端的 Python CGI 脚本
【发布时间】:2014-03-11 19:13:15
【问题描述】:

我在让这个 python web 脚本运行并在网页上打印结果时遇到了一些麻烦。这是我正在玩的一个小项目,它将在 OpenWRT 路由器上运行。

当无线客户端连接到接入点时,CGI 脚本将针对从 HTTP 会话中检索到的设备 IP 地址运行,并显示是否有任何端口打开,这可能会使个人对公共网络进行某种攻击。

我可以通过wireshark 数据包捕获确认端口扫描正常运行。

此时我不断收到一个 CGI 错误,说明 Nmap 语法存在错误 - 尽管我知道这在 Python shell 中有效

我得到的错误是 -

A problem occurred in a Python script. Here is the sequence of function calls leading up to the error, in the order they occurred.
 /Applications/XAMPP/xamppfiles/cgi-bin/test.py in ()
     20 for client in nm.scan():
     21         print('----------------------------------------------------')
=>   22         print('Host : %i' % (nm[client].all_protocols().keys))
     23 """
     24 
nm = <nmap.nmap.PortScanner object>, client = 'nmap', ].all_protocols undefined
 /Applications/XAMPP/xamppfiles/cgi-bin/build/bdist.macosx-10.8-intel/egg/nmap/nmap.py in __getitem__(self=<nmap.nmap.PortScanner object>, host='nmap')

<type 'exceptions.KeyError'>: 'nmap'
      args = ('nmap',)
      message = 'nmap' 

这是我的代码

#!/usr/bin/env python2.7
import cgi
import os
import nmap
import cgitb; cgitb.enable()  # for troubleshooting

print "Content-type: text/html"
print
print """
<html>
<head><title>Sample CGI Script</title></head>
<body>
<h3> Sample CGI Script </h3>
"""
client = cgi.escape(os.environ["REMOTE_ADDR"])
print ('your IP Address is: ' + client)
nm = nmap.PortScanner()
nm.scan(client, '21')

for client in nm.scan():
    print('----------------------------------------------------')
    print('Host : %i' % (nm[client].all_protocols().keys))
"""

</body> 
</html>
 """

【问题讨论】:

    标签: python python-2.7 cgi web.py nmap


    【解决方案1】:
    #!/usr/bin/env python
    
    """ Nmap CGI """
    
    import cgi
    import os
    from libnmap.process import NmapProcess
    from libnmap.parser import NmapParser
    
    print 'Content-Type: text/html'
    print # Blank line marking end of HTTP headers
    
    def main(target):
        """ I'm a lumberjack and i'm ok """
        nmap = NmapProcess(target, options="-T5 -p 21")
        nmap.run()
        nmap_report = NmapParser.parse(nmap.stdout)
    
        for host in nmap_report.hosts:
            address = host.address
            hostname = host.hostnames[0]
            ports = host.get_ports()
            ports = '<br/>'.join(["Port: %s Proto: %s<br/>" % (_id, name)
                                for (_id, name) in host.get_ports()])
    
            print """
            <center><hr style='margin-top:5%'>
            Hostname: {hostname} <br/> Address: {address} <br>
            Open ports: {ports}
            <hr>
            </center>
            """.format(address=address, ports=ports, hostname=hostname)
    
    main(cgi.escape(os.environ["REMOTE_ADDR"]))
    

    输出

    Content-Type: text/html
    
    
            <center><hr style='margin-top:5%'>
            Hostname: localhost <br/> Address: 127.0.0.1 <br>
            Open ports: Port: 21 Proto: tcp<br/>
            <hr>
            </center>
    

    【讨论】:

      猜你喜欢
      • 2011-06-27
      • 2018-05-25
      • 2011-02-10
      • 2023-03-28
      • 2021-05-18
      • 2015-08-01
      • 2013-07-02
      • 2017-02-21
      • 1970-01-01
      相关资源
      最近更新 更多