pwn*_*oot 9 python python-3.x toml
我正在用 python 编写一个扫描器,它将收集有关目标的各种信息,例如开放端口、版本信息等。还使用保存单个扫描的配置设置的 toml 文件。
我需要一种方法来存储扫描结果。到目前为止,我正在使用一个包含所有目标数据的类。有没有办法将结果存储在文件中并让库函数根据要求解析和打印它们?
在 toml 表示中我正在考虑类似的事情
[target]
ip = xx.xx.xx.xx
[target.os]
os = 'win 10'
Arch = 'x64'
[target.ports]
ports = ['1', '2']
[target.ports.1]
service = 'xxx'
ver = '5.9'
Run Code Online (Sandbox Code Playgroud)
有没有办法以这种方式将扫描结果转储到 toml 文件?或者还有其他方法可以做得更好吗?
Mus*_*ish 10
toml库可以为您做到这一点。还有其他类似的方法json,pyyaml其工作方式几乎相同。在您的示例中,您首先需要按以下格式将信息存储在字典中:
data = {
"target": {
"ip": "xx.xx.xx.xx",
"os": {
"os": "win 10",
"Arch": "x64"
},
"ports": {
"ports": ["1", "2"],
"1": {
"service": "xxx",
"ver": "5.9",
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后,你可以这样做:
import toml
toml_string = toml.dumps(data) # Output to a string
output_file_name = "output.toml"
with open(output_file_name, "w") as toml_file:
toml.dump(data, toml_file)
Run Code Online (Sandbox Code Playgroud)
同样,您也可以使用以下命令将 toml 文件加载为字典格式:
import toml
toml_dict = toml.loads(toml_string) # Read from a string
input_file_name = "input.toml"
with open(input_file_name) as toml_file:
toml_dict = toml.load(toml_file)
Run Code Online (Sandbox Code Playgroud)
如果toml您不想使用yamlor json,则只需在所有命令中替换toml为yamlor即可。json它们都使用相同的调用约定。