Python request.get function returning 404 on all directories even valid ones

31 Views Asked by At

I have created a port scanning and web directory enumerator (the directory enumerator is heavily inspired by one found on github), and it keeps returning with 404 when dirb responds 200 to the same directory? The test application is the metasploitable2 web server

`

import socket
import ipaddress
import requests
import os
from sys import argv

#While true loops allow the program to ask the user to
#input the value again without having to restart from the start
while True:
    try: #User inputed target IP
        Target_ip = ipaddress.ip_address(input("Please input Target IP address"))
        Target_ipstr = str(Target_ip)
        #Error handle
    except ValueError:
        print("Please input valid IP address")
    else:
         break

while True:
    try:
        #User inputted port start
        Port_start = int(input("Please input a port starting"))
        #Error handle
    except ValueError:
        print("Please enter a valid port number")
        #test if port is valid
    if Port_start>=1 and Port_start<=65536:
        break
    else:
        print("Please enter a valid port number")
while True:
    try:
        #User inputted port start
        Port_end = int(input("Please input a port ending"))
    except ValueError:
        print("Please enter a valid port number")
        #test if port is valid
    if Port_end>=Port_start and Port_end<=65536:
       break
    else:
        print("Please enter a valid port number")
#Loop that takes the port range and tests the ports
#until the end port and breaks when done
for port in range(Port_start, Port_end+1):
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.settimeout(0.1)
    result = sock.connect_ex((Target_ipstr, port))

    if result == 0:
        print(f"Port {port} is open")
        sock.close()
#Advises the user that the scan is complete
print("Port scan complete")

#start enumeration
print("Starting enumeration")

def Enumeration():
    
    print("Please input url as format http://hostname.com or ip")
    arr=[]
    url = str(input("Input URL to attack:"))
    wordlist = str(input("Enter path to wordlist"))

    try:
            Status=requests.get(url)
            if Status.status_code == 200:
                print('Web page is available')
            else:
                print("Web Page is down")
            if os.path.exists(os.getcwd()+wordlist):
                      fs=open(os.getcwd()+wordlist,"r")
                      print("Starting web enumeration")
                      for i in fs:
                              print(url+"/"+i)
                              rq=requests.get(url+"/"+i)
                              if rq.status_code == 200:
                                     print(">OK".rjust(len(url+"/"+i)+5,'-'))
                                     arr.append(str(url+"/"+i))
                              else:
                                     print(">404".rjust(len(url+"/"+i)+5,'-'))

                      fs.close()
                      print("output".center(100,'-'))
                      l=1
                      for i in arr:
                          print(l, "> ", i)
                          l+=1
            else:
                    print(os.getcwd()+wordlist+" doesn't exist in the directory.")
    except Exception as e:
        print(e)
                
Enumeration()

I have played around with the wordlist variable and the request get but cant seem to get it to work any advice is appreciated

0

There are 0 best solutions below