chaos2sae 1.0.120425 documentation

Version: 1.0.120425

整个儿的

最终,完成所有功能的配置和代码:

config.yaml

---
name: urisaok
version: 2
...

index.wsgi

 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
# -*- coding: utf-8 -*-
VERSION = "urisaok v12.4.25"
import sae
import urllib2 as urilib
from bottle import Bottle, request

app = Bottle()

@app.route('/')
def hello():
    return '''Hollo, there! 
    - Bottle @ SAE 4 urisaok!
    - {v12.04.23} from YY talking..
    usage:
    $ curl -d 'uri=hrrp://sina.com' http://urisaok.sinaapp.com/qchk
    or
    $ curl -d 'uri=hrrp://sina.com' http://urisaok.sinaapp.com/chk
                    +-- want check url
    '''

class Borg():
    '''base http://blog.youxu.info/2010/04/29/borg
        - 单例式配置收集类
    '''
    __collective_mind = {}
    def __init__(self):
        self.__dict__ = self.__collective_mind
    APPKEY = "k-60666"
    SECRET = "99fc9fdbc6761f7d898ad25762407373"
    OPEN_HOST = "open.pc120.com"
    APITYPE = "/phish/"
    PHISHTYPE = {'-1':'未知'
        ,'0':'非钓鱼'
        ,'1':'钓鱼'
        ,'2':'网站高风险,有钓鱼嫌疑'
        }
# init all var
cfg = Borg()

@app.route('/chk/', method="POST")
@app.route('/chk', method="POST")
def chk():
    uri = request.forms.get('uri')
    result = _askCloud(cfg.APITYPE, uri)
    return "/chk:: %s"% cfg.PHISHTYPE[str(result)]

@app.route('/qchk/', method="POST")
@app.route('/qchk', method="POST")
def qchk():
    url = request.forms.get('uri').split("/")
    if 1 == len(url):
        uri = url[0]
    else:
        uri = url[2]
    #k = urlsafe_b64encode(uri)
    k = uri
    print k
    import sae.kvdb
    #print "kv.get_info() %s"% kv.get_info()
    kv = sae.kvdb.KVClient()
    v = kv.get(k)
    print "kv.get(uri)~ type=%s var=%s"%(type(v),v)
    if None == v:
        result = _askCloud(cfg.APITYPE, uri)
        kv.add(k, result)
        return "/qchk(KSC)::\t %s" % cfg.PHISHTYPE[str(result)]
    else:
        return "/qchk(KVDB)::\t %s" % cfg.PHISHTYPE[str(v)]

import base64
import time
from hashlib import md5
from base64 import urlsafe_b64encode
def _askCloud(api_path, url):
    args = __genQueryArgs(api_path, url)
    api_url = "http://%s%s?%s"% (cfg.OPEN_HOST, cfg.APITYPE ,args)
    print api_url
    result = eval(urilib.urlopen(api_url).read())
    print result
    if result['success'] == 1:
        return result['phish']
    else:
        return result

def __genQueryArgs(api_path, url):
    args = "appkey=" + cfg.APPKEY
    args += "&q=" + base64.urlsafe_b64encode(url)
    args += "&timestamp=" + "%.3f" % (time.time())
    sign_base_string = api_path + "?" + args
    args += "&sign=" + md5(sign_base_string + cfg.SECRET).hexdigest()
    return args

application = sae.create_wsgi_app(app)
#from sae.ext.shell import ShellMiddleware
#application = sae.create_wsgi_app(ShellMiddleware(app, '1q2w3e4r5t'))