-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathevaluate.py
More file actions
282 lines (248 loc) · 10.4 KB
/
Copy pathevaluate.py
File metadata and controls
282 lines (248 loc) · 10.4 KB
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#!/usr/bin/env python
###############################################################################
# Git-based CTF
###############################################################################
#
# Author: SeongIl Wi <seongil.wi@kaist.ac.kr>
# Jaeseung Choi <jschoi17@kaist.ac.kr>
# Sang Kil Cha <sangkilc@kaist.ac.kr>
#
# Copyright (c) 2018 SoftSec Lab. KAIST
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import re
import sys
import csv
import json
import time
import calendar
from issue import is_closed, create_comment, close_issue
from issue import create_label, update_label, get_github_issue
from cmd import run_command
from utils import load_config, rmdir, rmfile, iso8601_to_timestamp, is_timeover
from github import Github, get_github_path
from git import clone, checkout, get_next_commit_hash
from verify_issue import verify_issue
msg_file = 'msg' # Temporarily store commit message
def failure_action(repo_owner, repo_name, issue_no, comment, id, github):
create_label(repo_owner, repo_name, "failed", "000000", \
"Verification failed.", github)
update_label(repo_owner, repo_name, issue_no, github, "failed")
create_comment(repo_owner, repo_name, issue_no, comment, github)
close_issue(repo_owner, repo_name, issue_no, github)
mark_as_read(id, github)
def get_target_repos(config):
repos = []
for team in config['teams']:
repos.append(config['teams'][team]['repo_name'])
return repos
def is_issue(noti):
return noti['subject']['type'] == 'Issue'
def is_target(noti, target_repos):
return noti['repository']['name'] in target_repos
def get_issue_number(noti):
return int(noti['subject']['url'].split('/')[-1])
def get_issue_id(noti):
return noti['url'].split('/')[-1]
def get_issue_gen_time(noti):
return iso8601_to_timestamp(noti['updated_at'])
def get_issues(target_repos, github):
issues = []
query = '/notifications'
try:
notifications, interval = github.poll(query)
except ConnectionError:
return [], 60
for noti in reversed(notifications):
if noti['unread'] and is_issue(noti) and is_target(noti, target_repos):
num = get_issue_number(noti)
id = get_issue_id(noti)
gen_time = get_issue_gen_time(noti)
issues.append((noti['repository']['name'], num, id, gen_time))
return issues, interval
def mark_as_read(issue_id, github):
query = '/notifications/threads/' + issue_id
return github.patch(query, None)
def get_defender(config, target_repo):
teams = config['teams']
defender = None
for team in teams:
if teams[team]['repo_name'] == target_repo:
defender = team
break
return defender
def sync_scoreboard(scoreboard_dir):
run_command('git reset --hard', scoreboard_dir)
run_command('git pull', scoreboard_dir)
def write_score(stamp, info, scoreboard_dir, pts):
with open(os.path.join(scoreboard_dir, 'score.csv'), 'a') as f:
attacker = info['attacker']
defender = info['defender']
branch = info['branch']
kind = info['bugkind']
f.write('%s,%s,%s,%s,%s,%d\n' % (stamp, attacker, defender, branch, \
kind, pts))
def write_message(info, scoreboard_dir, pts):
with open(os.path.join(scoreboard_dir, msg_file), 'w') as f:
attacker = info['attacker']
defender = info['defender']
branch = info['branch']
kind = info['bugkind']
f.write('[Score] %s +%d\n\n' % (attacker, pts))
if pts == 0: # Protocol to indicate successfull defense
f.write('%s defended `%s` %s with %s' % (defender, branch, attacker, kind))
else:
f.write('%s attacked `%s` %s of %s' % (attacker, branch, kind, defender))
def commit_and_push(scoreboard_dir):
_, _, r = run_command('git add score.csv', scoreboard_dir)
if r != 0:
print('[*] Failed to git add score.csv.')
return False
_, _, r = run_command('git commit -F %s' % msg_file, scoreboard_dir)
if r != 0:
print('[*] Failed to commit score.csv.')
return False
_, _, r = run_command('git push origin master', scoreboard_dir)
if r != 0:
print('[*] Failed to push the score.')
return False
rmfile(os.path.join(scoreboard_dir, msg_file))
return True
def find_the_last_attack(scoreboard_dir, timestamp, info):
last_commit = None
scoreboard_path = os.path.join(scoreboard_dir, 'score.csv')
if os.path.isfile(scoreboard_path):
with open(scoreboard_path) as f:
reader = csv.reader(f, delimiter=',')
for row in reader:
if int(row[0]) >= timestamp and len(row[4]) == 40:
if row[1] == info['attacker'] and row[2] == info['defender']:
if row[3] == info['branch']:
last_commit = row[4]
return last_commit
def get_next_commit(last_commit, defender, branch, config):
repo_name = config['teams'][defender]['repo_name']
rmdir(repo_name)
clone(config['repo_owner'], repo_name)
next_commit_hash = get_next_commit_hash(repo_name, branch, last_commit)
rmdir(repo_name)
print next_commit_hash
if next_commit_hash == '':
return None
else:
return next_commit_hash
# XXX: Calling verify_issue() multiple times involves redundant process
# internally. We may consider replacing this by calling fetch() once and then
# calling verify_exploit() multiple times.
def process_unintended(repo_name, num, config, gen_time, info, scoreboard, id,
github, repo_owner):
unintended_pts = config['unintended_pts']
target_commit = find_the_last_attack(scoreboard, gen_time, info)
if target_commit is None:
# This exploit is previously unseen, give point.
write_score(gen_time, info, scoreboard, unintended_pts)
write_message(info, scoreboard, unintended_pts)
commit_and_push(scoreboard)
else:
while True:
target_commit = get_next_commit(target_commit, \
info['defender'], info['branch'], config)
if target_commit is None:
print '[*] No more commit to verify against'
break
_, verified_commit, _, _ = \
verify_issue(info['defender'], repo_name, num, config, \
github, target_commit)
info['bugkind'] = target_commit
if verified_commit is None:
# Found a correct patch that defeats the exploit.
current_time = int(time.time())
write_score(current_time, info, scoreboard, 0)
write_message(info, scoreboard, 0)
commit_and_push(scoreboard)
mark_as_read(id, github)
create_label(repo_owner, repo_name, "defended", "0000ff", \
"Defended.", github)
update_label(repo_owner, repo_name, num, github, "defended")
break
else:
# Exploit still works on this commit, update score and continue
write_score(gen_time, info, scoreboard, unintended_pts)
write_message(info, scoreboard, unintended_pts)
commit_and_push(scoreboard)
def process_issue(repo_name, num, id, config, gen_time, github, scoreboard):
repo_owner = config['repo_owner']
if is_closed(repo_owner, repo_name, num, github):
mark_as_read(id, github)
return
title, _, _, _ = get_github_issue(repo_owner, repo_name, num, github)
create_label(repo_owner, repo_name, "eval", "DA0019", \
"Exploit is under review.", github)
update_label(repo_owner, repo_name, num, github, "eval")
defender = get_defender(config, repo_name)
if defender is None:
print '[*] Fatal error: unknown target %s.' % repo_name
sys.exit()
return
branch, commit, attacker, log = verify_issue(defender, repo_name, num, \
config, github)
if branch is None:
log = "```\n" + log + "```"
failure_action(repo_owner, repo_name, num, \
log + '\n\n[*] The exploit did not work.', id, github)
return
if config['individual'][attacker]['team'] == defender:
failure_action(repo_owner, repo_name, num, \
'[*] Self-attack is not allowed: %s.' % attacker, \
id, github)
return
create_label(repo_owner, repo_name, "verified", "9466CB", \
"Successfully verified.", github)
update_label(repo_owner, repo_name, num, github, "verified")
kind = commit
info = {'attacker': attacker, 'defender': defender,
'branch': branch, 'bugkind': kind}
sync_scoreboard(scoreboard)
process_unintended(repo_name, num, config, gen_time, info, scoreboard,
id, github, repo_owner)
def prepare_scoreboard_repo(url):
path = get_github_path(url).split('/')
scoreboard_owner = path[0]
scoreboard_name = path[1]
scoreboard_dir = '.score'
clone(scoreboard_owner, scoreboard_name, False, scoreboard_dir)
return scoreboard_dir
def start_eval(config, github):
target_repos = get_target_repos(config)
scoreboard = prepare_scoreboard_repo(config['score_board'])
finalize = False
while (not finalize):
if (is_timeover(config)):
finalize = True
issues, interval = get_issues(target_repos, github)
if not issues:
print('[*] No news. Sleep for %d seconds.' % interval)
time.sleep(interval)
continue
print('[*] %d new issues.' % len(issues))
for repo, num, id, gen_time in issues:
process_issue(repo, num, id, config, gen_time, github, scoreboard)
print '[*] Time is over!'
return
def evaluate(config_file, token):
reload(sys)
sys.setdefaultencoding('utf-8')
config = load_config(config_file)
github = Github(config['player'], token)
return start_eval(config, github)