Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/mathparse_honeypot.py
blob: f8d5f2cf7feff56e240f8a3701ae12aceee987a5 (plain)
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
#!/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('mathparse')
_logger.setLevel(logging.DEBUG)

import sys, random, time, re, ast, parser


class mathparse_honeypot:

    def __init__(self, runaslib=True):
 
        # Seed the random number generator        
        random.seed(time.time())

    def parse(self, drop):
        question = drop[0]
        answer = drop[1]

        # look for "[1-10]" etc.
        regexp = r"\[(\d+)-(\d+)]"
        _logger.debug('begin parse: ' + str(question))  
        question = re.split(regexp, question)
        _logger.debug('split q1: ' + str(question))  
        # "\1" stands for random_value[0]
        random_values = []
        for i in range(1, len(question), 3):
            _logger.debug(len(question))
            rand_val = str(random.randint(int(question[i]), int(question[i+1])+1))
            random_values.append(rand_val)
            question[i] = rand_val
            question[i+1] = ""
            _logger.debug('q1[' + str(i) + ']: ' + str(question))
        # make it one string again
        question = u"".join(question)
        _logger.debug('join q1: ' + str(question))

        # look for markers like "\1"
        regexp = r"\\(\d+)"
        question = re.split(regexp, question)
        _logger.debug('split q2: ' + str(question))
        for i in range(1, len(question), 2):
            #no error/range checking so far 
            question[i] = random_values[int(question[i])-1]
            _logger.debug('q2[' + str(i) + ']: ' + str(question))
        question = u"".join(question)
        _logger.debug('join q2: ' + str(question))

        # look for expressions to evaluate: "{...}"
        regexp = r"\{(.*)}"
        question = re.split(regexp, question)
        _logger.debug('split q3: ' + str(question))
        for i in range(1, len(question), 2):
            # Sicherheitsluecke! 
            # vgl. http://effbot.org/zone/librarybook-core-eval.htm
            question[i] = str(eval(question[i]))
            _logger.debug('q3[' + str(i) + ']: ' + str(question))
        question = u"".join(question)
        _logger.debug('join q3: ' + str(question)) 

        regexp = r"\\(\d+)"
        answer = re.split(regexp, answer)
        _logger.debug('split a1: ' + str(question))     
        for i in range(1, len(answer), 2):
            answer[i] = random_values[int(answer[i])-1]
            _logger.debug('a1[' + str(i) + ']: ' + str(question))       
        answer = u"".join(answer)
        _logger.debug('join a1: ' + str(question))
    
        regexp = r"\{(.*)}"
        answer = re.split(regexp, answer)
        _logger.debug('split a2: ' + str(question))     
        for i in range(1, len(answer), 2):
            answer[i] = str(eval(answer[i]))
            # answer[i] = str(ast.literal_eval(ast.parse(answer[i])))
            _logger.debug('a2[' + str(i) + ']: ' + str(question))                 
        answer = u"".join(answer)
        _logger.debug('join a2: ' + str(question))

        return [question,answer]


if __name__ == '__main__':
    mathparse_honeypot(False)