Skip to content

Commit 9d50918

Browse files
authored
Core fixes. Provide global variables. Update runner. Upgrade module-package system. (#14)
* Clean init all the modules with the __init__.py file * Remove unused gitkeeps * Add base inits for script packages * Update main file * Set __file__ variable at the module level * Add inits for the modules * Modify all the modules * Delete one more .gitkeep * Add WIP test for user_greeting * Update test module for user_greeting * Fix module runner a bit * Add relative imports to the modules * Add run tests script * Update README.md * Fix module-package runner * Update requirements * Format everything with Black * Handle module and execution errors with runner
1 parent 01b2b3e commit 9d50918

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+712
-411
lines changed

README.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,79 @@
66
virtualenv -p python3 venv
77
(or python3 -m venv venv)
88
pip3 install -r requirements.txt
9+
```
10+
11+
## Running as a framework
12+
_First of all: provide some arguments in the `main.py` file to collect information based on your data (WIP now, will be improved later)_
13+
14+
To run the framework:
15+
```bash
916
python3 main.py
1017
```
18+
To run the tests:
19+
```bash
20+
chmod +x run_tests.sh
21+
./run_tests.sh
22+
```
23+
_or you can run them like this, of course:_
24+
```bash
25+
python3 -m unittest discover -v
26+
```
27+
## Running as a separated module
28+
Basic:
29+
```python3
30+
python3 -m src.scripts.<category>.<name> any_arguments_here
31+
```
32+
Example command:
33+
```bash
34+
python3 -m src.scripts.other.user_greeting JohnDoe
35+
```
36+
Example output:
37+
```
38+
{'message': "Successfully finished! (args: (), kwargs: {'username': "
39+
"'johndoe'})",
40+
'result': 'Hello, JohnDoe!',
41+
'status': 'success'}
42+
43+
```
1144

1245
## Create your own script
1346
Use the following structure:
47+
1. Create your own module directory in the following way:
48+
```
49+
/src/scripts/<choose_your_category_here>/<your_script_name>/<script_files>
50+
```
51+
2. Provide the following structure of your script directory:
52+
```
53+
your_script_name
54+
├── __init__.py - use this module to set the default parent directory (you can copy this file from any other script)
55+
├── __main__.py - use this module to provide some basic interface to use your script as a module (the same as if __name__ == "__main__")
56+
├── module.py - use this module to describe the basic logic of your module (you can import it in the __main__.py to provide interface)
57+
└── test_module.py - use this module for unittest tests
58+
```
59+
3. Create the `__init__.py` file. An example of the `__init__.py` boilerplate structure can be seen below:
60+
```python3
61+
import sys
62+
from pathlib import Path
63+
64+
__root_dir = Path(__file__).parents[4]
65+
sys.path.append(str(__root_dir))
66+
67+
```
68+
4. Create the `__main__.py` file. An example of the `__main__.py` boilerplate structure can be seen below:
69+
```python3
70+
#!/usr/bin/env python3
71+
72+
from pprint import pprint
73+
from sys import argv
74+
75+
from src.core.utils.module import run_module
76+
from .module import Runner
77+
78+
result = run_module(Runner, args=argv, arg_name="username", arg_default="johndoe")
79+
pprint(result)
80+
```
81+
5. Create the module itself. An example of the basic `module.py` file can be seen below:
1482
```python3
1583
#!/usr/bin/env python3
1684

@@ -60,3 +128,4 @@ class Runner(OsintRunner):
60128
argument = kwargs.get("my_argument", "Arguments were not provided!")
61129
return ScriptResponse.success(message=f"Script finished with argument {argument}")
62130
```
131+
6. For `test_module.py` you can use any required tests (as you wish). A test case for your module is required to keep the project clean.

main.py

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,33 @@
1313
from src.core.case.osint import OsintCase
1414
from src.core.case.recon import ReconCase
1515
from src.core.case.base import BaseCase
16+
from src.core.utils.log import Logger
17+
18+
19+
logger = Logger.get_logger(name="osint-framework")
20+
21+
22+
def run_case(case_class: type, *args, **kwargs):
23+
"""
24+
Define and smoke run the BaseCase
25+
:param case_class: original class of the case
26+
:param args: some args
27+
:param kwargs: some kwargs
28+
:return: result of the execution
29+
"""
30+
logger.info(f"start {case_class.__name__} case processing")
31+
case = case_class()
32+
case.process(*args, **kwargs)
33+
return case.get_results()
1634

1735

1836
if __name__ == "__main__":
19-
# Will return 2 results from "other" category scripts
20-
other_case = BaseCase()
21-
other_case.process(
22-
username="johndoe", email="johndoe@gmail.com", fullname="John Doe",
23-
)
24-
other_case_results = other_case.get_results()
25-
26-
# Will return 1 result from "recon" category scripts
27-
recon_case = ReconCase()
28-
recon_case.process(url="https://facebook.com")
29-
recon_case_results = recon_case.get_results()
30-
31-
# Will return nothing (no scripts for now, sorry!)
32-
osint_case = OsintCase()
33-
osint_case.process(username="any_value_here")
34-
osint_case_results = osint_case.get_results()
35-
36-
# Print out all the results
37-
for result in other_case_results, recon_case_results, osint_case_results:
38-
pprint(result)
37+
# fmt: off
38+
base_case = run_case(case_class=BaseCase, username="johndoe", email="johndoe@gmail.com", fullname="John Doe")
39+
osint_case = run_case(case_class=OsintCase, username="johndoe", email="johndoe@gmail.com", fullname="John Doe")
40+
recon_case = run_case(case_class=ReconCase, url="https://facebook.com")
41+
42+
pprint(base_case)
43+
pprint(osint_case)
44+
pprint(recon_case)
45+
# fmt: on

requirements.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
1+
aiodns==2.0.0
2+
aiohttp==3.6.2
3+
aiosmtpd==1.2
4+
async-timeout==3.0.1
5+
atpublic==1.0
6+
attrs==19.3.0
17
certifi==2020.6.20
8+
cffi==1.14.0
29
chardet==3.0.4
310
idna==2.10
11+
mmh3==2.5.1
12+
multidict==4.7.6
13+
pycares==3.1.1
14+
pycparser==2.20
415
requests==2.24.0
516
urllib3==1.25.9
17+
verify-email==2.4.1
18+
yarl==1.4.2

run_tests.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env bash
2+
3+
python3 -m unittest discover -v
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

src/core/handlers/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)