-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpt.py
134 lines (115 loc) · 4.46 KB
/
pt.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
128
129
130
131
132
133
134
from Definitions import Environment
import TerminalModule
import DisasmTools.Terminal
import MiscTools.Terminal
import FixTools.Terminal
import SrchTools.Terminal
import time
class PseudoTerminal(TerminalModule.TerminalModule, object):
"""
Contains all of the other terminal modules, as well as essential commands
"""
def __init__(self, fmt='[+] pt (main terminal)'):
# the module itelf also has a summary format, this is created by TerminalModule
super(PseudoTerminal, self).__init__(fmt)
try:
# each command in the module is added to the help and fmt records
self.registerCommand(self.help, "help (command/module)")
self.registerCommand(self.fmt, "fmt (command/module)")
self.registerCommand(self.echo, "echo (msg)")
self.registerCommand(self.time, "time (func, func_args)")
self.registerCommand(self.clear, "clear (n=32)")
self.registerCommand(self.env, "env (key=Val)")
self.registerCommand(self.clrenv, "clrenv ()")
# __init__ of modules should set up things similarly to pt
self.dis = DisasmTools.Terminal.DisTerminal()
self.srch = SrchTools.Terminal.SrchTerminal()
self.fix = FixTools.Terminal.fix()
self.misc = MiscTools.Terminal.MiscTerminal()
self.registerModule(self.dis)
self.registerModule(self.srch)
self.registerModule(self.fix)
self.registerModule(self.misc)
self._initialized = True
except Exception as e:
print(e)
self._initialized = False
@staticmethod
def echo(msg):
# type: (str) -> None
"""
simply echos a message to the terminal
:param msg: message to echo
"""
print(msg)
@staticmethod
def time(func, *args, **kwargs):
"""
Calls and times the passed in function in ms
:param func: the function to call and time
:param args: arguments to the function
:param kwargs: keyworded arguments to the function
:return: what the called function returns
"""
stopwatch_ms = int(round(time.time()*1000))
output = func(*args, **kwargs)
stopwatch_ms = int(round(time.time()*1000)) - stopwatch_ms
print("Execution time: %s ms" % (stopwatch_ms))
return output
@staticmethod
def clear(n=32):
"""
prints n new lines
"""
if n < 0: n = 0
for i in range(n): print('')
@staticmethod
def env(**kwargs):
"""
changes the value of an environmental variable within pt.
Those environmental variables are used by other commands and modules, and must be configured
:param kwargs: key, value pairs to assign as variables within pt
"""
for key in kwargs.keys():
# only set it if the key actually already exiasts
if key in Environment.env:
Environment.env[key] = kwargs[key]
else:
raise KeyError("key %s was not found in the Definitions/Environment.py" % key)
@staticmethod
def clrenv():
"""
sets the Environment back to its original state
"""
for key in Environment.env.keys():
if type(Environment.env[key]) == str:
Environment.env[key] = ''
else:
Environment.env[key] = None
def main():
# ida caches source. In active development, this forces it to re-read source
import os
import sys
if os.getcwd() not in sys.path: sys.path.append(os.getcwd())
import idaapi
import Definitions, DisasmTools, FixTools, IDAItems, MiscTools, SrchTools
def require_project():
Definitions.require_package()
DisasmTools.require_package()
FixTools.require_package()
IDAItems.require_package()
MiscTools.require_package()
SrchTools.require_package()
import imp
require_project()
# if environment_path:
# environment = imp.load_source('environment', environment_path)
# environment = environment.MyClass()
global pt
pt = PseudoTerminal()
if pt._initialized:
pt.echo("PseudoTerminal, ready for combat!")
else:
print('Initalized Environment to Default')
if __name__ == '__main__':
main()