有什么方法可以优化/正确完善这个 <50 行脚本吗?

发布于 2024-11-17 09:53:27 字数 1126 浏览 2 评论 0 原文

我仍在学习 python,我决定深入研究的第一个项目是对大型 nmap 日志进行排序,拉出开放端口,并将它们转储到 IP:Port 格式的单独文本文件中。它可以工作,但是有更好的方法来写这个吗?这就是我最终得到的结果:

import sys
import string

"""
Written 6/24/2011 to pull out OPEN ports of an nmap proxy scan
Command:
nmap 218.9-255.0-255.0-255 -p 8080,3128,1080 -M 50 -oG PLog3.txt
"""
if len(sys.argv) != 3:
    print 'Usage: python proxy.py <input file> <output file>'
    print 'nmap 218.1-255.0-255.0-255 -p 8080,3128,1080 -M 50 -oG PLog.txt'
    print 'Example: python ./proxy.py PLog.txt proxies.txt'
    sys.exit(1)

r = open(sys.argv[1], 'r')
o = open(sys.argv[2], 'w')

pat80 = '80/open/'
pat8080 = '8080/open'
pat3128 = '3128/open'

for curline in r.xreadlines():
    sift = string.split(curline, ' ')
    ip = sift[1]

if curline.find(pat3128) >= 0:
    curport = '3128'

elif curline.find(pat8080) >= 0:
    curport = '8080'

elif curline.find(pat80) >= 0:
    curport = '80'

else:
    curport = '100'
    pass


if (curport == '3128') or (curport == '8080') or (curport == '80'):
    o.write(ip + ':' + curport + '\n')
    print ip + ':' + curport

else:
    pass

I'm still learning python, and one of the first projects I decided to dive into was something to sort through large nmap logs, pull out the OPEN ports, and dump them to a separate text file in IP:Port format. It works, but is there a better way to write this? Here's what I ended up with:

import sys
import string

"""
Written 6/24/2011 to pull out OPEN ports of an nmap proxy scan
Command:
nmap 218.9-255.0-255.0-255 -p 8080,3128,1080 -M 50 -oG PLog3.txt
"""
if len(sys.argv) != 3:
    print 'Usage: python proxy.py <input file> <output file>'
    print 'nmap 218.1-255.0-255.0-255 -p 8080,3128,1080 -M 50 -oG PLog.txt'
    print 'Example: python ./proxy.py PLog.txt proxies.txt'
    sys.exit(1)

r = open(sys.argv[1], 'r')
o = open(sys.argv[2], 'w')

pat80 = '80/open/'
pat8080 = '8080/open'
pat3128 = '3128/open'

for curline in r.xreadlines():
    sift = string.split(curline, ' ')
    ip = sift[1]

if curline.find(pat3128) >= 0:
    curport = '3128'

elif curline.find(pat8080) >= 0:
    curport = '8080'

elif curline.find(pat80) >= 0:
    curport = '80'

else:
    curport = '100'
    pass


if (curport == '3128') or (curport == '8080') or (curport == '80'):
    o.write(ip + ':' + curport + '\n')
    print ip + ':' + curport

else:
    pass

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

纸伞微斜 2024-11-24 09:53:27

您可以像这样循环遍历文件。无需使用xreadlines()with 确保当 r 超出范围时关闭文件

with open(sys.argv[1], 'r') as r:
    for curline in r:
        sift = string.split(curline, ' ')
        ip = sift[1]

    ...

在元组中查找比 or 链更整洁

if curport in ('3128', '8080', '80'):

You can loop over a file like this. There is no need to use xreadlines(). with makes sure the file is closed when r goes out of scope

with open(sys.argv[1], 'r') as r:
    for curline in r:
        sift = string.split(curline, ' ')
        ip = sift[1]

    ...

Looking in a tuple is neater than the chain of or

if curport in ('3128', '8080', '80'):
掐死时间 2024-11-24 09:53:27

因为我似乎记得使用 python 解析 nmap 输出文件是我的第一个 python 应用程序之一,所以我可以提出一些建议:
1) 如果您想学习 XML 解析和 python,建议使用替代 XML 格式 nmap。这样做的优点是,与纯文本输出不同,XML 输出不太可能以小但破坏脚本的方式进行更改。 (基本上,字符串字段的匹配对于快速破解来说非常有用,但几乎肯定会在路上咬你,因为我发现 nmap 更新时,它们稍微改变了我正在解析的列之一的格式......我还认为,当我们升级其中一个 Windows 盒子时,操作系统或服务字段中的一些文本与我匹配的内容相匹配。如果您有兴趣沿着这条路走下去,我可以看看我是否有我的 nmap。使用 xpath 的解析器位于

2) 如果你想要为了坚持文本输出和正则表达式,我建议学习分组。
具体来说,您可以定义一个组并进行检查,而不是为每个端口创建自定义模式。

import re
r = re.compile("(/d+)/open") # match one or more digits followed by /open
mm = r.match(line) #mm will either be None or a match result object, if mm is not None, you can do mm.groups()[0] to get the port #.

Since I seem to remember using python to parse nmap output files was one of my first python applications, I can make a couple of recommendations:
1) If you'd like to learn XML parsing and python, using the alternate XML format of nmap would be advised. This has the advantage that the XML output is less like to change in small but script breaking ways unlike the plain text output. (Basically, matching on string fields is great for a quick hack but is almost guaranteed to bite you down the road, as I found out when nmap was updated and they slightly changed the format of one of the columns I was parsing on... also think I got bit when we upgraded one of the Windows boxes and some of the text in the OS or services fields matched something I was matching on. If you're interested in going down this path, I can see if I have my nmap parser using xpath lying around

2) If you want to stick with text output and regexp, I'd suggest learning about grouping.
Specifically, rather than creating custom patterns for each port, you can define a group and check that out instead.

import re
r = re.compile("(/d+)/open") # match one or more digits followed by /open
mm = r.match(line) #mm will either be None or a match result object, if mm is not None, you can do mm.groups()[0] to get the port #.
草莓味的萝莉 2024-11-24 09:53:27
import sys
import string

"""
Written 6/24/2011 to pull out OPEN ports of an nmap proxy scan
Command:
nmap 218.9-255.0-255.0-255 -p 8080,3128,1080 -M 50 -oG PLog3.txt
"""

def get_port(line):
    port_mapping = {
        '80/open/': '80', # Is the backslash special here?
        # If they're really all supposed to have the same form,
        # then we can simplify more.
        '8080/open': '8080',
        '3128/open': '3128'
    }
    for pattern, port in port_mapping:
        if pattern in line: return port
    return None # this would be implied otherwise,
    # but "explicit is better than implicit"
    # and this function intends to return a value.


def main(in_name, out_name):
    with file(in_name, 'r') as in_file:
        ips = (get_port(line.split(' ')[1]) for line in in_file)
        with file(out_name, 'w') as out_file:
            for ip in ips:
                if ip == None: continue
                output = '%s:%s' % (ip, curport)
                out_file.write(output + '\n')
                print output


def usage():
    print 'Usage: python proxy.py <input file> <output file>'
    print 'nmap 218.1-255.0-255.0-255 -p 8080,3128,1080 -M 50 -oG PLog.txt'
    print 'Example: python ./proxy.py PLog.txt proxies.txt'


if __name__ == '__main__':
    if len(sys.argv) != 3: usage()
    else: main(*sys.argv[1:])
import sys
import string

"""
Written 6/24/2011 to pull out OPEN ports of an nmap proxy scan
Command:
nmap 218.9-255.0-255.0-255 -p 8080,3128,1080 -M 50 -oG PLog3.txt
"""

def get_port(line):
    port_mapping = {
        '80/open/': '80', # Is the backslash special here?
        # If they're really all supposed to have the same form,
        # then we can simplify more.
        '8080/open': '8080',
        '3128/open': '3128'
    }
    for pattern, port in port_mapping:
        if pattern in line: return port
    return None # this would be implied otherwise,
    # but "explicit is better than implicit"
    # and this function intends to return a value.


def main(in_name, out_name):
    with file(in_name, 'r') as in_file:
        ips = (get_port(line.split(' ')[1]) for line in in_file)
        with file(out_name, 'w') as out_file:
            for ip in ips:
                if ip == None: continue
                output = '%s:%s' % (ip, curport)
                out_file.write(output + '\n')
                print output


def usage():
    print 'Usage: python proxy.py <input file> <output file>'
    print 'nmap 218.1-255.0-255.0-255 -p 8080,3128,1080 -M 50 -oG PLog.txt'
    print 'Example: python ./proxy.py PLog.txt proxies.txt'


if __name__ == '__main__':
    if len(sys.argv) != 3: usage()
    else: main(*sys.argv[1:])
橘寄 2024-11-24 09:53:27

查看 argparse 来处理参数。

拆分为函数。

使用ma​​in结构。

查看 csv 模块。您可以将分隔符设置为空格。

再看看 re 表达式。您可以使用一个 re 表达式来完成此操作,其中它是不同模式的“或”。

Check out argparse for handling the arguments.

Split into functions.

Use the main construct.

Look at the csv module. You can set the delimiter to a space.

Look again at the re expression. You can do it with one re expression where it is an 'or' of the different patterns.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文