How to implement Mac OS HTTP monitor - macos

I need write application that monitoring all http requests/responces on mac os. How to register proxy server in Mac OS.

I found simple example - python tcp server that redirect requests/responses and code that automation set Mac OS proxies settings.
Python script was taken from http://www.cppfun.com/python-2-7-simple-http-proxy-server.htm, Mac OS proxies settings configurator from How to set proxy settings on MacOS using python
import socket, sys
import os
from thread import *
max_conn = 8
buffer_size = 8192
def proxy_on(port):
os.system('networksetup -setwebproxy Ethernet '+'127.0.0.1'+' '+str(port))
def proxy_off():
os.system('networksetup -setwebproxystate Ethernet off')
def app():
try:
listen_port = int(raw_input("[*] Enter listening port(a number eg 8098):"))
proxy_on(listen_port)
start(listen_port)
except KeyboardInterrupt:
print "\n[*] User requested interrupt\n[*] Program exiting ..."
sys.exit()
def start(listen_port):
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('', listen_port))
s.listen(max_conn)
print "[*] Init sockets ... Done"
print "[*] Sockets bind success ..."
print "[*] Proxy server success [%d]\n" % listen_port
except Exception, e:
print "\n", e
print "[*] Unable to init socket, maybe try another port"
sys.exit(2)
while True:
try:
conn, addr = s.accept()
data = conn.recv(buffer_size)
start_new_thread(conn_str, (conn, data, addr))
except KeyboardInterrupt:
s.close()
print "\n[*] Proxy server shutdown ..."
print "[*] Have a good day !"
proxy_off()
sys.exit(1)
s.close()
def conn_str(conn, data, addr):
try:
first_line = str(data).split('\n')[0]
url = first_line.split(' ')[1]
print url
host, port = get_host_and_port(url)
print host, port, data
proxy_server(host, port, conn, addr, data)
except Exception, e:
print "\n", e
print "[*] Get the http url or port fail ..."
sys.exit(1)
def get_host_and_port(url):
import urllib
_, rest = urllib.splittype(url)
host, rest = urllib.splithost(rest)
host, port = urllib.splitport(host)
if port is None:
port = 80
return host,port
def proxy_server(host, port, conn, addr, data):
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))
s.send(data)
while True:
reply = s.recv(buffer_size)
if len(reply) > 0:
conn.send(reply)
dat = float(len(reply))/1024.0
dat = "%.3s KB" % str(dat)
print "[*] Request done : %s => %s <=" % (addr[0], dat)
else:
break
s.close()
conn.close()
except socket.error, (value, message):
print "\n[*] Socket error %d:%s" % (value, message)
s.close()
conn.close()
sys.exit(1)
if __name__ == '__main__':
# power by cppfun.com
app()
# also you can change it yourself

Related

issue about pexpect logfile_read

use pexpect SSH connections to run cmds on remote server, the command can be executed, but the results displayed on the terminal are not as expected, code like this(At first there was no time.sleep, it was added for debugging)
import logging
import time
from pexpectUtility import Session
logger = logging.getLogger(__name__)
def test_create_and_show():
cliPrompt = 'dev-r0'
hostPrompt = 'admin#dev-r0'
aa = Session()
aa.connect("admin","password", "10.10.0.10")
time.sleep(2)
aa.child.sendline("sonic-cli")
aa.child.expect(cliPrompt, 3)
tTime = 0
time.sleep(tTime)
aa.child.sendline("configure terminal")
aa.child.expect(cliPrompt, 3)
time.sleep(tTime)
aa.child.sendline("end")
aa.child.expect(cliPrompt, 3)
time.sleep(tTime)
aa.child.sendline("exit")
aa.child.expect(hostPrompt, 3)
aa.disconnect()
the pexpectUtility.py
import sys
import logging as log
if sys.platform == 'win32':
import WExpect as pexpect
spawn_class = pexpect.spawn_windows
else:
import pexpect
spawn_class = pexpect.spawn
class MutliIO:
def __init__(self, *fds):
self.fds = fds
def write(self, data):
for fd in self.fds:
fd.write(data)
def flush(self):
for fd in self.fds:
fd.flush()
class Session(spawn_class):
def __init__(self):
self.child = None
def connect(self, username, password, serverIp, protocol='ssh'):
self.protocol = protocol
self.username = username
self.password = password
self.serverIp = serverIp
if protocol == 'ssh':
cmd = "ssh -x -o StrictHostKeyChecking=no -l %s " % self.username
else:
cmd = "telnet "
cmd = cmd + serverIp
log.info('Connecting to Dut: %s\n' %(cmd))
expect_list = ['ogin: $', '[P|p]assword:', '\[confirm\] $',
'\[confirm yes/no\]:', '\[yes/no\]:', '\(yes/no\)\?',
'\[y/n\]:', '--More--', 'ONIE:/ #',
pexpect.TIMEOUT, pexpect.EOF]
self.child = spawn_class(cmd)
logfile = open('pexpect.log', 'w')
self.child.logfile_read = MutliIO(sys.stdout)
# self.child.logfile_read = MutliIO(sys.stdout, logfile)
# self.child.logfile_read = MutliIO(logfile)
try:
re = self.child.expect(expect_list, 10)
log.debug("expect pwd: {}".format(re))
except Exception as err:
log.error('%s' %err)
raise
# login
try:
self.child.sendline(self.password)
except Exception as err:
raise RuntimeError("login failed!", err)
def disconnect(self):
self.child.sendline("exit")
self.child.expect(pexpect.EOF)
self.child.close()
if self.child.logfile_read != None:
self.child.logfile_read = None
Executed commands are repeated displayed, just like batch input. log is as follows:
admin#dev-r0:~$ sonic-cli
configure terminal
configure terminal
end
exit
dev-r0# configure terminal
dev-r0(config)# end
dev-r0# exit
admin#dev-r0:~$ exit
logout
Connection to 10.10.0.10 closed.
When I set tTime to 5 (each command interval is 5 seconds) the log is as expected,I think this is not a good solution,I also want to know the root cause
admin#dev-r0:~$ sonic-cli
dev-r0# configure terminal
dev-r0(config)# end
dev-r0# exit
admin#dev-r0:~$ exit
logout
Connection to 10.10.0.10 closed.
When I directly use expect to implement the above operation, there is no need to wait for 5 seconds between commands, and the log displayed by the terminal is normal.
why pexpect has this issue? how to solve this? Thanks in advance
This is not the whole answer, but a first point to fix. After the
sendline("sonic-cli") the first expect() is going to return
immediately, as it will match the prompt admin#dev-r0:~$ which is already
there waiting, before the sonic-cli command arrives. This means the next
command configure terminal is sent immediately after sonic-cli.
You should enhance the connect() routine to expect the admin#dev-r0:~$
prompt before returning, or use this expect instead of the sleep(2) which
should not be necessary.
Referring to the sample code of pexpect on the Internet, I found that the root cause is a code problem: missing a expect() after sendline()
The changes are as follows:
# login
try:
self.child.sendline(self.password)
HOST_PROMPT = '\$' # remote server prompt
re = self.child.expect(HOST_PROMPT)
except Exception as err:
raise RuntimeError("login failed!", err)

Python: Port Scanner -- No port is shown

import socket
from IPy import IP
def scan(target, port_num):
converted_ip = check_ip(target)
print(f"\n [-_0 Scanning Target]: {target}")
for port in range(1, port_num):
scan_port(converted_ip, port)
def check_ip(ip):
try:
IP(ip)
return (ip)
except ValueError:
return socket.gethostbyname(ip)
def get_banner(s):
return s.recv(1024)
def scan_port(ipaddress, port):
try:
sock = socket.socket()
sock.settimeout(0.5)
sock.connect((ipaddress, port))
try:
banner = get_banner(sock)
print(f"[+] Open Port {port}: {banner.decode().strip()}")
except:
print(f"[+] open Port {port}")
except:
pass
if __name__ == "__main__":
targets = input("[+] Enter Targets to Scan (split multiple targets with ,): ")
port_num = input("Enter Numbers of Ports you want to Scan: ")
if ',' in targets:
for ip_add in targets.split(","):
scan(ip_add.strip(" "), port_num)
else:
scan(targets, port_num)
guys, I wrote a simple port scanner as above, but whenever I tried to scan the ip address(IPv$) of my computer, or linux system in my vitual machine, no open port is shown.
Q1. Can someone tell me how to fix this?
Thank you for your help.

How to interact with a running python script

As a start ive got a basic script which reads local unix syslog (/var/log/messages)
i want to build a tool which opens a socket (19999) locally and allows admin commands to be sent / processed.
As something i can build on basically i want to have the script on start up do the follow :
- open port 19999 locally
- start reading syslog storing "line" as the last line it has processed.
- when admin command of "printline" is seen print last known variable for "line"
Ive got basics done i think (script is below) where i have it open the relevant ports and it prints the commands sent to it from another client tool however it never starts to read the syslog.
#!/usr/bin/python
import socket
import subprocess
import sys
import time
from threading import Thread
MAX_LENGTH = 4096
def handle(clientsocket):
while 1:
buf = clientsocket.recv(MAX_LENGTH)
if buf == '': return #client terminated connection
print buf
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
PORT = 19999
HOST = '127.0.0.1'
serversocket.bind((HOST, PORT))
serversocket.listen(10)
while 1:
#accept connections from outside
(clientsocket, address) = serversocket.accept()
ct = Thread(target=handle, args=(clientsocket,))
ct.start()
def follow(thefile):
thefile.seek(0,2)
while True:
line = thefile.readline()
if not line:
time.sleep(0.1)
continue
yield line
if __name__ == '__main__':
logfile = open("/capture/log/uifitz/messages","r")
loglines = follow(logfile)
for line in loglines:
print line,
Any help would be appreciated. Python 2.6 by the way.

WinError 10038 an operaton was attempted on something that is not a socket

I'm trying to make chatting server in Python and I can't solve it. I'm running my code in CMD by using command python client.py localhost 9009.
this is the code that I am using:
#chat_client.py
import sys
import socket
import select
def chat_client():
if(len(sys.argv) < 3):
print("Usage: python chat_client.py hostname port")
sys.exit()
host = sys.argv[1]
port = int(sys.argv[2])
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(2)
# connect to remote host
try:
s.connect((host, port))
except:
print("Unable to connect")
sys.exit()
print("Connected to remote host. You can start sending messages")
sys.stdout.write("[Me] "); sys.stdout.flush()
while 1:
socket_list = [sys.stdin, s]
# Get the list sockets which are readable
read_sockets, write_sockets, error_sockets = select.select(socket_list , [], [])
for sock in read_sockets:
if sock == s:
# incoming message from remote server, s
data = sock.recv(4096)
if not data:
print("\nDisconnected from chat server")
sys.exit()
else:
#print data
sys.stdout.write(data)
sys.stdout.write("[Me] "); sys.stdout.flush()
else:
# user entered a message
msg = sys.stdin.readline()
s.send(msg)
sys.stdout.write("[Me] "); sys.stdout.flush()
if __name__ == "__main__":
sys.exit(chat_client())
And this is the error that I'm getting:
I don't have a clue how to fix it. Help would be appreciated! :)
[Me] Traceback (most recent call last):
File "client.py", line 54, in <module>
sys.exit(chat_client())
File "client.py", line 32, in chat_client
read_sockets, write_sockets, error_sockets = select.select(socket_list , [],
[])

How do I create a script that does a continous ping?

The output initially is:
Reply from
Reply from
Reply from
Then I'll get, when the connection is lost:
Request timed out.
Request timed out.
Request timed out.
When it's reconnected the response is:
Reply from
Reply from
Reply from
Then I proceed to open a telnet session to my system and run python code.
When I use our utility I get this error below:
def fin():
tb_session.close_connection()
conftest.py:38:
import os
import subprocess
import re
bFlag = True
while bFlag == True:
a = subprocess.check_output('ping 127.0.0.1')
b = a.split('\n')
iReply = 0
for line in b:
print line
if re.search("Reply", line) == None:
pass
else:
iReply+=1
if iReply == 4:
print "Total replies are 4"
else:
print "Ping broken"
bFlag = False

Resources