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
|
#!/usr/bin/env python
#
# Copyright (C) 2010, Johannes Ponader
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
import logging
_logger = logging.getLogger('xml_honeypot')
_logger.setLevel(logging.DEBUG)
# we need to parse some xml files
from xml.sax import saxutils, handler
from xml.sax import make_parser
from xml.sax.handler import feature_namespaces
class content_handler(handler.ContentHandler):
def __init__(self):
_logger.debug("content_handler init")
def startDocument(self):
self.drops = []
def endDocument(self):
_logger.debug('end of xml document')
def startElement(self, name, attrs):
# we don't check for errors at the moment, and ignore many features
if name == 'question' or name == 'answer':
self.wait_for_characters = name
else:
self.wait_for_characters = False
_logger.debug("element: " + name + ", attributes: " + str(attrs))
def characters(self, content):
# it's a hack for now, i just take the first chunk of content, this might produce errors
if self.wait_for_characters == 'question':
self.question = content
self.wait_for_characters = ''
if self.wait_for_characters == 'answer':
self.answer = content
self.wait_for_characters = ''
self.drops.append([self.question, self.answer])
_logger.debug('q/a: ' + self.question + '/' + self.answer)
_logger.debug('content: ' + content)
class DTD_handler(handler.DTDHandler):
def __init__(self):
_logger.debug("DTD_handler init")
class error_handler(handler.ErrorHandler):
def __init__(self):
_logger.debug("error_handler init")
def warning(self, exception):
_logger.debug(exception)
def error(self, exception):
_logger.debug(exception)
def fatalError(self, exception):
_logger.debug(exception)
class xml_honeypot:
def __init__(self, runaslib=True):
# see http://pyxml.sourceforge.net/topics/howto/node12.html
# Create a parser
self.parser = make_parser()
# Tell the parser we are not interested in XML namespaces
self.parser.setFeature(feature_namespaces, 0)
# Create the handler
self.content_handler = content_handler()
self.DTD_handler = DTD_handler()
self.error_handler = error_handler()
# Tell the parser to use our handler
self.parser.setContentHandler(self.content_handler)
self.parser.setDTDHandler(self.DTD_handler)
self.parser.setErrorHandler(self.error_handler)
def load_drops(self):
# Parse the input
self.xml_filename = 'my_first_questions.honeypot'
# xml_file = open(self.xml_filename, 'r')
self.parser.parse(self.xml_filename)
if __name__ == '__main__':
xml_honeypot(False)
|