-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjvn_plt_server.py
220 lines (173 loc) · 6.48 KB
/
jvn_plt_server.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#!/usr/bin/python3.6
# -*- coding: utf-8 -*-
#
# JVN Vulnerability Infomation Managed System
#
#
import matplotlib as ml
import matplotlib.pyplot as plt
import pandas as pd
import os
import psycopg2
import configparser
from datetime import datetime
from http.server import HTTPServer, BaseHTTPRequestHandler
import numpy as np
ml.use("Agg")
################################################################################
# const configuration
################################################################################
# 設定ファイルの取り込み
config = configparser.ConfigParser()
config.read(os.path.join(os.path.abspath(os.path.dirname(__file__)), "jvn.conf"))
CONNECTION_CONFIG = {
"host": config.get("db", "host"),
"port": config.get("db", "port"),
"database": config.get("db", "database"),
"user": config.get("db", "user"),
"password": config.get("db", "password"),
}
PORT = int(config.get("plt", "port"))
font = {"family": "VL Gothic"}
ml.rc("font", **font)
plt.rcParams["figure.figsize"] = 12.0, 6.0
################################################################################
# collect data
################################################################################
class JvnItem(object):
def __init__(self, col, label):
self.col = col
self.label = label
class JvnGraph(object):
def __init__(self, df, item1, item2):
"""コンストラクタ"""
self.df = df
self.item1 = item1
self.item2 = item2
def makeDataFrameYear():
"""年別脆弱性件数を取得
脆弱性発見日・IPA公表日の件数を取得する
"""
connection = psycopg2.connect(**CONNECTION_CONFIG)
stmt = """select y, count(y) as cnt
from (select to_char(public_date,'YYYY') as y from jvn_vulnerability) a
group by y order by y;"""
df = pd.read_sql(sql=stmt, con=connection, index_col="y")
stmt = """select y, count(y) as cnt
from (select to_char(issued_date,'YYYY') as y from jvn_vulnerability) a
group by y order by y;"""
issued_df = pd.read_sql(sql=stmt, con=connection, index_col="y")
connection.close()
df["icnt"] = issued_df["cnt"]
return JvnGraph(df, JvnItem("cnt", "発見日"), JvnItem("icnt", "IPA公表日"))
def makeDataFrameCweYear():
"""脆弱性種別数を取得
脆弱性別の件数を取得する
"""
connection = psycopg2.connect(**CONNECTION_CONFIG)
stmt = """select to_char(public_date, 'yyyy') as yyyy,
count(cweid) as bcnt
from jvn_vulnerability
where cweid in ('CWE-119','CWE-120','CWE-121','CWE-122','CWE-124')
group by yyyy order by yyyy"""
buferr = pd.read_sql(sql=stmt, con=connection, index_col="yyyy")
stmt = """select to_char(public_date, 'yyyy') as yyyy,
count(cweid) as ccnt
from jvn_vulnerability
where cweid in ('CWE-79','CWE-80')
group by yyyy order by yyyy"""
xss = pd.read_sql(sql=stmt, con=connection, index_col="yyyy")
connection.close()
df = pd.concat([buferr, xss], axis=1, sort=True)
t = datetime.now()
df = df[df.index.values < str(t.year)]
return JvnGraph(df, JvnItem("ccnt", "クロスサイトスクリプティング"), JvnItem("bcnt", "バッファエラー"))
################################################################################
# display data
################################################################################
# plt.rcParams['axes.prop_cycle'].by_key()['color']
# ['#1f77b4',
# '#ff7f0e',
# ...
# '#17becf']
PLOT_COLOR_1 = "#1f77b4"
PLOT_COLOR_2 = "#ff7f0e"
def makeBarChart(hfd, jg):
"""棒グラフ表示
UI用はこちらの実装
"""
df = jg.df
plt.figure()
plt.tick_params(labelsize=10)
plt.xlabel("年", fontsize=10)
plt.ylabel("件数", fontsize=10)
plt.title("脆弱性発生件数(%s〜%s)" % (df.index.min(), df.index.max()), fontsize=18)
left = np.arange(len(df))
space = 0.4
p1 = plt.bar(
left, df[jg.item1.col], color=PLOT_COLOR_1, width=space, align="center"
)
p2 = plt.bar(
left + space, df[jg.item2.col], color=PLOT_COLOR_2, width=space, align="center"
)
plt.xticks(left + space / 2, df.index)
plt.legend((p1, p2), (jg.item1.label, jg.item2.label), fontsize=8)
plt.savefig(hfd, format="png")
plt.close()
def makeLineChart(hfd, jg):
"""折れ線グラフ表示
UI用はこちらの実装
"""
df = jg.df
plt.figure()
plt.tick_params(labelsize=10)
plt.xlabel("年", fontsize=10)
plt.ylabel("件数", fontsize=10)
plt.title("脆弱性発生件数(%s〜%s)" % (df.index.min(), df.index.max()), fontsize=18)
plt.plot(df[jg.item1.col], color=PLOT_COLOR_1, label=jg.item1.label)
plt.plot(df[jg.item2.col], color=PLOT_COLOR_2, label=jg.item2.label)
plt.legend(bbox_to_anchor=(0, 1.0), loc="upper left", fontsize=8)
plt.savefig(hfd, format="png")
plt.close()
################################################################################
# http handler
################################################################################
class JvnImageHandler(BaseHTTPRequestHandler):
"""Web サーバを実装
API機能を提供する
"""
def do_GET(self):
self.send_response(200)
if self.path == "/barchart":
self.send_header("Content-type", "image/png")
self.end_headers()
makeBarChart(self.wfile, makeDataFrameYear())
elif self.path == "/linechart":
self.send_header("Content-type", "image/png")
self.end_headers()
makeLineChart(self.wfile, makeDataFrameYear())
if self.path == "/cwebarchart":
self.send_header("Content-type", "image/png")
self.end_headers()
makeBarChart(self.wfile, makeDataFrameCweYear())
elif self.path == "/cwelinechart":
self.send_header("Content-type", "image/png")
self.end_headers()
makeLineChart(self.wfile, makeDataFrameCweYear())
else:
self.send_header("Content-type", "text/plain")
self.end_headers()
self.wfile.write(b"Hello\r\n")
################################################################################
# main
################################################################################
if __name__ == "__main__":
handler = JvnImageHandler
server = HTTPServer(("", PORT), handler)
try:
server.serve_forever()
except KeyboardInterrupt:
pass
finally:
server.server_close()