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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
|
import struct, random, string, subprocess, os, sys, hashlib from collections import defaultdict import resource PTRACE_TRACEME = 0 PTRACE_PEEKTEXT = 1 PTRACE_PEEKDATA = 2 PTRACE_PEEKUSER = 3 PTRACE_POKETEXT = 4 PTRACE_POKEDATA = 5 PTRACE_POKEUSER = 6 PTRACE_CONT = 7 PTRACE_KILL = 8 PTRACE_SINGLESTEP = 9 PTRACE_GETREGS = 12 PTRACE_SETREGS = 13 PTRACE_GETFPREGS = 14 PTRACE_SETFPREGS = 15 PTRACE_ATTACH = 16 PTRACE_DETACH = 17 PTRACE_GETFPXREGS = 18 PTRACE_SETFPXREGS = 19 PTRACE_SYSCALL = 24 PTRACE_SETOPTIONS = 16896 PTRACE_GETEVENTMSG = 16897 PTRACE_GETSIGINFO = 16898 PTRACE_SETSIGINFO = 16899 PTRACE_LISTEN = 16904 PTRACE_O_TRACESYSGOOD = 1 PTRACE_O_TRACEFORK = 2 PTRACE_O_TRACEVFORK = 4 PTRACE_O_TRACECLONE = 8 PTRACE_O_TRACEEXEC = 16 PTRACE_O_TRACEVFORKDONE = 32 PTRACE_O_TRACEEXIT = 64 PTRACE_O_MASK = 127 PTRACE_O_TRACESECCOMP = 128 PTRACE_O_EXITKILL = 1048576 PTRACE_O_SUSPEND_SECCOMP = 2097152 PTRACE_SEIZE = 16902 import ctypes from ctypes import * from ctypes import get_errno, cdll from ctypes.util import find_library
class user_regs_struct(Structure): _fields_ = ( ('r15', c_ulong), ('r14', c_ulong), ('r13', c_ulong), ('r12', c_ulong), ('rbp', c_ulong), ('rbx', c_ulong), ('r11', c_ulong), ('r10', c_ulong), ('r9', c_ulong), ('r8', c_ulong), ('rax', c_ulong), ('rcx', c_ulong), ('rdx', c_ulong), ('rsi', c_ulong), ('rdi', c_ulong), ('oax', c_ulong), ('rip', c_ulong), ('cs', c_ulong), ('eflags', c_ulong), ('rsp', c_ulong), ('ss', c_ulong), ('fs_base', c_ulong), ('gs_base', c_ulong), ('ds', c_ulong), ('es', c_ulong), ('fs', c_ulong), ('gs', c_ulong))
libc = CDLL('libc.so.6', use_errno=True) ptrace = libc.ptrace ptrace.argtypes = [c_uint, c_uint, c_long, c_long] ptrace.restype = c_long
def mem_read(pid, pos=-1, tlen=8): fd = os.open('/proc/%d/mem' % pid, os.O_RDONLY) if pos >= 0: os.lseek(fd, pos, 0) buf = b'' while 1: cd = os.read(fd, tlen - len(buf)) if cd == b'': break buf += cd if len(buf) == tlen: break
os.close(fd) return buf
def pkiller(): from ctypes import cdll import ctypes cdll['libc.so.6'].prctl(1, 9)
def pnx(status):
def num_to_sig(num): sigs = [ 'SIGHUP', 'SIGINT', 'SIGQUIT', 'SIGILL', 'SIGTRAP', 'SIGABRT', 'SIGBUS', 'SIGFPE', 'SIGKILL', 'SIGUSR1', 'SIGSEGV', 'SIGUSR2', 'SIGPIPE', 'SIGALRM', 'SIGTERM', 'SIGSTKFLT', 'SIGCHLD', 'SIGCONT', 'SIGSTOP', 'SIGTSTP', 'SIGTTIN', 'SIGTTOU', 'SIGURG', 'SIGXCPU', 'SIGXFSZ', 'SIGVTALRM', 'SIGPROF', 'SIGWINCH', 'SIGIO', 'SIGPWR', 'SIGSYS'] if num - 1 < len(sigs): return sigs[(num - 1)] else: return hex(num)[2:]
status_list = [] status_list.append(hex(status)) ff = [os.WCOREDUMP, os.WIFSTOPPED, os.WIFSIGNALED, os.WIFEXITED, os.WIFCONTINUED] for f in ff: if f(status): status_list.append(f.__name__) break else: status_list.append('')
status_list.append(num_to_sig(status >> 8 & 255)) ss = (status & 16711680) >> 16 ptrace_sigs = ['PTRACE_EVENT_FORK', 'PTRACE_EVENT_VFORK', 'PTRACE_EVENT_CLONE', 'PTRACE_EVENT_EXEC', 'PTRACE_EVENT_VFORK_DONE', 'PTRACE_EVENT_EXIT', 'PTRACE_EVENT_SECCOMP'] if ss >= 1: if ss - 1 <= len(ptrace_sigs): status_list.append(ptrace_sigs[(ss - 1)]) else: status_list.append(hex(ss)[2:]) return status_list
def main(): pipe = subprocess.PIPE fullargs = ['./s'] p = subprocess.Popen(fullargs, close_fds=True, preexec_fn=pkiller) pid = p.pid opid = pid pid, status = os.waitpid(-1, 0) ptrace(PTRACE_SETOPTIONS, pid, 0, PTRACE_O_TRACESECCOMP | PTRACE_O_EXITKILL | PTRACE_O_TRACECLONE | PTRACE_O_TRACEVFORK) ptrace(PTRACE_CONT, pid, 0, 0) SXX = set() regs = user_regs_struct() while True: pid, status = os.waitpid(-1, 0) ssy = pnx(status) if ssy[1] == 'WIFEXITED': break if ssy[2] == 'SIGSEGV': break if ssy[2] == 'SIGTRAP': res = ptrace(PTRACE_GETREGS, pid, 0, ctypes.addressof(regs)) nn = mem_read(pid, regs.rip, 1)[0] if nn == 0x48: regs.rax = regs.rdi regs.rdi = regs.rsi ptrace(PTRACE_SETREGS, pid, 0, ctypes.addressof(regs)) else: if nn == 0x11 or nn == 0x21 or nn == 0x31: offd = {17:0, 33:40, 49:72} vv = mem_read(pid, regs.rsp + offd[nn], 8) vv = struct.unpack('<Q', vv)[0] SXX.add(vv) regs.rip += 1 ptrace(PTRACE_SETREGS, pid, 0, ctypes.addressof(regs)) elif nn == 0x12 or nn == 0x22 or nn == 0x32: offd = {18:0, 34:40, 50:72} vv = mem_read(pid, regs.rsp + offd[nn], 8) vv = struct.unpack('<Q', vv)[0] if vv not in SXX: print('\n\n!!!Stack Violation Detected!!!\n\n') regs.rip = 0 ptrace(PTRACE_SETREGS, pid, 0, ctypes.addressof(regs)) break SXX.remove(vv) regs.rip += 1 ptrace(PTRACE_SETREGS, pid, 0, ctypes.addressof(regs)) res = ptrace(PTRACE_CONT, pid, 0, 0)
try: p.kill() except OSError: pass
while 1: try: pid, status = os.waitpid(-1, 0) ssy = pnx(status) except ChildProcessError: break
if __name__ == '__main__': sys.exit(main())
|