Skip to content

Instantly share code, notes, and snippets.

@antlauzon
Last active January 18, 2023 06:02
Show Gist options
  • Save antlauzon/5146e029c815666042ace9807a2a7ae9 to your computer and use it in GitHub Desktop.
Save antlauzon/5146e029c815666042ace9807a2a7ae9 to your computer and use it in GitHub Desktop.
Salisbury Obfsproxy Transport
#!/usr/bin/python
# -*- coding: utf-8 -*-
import uuid
from obfsproxy.transports.base import BaseTransport
from itertools import product
SALISBURY = 'salisbury'
class SalisburyTransport(BaseTransport):
def __init__(self):
permutations = (sorted(set((c.upper(), c.lower()))) for c in SALISBURY)
self._ring_keys = list(map(''.join, product(*permutations)))
self._ring = {}
for i, salisbury in enumerate(self._ring_keys):
self._ring[salisbury] = i
if not hasattr(self, 'key'):
self._key = 0
self._edge_cache = ''
super(SalisburyTransport, self).__init__()
def _desalisburize(self, salisburies):
decoded = ''
i = 0
salisbury = self._edge_cache + salisburies[i:i+9-len(self._edge_cache)]
while salisbury and len(salisbury) == 9:
decoded += chr((self._ring[salisbury] + self._key) % 256)
i += 9 - len(self._edge_cache)
self._edge_cache = ''
salisbury = salisburies[i:i+9]
self._edge_cache = salisbury
return decoded
def _salisburize(self, data):
salisburies = ''
for byte in data:
salisburies += self._ring_keys[(ord(byte) + self._key) % 256]
return salisburies
def receivedDownstream(self, data):
decoded = self._desalisburize(data.peek())
data.drain()
self.circuit.upstream.write(decoded)
def receivedUpstream(self, data):
self.circuit.downstream.write(self._salisburize(data.read()))
class SalisburyClient(SalisburyTransport):
pass
class SalisburyServer(SalisburyTransport):
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment