我们从Python开源项目中,提取了以下33个代码示例,用于说明如何使用socks.SOCKS4。
def _install_socks_proxy_opener(proxytype, proxyaddr, proxyport=None): """ Install a socks proxy handler so that all urllib2 requests are routed through the socks proxy. """ try: import socks from sockshandler import SocksiPyHandler except ImportError: warn('WARNING: Failed to load PySocks module. Try installing it with `pip install PySocks`.') return if proxytype == 4: proxytype = socks.SOCKS4 elif proxytype == 5: proxytype = socks.SOCKS5 else: abort("Unknown Socks Proxy type {0}".format(proxytype)) opener = urllib2.build_opener(SocksiPyHandler(proxytype, proxyaddr, proxyport)) urllib2.install_opener(opener)
def init_proxy(proxy): res = urlparse(proxy) use_proxy = True if res.scheme == 'socks4': mode = socks.SOCKS4 elif res.scheme == 'socks5': mode = socks.SOCKS5 elif res.scheme == 'http': mode = socks.HTTP else: use_proxy = False ksprint.print_error('Unknown proxy "%s", starting without proxy...' % proxy) if use_proxy: socks.set_default_proxy(mode, res.netloc.split(':')[0], int(res.netloc.split(':')[1])) soc.socket = socks.socksocket ksprint.print_success('[*] Proxy "%s" using' % proxy)
def _update(__version__, __code_name__, language, socks_proxy): try: if socks_proxy is not None: socks_version = socks.SOCKS5 if socks_proxy.startswith('socks5://') else socks.SOCKS4 socks_proxy = socks_proxy.rsplit('://')[1] socks.set_default_proxy(socks_version, str(socks_proxy.rsplit(':')[0]), int(socks_proxy.rsplit(':')[1])) socket.socket = socks.socksocket socket.getaddrinfo = getaddrinfo data = requests.get(url, headers={"User-Agent": "OWASP Nettacker"}).content if version() is 3: data = data.decode("utf-8") if __version__ + ' ' + __code_name__ == data.rsplit('\n')[0]: info(messages(language, 103)) else: warn(messages(language, 101)) warn(messages(language, 85)) except: warn(messages(language, 102)) return
def _check(__version__, __code_name__, language, socks_proxy): try: if socks_proxy is not None: socks_version = socks.SOCKS5 if socks_proxy.startswith('socks5://') else socks.SOCKS4 socks_proxy = socks_proxy.rsplit('://')[1] socks.set_default_proxy(socks_version, str(socks_proxy.rsplit(':')[0]), int(socks_proxy.rsplit(':')[1])) socket.socket = socks.socksocket socket.getaddrinfo = getaddrinfo data = requests.get(url, headers={"User-Agent": "OWASP Nettacker"}).content if version() is 3: data = data.decode("utf-8") if __version__ + ' ' + __code_name__ == data.rsplit('\n')[0]: info(messages(language, 103)) else: warn(messages(language, 101)) except: warn(messages(language, 102)) return
def __connect_to_port(port, timeout_sec, target, retries, language, num, total, time_sleep, ports_tmp_filename, socks_proxy): exit = 0 if socks_proxy is not None: socks_version = socks.SOCKS5 if socks_proxy.startswith('socks5://') else socks.SOCKS4 socks_proxy = socks_proxy.rsplit('://')[1] socks.set_default_proxy(socks.SOCKS5, str(socks_proxy.rsplit(':')[0]), int(socks_proxy.rsplit(':')[1])) socket.socket = socks.socksocket socket.getaddrinfo = getaddrinfo while 1: try: if timeout_sec is not None: my_ftp = FTP(timeout=timeout_sec) else: my_ftp = FTP() my_ftp.connect(target, int(port)) exit = 0 break except: exit += 1 if exit is retries: error(messages(language, 68).format(target, port, str(num), str(total))) try: f = open(ports_tmp_filename, 'a') f.write(str(port) + '\n') f.close() except: pass break time.sleep(time_sleep)
def test(target, retries, timeout_sec, user_agent, http_method, socks_proxy): if socks_proxy is not None: socks_version = socks.SOCKS5 if socks_proxy.startswith('socks5://') else socks.SOCKS4 socks_proxy = socks_proxy.rsplit('://')[1] socks.set_default_proxy(socks_version, str(socks_proxy.rsplit(':')[0]), int(socks_proxy.rsplit(':')[1])) socket.socket = socks.socksocket socket.getaddrinfo = getaddrinfo n = 0 while 1: try: if http_method == "GET": r = requests.get(target, timeout=timeout_sec, headers=user_agent, verify=True) elif http_method == "HEAD": r = requests.head(target, timeout=timeout_sec, headers=user_agent, verify=True) return 0 except: n += 1 if n is retries: return 1
def load_proxy_config(): global default_socket if config.PROXY_ENABLE: if config.PROXY_TYPE == "HTTP": proxy_type = socks.HTTP elif config.PROXY_TYPE == "SOCKS4": proxy_type = socks.SOCKS4 elif config.PROXY_TYPE == "SOCKS5": proxy_type = socks.SOCKS5 else: xlog.error("proxy type %s unknown, disable proxy", config.PROXY_TYPE) raise socks.set_default_proxy(proxy_type, config.PROXY_HOST, config.PROXY_PORT, config.PROXY_USER, config.PROXY_PASSWD)
def load_proxy_config(): if config.PROXY_ENABLE: if config.PROXY_TYPE == "HTTP": proxy_type = socks.HTTP elif config.PROXY_TYPE == "SOCKS4": proxy_type = socks.SOCKS4 elif config.PROXY_TYPE == "SOCKS5": proxy_type = socks.SOCKS5 else: xlog.error("proxy type %s unknown, disable proxy", config.PROXY_TYPE) config.PROXY_ENABLE = 0 return socks.set_default_proxy(proxy_type, config.PROXY_HOST, config.PROXY_PORT, config.PROXY_USER, config.PROXY_PASSWD)
def __init__(self, config): '''Takes dictionary and gets all information, if it is not in dictionary uses default''' self.host = config.get("host", "irc.freenode.net") self.port = config.get("port", 6667) self.ipv6 = config.get("IPv6", False) self.ssl = config.get("SSL", False) self.sasl = config.get("SASL", False) self.do_auth = config.get("do_auth", False) self.auth_pass = config.get("auth_pass") self.auth_user = config.get("auth_user") self.nick = config.get("nick", "EzzyBot") self.ident = config.get("ident", "EzzyBot") self.realname = config.get("realname", "EzzyBot: a simple python framework for IRC bots.") self.channels = config.get("channels", ["#EzzyBot"]) self.analytics = config.get("analytics", True) self.quit_message = config.get("quit_message", "EzzyBot: a simple python framework for IRC bots.") self.flood_protection = config.get("flood_protection", True) self.permissions = config.get("permissions", {}) self.proxy = config.get("proxy", False) self.proxy_type = config.get("proxy_type", "SOCKS5") self.proxy_host = config.get("proxy_host", "") self.proxy_port = config.get("proxy_port", 1080) self.proxy_type = {"SOCKS5": socks.SOCKS5, "SOCKS4": socks.SOCKS4}[self.proxy_type] self.log_channel = config.get("log_channel", "#ezzybot-debug") self.password = config.get("pass") self.fifo = config.get("fifo", True) self.command_limiting_initial_tokens = config.get("command_limiting_initial_tokens", 20) self.command_limiting_message_cost = config.get("command_limiting_message_cost", 4) self.command_limiting_restore_rate = config.get("command_limiting_restore_rate", 0.13) self.limit_override = config.get("limit_override", ["admin", "dev"]) self.add_devs = config.get("add_devs", False) self.pingfreq = 60 self.timeout = self.pingfreq * 2
def __connect_to_port(port, timeout_sec, target, retries, language, num, total, time_sleep, ports_tmp_filename, thread_number, total_req, socks_proxy): exit = 0 port = int(port) if socks_proxy is not None: socks_version = socks.SOCKS5 if socks_proxy.startswith('socks5://') else socks.SOCKS4 socks_proxy = socks_proxy.rsplit('://')[1] socks.set_default_proxy(socks_version, str(socks_proxy.rsplit(':')[0]), int(socks_proxy.rsplit(':')[1])) socket.socket = socks.socksocket socket.getaddrinfo = getaddrinfo while 1: try: if timeout_sec is not None: server = smtplib.SMTP(target, int(port), timeout=timeout_sec) else: server = smtplib.SMTP(target, int(port)) server.starttls() server.quit() exit = 0 break except: exit += 1 if exit is retries: error(messages(language, 74).format(target, port, str(num), str(total))) try: f = open(ports_tmp_filename, 'a') f.write(str(port) + '\n') f.close() except: pass break time.sleep(time_sleep)
def connect(host, port, timeout_sec, log_in_file, language, time_sleep, thread_tmp_filename, socks_proxy): _HOST = messages(language, 53) _USERNAME = messages(language, 54) _PASSWORD = messages(language, 55) _PORT = messages(language, 56) _TYPE = messages(language, 57) _DESCRIPTION = messages(language, 58) time.sleep(time_sleep) try: if socks_proxy is not None: socks_version = socks.SOCKS5 if socks_proxy.startswith('socks5://') else socks.SOCKS4 socks_proxy = socks_proxy.rsplit('://')[1] socks.set_default_proxy(socks_version, str(socks_proxy.rsplit(':')[0]), int(socks_proxy.rsplit(':')[1])) socket.socket = socks.socksocket socket.getaddrinfo = getaddrinfo s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) if timeout_sec is not None: s.settimeout(timeout_sec) s.connect((host, port)) s.close() info(messages(language, 80).format(host, port)) save = open(log_in_file, 'a') save.write(json.dumps({_HOST: host, _USERNAME: '', _PASSWORD: '', _PORT: port, _TYPE: 'tcp_connect_port_scan', _DESCRIPTION: messages(language, 79)}) + '\n') save.close() thread_write = open(thread_tmp_filename, 'w') thread_write.write('0') thread_write.close() return True except: return False
def _connect(self): """??????whois?? ???socks.py (ver 1.5.7)""" # whois???ip???ip global _server_ip, _proxy_socks host = _server_ip.get_server_ip(self.whois_srv) # ????? host = host if host else self.whois_srv # ??ip???????????? self.tcpCliSock = socks.socksocket() # ??socket?? self.tcpCliSock.settimeout(TIMEOUT) # ?????? if Proxy_Flag: # socks???? proxy_info = _proxy_socks.get_proxy_socks(self.whois_srv) # ??IP if proxy_info is not None: # ???? if proxy_info['mode'] == 'SOCKS5': self.tcpCliSock.set_proxy(proxy_type=socks.SOCKS5, # socks?? addr=proxy_info['ip'], # socks??? port=proxy_info['port'], # ?? username=proxy_info['username'], # ??? password=proxy_info['password']) # ?? elif proxy_info['mode'] == 'SOCKS4': self.tcpCliSock.set_proxy(proxy_type=socks.SOCKS4, # socks?? addr=proxy_info['ip'], # socks??? port=proxy_info['port']) # ?? data_result = "" try: self.tcpCliSock.connect((host, 43)) # ??whois??? self.tcpCliSock.send(self.request_data + '\r\n') # ???? except Exception as e: # Exception??socks.py ?????? if str(e).find("timed out") != -1 or \ str(e).find("TTL expired") != -1: # ???? self.tcpCliSock.close() return "ERROR -1" elif str(e).find("Temporary failure in name resolution") != -1 or \ str(e).find("cannot connect to identd on the client") != -1 or \ str(e).find("unreachable") != -1: self.tcpCliSock.close() return "ERROR -2" else: self.tcpCliSock.close() return "ERROR OTHER" # ???? while True: try: data_rcv = self.tcpCliSock.recv(1024) # ?????? except: self.tcpCliSock.close() return "ERROR -3" if not len(data_rcv): self.tcpCliSock.close() return data_result # ?????? data_result = data_result + data_rcv # ????????
def login(user, passwd, target, port, timeout_sec, log_in_file, language, retries, time_sleep, thread_tmp_filename, socks_proxy): _HOST = messages(language, 53) _USERNAME = messages(language, 54) _PASSWORD = messages(language, 55) _PORT = messages(language, 56) _TYPE = messages(language, 57) _DESCRIPTION = messages(language, 58) exit = 0 if socks_proxy is not None: socks_version = socks.SOCKS5 if socks_proxy.startswith('socks5://') else socks.SOCKS4 socks_proxy = socks_proxy.rsplit('://')[1] socks.set_default_proxy(socks_version, str(socks_proxy.rsplit(':')[0]), int(socks_proxy.rsplit(':')[1])) socket.socket = socks.socksocket socket.getaddrinfo = getaddrinfo while 1: try: if timeout_sec is not None: my_ftp = FTP(timeout=timeout_sec) else: my_ftp = FTP() my_ftp.connect(target, port) exit = 0 break except: exit += 1 if exit is retries: warn(messages(language, 65).format(target, port, user, passwd)) return 1 time.sleep(time_sleep) flag = 1 try: my_ftp.login(user, passwd) flag = 0 except: pass if flag is 0: try: tmpl = [] tmp = my_ftp.retrlines('LIST', tmpl.append) info(messages(language, 70).format(user, passwd, target, port)) save = open(log_in_file, 'a') save.write( json.dumps({_HOST: target, _USERNAME: user, _PASSWORD: passwd, _PORT: port, _TYPE: 'ftp_brute', _DESCRIPTION: messages(language, 66)}) + '\n') save.close() except: info(messages(language, 70).format(user, passwd, target, port) + ' ' + messages(language, 71)) save = open(log_in_file, 'a') save.write(json.dumps({_HOST: target, _USERNAME: user, _PASSWORD: passwd, _PORT: port, _TYPE: 'FTP', _DESCRIPTION: messages(language, 67)}) + '\n') save.close() thread_write = open(thread_tmp_filename, 'w') thread_write.write('0') thread_write.close() else: pass return flag
def login(user, passwd, target, port, timeout_sec, log_in_file, language, retries, time_sleep, thread_tmp_filename, socks_proxy): _HOST = messages(language, 53) _USERNAME = messages(language, 54) _PASSWORD = messages(language, 55) _PORT = messages(language, 56) _TYPE = messages(language, 57) _DESCRIPTION = messages(language, 58) exit = 0 flag = 1 if socks_proxy is not None: socks_version = socks.SOCKS5 if socks_proxy.startswith('socks5://') else socks.SOCKS4 socks_proxy = socks_proxy.rsplit('://')[1] socks.set_default_proxy(socks_version, str(socks_proxy.rsplit(':')[0]), int(socks_proxy.rsplit(':')[1])) socket.socket = socks.socksocket socket.getaddrinfo = getaddrinfo while 1: try: paramiko.Transport((target, int(port))) paramiko_logger = logging.getLogger("paramiko.transport") paramiko_logger.disabled = True flag = 0 exit = 0 break except: exit += 1 if exit is retries: warn(messages(language, 76).format(target, str(port), user, passwd)) return 1 time.sleep(time_sleep) if flag is 0: try: ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) if timeout_sec is not None: ssh.connect(hostname=target, username=user, password=passwd, port=int(port), timeout=timeout_sec) else: ssh.connect(hostname=target, username=user, password=passwd, port=int(port)) info(messages(language, 70).format(user, passwd, target, port)) save = open(log_in_file, 'a') save.write( json.dumps({_HOST: target, _USERNAME: user, _PASSWORD: passwd, _PORT: port, _TYPE: 'ssh_brute', _DESCRIPTION: messages(language, 66)}) + '\n') save.close() thread_write = open(thread_tmp_filename, 'w') thread_write.write('0') thread_write.close() except: pass else: pass return flag
def login(user, passwd, target, port, timeout_sec, log_in_file, language, retries, time_sleep, thread_tmp_filename, socks_proxy): _HOST = messages(language, 53) _USERNAME = messages(language, 54) _PASSWORD = messages(language, 55) _PORT = messages(language, 56) _TYPE = messages(language, 57) _DESCRIPTION = messages(language, 58) exit = 0 if socks_proxy is not None: socks_version = socks.SOCKS5 if socks_proxy.startswith('socks5://') else socks.SOCKS4 socks_proxy = socks_proxy.rsplit('://')[1] socks.set_default_proxy(socks_version, str(socks_proxy.rsplit(':')[0]), int(socks_proxy.rsplit(':')[1])) socket.socket = socks.socksocket socket.getaddrinfo = getaddrinfo while 1: try: if timeout_sec is not None: server = smtplib.SMTP(target, int(port), timeout=timeout_sec) else: server = smtplib.SMTP(target, int(port)) server.starttls() exit = 0 break except: exit += 1 if exit is retries: warn(messages(language, 73).format(target, port, user, passwd)) return 1 time.sleep(time_sleep) flag = 1 try: server.login(user, passwd) flag = 0 except smtplib.SMTPException as err: pass if flag is 0: info(messages(language, 70).format(user, passwd, target, port)) save = open(log_in_file, 'a') save.write(json.dumps({_HOST: target, _USERNAME: user, _PASSWORD: passwd, _PORT: port, _TYPE: 'smtp_brute', _DESCRIPTION: messages(language, 66)}) + '\n') save.close() thread_write = open(thread_tmp_filename, 'w') thread_write.write('0') thread_write.close() else: pass try: server.quit() except: pass return flag
def check(target, user_agent, timeout_sec, log_in_file, language, time_sleep, thread_tmp_filename, retries, http_method, socks_proxy): _HOST = messages(language, 53) _USERNAME = messages(language, 54) _PASSWORD = messages(language, 55) _PORT = messages(language, 56) _TYPE = messages(language, 57) _DESCRIPTION = messages(language, 58) status_codes = [200, 401, 403] directory_listing_msgs = ["<title>Index of /", "<a href=\"\\?C=N;O=D\">Name</a>", "Directory Listing for", "Parent Directory</a>", "Last modified</a>", "<TITLE>Folder Listing.", "- Browsing directory "] time.sleep(time_sleep) try: if socks_proxy is not None: socks_version = socks.SOCKS5 if socks_proxy.startswith('socks5://') else socks.SOCKS4 socks_proxy = socks_proxy.rsplit('://')[1] socks.set_default_proxy(socks_version, str(socks_proxy.rsplit(':')[0]), int(socks_proxy.rsplit(':')[1])) socket.socket = socks.socksocket socket.getaddrinfo = getaddrinfo n = 0 while 1: try: if http_method == "GET": r = requests.get(target, timeout=timeout_sec, headers=user_agent, verify=True) elif http_method == "HEAD": r = requests.head(target, timeout=timeout_sec, headers=user_agent, verify=True) content = r.content break except: n += 1 if n is retries: warn(messages(language, 106).format(target)) return 1 if version() is 3: content = content.decode('utf8') if r.status_code in status_codes: info(messages(language, 38).format(target, r.status_code, r.reason)) thread_write = open(thread_tmp_filename, 'w') thread_write.write('0') thread_write.close() save = open(log_in_file, 'a') save.write(json.dumps({_HOST: target_to_host(target), _USERNAME: '', _PASSWORD: '', _PORT: int(target.rsplit(':')[2].rsplit('/')[0]), _TYPE: 'dir_scan', _DESCRIPTION: messages(language, 38).format(target, r.status_code, r.reason)}) + '\n') save.close() if r.status_code is 200: for dlmsg in directory_listing_msgs: if dlmsg in content: info(messages(language, 104).format(target)) save = open(log_in_file, 'a') save.write(json.dumps({_HOST: target_to_host(target), _USERNAME: '', _PASSWORD: '', _PORT: int(target.rsplit(':')[1].rsplit('/')[0]), _TYPE: 'dir_scan', _DESCRIPTION: messages(language, 104).format(target)}) + '\n') save.close() break return True except: return False