-
Notifications
You must be signed in to change notification settings - Fork 0
/
knowyourentity.py
127 lines (98 loc) · 3.33 KB
/
knowyourentity.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
"""
Know Your Entity main script.
Handles arguments, logging and input validation
"""
import ast
import argparse
import importlib
import ipaddress
import logging
import sys
import conf
import ai_gen
logger = logging.getLogger(__name__)
def parse_args():
"""Parses the arguments provided via CLI."""
# Define the argument parser
parser = argparse.ArgumentParser(
prog="knowyourentity.py",
description="An OSINT federated information retrieval framework.",
epilog="Developed by Enrico Renna. Licenced under GPLv3.0.",
)
# Provide the viable arguments
parser.add_argument(
"entity",
type=str,
help="The entity to be analysed. Currently only IP addresses are supported.",
)
parser.add_argument(
"-v",
"--verbose",
type=str,
choices=["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"],
default="INFO",
help="The level of verbosity of the script. Default and recommended is INFO.",
)
# Parse and return arguments
return parser.parse_args()
def log(args):
"""Configure logging for the framework"""
# Setup logging variables
log_file = "./logs/" + args.entity + ".log"
# Configure logger
logging.basicConfig(
filename=log_file,
encoding="utf-8",
format="%(asctime)s %(levelname)-8s %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
level=args.verbose,
)
# Also log to stdout
logging.getLogger().addHandler(logging.StreamHandler(sys.stdout))
# Begin logging
logger.info("Know Your Entity has started.")
logger.info("The entity to be investigated is %s.", args.entity)
logger.info("The verbosity has been set to %s.", args.verbose)
def validate_input(entity):
"""Verify whether the input is a valid IP address and returns version"""
try:
address = ipaddress.ip_address(entity)
if address.is_private:
logger.warning(
"The IPv%s address %s is a private address reserved for internal network "
"use, not accessible from the internet, and typically used for "
"communication within a local network.",
str(address.version),
entity,
)
done()
return address.version
except ValueError as e:
logger.critical("The entity %s. The program will now exit.", str(e))
done()
def done():
"""Nothing else to do, exit."""
logger.info("\n\nAll operations have been completed. Exiting...")
sys.exit(1)
def main():
"""Main function, calls other functions."""
args = parse_args() # Retrieve arguments
log(args)
version = validate_input(args.entity)
config = conf.read_config("knowyourentity")
tools = ast.literal_eval(config["Settings"]["tools"])
if version != 4:
ipv4_only = ast.literal_eval(config["Settings"]["ipv4_only"])
tools = [tool for tool in tools if tool not in ipv4_only]
intelligence = ""
for tool in tools:
pkg = importlib.import_module(tool)
intelligence += pkg.analyse(args.entity)
ai_gen.generate(args.entity, intelligence)
f_name = "./intelligence/" + args.entity + ".txt"
output = open(f_name, "w+", encoding="utf-8")
output.write(intelligence)
output.close()
done()
if __name__ == "__main__":
main()