How to set proxy settings on MacOS using python - python

How to change the internet proxy settings using python in MacOS to set Proxy server and Proxy port
I do that with windows using this code:
import _winreg as winreg
INTERNET_SETTINGS = winreg.OpenKey(winreg.HKEY_CURRENT_USER, r'Software\Microsoft\Windows\CurrentVersion\Internet Settings', 0, winreg.KEY_ALL_ACCESS)
def set_key(name, value):
_, reg_type = winreg.QueryValueEx(INTERNET_SETTINGS, name)
winreg.SetValueEx(INTERNET_SETTINGS, name, 0, reg_type, value)
set_key('ProxyEnable', 0)
set_key('ProxyOverride', u'*.local;<local>') # Bypass the proxy for localhost
set_key('ProxyServer', u'proxy.example.com:8080')
is this possible to do it on MacOS ?

After a long time of search, I found this way of how to change proxy on MacOs using python.
We need to use networksetup via terminal.
To set HTTP proxy server on MacOS using python:
import os
proxy = "proxy.example.com"
port = 8080
def Proxy_on():
os.system('networksetup -setwebproxy Ethernet '+proxy+' '+port)
Proxy_on()
and to turn it off:
import os
proxy = "proxy.example.com"
port = 8080
def Proxy_off():
os.system('networksetup -setwebproxystate Ethernet off')
Proxy_off()
If the network service isn't named just "Ethernet", you may need to parse networksetup -listallnetworkservices or -listnetworkserviceorder to get the correct name.

Related

Masking OS / PC name in Python

I'm using various commands in Python to get the PC names:
platform.node()
socket.gethostname()
os.environ['COMPUTERNAME']
Is it possible to "mask" or "spoof" your PC name so that these commands get a pre-defined name? (not the real PC name)
As stated in the SMTPLIB doc :
class smtplib.SMTP(host='', port=0, local_hostname=None, [timeout, ]source_address=None) :
…
If specified, local_hostname is used as the FQDN of the local host in the HELO/EHLO command. Otherwise, the local hostname is found using socket.getfqdn()

Using mitmproxy inside a Python script to connect to upstream proxy (with user & password)

I am trying to use mitmproxy behind a company proxy that requires a user/password login.
The setup is:
Local PC's browser -> mitmproxy (on local PC) -> company proxy -> internet.
Based on this SO thread, this is how you use mitmproxy within a Python program. This example works fine when there's no proxy.
from mitmproxy.options import Options
from mitmproxy.proxy.config import ProxyConfig
from mitmproxy.proxy.server import ProxyServer
from mitmproxy.tools.dump import DumpMaster
class Addon(object):
def __init__(self):
pass
def request(self, flow):
# examine request here
pass
def response(self, flow):
# examine response here
pass
if __name__ == "__main__":
options = Options(listen_host='0.0.0.0', listen_port=8080, http2=True)
m = DumpMaster(options, with_termlog=False, with_dumper=False)
config = ProxyConfig(options)
m.server = ProxyServer(config)
m.addons.add(Addon())
try:
print('starting mitmproxy')
m.run()
except KeyboardInterrupt:
m.shutdown()
Assuming the company proxy is at IP "1.2.3.4" port 3128 and requires a
login USER and PASSWORD, how can I change this script to have mitproxy
use that proxy instead of going to the internet directly?
Addition info: I am not using mitmdump with the script-parameter to run this script.
The goal is to run this from Python 3.8 with a pip-installed mitmproxy

Selenium WebDriver + Tor as proxy with Stem?

I need to confirm If Stem can be used to launch a Tor process that exposes 127.0.0.1:port, then use it on selenium scripts as proxy (SOCKS).
I'm using Python 3.4.2 , Stem 1.3.0, and Tor (tor-win32-tor-0.2.5.10 expert
bundle) on Windows.
This piece of code works with a standard SOCKS proxy.
from selenium import webdriver
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
profile = FirefoxProfile()
profile.set_preference('network.proxy.type', 1)
profile.set_preference('network.proxy.socks', '127.0.0.1')
profile.set_preference('network.proxy.socks_port', 9000)
driver = webdriver.Firefox(profile)
driver.implicitly_wait(30)
driver.get('http://www.reddit.com')
But I can't manage to get it working with Tor as my proxy. I tried to create a Tor process, and its created. But I don't really know If it's working properly. I don't get errors in my tor_error_log.txt
# File: stem_process.py
import stem.process
import stem
stem.process.launch_tor_with_config(
config = {
'SocksPort': '9000',
'ControlPort': '9051',
'ExitNodes': '{us}',
'Log': [
'NOTICE stdout',
'ERR file c:\\tor-win32-tor-0.2.5.10\\Tor\\tor_error_log.txt',
],
},
tor_cmd = 'C:\\tor-win32-tor-0.2.5.10\\Tor\\tor.exe',
)
Then I tried two ways to create the connection or authenticate. The first one is using with and stem.control.controller. And the second at lower level with stem.socket and stem.connection
The first one:
# File: stem_test1.py
from stem.control import Controller
with Controller.from_port(address='127.0.0.1', port=9051) as controller: #port = 9051
controller.authenticate()
print("Tor is running version %s" % controller.get_version())
'''
# Output:
Tor is running version 0.2.5.10 (git-13318a95ddfbbf8d)
'''
The second one:
# File: stem_test2.py
import sys
import stem
import stem.connection
import stem.socket
if __name__ == '__main__':
try:
control_socket = stem.socket.ControlPort(port = 9051)
stem.connection.authenticate(control_socket)
except stem.SocketError as exc:
print('Unable to connect to tor on port 9051: %s' % exc)
sys.exit(1)
except stem.connection.AuthenticationFailure as exc:
print('Unable to authenticate: %s' % exc)
sys.exit(1)
print("Issuing 'GETINFO version' query...\n")
control_socket.send('GETINFO version')
print(control_socket.recv())
'''
# Output:
Issuing 'GETINFO version' query...
version=0.2.5.10 (git-13318a95ddfbbf8d)
OK
'''
And both run without errors... But when I use the code to call the Firefox WebDriver instance with 127.0.0.1:9000 as proxy (also tried with 127.0.0.1:9051, because I don't really know the difference between socksPort and controlPort) It doesn't work.
Stem can't create a tor process, its only a library for connecting to an existing tor server for inspection/control via the control port.
To create the tor process itself, you need to have your system start it up with upstart/launchctl/etc. Alternatively, you can just type tor from the commandline if its installed and it'll run in the foreground.
With that, to use stem you'll need to edit your torrc to a. enable the ControlPort, and b. set an authentication method (cookieauth or hashed password stored in your torrc). The default tor SocksPort is 9050 and ControlPort is 9051.
The SocksPort is the one you route your traffic (i.e. firefox) through, the ControlPort is what you connect stem to.
Mind you, only if you even need stem, since it just seems like you're trying to start a tor instance with it (and thats not do-able), if you get it running on your system vanilla, it'll work with selenium/firefox as you've got it configured (well, default port is 9050 not 9000)

Webdriver connected to selenium-server:4444 a through proxy

When I run (WinXP OS, Python 2.7)
wd = webdriver.Remote (command_executor = 'http://127.0.0.1:4444/hub', desired_capabilities = webdriver.DesiredCapabilities.INTERNETEXPLORER)
in my system there is a proxy server by default, and is connected to selenium-server:4444 a through proxy. How to make that connection went directly to selenium-server:4444.
It is a bit late, but I stumbled over the same problem today and solved it, so for the next one who searches, here is the solution:
The system proxy settings are fetched from the *_proxy windows environment variables (http_proxy, https_proxy, ftp_proxy, ...), so if you have a company proxy defined there it will be used.
Add a new environment variable in windows options or, if you use intelliJ IDEA, in the Run configuration settings:
no_proxy=localhost,127.0.0.1
The reason you will find in python-2.7.6/Lib/urllib.py, around line 1387:
def proxy_bypass_environment(host):
"""Test if proxies should not be used for a particular host.
Checks the environment for a variable named no_proxy, which should
be a list of DNS suffixes separated by commas, or '*' for all hosts.
"""
no_proxy = os.environ.get('no_proxy', '') or os.environ.get('NO_PROXY', '')
# '*' is special case for always bypass
if no_proxy == '*':
return 1
# strip port off host
hostonly, port = splitport(host)
# check if the host ends with any of the DNS suffixes
no_proxy_list = [proxy.strip() for proxy in no_proxy.split(',')]
for name in no_proxy_list:
if name and (hostonly.endswith(name) or host.endswith(name)):
return 1
# otherwise, don't bypass
return 0

Port forwarding with Paramiko

I'm trying to do some port forwarding from a python app using Paramiko. I can set up the SSH connection just fine, but I'm a bit stumped as to how to use paramiko.Transport. I've already found this file, but I can't work out what's going on in it. From looking at the paramiko.Transport docs, it seems that a single line using the open_channel function, but I can't work out how to implement that. I'm trying to replicate a simple ssh -L 8000:localhost:8000.
Can anyone help me out?
Please find some code using paramiko-1.7.7.1, pycrypto-2.6 and the forward.py script from which I did remove code from the line 115 to the end (to avoid options parsing).
import paramiko, sys
from forward import forward_tunnel
remote_host = "target_host"
remote_port = 8000
local_port = 8000
ssh_host = "my_ssh_host"
ssh_port = 22
user = "login"
password = "s3cr3t"
transport = paramiko.Transport((ssh_host, ssh_port))
# Command for paramiko-1.7.7.1
transport.connect(hostkey = None,
username = user,
password = password,
pkey = None)
try:
forward_tunnel(local_port, remote_host, remote_port, transport)
except KeyboardInterrupt:
print 'Port forwarding stopped.'
sys.exit(0)
I've tested it successfully from a Windows station, using a ssh server under Red Hat and pointing to a 3rd server. (I'm using Python 2.7.2)
Hope it helps,
You can use https://github.com/pahaz/sshtunnel
pip install sshtunnel
Code example:
import sshtunnel
with sshtunnel.open(
(ssh_host, ssh_port),
ssh_host_key=None,
ssh_username=ssh_user,
ssh_password=ssh_password,
ssh_private_key=None,
remote_bind_address=(REMOTE_HOST, REMOTE_PORT)) as server:
def do_something(port):
# Do something with port
pass
print("LOCAL PORT:", server.local_bind_port)
do_something(server.local_bind_port)

Categories

Resources