Created
April 18, 2023 15:51
-
-
Save RaphGL/ee0deb7ee49af228d20363f5c2f2b590 to your computer and use it in GitHub Desktop.
Very basic port scanner
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <arpa/inet.h> | |
#include <netinet/in.h> | |
#include <stdio.h> | |
#include <sys/socket.h> | |
#include <unistd.h> | |
#define MAXPORTNUM 65535 | |
int main(int argc, char **argv) { | |
if (argc < 2) { | |
puts("No address was given."); | |
return 1; | |
} | |
int sock = socket(AF_INET, SOCK_STREAM, 0); | |
if (sock < 0) { | |
perror("Could not open socket"); | |
return 2; | |
} | |
for (int port = 0; port <= MAXPORTNUM; port++) { | |
struct sockaddr_in address = (struct sockaddr_in){ | |
.sin_family = AF_INET, | |
.sin_addr = inet_addr(argv[1]), | |
.sin_port = port, | |
}; | |
int conn = connect(sock, (struct sockaddr *)&address, sizeof(address)); | |
if (conn < 0) { | |
printf("Closed port: %d\n", port); | |
} else { | |
printf("Open port: %d\n", port); | |
} | |
close(conn); | |
} | |
close(sock); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket | |
from sys import argv | |
MAXPORTNUM = 65535 | |
def main(addr): | |
sock = socket.socket() | |
open_ports = [] | |
for port in range(MAXPORTNUM + 1): | |
try: | |
sock.connect((addr, port)) | |
except: | |
print(f"Closed port: {port}") | |
else: | |
open_ports.append(port) | |
print(f"Open port: {port}") | |
sock.close() | |
if len(open_ports) == 0: | |
print("\nAll ports are closed") | |
else: | |
print("The following ports are opened") | |
for port in open_ports: | |
print(port) | |
if __name__ == "__main__": | |
main(argv[1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment