2019-12-20 05:22:51 +00:00
|
|
|
"""Onionr - Private P2P Communication.
|
2019-12-14 19:45:18 +00:00
|
|
|
|
2019-12-20 05:22:51 +00:00
|
|
|
Prevent eval/exec/os.system and log it
|
2019-12-14 19:45:18 +00:00
|
|
|
"""
|
|
|
|
import base64
|
2019-12-18 09:58:47 +00:00
|
|
|
import platform
|
2019-12-14 19:45:18 +00:00
|
|
|
|
|
|
|
import logger
|
|
|
|
from utils import identifyhome
|
|
|
|
from onionrexceptions import ArbitraryCodeExec
|
|
|
|
"""
|
2022-03-18 00:56:31 +00:00
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
2019-12-14 19:45:18 +00:00
|
|
|
|
2022-03-18 00:56:31 +00:00
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
2019-12-14 19:45:18 +00:00
|
|
|
|
2022-03-18 00:56:31 +00:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2019-12-14 19:45:18 +00:00
|
|
|
"""
|
|
|
|
|
2022-06-05 20:11:53 +00:00
|
|
|
untrusted_exec = True
|
2019-12-14 19:45:18 +00:00
|
|
|
|
2019-12-18 09:58:47 +00:00
|
|
|
def block_system(cmd):
|
2019-12-20 05:22:51 +00:00
|
|
|
"""Prevent os.system except for whitelisted commands+contexts."""
|
2020-08-12 22:21:11 +00:00
|
|
|
logger.warn('POSSIBLE EXPLOIT DETECTED, SEE LOGS', terminal=True)
|
|
|
|
logger.warn(f'POSSIBLE EXPLOIT: shell command not in whitelist: {cmd}')
|
|
|
|
raise ArbitraryCodeExec('os.system command not in whitelist')
|
2019-12-18 09:58:47 +00:00
|
|
|
|
|
|
|
|
2019-12-14 19:45:18 +00:00
|
|
|
def block_exec(event, info):
|
2019-12-20 05:22:51 +00:00
|
|
|
"""Prevent arbitrary code execution in eval/exec and log it."""
|
2019-12-14 19:45:18 +00:00
|
|
|
# because libraries have stupid amounts of compile/exec/eval,
|
|
|
|
# We have to use a whitelist where it can be tolerated
|
2020-01-29 21:44:44 +00:00
|
|
|
# Generally better than nothing, not a silver bullet
|
2022-06-05 20:11:53 +00:00
|
|
|
if untrusted_exec:
|
|
|
|
return
|
2019-12-14 19:45:18 +00:00
|
|
|
whitelisted_code = [
|
2022-09-26 20:06:05 +00:00
|
|
|
|
2019-12-14 19:45:18 +00:00
|
|
|
]
|
2022-01-31 05:59:34 +00:00
|
|
|
whitelisted_source = [
|
|
|
|
]
|
2019-12-14 19:45:18 +00:00
|
|
|
home = identifyhome.identify_home()
|
|
|
|
|
2019-12-22 19:42:10 +00:00
|
|
|
code_b64 = base64.b64encode(info[0].co_code).decode()
|
2020-02-22 11:44:21 +00:00
|
|
|
if code_b64 in whitelisted_source:
|
|
|
|
return
|
2019-12-22 19:42:10 +00:00
|
|
|
|
2019-12-14 19:45:18 +00:00
|
|
|
for source in whitelisted_code:
|
|
|
|
if info[0].co_filename.endswith(source):
|
|
|
|
return
|
|
|
|
|
2020-02-08 09:07:07 +00:00
|
|
|
if 'plugins/' in info[0].co_filename:
|
2019-12-14 19:45:18 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
logger.warn('POSSIBLE EXPLOIT DETECTED, SEE LOGS', terminal=True)
|
|
|
|
logger.warn('POSSIBLE EXPLOIT DETECTED: ' + info[0].co_filename)
|
|
|
|
logger.warn('Prevented exec/eval. Report this with the sample below')
|
|
|
|
logger.warn(f'{event} code in base64 format: {code_b64}')
|
|
|
|
raise ArbitraryCodeExec("Arbitrary code (eval/exec) detected.")
|