Example #1
0
	def onSymmetryConnectData(self, data):
		if self.proxyMode:
			self.send(data)
			return
		if self.messageParse.appendData(data):
			method = self.messageParse.method()
			path = self.messageParse.path()
			
			if method == "CONNECT":
				path = "https://"+path
				self.sendDataToSymmetryConnect("HTTP/1.1 200 OK\r\n\r\n")
				self.proxyMode = True
			addr,port = parserUrlAddrPort(path)
			if addr.find("status.dddproxy.com")>0:
				path = path.split("?")
				self.onHTTP(method)
			elif addr in ["127.0.0.1","localhost"]:
				self.server.addCallback(self.onClose)
			else:
				if method != "CONNECT":
					m = re.search("^(?:(?:http)://[^/]+)(.*)$", path)
					if m:
						dataCache = "%s %s %s\r\n"%(method,m.group(1),self.messageParse.httpVersion())
						dataCache += self.messageParse.HeaderString()+"\r\n"
						dataCache += self.messageParse.getBody()
						self.send(dataCache)
					else:
						self.close()
						
					self.connectName =  "	<	" + self.filenoStr()+" "+self.messageParse.method()+" "+self.messageParse.path()					
					self.messageParse.clear()
# 					print addr,port,dataCache
				if self.connectStatus() == 0:
					self.connect((addr,port))
	def onRecv(self, data):

		self.preConnectRecvCache += data
		if self.mode == "proxy":
			if self.serverAuthPass and self.preConnectRecvCache:
				if self.connectHost:
					analysis.incrementData(self.address[0], domainAnalysisType.incoming, self.connectHost, len(self.preConnectRecvCache))
					
				self.sendDataToSymmetryConnect(self.preConnectRecvCache)
				self.preConnectRecvCache = ""
			return
		if self.httpMessageParse.appendData(data):
			method = self.httpMessageParse.method()
			path = self.httpMessageParse.path()
			self.connectName = self.filenoStr() + "	" + method + "	" + path
			if not path.startswith("http://") and method in ["GET", "POST"]:
				path = path.split("?")
				self.onHTTP(self.httpMessageParse.headers,
						method,
						path[0],
						path[1] if len(path) > 1 else "",
						self.httpMessageParse.getBody() if method == "POST" else "")
				self.mode = "http"
			else:
				
				self.mode = "proxy"
				
				connect = localToRemoteConnectManger.getConnect()
				
				if path.find("status.dddproxy.com")>0:
					try:
						connect = None
						jsonMessage = self.httpMessageParse.getBody()
						jsonBody = json.loads(jsonMessage)
						connectList = localToRemoteConnectManger.getConnectHost(jsonBody["host"],jsonBody["port"])
						if connectList:
							for _,v in connectList.items():
								connect = v
					except:
						pass
				
				if connect:
					connect.addLocalRealConnect(self)
				else:
					self.close()
				
				self.connectHost = parserUrlAddrPort("https://" + path if method == "CONNECT" else path)[0]
				analysis.incrementData(self.address[0], domainAnalysisType.connect, self.connectHost, 1)
		else:
			pass
	def onlocalRecv(self,data):
		self.dataCache += data
		if self.sock:
			if self.dataCache:
				self.send(self.dataCache)
				self.dataCache = ""
			return
		if self.messageParse.appendData(data):
			method = self.messageParse.method()
			path = self.messageParse.path()
			
			if method == "CONNECT":
				path = "https://"+path
				self.dataCache = ""
			addr,port = parserUrlAddrPort(path)
			if addr.find("status.dddproxy.com")>0:
				path = path.split("?")
				self.onHTTP(method)
			elif addr in ["127.0.0.1","localhost"]:
				self.server.addCallback(self.onClose)
			else:
				self.connect((addr,port))
	def onHTTP(self, header, method, path, query, post):
# 		log.log(1,self,header,method,path,query,post)
		if method == "POST":
			postJson = json.loads(post)
			opt = postJson["opt"]
			respons = {}

			if(opt == "status"):
				respons = self.server.dumpConnects()
			elif(opt == "serverList"):
				respons["pac"] = "http://" + self.httpMessageParse.getHeader("host") + "/pac"
				respons["list"] = settingConfig.setting(settingConfig.remoteServerList)
				respons["version"] = version.version
			elif opt == "setServerList":
				settingConfig.setting(settingConfig.remoteServerList, postJson["data"])
				respons["status"] = "ok"
# 			elif opt == "testRemoteProxy":
# 				respons["status"] = ""
			elif opt == "domainList":
				
				if "action" in postJson:
					action = postJson["action"]
					domain = postJson["domain"]
					respons={"status":"ok"}
					if action == "delete":
						domainConfig.config.removeDomain(domain)
					elif action == "open":
						domainConfig.config.openDomain(domain)
					elif action == "close":
						domainConfig.config.closeDomain(domain)
					else:
						respons={"status":"no found action"}
				else:
					respons["domainList"] = domainConfig.config.getDomainListWithAnalysis()
			elif opt == "analysisData":
				respons["analysisData"] = analysis.getAnalysisData(
																selectDomain=postJson["domain"],
																startTime=postJson["startTime"],
																todayStartTime=postJson["todayStartTime"]
																)
			elif opt == "addDomain":
				url = postJson["url"]
				host = parserUrlAddrPort(url)[0]
				if host:
					host = getDomainName(host)
				else:
					host = url if getDomainName(url) else ""
				respons["status"] = "ok" if domainConfig.config.addDomain(host) else "error"
			self.reseponse(respons,connection=self.httpMessageParse.connection())
		elif path == "/pac":
			content = self.getFileContent(dirname(__file__) + "/template/pac.js")
			content = content.replace("{{domainWhiteListJson}}", json.dumps(domainConfig.config.getDomainList(0)))
			content = content.replace("{{domainListJson}}", json.dumps(domainConfig.config.getDomainList(1)))
			content = content.replace("{{proxy_ddr}}", self.httpMessageParse.getHeader("host"))
			self.reseponse(content,connection=self.httpMessageParse.connection())
		else:
			if path == "/":
				path = "/index.html"
			content = self.getFileContent(dirname(__file__) + "/template" +path)
			if content:
				
				self.reseponse(content,ContentType=get_mime_type(path),connection=self.httpMessageParse.connection())
			else:
				self.reseponse("\"" + path + "\" not found", code=404,connection=self.httpMessageParse.connection())
Example #5
0
    def onSymmetryConnectData(self, data):
        if self.proxyMode:
            self.send(data)
            return

        if len(data) and data[0] == '\x05':  # socks5
            # 			print "local >> ", len(data), binascii.b2a_hex(data)
            if (data[1] == '\x02' or data[1] == '\x01') and len(data) <= 4:
                self.sendDataToSymmetryConnect(b"\x05\x00")
            elif data[1] == '\x01':

                def connectOk(ok):
                    if ok:
                        send = b"\x05\x00\x00\x01"
                        local = self._sock.getsockname()
                        send += socket.inet_aton(local[0]) + struct.pack(
                            ">H", local[1])
                    else:
                        send = b"\x05\x05\x00\x01\x00\x00\x00\x00\x00\x00"
# 					print "local << ", len(send), binascii.b2a_hex(send)
                    self.sendDataToSymmetryConnect(send)
                    self.proxyMode = True

                host = "<None>"
                if data[3] == '\x01':
                    host = "%d.%d.%d.%d" % (ord(data[4]), ord(
                        data[5]), ord(data[6]), ord(data[7]))
                    port = ord(data[8]) * 0x100 + ord(data[9])
                    return self.connect((host, port), cb=connectOk)
                elif data[3] == "\x03":
                    hostendindex = 5 + ord(data[4])
                    host = data[5:hostendindex]
                    port = ord(data[hostendindex]) * 0x100 + ord(
                        data[hostendindex + 1])
                    return self.connect((host, port), cb=connectOk)
                else:
                    reply = b"\x05\x07\x00\x01\x00\x00\x00\x00\x00\x00"

                self.connectName = "	<	%s	Socks5:%s(%s)" % (
                    self.filenoStr(), host, self.addressIp)
                self.sendDataToSymmetryConnect(reply)
            else:
                self.close()
        elif len(data) and data[0] == '\x04':  # socks4/socks4a

            def connectOk(ok):
                if ok:
                    send = b"\x00\x5A" + data[2:8]
                else:
                    send = b"\x00\x5B"
# 				print "local << ", len(send), binascii.b2a_hex(send)
                self.sendDataToSymmetryConnect(send)
                self.proxyMode = True


# 			print "local >> ", len(data), binascii.b2a_hex(data)

            if data[1] == '\x01' or data[1] == '\x02':
                host = "%d.%d.%d.%d" % (ord(data[4]), ord(data[5]), ord(
                    data[6]), ord(data[7]))
                version = "Socks4"
                if host.startswith("0.0.0.") and ord(data[7]) != 0:  # socks4a
                    splits = data[8:].split("\x00")
                    host = splits[-2]
                    version = "Socks4a"
                port = ord(data[2]) * 0x100 + ord(data[3])
                self.connectName = "	<	%s	%s:%s(%s)" % (
                    self.filenoStr(), version, host, self.addressIp)
                return self.connect((host, port), cb=connectOk)
            else:
                self.sendDataToSymmetryConnect(b'\x04\x91')
                self.close()
        else:
            httpmessagedone = self.messageParse.appendData(data)
            if self.messageParse.headerOk():
                method = self.messageParse.method()
                path = self.messageParse.path()
                connectOk = None
                if method == "CONNECT":
                    path = "https://" + path

                    def _connectOk(ok):
                        self.sendDataToSymmetryConnect(
                            "HTTP/1.1 200 OK\r\n\r\n")

                    connectOk = _connectOk
                    self.proxyMode = True
                addr, port = parserUrlAddrPort(path)
                if addr.find("status.dddproxy.com") > 0:
                    path = path.split("?")
                    if httpmessagedone:
                        self.onHTTP(method)
                elif addr in ["127.0.0.1", "localhost"]:
                    self.server.addCallback(self.onClose)
                else:
                    if self.connectStatus() == 0:
                        if method != "CONNECT":
                            m = re.search("^(?:(?:http)://[^/]+)(.*)$", path)
                            if m:

                                def _connectOk(ok):
                                    dataCache = "%s %s %s\r\n" % (
                                        method, m.group(1),
                                        self.messageParse.httpVersion())
                                    dataCache += self.messageParse.HeaderString(
                                    ) + "\r\n"
                                    dataCache += self.messageParse.readingBody(
                                    )
                                    self.send(dataCache)

                                connectOk = _connectOk
                            else:
                                self.close()

                            self.connectName = "	<	" + self.filenoStr(
                            ) + " " + self.messageParse.method(
                            ) + " " + self.messageParse.path()
                        self.connect((addr, port), cb=connectOk)
                    elif self.connectStatus() == 1:
                        self.send(self.messageParse.readingBody())
                        if httpmessagedone:
                            self.messageParse.clear()
Example #6
0
    def onRecv(self, data):

        self.preConnectRecvCache += data
        if self.mode == "proxy":

            if not self.connectHost and self.socksMode and len(
                    self.preConnectRecvCache) > 4:
                _d = self.preConnectRecvCache
                port = 0
                version = "Socks5"
                setConnectHost = False
                if (_d[0] == "\x05"):
                    if _d[3] == '\x01':
                        self.connectHost = "%d.%d.%d.%d" % (ord(
                            _d[4]), ord(_d[5]), ord(_d[6]), ord(_d[7]))
                        port = ord(_d[8]) * 0x100 + ord(_d[9])
                        setConnectHost = True
                    elif _d[3] == "\x03":
                        hostendindex = 5 + ord(_d[4])
                        self.connectHost = _d[5:hostendindex]
                        port = ord(_d[hostendindex]) * 0x100 + ord(
                            _d[hostendindex + 1])
                        setConnectHost = True
                elif _d[0] == "\x04":
                    if _d[1] == '\x01' or _d[1] == '\x02':
                        self.connectHost = "%d.%d.%d.%d" % (ord(
                            _d[4]), ord(_d[5]), ord(_d[6]), ord(_d[7]))
                        version = "Socks4"
                        if self.connectHost.startswith("0.0.0.") and ord(
                                _d[7]) != 0:  # socks4a
                            splits = _d[8:].split("\x00")
                            self.connectHost = splits[-2]
                            version = "Socks4a"
                        setConnectHost = True
                        port = ord(_d[2]) * 0x100 + ord(_d[3])
                if setConnectHost:
                    analysis.incrementData(self.address[0],
                                           domainAnalysisType.connect,
                                           self.connectHost, 1)
                self.connectName = self.symmetryConnectManager.filenoStr(
                ) + "	<	" + self.filenoStr(
                ) + "	" + version + ":" + self.connectHost + ":%d" % (port)

            if self.serverAuthPass and self.preConnectRecvCache:
                if self.connectHost:
                    analysis.incrementData(self.address[0],
                                           domainAnalysisType.incoming,
                                           self.connectHost,
                                           len(self.preConnectRecvCache))

                self.sendDataToSymmetryConnect(self.preConnectRecvCache)
                self.preConnectRecvCache = ""
            return
        if data[0] == '\x05' or data[0] == '\x04':  # socks5
            if data[1] == '\x02' or data[1] == '\x01':
                self.setToProxyMode()
                self.socksMode = True
            else:
                print "local >> ", len(data), binascii.b2a_hex(data)
                pass
        else:
            httpmessagedone = self.httpMessageParse.appendData(data)
            if self.httpMessageParse.headerOk() and httpmessagedone:
                method = self.httpMessageParse.method()
                path = self.httpMessageParse.path()
                self.connectName = self.filenoStr() + "	" + method + "	" + path
                if not path.startswith("http://") and method in [
                        "GET", "POST"
                ]:
                    path = path.split("?")
                    self.onHTTP(
                        self.httpMessageParse.headers, method, path[0],
                        path[1] if len(path) > 1 else "",
                        self.httpMessageParse.getBody()
                        if method == "POST" else "")
                    self.mode = "http"
                else:
                    host = None
                    port = None
                    if path.find("status.dddproxy.com") > 0:

                        jsonMessage = self.httpMessageParse.getBody()
                        try:
                            jsonBody = json.loads(jsonMessage)
                        except:
                            jsonBody = {}
                        host = jsonBody["host"]
                        port = jsonBody["port"]
                    if self.setToProxyMode(host=host, port=port):
                        self.connectHost = parserUrlAddrPort(
                            "https://" +
                            path if method == "CONNECT" else path)[0]
                        analysis.incrementData(self.address[0],
                                               domainAnalysisType.connect,
                                               self.connectHost, 1)
Example #7
0
    def onHTTP(self, header, method, path, query, post):
        # 		log.log(1,self,header,method,path,query,post)
        if method == "POST":
            postJson = json.loads(post)
            opt = postJson["opt"]
            respons = {}

            if (opt == "status"):
                respons = self.server.dumpConnects()
            elif (opt == "serverList"):
                respons["pac"] = "http://" + self.httpMessageParse.getHeader(
                    "host") + "/pac"
                respons["proxyAddr"] = parserUrlAddrPort(respons["pac"])
                respons["list"] = settingConfig.setting(
                    settingConfig.remoteServerList)
                respons["version"] = version.version
            elif opt == "setServerList":
                settingConfig.setting(settingConfig.remoteServerList,
                                      postJson["data"])
                respons["status"] = "ok"
# 			elif opt == "testRemoteProxy":
# 				respons["status"] = ""
            elif opt == "domainList":

                if "action" in postJson:
                    action = postJson["action"]
                    domain = postJson["domain"]
                    respons = {"status": "ok"}
                    if action == "delete":
                        domainConfig.config.removeDomain(domain)
                    elif action == "open":
                        domainConfig.config.openDomain(domain)
                    elif action == "close":
                        domainConfig.config.closeDomain(domain)
                    else:
                        respons = {"status": "no found action"}
                else:
                    respons[
                        "domainList"] = domainConfig.config.getDomainListWithAnalysis(
                        )
            elif opt == "analysisData":
                respons["analysisData"] = analysis.getAnalysisData(
                    selectDomain=postJson["domain"],
                    startTime=postJson["startTime"],
                    todayStartTime=postJson["todayStartTime"])
            elif opt == "restore":
                if postJson["clearAll"]:
                    domainConfig.config.setting = {}
                domainList = postJson["domainList"]
                for domain in domainList:
                    domainConfig.config.addDomain(
                        domain[0],
                        Open=domain[1],
                        updateTime=domain[2] if len(domain) > 2 else 0)
                respons["status"] = "ok"
            elif opt == "addDomain":
                url = postJson["url"]
                host = parserUrlAddrPort(url)[0]
                if host:
                    host = getDomainName(host)
                else:
                    host = url if getDomainName(url) else ""
                respons["status"] = "ok" if domainConfig.config.addDomain(
                    host) else "error"
            self.reseponse(respons,
                           connection=self.httpMessageParse.connection())
        elif path == "/pac":
            content = self.getFileContent(
                dirname(__file__) + "/template/pac.js")
            content = content.replace(
                "{{domainWhiteListJson}}",
                json.dumps(domainConfig.config.getDomainList(0)))
            content = content.replace(
                "{{domainListJson}}",
                json.dumps(domainConfig.config.getDomainList(1)))
            content = content.replace("{{proxy_ddr}}",
                                      self.httpMessageParse.getHeader("host"))
            self.reseponse(content,
                           connection=self.httpMessageParse.connection())

        else:
            if path == "/":
                path = "/index.html"
            content = self.getFileContent(
                dirname(__file__) + "/template" + path)
            if content and path.find("..") == -1:
                self.reseponse(content,
                               ContentType=get_mime_type(path),
                               connection=self.httpMessageParse.connection())
            else:
                self.reseponse("\"" + path + "\" not found",
                               code=404,
                               connection=self.httpMessageParse.connection())