Skip to content

Instantly share code, notes, and snippets.

@JosephRedfern
Created July 1, 2020 10:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JosephRedfern/1f093961191c5a4663850f7cd5ef59e9 to your computer and use it in GitHub Desktop.
Save JosephRedfern/1f093961191c5a4663850f7cd5ef59e9 to your computer and use it in GitHub Desktop.
# basic whitelisting for socket.socket.connect.
import socket
CONNECTION_WHITELIST = [('localhost', 80)]
unguarded_connect = socket.socket.connect
def guarded_connect(self, *args, **kwargs):
con = args[0]
if con not in CONNECTION_WHITELIST:
raise Exception(f"Not allowed! {con} not in whitelist")
return unguarded_connect(self, *args, **kwargs)
socket.socket.connect = guarded_connect
# this works
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect(("localhost", 80))
s.sendall(b'hello world')
data = s.recv(1024)
# this doesn't
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect(("google.com", 80))
s.sendall(b'hello world')
data = s.recv(1024)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment