Port scan with python

Introduction

I learned from a CTF book that port scanning can be done with python Start with the curiosity of "What? Can python do that?" In the process, I learned that multithreading is much faster than running single. Since it's a big deal, I'll leave both for comparison.

Be careful

Because it is NG to do port scan to an external site In the experiment, start up a virtual server using Virtualbox on your own PC I did a port scan on it. That's why "10.0.0.2" written in the source code of ↓↓ is the IP of your own virtual server.

1. 1. Simple port scan

Reference: https://qiita.com/najayama/items/728682bcae824c902046

The code I learned the most is this. There is no waste. Instead it's slow. On the contrary, because it was slow, I could feel the "effectiveness of multithreading".

simple-port-scanner.py


import socket

max_port = 6000
min_port = 1

target_host = input("Input target host name or address: ")

for port in range(min_port, max_port):
    #target_Attempt to connect to port number port of host
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    return_code = sock.connect_ex((target_host, port))
    sock.close()

    #socket.connect_ex returns 0 on success
    if return_code == 0:
        print("Port %d open!" % (port))

print("Complete!")

2. Multithreaded port scan

Reference: https://www.valuestar.work/news/archives/20

This is no less wasteful than it is. I use thread well so it's fast enough to die. 1. 1. Then it took about 1 second per port It took quite a while to do ports 1 to 1024, but with this program, the process was completed in about 3 seconds. thread is bad.

This time, I used it as a reference in the rush, so it is not a copy of the code, so it is not in a comparable form. I wish I could shape it so that I could compare it later ...

multi-port-scanner.py


import socket
import threading

scan_range = [1, 10000];

host = "10.0.0.2";

threads = [];
ports = [];
isopen = [];

def Run(port, i):
    con = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    return_code = con.connect_ex((host, port))
    con.close()

    if return_code == 0:
        isopen[i] = 1;
    
    
count = 0;
for port in range(scan_range[0], scan_range[1]):
    ports.append(port);
    isopen.append(0);
    thread = threading.Thread(target=Run, args=(port, count));
    thread.start();
    threads.append(thread);
    count = count + 1;

for i in range(len(threads)):
    threads[i].join();
    if isopen[i] == 1:
        print("%d open" % ports[i]);

in conclusion

I looked it up to study portscan The destructive power of thread left an impression on me more than I expected. Let's slowly look at the source code and organize it later

~ End ~

Recommended Posts

Port scan with python
FizzBuzz with Python3
Scraping with Python
Statistics with python
Scraping with Python
Python with Go
Twilio with Python
Integrate with Python
Play with 2016-Python
AES256 with python
Tested with Python
python starts with ()
with syntax (Python)
Bingo with python
Zundokokiyoshi with python
Excel with Python
Microcomputer with Python
Cast with python
Serial communication with Python
Zip, unzip with python
Django 1.11 started with Python3.6
Primality test with Python
Python with eclipse + PyDev.
Socket communication with Python
Data analysis with python 2
Scraping with Python (preparation)
[Python] Python and security-② Port scanning tool made with Python
Try scraping with Python.
Learning Python with ChemTHEATER 03
Sequential search with Python
"Object-oriented" learning with python
Run Python with VBA
Handling yaml with python
Solve AtCoder 167 with python
[Python] Use JSON with Python
Learning Python with ChemTHEATER 05-1
Learn Python with ChemTHEATER
Run prepDE.py with python3
1.1 Getting Started with Python
Collecting tweets with Python
Binarization with OpenCV / Python
3. 3. AI programming with Python
Non-blocking with Python + uWSGI
Scraping with Python + PhantomJS
Posting tweets with python
Drive WebDriver with python
Use mecab with Python3
[Python] Redirect with CGIHTTPServer
Voice analysis with python
Think yaml with python
Operate Kinesis with Python
Getting Started with Python
Use DynamoDB with Python
Handle Excel with python
Ohm's Law with Python
Primality test with python
Run Blender with python
Solve Sudoku with Python
Python starting with Windows 7
Heatmap with Python + matplotlib
Multi-process asynchronously with python