Skip to content

Commit

Permalink
Organization code and update Testing
Browse files Browse the repository at this point in the history
  • Loading branch information
joaolfp committed Nov 9, 2024
1 parent cfa28f2 commit c9c8fdb
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 120 deletions.
12 changes: 9 additions & 3 deletions .github/workflows/Testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,14 @@ jobs:
with:
python-version: ${{ matrix.python-version }}

- name: Install Poetry
uses: snok/[email protected]
with:
virtualenvs-create: false
virtualenvs-in-project: false

- name: Install dependencies
run: pip install --upgrade -r requirements.txt
run: make setup

- name: Build
run: python -c "import dirMagic"
- name: Run build
run: make build
13 changes: 12 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,13 @@
directories_found.txt
.DS_Store
*.py[cod]
*$py.class
.pytest_cache
*egg-info
dist
.DS_STORE
__pycache__

.coverage
coverage.xml
.vscode/
.idea
18 changes: 18 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
setup: ## Dependencies
poetry update
poetry install
poetry shell

inspect: ## Run code analysis
poetry run flake8 dir_magic tests

test: ## Run tests
poetry run pytest -vv --cov-report=xml --cov=dir_magic tests/

build: ## Run build
poetry build

deploy: ## Deploy package
poetry config pypi-token.pypi $(token)
poetry build
poetry publish
23 changes: 23 additions & 0 deletions dir_magic/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import sys
from dir_magic.header import Header
from dir_magic.dirMagic import DirMagic

if __name__ == "__main__":
Header().setup()
dir_magic = DirMagic()

print("1 - Find Directories")
print("2 - Find Subdomains \n")

try:
item = input("Choose some of the options: ")

if item == "1":
dir_magic.find_dir()
elif item == "2":
dir_magic.find_subdomains()
else:
print("This option does not exist")
except KeyboardInterrupt:
print("\n Bye!!!")
sys.exit()
200 changes: 84 additions & 116 deletions dir_magic/dirMagic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,119 +3,87 @@
import dns.resolver
from rich.console import Console

def header():
print(r"""
_______ __ .______ .___ ___. ___ _______ __ ______
| \ | | | _ \ | \/ | / \ / _____|| | / |
| .--. || | | |_) | | \ / | / ^ \ | | __ | | | ,----'
| | | || | | / | |\/| | / /_\ \ | | |_ | | | | |
| '--' || | | |\ \----. | | | | / _____ \ | |__| | | | | `----.
|_______/ |__| | _| `._____| |__| |__| /__/ \__\ \______| |__| \______| (1.1.1)
About: DirMagic is a library written in Python to brute force directories and subdomains
Author: João Lucas
Language: Python
GitHub: https://github.com/heroesofcode/DirMagic
""")

def find_dir():
try:
print("\n")
url = input("URL: ")
wordlist = input("WordList: ")

console = Console()
console.print("\n -------------- search .... --------------", style="#af00ff")

all_results = []

with open(wordlist, "r") as file:
directories = file.readlines()

for directory in directories:
directory = directory.strip()
response = requests.get(url + directory)

if response.status_code != 404:
print("{} - {}".format(url + directory, response.status_code))
all_results.append("{} - {}".format(url + directory, response.status_code))

with open("directories_found.txt", 'w') as file:
for item in all_results:
file.write("{}\n".format(item))

console.print("Saved in the directories_found.txt file", style="#008000")
except KeyboardInterrupt:
print("\n Bye!!!")
sys.exit()
except Exception as e:
print(f"An error happened: {e}")
sys.exit()

def test_subdomain(subdomain):
try:
resolver = dns.resolver.Resolver()
responses = resolver.resolve(subdomain, "A")
return True
except (dns.resolver.NXDOMAIN, dns.resolver.NoAnswer, dns.resolver.NoNameservers):
return False
except Exception as e:
print(f"An error happened: {e}")
return False

def find_subdomains():
try:
print("\n")
domain = input("Domain: ")
wordlist = input("WordList: ")

console = Console()
console.print("\n -------------- search --------------", style="#af00ff")

all_results = []

with open(wordlist, "r") as file:
words = file.read().splitlines()

for word in words:
subdomain = "{}.{}".format(word, domain)

if test_subdomain(subdomain):
try:
response = requests.get("http://" + subdomain)
if response.status_code != 404:
print("{}".format(subdomain))
all_results.append(subdomain)
except requests.RequestException as e:
print(f"Failed to request {subdomain}: {e}")

with open("subdomains_found.txt", 'w') as file:
for item in all_results:
file.write("{}\n".format(item))

console.print("Saved in the subdomains_found.txt file", style="#008000")
except KeyboardInterrupt:
print("\n Bye!!!")
sys.exit()
except Exception as e:
print(f"An error happened: {e}")
sys.exit()

if __name__ == "__main__":
header()

print("1 - Find Directories")
print("2 - Find Subdomains \n")

try:
item = input("Choose some of the options: ")

if item == "1":
find_dir()
elif item == "2":
find_subdomains()
else:
print("This option does not exist")
except KeyboardInterrupt:
print("\n Bye!!!")
sys.exit()
class DirMagic(object):

def find_dir(self):
try:
print("\n")
url = input("URL: ")
wordlist = input("WordList: ")

console = Console()
console.print("\n -------------- search .... --------------", style="#af00ff")

all_results = []

with open(wordlist, "r") as file:
directories = file.readlines()

for directory in directories:
directory = directory.strip()
response = requests.get(url + directory)

if response.status_code != 404:
print("{} - {}".format(url + directory, response.status_code))
all_results.append("{} - {}".format(url + directory, response.status_code))

with open("directories_found.txt", 'w') as file:
for item in all_results:
file.write("{}\n".format(item))

console.print("Saved in the directories_found.txt file", style="#008000")
except KeyboardInterrupt:
print("\n Bye!!!")
sys.exit()
except Exception as e:
print(f"An error happened: {e}")
sys.exit()

def test_subdomain(self, subdomain):
try:
resolver = dns.resolver.Resolver()
responses = resolver.resolve(subdomain, "A")
return True
except (dns.resolver.NXDOMAIN, dns.resolver.NoAnswer, dns.resolver.NoNameservers):
return False
except Exception as e:
print(f"An error happened: {e}")
return False

def find_subdomains(self):
try:
print("\n")
domain = input("Domain: ")
wordlist = input("WordList: ")

console = Console()
console.print("\n -------------- search --------------", style="#af00ff")

all_results = []

with open(wordlist, "r") as file:
words = file.read().splitlines()

for word in words:
subdomain = "{}.{}".format(word, domain)

if self.test_subdomain(subdomain):
try:
response = requests.get("http://" + subdomain)
if response.status_code != 404:
print("{}".format(subdomain))
all_results.append(subdomain)
except requests.RequestException as e:
print(f"Failed to request {subdomain}: {e}")

with open("subdomains_found.txt", 'w') as file:
for item in all_results:
file.write("{}\n".format(item))

console.print("Saved in the subdomains_found.txt file", style="#008000")
except KeyboardInterrupt:
print("\n Bye!!!")
sys.exit()
except Exception as e:
print(f"An error happened: {e}")
sys.exit()
16 changes: 16 additions & 0 deletions dir_magic/header.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Header(object):

def setup(self):
print(r"""
_______ __ .______ .___ ___. ___ _______ __ ______
| \ | | | _ \ | \/ | / \ / _____|| | / |
| .--. || | | |_) | | \ / | / ^ \ | | __ | | | ,----'
| | | || | | / | |\/| | / /_\ \ | | |_ | | | | |
| '--' || | | |\ \----. | | | | / _____ \ | |__| | | | | `----.
|_______/ |__| | _| `._____| |__| |__| /__/ \__\ \______| |__| \______| (1.1.1)
About: DirMagic is a library written in Python to brute force directories and subdomains
Author: João Lucas
Language: Python
GitHub: https://github.com/heroesofcode/DirMagic
""")

0 comments on commit c9c8fdb

Please sign in to comment.