This repository has been archived by the owner on Jun 8, 2018. It is now read-only.
/
server.py
executable file
·132 lines (103 loc) · 4.3 KB
/
server.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
#!/usr/bin/python
'''
Copyright 2016 Seth VanHeulen
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 3 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, see <http://www.gnu.org/licenses/>.
'''
import argparse
import array
import hashlib
import math
import random
import sys
from Crypto.Cipher import Blowfish
from Crypto.Util import Counter
from twisted.internet import reactor
from twisted.internet.protocol import Protocol, ClientFactory
from twisted.python import log
from twisted.web.http import HTTPFactory
from twisted.web.proxy import ProxyRequest, Proxy
from twisted.web.server import Site
from twisted.web.static import File
class TunnelProtocol(Protocol):
def connectionMade(self):
self.factory._request.channel._openTunnel(self)
self.factory._request.transport.write(b'HTTP/1.1 200 Connection established\r\n\r\n')
def dataReceived(self, data):
self.factory._request.transport.write(data)
class TunnelProtocolFactory(ClientFactory):
protocol = TunnelProtocol
def __init__(self, request):
self._request = request
def clientConnectionFailed(self, connector, reason):
self._request.setResponseCode(502, b'Bad Gateway')
self._request.finish()
class TunnelProxyRequest(ProxyRequest):
def process(self):
if self.method == b'CONNECT':
try:
host, port = self.uri.split(b':', 1)
host = host.decode()
port = int(port)
except ValueError:
self.setResponseCode(400, b'Bad Request')
self.finish()
else:
self.reactor.connectTCP(host, port, TunnelProtocolFactory(self))
else:
if self.uri == b'http://spector.capcom.co.jp/3ds/mhx_jp/arc/quest/q1010001.arc':
self.uri = b'http://localhost:8081/JPN_event_encrypted.arc'
elif self.uri == b'http://spector.capcom.co.jp/3ds/mhx_jp/arc/quest/q1020001.arc':
self.uri = b'http://localhost:8081/JPN_challenge_encrypted.arc'
ProxyRequest.process(self)
class TunnelProxy(Proxy):
requestFactory = TunnelProxyRequest
def __init__(self):
self._tunnel = None
Proxy.__init__(self)
def _openTunnel(self, tunnel):
self._tunnel = tunnel
self._producer.resumeProducing()
def dataReceived(self, data):
if self._tunnel:
self._tunnel.transport.write(data)
else:
Proxy.dataReceived(self, data)
def connectionLost(self, reason):
if self._tunnel:
self._tunnel.transport.loseConnection()
self._tunnel = None
Proxy.connectionLost(self, reason)
class TunnelProxyFactory(HTTPFactory):
protocol = TunnelProxy
def encrypt(buff, key):
nonce = array.array('I', [random.getrandbits(32)])
buff += hashlib.sha1(buff).digest()
length = len(buff)
buff = array.array('I', buff + b'\x00' * (8 - length % 8))
buff.byteswap()
counter = Counter.new(32, prefix=nonce.tostring(), initial_value=0, little_endian=True)
cipher = Blowfish.new(key, Blowfish.MODE_CTR, counter=counter)
buff = array.array('I', cipher.encrypt(buff.tostring()))
buff.byteswap()
buff = buff.tostring()[:length]
nonce.byteswap()
return buff + nonce.tostring()
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Runs a proxy server on port 8080 for installing genhax exploit quests')
parser.add_argument('key', help='DLC encryption key')
args = parser.parse_args()
open('JPN_event_encrypted.arc', 'wb').write(encrypt(open('JPN_event.arc', 'rb').read(), args.key))
open('JPN_challenge_encrypted.arc', 'wb').write(encrypt(open('JPN_challenge.arc', 'rb').read(), args.key))
log.startLogging(sys.stderr)
reactor.listenTCP(8080, TunnelProxyFactory())
reactor.listenTCP(8081, Site(File('./')))
reactor.run()