Skip to content
This repository was archived by the owner on Nov 11, 2018. It is now read-only.

Commit abe496a

Browse files
committed
Initial commit
0 parents  commit abe496a

8 files changed

Lines changed: 699 additions & 0 deletions

File tree

.gitignore

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Based on .gitignore from https://github.com/pypa/sampleproject
2+
3+
# Backup files
4+
*.~
5+
6+
# Byte-compiled / optimized / DLL files
7+
__pycache__/
8+
*.py[cod]
9+
10+
# C extensions
11+
*.so
12+
13+
# Distribution / packaging
14+
bin/
15+
build/
16+
develop-eggs/
17+
dist/
18+
eggs/
19+
lib/
20+
lib64/
21+
parts/
22+
sdist/
23+
var/
24+
*.egg-info/
25+
.installed.cfg
26+
*.egg
27+
MANIFEST
28+
29+
# Installer logs
30+
pip-log.txt
31+
pip-delete-this-directory.txt
32+
33+
# Translations
34+
*.mo

README.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
[![PyPI](https://img.shields.io/pypi/v/maybe.svg)](https://pypi.python.org/pypi/maybe) ![Python versions](https://img.shields.io/pypi/pyversions/maybe.svg)
2+
3+
---
4+
5+
6+
```
7+
rm -rf pic*
8+
```
9+
10+
Are you sure? Are you *one hundred percent* sure?
11+
12+
13+
# `maybe`...
14+
15+
... allows you to run a command and see what it does to your files *without actually doing it!* After reviewing the operations listed, you can then decide whether you really want these things to happen or not.
16+
17+
![Screenshot](screenshot.png)
18+
19+
20+
## What is this sorcery?!?
21+
22+
`maybe` runs processes under the control of [ptrace](https://en.wikipedia.org/wiki/Ptrace) (with the help of the excellent [python-ptrace](https://bitbucket.org/haypo/python-ptrace/) library). When it intercepts a system call that is about to make changes to the file system, it logs that call, and then modifies CPU registers to both redirect the call to an invalid syscall ID (effectively turning it into a no-op) and set the return value of that no-op call to one indicating success of the original call.
23+
24+
As a result, the process believes that everything it is trying to do is actually happening, when in reality nothing is.
25+
26+
That being said, `maybe` **should :warning: NEVER :warning: be used to run untrusted code** on a system you care about! A process running under `maybe` can still do serious damage to your system because only a handful of syscalls are blocked. Currently, `maybe` is best thought of as an (alpha-quality) "what exactly will this command I typed myself do?" tool.
27+
28+
29+
## Installation
30+
31+
`maybe` requires [Python](https://www.python.org/) 2.7+ :snake:. It has been tested on Linux :penguin:, but should work on most Unixes, possibly including OS X. If you have the [pip](https://pip.pypa.io) package manager, all you need to do is run
32+
33+
```
34+
pip install maybe
35+
```
36+
37+
either as a superuser or from a [virtualenv](https://virtualenv.pypa.io) environment.
38+
39+
40+
## Usage
41+
42+
### Command line
43+
44+
```
45+
maybe COMMAND [ARGUMENT]...
46+
```
47+
48+
No other command line parameters are currently accepted.
49+
50+
### Example
51+
52+
```
53+
maybe mkdir test
54+
```
55+
56+
57+
## License
58+
59+
Copyright &copy; 2016 Philipp Emanuel Weidmann (<pew@worldwidemann.com>)
60+
61+
Released under the terms of the [GNU General Public License, version 3](https://gnu.org/licenses/gpl.html)

maybe/__init__.py

Whitespace-only changes.

maybe/maybe.py

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
#!/usr/bin/env python
2+
3+
# maybe - see what a program does before deciding whether you really want it to happen
4+
#
5+
# Copyright (c) 2016 Philipp Emanuel Weidmann <pew@worldwidemann.com>
6+
#
7+
# Nemo vir est qui mundum non reddat meliorem.
8+
#
9+
# Released under the terms of the GNU General Public License, version 3
10+
# (https://gnu.org/licenses/gpl.html)
11+
12+
13+
from sys import argv, exit
14+
from subprocess import call
15+
16+
from ptrace.tools import locateProgram
17+
from ptrace.debugger import ProcessSignal, NewProcessEvent, ProcessExecution, ProcessExit
18+
from ptrace.debugger.child import createChild
19+
from ptrace.debugger.debugger import PtraceDebugger
20+
from ptrace.func_call import FunctionCallOptions
21+
from ptrace.syscall import SYSCALL_PROTOTYPES, FILENAME_ARGUMENTS
22+
from ptrace.syscall.posix_constants import SYSCALL_ARG_DICT
23+
from ptrace.syscall.syscall_argument import ARGUMENT_CALLBACK
24+
25+
from syscall_filters import SYSCALL_FILTERS
26+
from utilities import T, SYSCALL_REGISTER, RETURN_VALUE_REGISTER
27+
28+
29+
# Register filtered syscalls with python-ptrace so they are parsed correctly
30+
SYSCALL_PROTOTYPES.clear()
31+
FILENAME_ARGUMENTS.clear()
32+
for syscall_filter in SYSCALL_FILTERS:
33+
SYSCALL_PROTOTYPES[syscall_filter.name] = syscall_filter.signature
34+
for argument in syscall_filter.signature[1]:
35+
if argument[0] == "const char *":
36+
FILENAME_ARGUMENTS.add(argument[1])
37+
38+
# Turn list into dictionary indexed by syscall name for fast filter retrieval
39+
SYSCALL_FILTERS = {syscall_filter.name: syscall_filter for syscall_filter in SYSCALL_FILTERS}
40+
41+
# Prevent python-ptrace from decoding arguments to keep raw numerical values
42+
SYSCALL_ARG_DICT.clear()
43+
ARGUMENT_CALLBACK.clear()
44+
45+
46+
def prepareProcess(process):
47+
process.syscall()
48+
process.syscall_state.ignore_callback = lambda syscall: syscall.name not in SYSCALL_FILTERS
49+
50+
51+
def parse_argument(argument):
52+
argument = argument.createText()
53+
if argument.startswith("'"):
54+
# Remove quotes from string argument
55+
return argument[1:-1]
56+
else:
57+
# Note that "int" with base 0 infers the base from the prefix
58+
return int(argument, 0)
59+
60+
61+
format_options = FunctionCallOptions(
62+
replace_socketcall=False,
63+
string_max_length=4096,
64+
)
65+
66+
67+
def get_operations(debugger):
68+
operations = []
69+
70+
while True:
71+
if not debugger:
72+
# All processes have exited
73+
break
74+
75+
# This logic is mostly based on python-ptrace's "strace" example
76+
try:
77+
syscall_event = debugger.waitSyscall()
78+
except ProcessSignal as event:
79+
event.process.syscall(event.signum)
80+
continue
81+
except NewProcessEvent as event:
82+
prepareProcess(event.process)
83+
event.process.parent.syscall()
84+
continue
85+
except ProcessExecution as event:
86+
event.process.syscall()
87+
continue
88+
except ProcessExit as event:
89+
continue
90+
91+
process = syscall_event.process
92+
syscall_state = process.syscall_state
93+
94+
syscall = syscall_state.event(format_options)
95+
96+
if syscall and syscall_state.next_event == "exit":
97+
# Syscall is about to be executed (just switched from "enter" to "exit")
98+
syscall_filter = SYSCALL_FILTERS[syscall.name]
99+
100+
arguments = [parse_argument(argument) for argument in syscall.arguments]
101+
102+
operation = syscall_filter.format(arguments)
103+
if operation is not None:
104+
operations.append(operation)
105+
106+
return_value = syscall_filter.substitute(arguments)
107+
if return_value is not None:
108+
# Set invalid syscall number to prevent call execution
109+
process.setreg(SYSCALL_REGISTER, -1)
110+
# Substitute return value to make syscall appear to have succeeded
111+
process.setreg(RETURN_VALUE_REGISTER, return_value)
112+
113+
process.syscall()
114+
115+
return operations
116+
117+
118+
def main():
119+
if len(argv) < 2:
120+
print(T.red("Error: No command given."))
121+
print("Usage: %s COMMAND [ARGUMENT]..." % argv[0])
122+
exit(1)
123+
124+
# This is basically "shlex.join"
125+
command = " ".join([(("'%s'" % arg) if (" " in arg) else arg) for arg in argv[1:]])
126+
127+
arguments = argv[1:]
128+
arguments[0] = locateProgram(arguments[0])
129+
130+
try:
131+
pid = createChild(arguments, False)
132+
except Exception as error:
133+
print(T.red("Error executing %s: %s." % (T.bold(command) + T.red, error)))
134+
exit(1)
135+
136+
debugger = PtraceDebugger()
137+
debugger.traceFork()
138+
debugger.traceExec()
139+
140+
process = debugger.addProcess(pid, True)
141+
prepareProcess(process)
142+
143+
operations = get_operations(debugger)
144+
145+
debugger.quit()
146+
147+
if operations:
148+
print("%s has prevented %s from performing %d file system operations:\n" %
149+
(T.bold("maybe"), T.bold(command), len(operations)))
150+
for operation in operations:
151+
print(" " + operation)
152+
try:
153+
choice = raw_input("\nDo you want to rerun %s and permit these operations? [y/N] " % T.bold(command))
154+
except KeyboardInterrupt:
155+
choice = ""
156+
if choice.lower() == "y":
157+
call(argv[1:])
158+
else:
159+
print("%s has not detected any file system operations from %s." %
160+
(T.bold("maybe"), T.bold(command)))
161+
162+
163+
if __name__ == "__main__":
164+
main()

0 commit comments

Comments
 (0)