firewall_blocking_outgoing_connections
Differenze
Queste sono le differenze tra la revisione selezionata e la versione attuale della pagina.
| Prossima revisione | Revisione precedente | ||
| firewall_blocking_outgoing_connections [2011/02/24 10:03] – creata toobaz | firewall_blocking_outgoing_connections [2011/03/17 08:11] (versione attuale) – sotot toobaz | ||
|---|---|---|---|
| Linea 7: | Linea 7: | ||
| ===== My solution ===== | ===== My solution ===== | ||
| - | My rudimentary solution is composed of 3 parts: | + | My rudimentary solution is composed of 4 simple Python scripts: |
| - | - a Python | + | - a script for opening ports, which must be ran on the server |
| - | - a Python | + | - a script to receive " |
| - | - (optional) a php script for driving | + | - a script acting as a bridge between **1.** and **2.** |
| + | - a script for probing ports, which must be ran from the client | ||
| + | |||
| + | Notice that you // | ||
| + | |||
| + | {{: | ||
| + | |||
| + | ==== The server scripts ==== | ||
| + | === The ports opener === | ||
| + | |||
| + | The script just runs a dummy service on each port from **N** (provided as argument) to **N+257** (I don't check more than 257 ports at the same time, to not incur in the restrictions imposed on the maximum number of opened files). | ||
| + | |||
| + | As suggested in the comment, you should replace " | ||
| + | | ||
| + | |||
| + | because socket.gethostname() probably won't work. | ||
| + | |||
| + | #! / | ||
| + | # opener.py | ||
| + | |||
| + | import socket, time, sys | ||
| + | |||
| + | try: | ||
| + | start = int( sys.argv[1] ) | ||
| + | except (IndexError, | ||
| + | print " | ||
| + | sys.exit(1) | ||
| + | |||
| + | sockets = [] | ||
| + | |||
| + | for port in range(start, | ||
| + | s = socket.socket(socket.AF_INET, | ||
| + | try: | ||
| + | # You should probably edit the following line! | ||
| + | s.bind( (socket.gethostname(), | ||
| + | s.listen(1) | ||
| + | except Exception as exc: | ||
| + | print port, exc | ||
| + | sockets.append( s ) | ||
| + | |||
| + | time.sleep(3600) | ||
| + | |||
| + | === The http control === | ||
| + | |||
| + | Put this in some place where Python files will be executed by the webserver (i.e. / | ||
| + | |||
| + | #! / | ||
| + | # http_control.py | ||
| + | |||
| + | import cgi | ||
| + | import cgitb | ||
| + | cgitb.enable() | ||
| + | |||
| + | print " | ||
| + | |||
| + | |||
| + | form_c = cgi.FormContentDict() | ||
| + | |||
| + | if ' | ||
| + | f = open( ' | ||
| + | f.write( form_c[' | ||
| + | print form_c | ||
| + | f.close() | ||
| + | |||
| + | === The bridge === | ||
| + | |||
| + | The above script creates | ||
| + | |||
| + | #! / | ||
| + | # bridge.py | ||
| + | |||
| + | import time, subprocess | ||
| + | |||
| + | port = None | ||
| + | proc = None | ||
| + | |||
| + | while True: | ||
| + | newf = open('/ | ||
| + | try: | ||
| + | newport = int( newf.read() ) | ||
| + | except ValueError: | ||
| + | newport = None | ||
| + | newf.close() | ||
| + | if newport != port: | ||
| + | if proc: | ||
| + | proc.terminate() | ||
| + | if newport != None: | ||
| + | print " | ||
| + | proc = subprocess.Popen([" | ||
| + | else: | ||
| + | print " | ||
| + | port = newport | ||
| + | |||
| + | time.sleep( 5 ) | ||
| + | |||
| + | |||
| + | |||
| + | ==== The client script ==== | ||
| + | |||
| + | This is a very simple port scanner: it tries to connect to a given range of ports on the given host (again, from **N**, the argument, to **N+257**). You must replace " | ||
| + | |||
| + | #! / | ||
| + | # prober.py | ||
| + | |||
| + | import socket, time, sys | ||
| + | |||
| + | try: | ||
| + | start = int( sys.argv[1] ) | ||
| + | except (IndexError, | ||
| + | print " | ||
| + | sys.exit(1) | ||
| + | |||
| + | if len(sys.argv) > 2: | ||
| + | host = sys.argv[2] | ||
| + | else: | ||
| + | # Edit the following line! | ||
| + | host = " | ||
| + | |||
| + | sockets = [] | ||
| + | |||
| + | | ||
| + | s = socket.socket(socket.AF_INET, | ||
| + | res = s.connect_ex( (host, port) ) | ||
| + | if res == 0: | ||
| + | print "PORT ", port, " OPEN" | ||
| + | else: | ||
| + | print port, res | ||
| + | |||
| + | |||
| + | ==== So, to recap ==== | ||
| + | === What to do === | ||
| + | |||
| + | If you have an alternate access to the server, just run on it " | ||
| + | ./opener.py 100 | ||
| + | and on the client run " | ||
| + | ./prober.py 100 | ||
| + | to test ports 100 - 357 (and try again with some other argument to test other ports ranges). | ||
| + | |||
| + | If you // | ||
| + | * save http_control.py to a place where Python scripts are ran by the webserver (and remember to make it executable) | ||
| + | * save opener.py and bridge.py to the same folder (and remember to make them executable) | ||
| + | * save prober.py on the client | ||
| + | * edit the specified lines in prober.py, bridge.py and opener.py | ||
| + | * run bridge.py (possibly | ||
| + | |||
| + | Then, once you are behind | ||
| + | * visit the page http:// | ||
| + | * on the client, run " | ||
| + | |||
| + | ./prober.py 100 | ||
| + | to test ports 100 - 357 (and try again with some other argument to test other ports ranges). | ||
| + | |||
| + | === Understanding output === | ||
| + | |||
| + | prober.py will print " | ||
firewall_blocking_outgoing_connections.1298541798.txt.gz · Ultima modifica: da toobaz
