Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigFileExtractor.py
More file actions
Latest commit
128 lines (103 loc) · 5.29 KB
/
Copy pathConfigFileExtractor.py
File metadata and controls
128 lines (103 loc) · 5.29 KB
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
128
importos
importos.path
importrandom
importstring
fromparamikoimportSSHClient, AutoAddPolicy, ssh_exception
fromlib.constantsimport (SAP_APPS_DIR, CREATE_TEMP_DIR_CMD, SCRIPT_PATH, DIR_RESULTS,
SOAP_CONFIG_COMMAND_EXTRACT, SOAP_DIR_RESULTS, SOAP_ERROR1, SOAP_ERROR2,
WEBDYNPRO_CONFIG_COMMAND_EXTRACT, WEBDYNPRO_DIR_RESULTS, SERVLETS_WEBXML_COMMAND_EXTRACT,
SERVLETS_DIR_RESULTS, PORTALAPP_CONFIG_COMMAND_EXTRACT, PORTALAPP_DIR_RESULTS,
SOAP_FILETYPE, WEBDYNPRO_FILETYPE, SERVLETS_FILETYPE, PORTALAPP_FILETYPE, DASH_NUMBER_LINE)
classErrorInOSCmdException(Exception):pass
classConfigFileExtractor:
def__init__(self, host, port, instance, sid, user, password=None, out_dir=DIR_RESULTS):
self.host=host
self.port=port
self.sid=sid
self.instance=instance
self.user=user
self.password=password
self.out_dir=out_dir
self._connect()
def_execute_cmd(self, cmd):
stdin, stdout, stderr=self.client.exec_command(cmd)
error=stderr.read()
iferror!=b"":
raiseErrorInOSCmdException(f"Error trying to execute command \"{cmd}\": {error}")
returnstdout.read()
def_connect(self):
print(f"[+] Trying to connect to {self.host} with user {self.user}...",end="")
self.client=SSHClient()
self.client.set_missing_host_key_policy(AutoAddPolicy())
self.client.load_system_host_keys()
try:
ifself.password:
self.client.connect(self.host,
port=self.port,
username=self.user,
password=self.password,
allow_agent=False,
look_for_keys=False)
else:
self.client.connect(self.host,
port=self.port,
username=self.user,
password=self.password)
exceptssh_exception.AuthenticationException:
print(f"\n[-] SSH authentication failed. Review the SSH user/password combination is correct and retry.")
exit(1)
def_copy_command_to_script(self, cmd):
ftp=self.client.open_sftp()
file=ftp.file(SCRIPT_PATH, "w")
file.chmod(0o755)
file.write(cmd)
file.flush()
ftp.close()
def_copy_dir(self, remote_source_dir, local_dest_dir):
full_path_local_dest_dir=f"{self.out_dir}/{self.host}/{local_dest_dir}"
ifnotos.path.exists(full_path_local_dest_dir):
os.makedirs(full_path_local_dest_dir)
ftp=self.client.open_sftp()
out=self._execute_cmd(f"ls {remote_source_dir}")
filenames=out.decode('ascii').split('\n')
forfilenameinfilenames:
iffilename!="":
ftp.get(f"{remote_source_dir}/{filename}", f"{full_path_local_dest_dir}/{filename}")
ftp.close()
def_extract_config_files(self, filetype, command, dest_dir_results):
print(f"\t[+] Extracting {filetype} files")
try:
random_dir_path="/tmp/"+"".join([random.choice(string.ascii_lowercase) foriinrange(10)])
app_dir=SAP_APPS_DIR.format(sid=self.sid, instance=self.instance)
print(f"\t\t[+] Checking if apps dir is reachable..")
out=self._execute_cmd(f"ls {app_dir}")
print(f"\t\t[+] Creating directory {random_dir_path}")
self._execute_cmd(CREATE_TEMP_DIR_CMD.format(random_dir_path))
print(f"\t\t[+] Copying script file")
self._copy_command_to_script(command.format(sap_apps_dir=app_dir, random_dir_path=random_dir_path))
print(f"\t\t[+] Executing script/command (this could take a few mins)")
self._execute_cmd(SCRIPT_PATH)
print(f"\t\t[+] Copying all config files to this machine.")
self._copy_dir(random_dir_path, dest_dir_results)
exceptExceptionase:
raisee
finally:
print(f"\t\t[+] Deleting script and random dir in server..")
self._execute_cmd(f'rm -r {SCRIPT_PATH}{random_dir_path}')
print(f"\t\t[+] Done!")
defextract_soap_files(self):
try:
self._extract_config_files(SOAP_FILETYPE,SOAP_CONFIG_COMMAND_EXTRACT, SOAP_DIR_RESULTS)
exceptErrorInOSCmdExceptionase:
ifSOAP_ERROR1instr(e) andSOAP_ERROR2instr(e):
print("\t\t[W] Servlet error found. Trying with command modification. ")
new_cmd=SOAP_CONFIG_COMMAND_EXTRACT.replace("-f 2","-f 1")
self._extract_config_files(SOAP_FILETYPE,new_cmd, SOAP_DIR_RESULTS)
else:
raisee
defextract_webdynpros_files(self):
self._extract_config_files(WEBDYNPRO_FILETYPE,WEBDYNPRO_CONFIG_COMMAND_EXTRACT, WEBDYNPRO_DIR_RESULTS)
defextract_portalapps_files(self):
self._extract_config_files(PORTALAPP_FILETYPE,PORTALAPP_CONFIG_COMMAND_EXTRACT, PORTALAPP_DIR_RESULTS)
defextract_servlets_files(self):
self._extract_config_files(SERVLETS_FILETYPE,SERVLETS_WEBXML_COMMAND_EXTRACT, SERVLETS_DIR_RESULTS)