Last active
December 11, 2015 02:29
-
-
Save fqrouter/4531012 to your computer and use it in GitHub Desktop.
DNS 单向代理
This file contains 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
import socket | |
import dpkt.ip | |
def main_loop(server_socket, raw_socket): | |
while True: | |
packet_bytes, from_ip = server_socket.recvfrom(4096) | |
packet = dpkt.ip.IP(packet_bytes) | |
dst = socket.inet_ntoa(packet.dst) | |
print('%s:%s => %s:%s' % (socket.inet_ntoa(packet.src), packet.data.sport, dst, packet.data.dport)) | |
raw_socket.sendto(packet_bytes, (dst, 0)) | |
server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | |
try: | |
server_socket.bind(('0.0.0.0', 19840)) | |
raw_socket = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_RAW) | |
try: | |
raw_socket.setsockopt(socket.SOL_IP, socket.IP_HDRINCL, 1) | |
main_loop(server_socket, raw_socket) | |
finally: | |
raw_socket.close() | |
finally: | |
server_socket.close() |
This file contains 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
from netfilterqueue import NetfilterQueue | |
import subprocess | |
import signal | |
import traceback | |
import socket | |
IMPERSONATOR_IP = 'x.x.x.x' | |
IMPERSONATOR_PORT = 19840 | |
udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) | |
def smuggle_packet(nfqueue_element): | |
try: | |
original_packet = nfqueue_element.get_payload() | |
print('smuggled') | |
udp_socket.sendto(original_packet, (IMPERSONATOR_IP, IMPERSONATOR_PORT)) | |
nfqueue_element.drop() | |
except: | |
traceback.print_exc() | |
nfqueue_element.accept() | |
nfqueue = NetfilterQueue() | |
nfqueue.bind(0, smuggle_packet) | |
def clean_up(*args): | |
subprocess.call('iptables -D OUTPUT -p udp --dst 8.8.8.8 --dport 53 -j QUEUE', shell=True) | |
signal.signal(signal.SIGINT, clean_up) | |
try: | |
subprocess.call('iptables -I OUTPUT -p udp --dst 8.8.8.8 --dport 53 -j QUEUE', shell=True) | |
print('running..') | |
nfqueue.run() | |
except KeyboardInterrupt: | |
print('bye') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment