forked from owtf/owtf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
error_handler.py
155 lines (137 loc) · 6.38 KB
/
error_handler.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
#!/usr/bin/env python
"""
owtf is an OWASP+PTES-focused try to unite great tools and facilitate pen testing
Copyright (c) 2011, Abraham Aranguren <name.surname@gmail.com> Twitter: @7a_ http://7-a.org
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the copyright owner nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
The error handler provides a centralised control for aborting the application
and logging errors for debugging later.
"""
import logging
import traceback
import sys
import cgi
import json
import urllib2
from framework.lib.exceptions import FrameworkAbortException, \
PluginAbortException
from framework.lib.general import cprint
class ErrorHandler(object):
Command = ''
PaddingLength = 100
def __init__(self, Core):
self.Core = Core
self.Padding = "\n" + "_" * self.PaddingLength + "\n\n"
self.SubPadding = "\n" + "*" * self.PaddingLength + "\n"
def SetCommand(self, command):
self.Command = command
def FrameworkAbort(self, message, report=True):
message = "Aborted by Framework: " + message
cprint(message)
self.Core.Finish(message, report)
return message
def get_option_from_user(self, options):
return raw_input(
"Options: 'e'+Enter= Exit" + options + ", Enter= Next test\n")
def UserAbort(self, level, partial_output = ''):
# Levels so far can be Command or Plugin
message = logging.info(
"\nThe " + level + " was aborted by the user: Please check the "
"report and plugin output files")
message = (
"\nThe " + level + " was aborted by the user: Please check the "
"report and plugin output files")
options = ""
if 'Command' == level:
options = ", 'p'+Enter= Move on to next plugin"
option = 'p'
if 'e' == option:
if 'Command' == level: # Try to save partial plugin results.
raise FrameworkAbortException(partial_output)
self.Core.Finish("Aborted by user") # Interrupted.
elif 'p' == option: # Move on to next plugin.
# Jump to next handler and pass partial output to avoid losing
# results.
raise PluginAbortException(partial_output)
return message
def LogError(self, message, trace=None):
try:
self.Core.DB.Error.Add(message, trace) # Log error in the DB.
except AttributeError:
cprint("ERROR: DB is not setup yet: cannot log errors to file!")
def AddOWTFBug(self, message):
# TODO: http://blog.tplus1.com/index.php/2007/09/28/the-python-logging-module-is-much-better-than-print-statements/
exc_type, exc_value, exc_traceback = sys.exc_info()
err_trace_list = traceback.format_exception(
exc_type, exc_value, exc_traceback)
err_trace = self.Core.AnonymiseCommand("\n".join(err_trace_list))
message = self.Core.AnonymiseCommand(message)
output = self.Padding + "OWTF BUG: Please report the sanitised " \
"information below to help make this better. Thank you." + \
self.SubPadding
output += "\nMessage: " + message + "\n"
output += "\nError Trace:"
output += "\n" + err_trace
output += "\n"+self.Padding
cprint(output)
self.LogError(message, err_trace)
def Add(self, message, bugType='owtf'):
if 'owtf' == bugType:
return self.AddOWTFBug(message)
else:
output = self.Padding + message + self.SubPadding
cprint(output)
self.LogError(message)
def AddGithubIssue(self, title='Bug report from OWTF', info=None, user=None):
# TODO: Has to be ported to use db and infact add to interface.
# Once db is implemented, better verbosity will be easy.
error_data = self.Core.DB.ErrorData()
for item in error_data:
if item.startswith('Message'):
title = item[len('Message:'):]
break
data = {'title':'[Auto-Generated] ' + title, 'body':''}
# For github markdown.
data['body'] = '#### OWTF Bug Report\n\n```' + \
'\n'.join(error_data) + '```\n'
if info:
data['body'] += "\n#### User Report\n\n"
data['body'] += info
if user:
data['body'] += "\n\n#### %s" % user
data = json.dumps(data) # Converted to string.
headers = {
"Content-Type": "application/json",
"Authorization":
"token " + self.Core.Config.Get("GITHUB_BUG_REPORTER_TOKEN")
}
request = urllib2.Request(
self.Core.Config.Get("GITHUB_API_ISSUES_URL"),
headers=headers,
data=data)
response = urllib2.urlopen(request)
decoded_resp = json.loads(response.read())
if response.code == 201:
cprint("Issue URL: " + decoded_resp["url"])
return True
else:
return False