forked from infobyte/faraday
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcfdbToCsv.py
executable file
·145 lines (108 loc) · 3.86 KB
/
cfdbToCsv.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/env python2.7
'''
Copyright (C) 2016 Infobyte LLC (http://www.infobytesec.com/)
Author: Ezequiel Tavella
See the file 'doc/LICENSE' for the license information
This script generate a CSV file with information about the cfdb database.
CSV Format:
cwe,name,description,resolution,exploitation,references
'''
from subprocess import call
from os import walk
import csv
URL_PROYECT = 'https://github.com/mubix/cfdb'
DB_PATH = './cfdb/'
class parseFile():
def __init__(self, file_md):
self.cwe = ''
self.name = None
self.description = None
self.resolution = None
self.explotation = None
self.references = None
self.file = file_md
self.parse()
def getContent(self):
result = []
while True:
subLine = self.file.readline().strip('\n\r')
if subLine != '\n':
#If EOF -> break
if subLine == '' :
break
if not subLine.startswith('##') :
result.append(subLine)
else:
break
return ''.join(result)
def parse(self):
line = self.file.readline()
while line != '':
title = line.startswith('Title: ')
description = line.startswith('Description: ')
resolution = line.startswith('## Remediation')
references = line.startswith('## References')
explotation = line.startswith('## Exploitation')
#Slice title... read line and continue with other line
if title:
self.name = line[title + 6:].strip('\n\r')
line = self.file.readline()
continue
#Read first line with \n and read the content
elif description:
line = self.file.readline()
self.description = self.getContent()
elif resolution:
line = self.file.readline()
self.resolution = self.getContent()
elif references:
line = self.file.readline()
self.references = self.getContent()
elif explotation:
line = self.file.readline()
self.explotation = self.getContent()
#Nothing here...read line
else:
line = self.file.readline()
def main():
#Get DB cfdb
print '[*]Execute git clone...'
return_code = call(['git', 'clone', URL_PROYECT])
if return_code != 0 and return_code != 128:
print '[!]Error:\n Git return code: ' + str(return_code)
file_csv = open('cfdb.csv','w')
file_csv.write(
'cwe,name,description,resolution,exploitation,references\n'
)
#CSV Writer
writer = csv.writer(
file_csv,
quotechar = '"',
delimiter = ',',
quoting = csv.QUOTE_ALL
)
#Get DB names...
print '[*]Looking for DBs...'
for (root, dirs, files) in walk(DB_PATH):
#Jump dirs without info
if root.find('.git') < 0 and root.find('.gitignore') < 0:
if root != './cfdb/':
print '[*]Parsing folder: ' + root
for file_db in files:
print '[_]File: ' + root + '/' + file_db
with open(root + '/' + file_db, 'r') as file_md:
csv_content = parseFile(file_md)
result = (
csv_content.cwe,
csv_content.name,
csv_content.description,
csv_content.resolution,
csv_content.explotation,
csv_content.references
)
writer.writerow(result)
print '[*]Parse folder finished...\n'
print '[*]All Finished... OK'
file_csv.close()
if __name__ == '__main__':
main()