Created
July 1, 2020 10:32
-
-
Save JosephRedfern/1f093961191c5a4663850f7cd5ef59e9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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