├── examples ├── requirements.txt ├── utils.py ├── README.md └── watch_syscall.py ├── .gitignore ├── vmi.mk ├── profile.h ├── r2.mk ├── Makefile ├── utils.h ├── x86-32.h ├── io_vmi.h ├── x86-64.h ├── README.md ├── profile.c ├── io_vmi.c ├── utils.c ├── debug_vmi.c └── LICENSE /examples/requirements.txt: -------------------------------------------------------------------------------- 1 | docopt 2 | ipython 3 | r2pipe 4 | libvmi 5 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # QtCreator 2 | *.config 3 | *.creator 4 | *.creator.user 5 | *.files 6 | *.includes 7 | 8 | # C 9 | *.so 10 | 11 | # Python 12 | venv/ 13 | __pycache__/ 14 | -------------------------------------------------------------------------------- /vmi.mk: -------------------------------------------------------------------------------- 1 | TARGETS = io_vmi.so debug_vmi.so 2 | VMI_CFLAGS = $(shell pkg-config --cflags libvmi glib-2.0 json-c) 3 | VMI_LDFLAGS = $(shell pkg-config --libs libvmi glib-2.0 json-c) 4 | -------------------------------------------------------------------------------- /profile.h: -------------------------------------------------------------------------------- 1 | #ifndef PROFILE_H 2 | #define PROFILE_H 3 | 4 | #include 5 | #include 6 | 7 | #include "io_vmi.h" 8 | 9 | bool load_symbols(RIO *io, vmi_instance_t vmi); 10 | 11 | #endif // PROFILE_H 12 | -------------------------------------------------------------------------------- /r2.mk: -------------------------------------------------------------------------------- 1 | SO_EXT=so 2 | R2_CFLAGS+=$(shell pkg-config --cflags r_bin r_util) 3 | R2_LDFLAGS+=$(shell pkg-config --libs r_util r_bin) 4 | R2_USER_PLUGINS=$(shell r2 -HUSER_PLUGINS) 5 | # support radare2 >= `2.9.0 6 | ifeq ($(R2_USER_PLUGINS),) 7 | R2_USER_PLUGINS=$(shell r2 -HR2_USER_PLUGINS) 8 | endif 9 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | include vmi.mk 2 | include r2.mk 3 | 4 | CFLAGS = -Wall -Wextra -fPIC -g -O2 5 | LDFLAGS = -shared 6 | CFLAGS += $(R2_CFLAGS) $(VMI_CFLAGS) 7 | LDFLAGS += $(R2_LDFLAGS) $(VMI_LDFLAGS) 8 | 9 | 10 | all: $(TARGETS) 11 | 12 | %.$(SO_EXT): %.c profile.c utils.c 13 | $(CC) $(CFLAGS) $^ -o $@ $(LDFLAGS) 14 | 15 | install: all 16 | mkdir -p $(R2_USER_PLUGINS) 17 | cp -f $(TARGETS) $(R2_USER_PLUGINS) 18 | 19 | clean: 20 | rm -f $(TARGETS) 21 | -------------------------------------------------------------------------------- /utils.h: -------------------------------------------------------------------------------- 1 | #ifndef UTILS_H 2 | #define UTILS_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | #include "io_vmi.h" 9 | 10 | void print_event(vmi_event_t *event); 11 | bool vaddr_equal(vmi_instance_t vmi, addr_t vaddr1, addr_t vaddr2); 12 | char* dtb_to_pname(vmi_instance_t vmi, addr_t dtb); 13 | status_t vmi_dtb_to_pid_extended_idle(vmi_instance_t vmi, addr_t dtb, vmi_pid_t *pid); 14 | bool attach_new_process(RDebug *dbg); 15 | bool is_target_process(RIOVmi *rio_vmi, const char *proc_name, uint64_t dtb); 16 | bool intercept_process(RDebug *dbg, int pid); 17 | 18 | 19 | #endif // !UTILS_h 20 | -------------------------------------------------------------------------------- /x86-32.h: -------------------------------------------------------------------------------- 1 | "=PC eip\n" 2 | "=SP esp\n" 3 | "=BP ebp\n" 4 | "=A0 eax\n" 5 | "=A1 ebx\n" 6 | "=A2 ecx\n" 7 | "=A3 edx\n" 8 | "=SN oeax\n" 9 | "gpr eax .32 0 0\n" 10 | "gpr ecx .32 4 0\n" 11 | "gpr edx .32 8 0\n" 12 | "gpr ebx .32 12 0\n" 13 | "gpr esp .32 16 0\n" 14 | "gpr ebp .32 20 0\n" 15 | "gpr esi .32 24 0\n" 16 | "gpr edi .32 28 0\n" 17 | "gpr eip .32 32 0\n" 18 | "gpr eflags .32 36 0\n" 19 | "seg cs .32 40 0\n" 20 | "seg ss .32 44 0\n" 21 | "seg ds .32 48 0\n" 22 | "seg es .32 52 0\n" 23 | "seg fs .32 56 0\n" 24 | "seg gs .32 60 0\n" 25 | "fpu st0 .80 64 0\n" 26 | "fpu st1 .80 74 0\n" 27 | "fpu st2 .80 84 0\n" 28 | "fpu st3 .80 94 0\n" 29 | "fpu st4 .80 104 0\n" 30 | "fpu st5 .80 114 0\n" 31 | "fpu st6 .80 124 0\n" 32 | "fpu st7 .80 134 0\n" 33 | "gpr fctrl .32 144 0\n" 34 | "gpr fstat .32 148 0\n" 35 | "gpr ftag .32 152 0\n" 36 | "gpr fiseg .32 156 0\n" 37 | "gpr fioff .32 160 0\n" 38 | "gpr foseg .32 164 0\n" 39 | "gpr fooff .32 168 0\n" 40 | "gpr fop .32 172 0\n" 41 | -------------------------------------------------------------------------------- /io_vmi.h: -------------------------------------------------------------------------------- 1 | #ifndef IO_VMI_H 2 | #define IO_VMI_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | 12 | typedef struct { 13 | char *vm_name; 14 | bool url_identify_by_name; 15 | char* proc_name; 16 | int pid; 17 | uint64_t pid_cr3; 18 | int current_vcpu; 19 | vmi_instance_t vmi; 20 | bool attached; 21 | vmi_event_t *sstep_event; 22 | // whether the action/command singlestep has been requested 23 | bool cmd_sstep; 24 | // table [rip] -> [vmi_event_t] 25 | // event might be int3 or mem_event 26 | GHashTable *bp_events_table; 27 | // if we are attaching to a new process being created 28 | // or if the process is already existing 29 | bool attach_new_process; 30 | } RIOVmi; 31 | 32 | typedef struct 33 | { 34 | RIOVmi *rio_vmi; 35 | uint64_t pid_cr3; 36 | addr_t bp_vaddr; 37 | RBreakpoint* bp; 38 | RBreakpointItem *bpitem; 39 | } bp_event_data; 40 | 41 | #endif // IO_VMI_H 42 | -------------------------------------------------------------------------------- /x86-64.h: -------------------------------------------------------------------------------- 1 | "=PC rip\n" 2 | "=SP rsp\n" 3 | "=BP rbp\n" 4 | "=A0 rax\n" 5 | "=A1 rbx\n" 6 | "=A2 rcx\n" 7 | "=A3 rdx\n" 8 | "=SN orax\n" 9 | "gpr fake .64 795 0\n" 10 | "gpr rax .64 0 0\n" 11 | "gpr rbx .64 8 0\n" 12 | "gpr rcx .64 16 0\n" 13 | "gpr rdx .64 24 0\n" 14 | "gpr rsi .64 32 0\n" 15 | "gpr rdi .64 40 0\n" 16 | "gpr rbp .64 48 0\n" 17 | "gpr rsp .64 56 0\n" 18 | "gpr r8 .64 64 0\n" 19 | "gpr r9 .64 72 0\n" 20 | "gpr r10 .64 80 0\n" 21 | "gpr r11 .64 88 0\n" 22 | "gpr r12 .64 96 0\n" 23 | "gpr r13 .64 104 0\n" 24 | "gpr r14 .64 112 0\n" 25 | "gpr r15 .64 120 0\n" 26 | "gpr rip .64 128 0\n" 27 | "gpr eflags .32 136 0\n" 28 | "seg cs .32 140 0\n" 29 | "seg ss .32 144 0\n" 30 | "seg ds .32 148 0\n" 31 | "seg es .32 152 0\n" 32 | "seg fs .32 156 0\n" 33 | "seg gs .32 160 0\n" 34 | "fpu st0 .80 164 0\n" 35 | "fpu st1 .80 174 0\n" 36 | "fpu st2 .80 184 0\n" 37 | "fpu st3 .80 194 0\n" 38 | "fpu st4 .80 204 0\n" 39 | "fpu st5 .80 214 0\n" 40 | "fpu st6 .80 224 0\n" 41 | "fpu st7 .80 234 0\n" 42 | "gpr fctrl .32 244 0\n" 43 | "gpr fstat .32 248 0\n" 44 | "gpr ftag .32 252 0\n" 45 | "gpr fiseg .32 256 0\n" 46 | "gpr fioff .32 260 0\n" 47 | "gpr foseg .32 264 0\n" 48 | "gpr fooff .32 268 0\n" 49 | "gpr fop .32 272 0\n" 50 | -------------------------------------------------------------------------------- /examples/utils.py: -------------------------------------------------------------------------------- 1 | # stdlib 2 | import logging 3 | import re 4 | import json 5 | from io import StringIO 6 | 7 | # 3rd 8 | from rekall import plugins, session 9 | 10 | class RekallVMI: 11 | 12 | def __init__(self, vm_name, hypervisor=""): 13 | self.vm_name = vm_name 14 | self.hypervisor = hypervisor 15 | 16 | rekall_url = 'vmi://{}/{}'.format(self.hypervisor, self.vm_name) 17 | logging.info('Initializing Rekall VMI address space') 18 | self.session = session.Session( 19 | filename=rekall_url, 20 | autodetect=["rsds"], 21 | autodetect_build_local='none', 22 | format='data', 23 | profile_path=[ 24 | "http://profiles.rekall-forensic.com" 25 | ]) 26 | 27 | def find_syscall(self, syscall_name): 28 | strio = StringIO() 29 | logging.info('Running ssdt plugin') 30 | self.session.RunPlugin("ssdt", output=strio) 31 | ssdt = json.loads(strio.getvalue()) 32 | for e in ssdt: 33 | if isinstance(e, list) and e[0] == 'r': 34 | if e[1]["divider"] is None: 35 | full_name = e[1]["symbol"]["symbol"] 36 | m = re.match(r'^(?P.+)!(?P.+)$', full_name) 37 | if m: 38 | name = m.group('name') 39 | if name == syscall_name: 40 | address = e[1]["symbol"]["address"] 41 | return (full_name, address) 42 | raise RuntimeError('Cannot find {} in ssdt'.format(syscall_name)) 43 | 44 | def get_winobj_fields(self): 45 | object_name_off = self.session.profile.get_obj_offset('_OBJECT_ATTRIBUTES', 'ObjectName') 46 | buffer_off = self.session.profile.get_obj_offset('_UNICODE_STRING', 'Buffer') 47 | return { 48 | 'object_name': { 49 | 'offset': object_name_off, 50 | 'size': 'P' 51 | }, 52 | 'buffer': { 53 | 'offset': buffer_off, 54 | 'size': 'P' 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # r2vmi 2 | 3 | [![Join the chat at https://gitter.im/r2vmi/Lobby](https://badges.gitter.im/r2vmi/Lobby.svg)](https://gitter.im/r2vmi/Lobby?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) 4 | 5 | Radare2 VMI IO and debugger plugins. 6 | 7 | These plugins allow you to debug remote process running in a VM, from the hypervisor-level, 8 | leveraging _Virtual Machine Introspection_. 9 | 10 | Based on `Libvmi` to access the VM memory and listen on hardware events. 11 | 12 | _Note: since hack.lu 2018, I shifted my work towards an **improved** version of this 13 | project which is more **flexible** and open to any reverse-engineering framework that can act as a **GDB frontend**:_ 14 | 15 | https://github.com/Wenzel/pyvmidbg 16 | 17 | What works: 18 | - Intercept a process by name/PID (_at `CR3` load_) 19 | - Read the registers 20 | - Single-step the process execution 21 | - Set breakpoints 22 | - software 23 | - hardware (_based on memory access permissions, page must be mapped_) 24 | - Load Kernel symbols 25 | 26 | # Demo 27 | 28 | [High quality link](https://drive.google.com/file/d/1R63TdJiP3TjN4t-4FFiPjSFajqfXgZ0C/view?usp=sharing) 29 | 30 | The following demonstrate how `r2vmi`: 31 | - intercepts `explorer.exe` process 32 | - sets a `software` breakpoint on `NtOpenKey` 33 | - how the breakpoint is hit (_ignoring hits by not targeted processes_) 34 | - using `radare2` to disassemble `NtOpenFile`'s function 35 | - singlestep the execution 36 | - opening a `Rekall` shell usin the [`VMIAddressSpace`](https://github.com/google/rekall/blob/master/rekall-core/rekall/plugins/addrspaces/vmi.py) to work on the VM's physical memory 37 | - running `pslist` plugin 38 | - running `dlllist` plugin and selecting a random `DLL`'s base address 39 | - seeking there in `radare2` and displaying the `MZ` header 40 | 41 | ![R2VMI_DEMO](https://github.com/Wenzel/wenzel.github.io/raw/master/public/images/r2vmi_demo.gif) 42 | 43 | # Requirements 44 | 45 | - `Xen 4.6` 46 | - `pkg-config` 47 | - [`libvmi`](http://libvmi.com/) 48 | - [`radare2`](https://github.com/radare/radare2) 49 | 50 | # Setup 51 | 52 | An complete installation guide is available on the [Wiki](https://github.com/Wenzel/r2vmi/wiki/Project-Setup) 53 | 54 | # Usage 55 | 56 | You need a virtual machine configured on top of `Xen`, and a process name/pid to intercept 57 | 58 | $ r2 -d vmi://: 59 | 60 | Example: 61 | 62 | $ r2 -d vmi://win7:firefox 63 | 64 | -------------------------------------------------------------------------------- /examples/README.md: -------------------------------------------------------------------------------- 1 | # r2vmi python examples 2 | 3 | This directory contains examples of use cases for `r2vmi`, on top of `r2pipe` Python bindings. 4 | 5 | # Setup 6 | 7 | $ virtualenv -p python3 venv 8 | $ source venv/bin/activate 9 | (venv) $ pip install -r requirements.txt 10 | 11 | ## Rekall 12 | 13 | The examples are using the Rekall's `VMIAddressSpace` which is not available in the latest release. 14 | 15 | You will have to install `Rekall` from the git repository `master` branch: 16 | 17 | (venv) pip install --upgrade setuptools pip wheel 18 | (venv) git clone https://github.com/google/rekall.git 19 | (venv) pip install --editable rekall/rekall-lib 20 | (venv) pip install --editable rekall/rekall-core 21 | (venv) pip install --editable rekall/rekall-agent 22 | (venv) pip install --editable rekall 23 | 24 | # Examples 25 | 26 | ## watch_syscall.py 27 | 28 | sudo ./venv/bin/python watch_syscall.py xenwin7 explorer NtOpenKey 29 | 30 | Output: 31 | 32 | INFO:rekall.1:Autodetected physical address space VMIAddressSpace 33 | INFO:rekall.1:Cache directory is not specified or invalid. Switching to memory cache. 34 | INFO:rekall.1:Loaded profile pe from Local Cache - (in 0.24178719520568848 sec) 35 | INFO:rekall.1:Loaded profile nt/undocumented from Local Cache - (in 0.2355349063873291 sec) 36 | INFO:rekall.1:Loaded profile nt/GUID/F8E2A8B5C9B74BF4A6E4A48F180099942 from Local Cache - (in 0.6623311042785645 sec) 37 | INFO:rekall.1:Loaded profile nt/eprocess_index from Local Cache - (in 0.27491211891174316 sec) 38 | INFO:rekall.1:Detected ntkrnlmp.pdb with GUID F8E2A8B5C9B74BF4A6E4A48F180099942 39 | INFO:rekall.1:Detected kernel base at 0xF80002617000 40 | INFO:rekall.1:Loaded profile win32k/index from Local Cache - (in 0.2426774501800537 sec) 41 | INFO:rekall.1:Loaded profile win32k/GUID/A9F6403F14074E9D8A07D0AA6F0C1CFF2 from Local Cache - (in 0.3131988048553467 sec) 42 | INFO:root:Loading symbols 43 | INFO:root:Adding breakpoint on NtOpenKey 44 | INFO:root:explorer - @NtOpenKey: \Registry\Machine\Software\Policies\Microsoft\Windows\Safer\CodeIdentifiers 45 | INFO:root:explorer - @NtOpenKey: AppCompatFlags\Layers 46 | INFO:root:explorer - @NtOpenKey: Custom\explorer.exe 47 | INFO:root:explorer - @NtOpenKey: \Registry\MACHINE\Software\Microsoft\Windows\CurrentVersion\SideBySide 48 | INFO:root:explorer - @NtOpenKey: \Registry\User\S-1-5-21-1625813105-2267665344-3627068389-1000_Classes 49 | -------------------------------------------------------------------------------- /examples/watch_syscall.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | # Watch Windows syscalls and print their ObjectName parameter 4 | 5 | """ 6 | watch_syscall. 7 | 8 | Usage: 9 | watch_syscall.py [options] 10 | 11 | Options: 12 | -h --help Show this screen. 13 | --version Show version. 14 | """ 15 | 16 | 17 | # stdlib 18 | import os 19 | import sys 20 | import logging 21 | import struct 22 | import signal 23 | from pprint import pprint 24 | from pathlib import Path 25 | 26 | # local 27 | from utils import RekallVMI 28 | 29 | # 3rd 30 | import r2pipe 31 | from docopt import docopt 32 | from IPython import embed 33 | 34 | interrupted = False 35 | 36 | def sigint_handler(sig, frame): 37 | print('Ctrl+C received, will quit at next breakpoint hit..') 38 | global interrupted 39 | interrupted = True 40 | 41 | 42 | def read_field(r2, win_field, from_addr): 43 | format = win_field['size'] 44 | offset = win_field['offset'] 45 | size = struct.calcsize(format) 46 | output = r2.cmdj('pxj {} @{}+{}'.format(size, from_addr, offset)) 47 | addr, *rest = struct.unpack(format, bytes(output)) 48 | return addr 49 | 50 | 51 | def main(args): 52 | level = logging.INFO 53 | logging.basicConfig(level=level) 54 | # catch SIGINT 55 | signal.signal(signal.SIGINT, sigint_handler) 56 | 57 | vm_name = args[''] 58 | target = args[''] 59 | syscall_name = args[''] 60 | 61 | # build rekall VMI session 62 | rekall = RekallVMI(vm_name, 'xen') 63 | # get syscall 64 | *rest, syscall_addr = rekall.find_syscall(syscall_name) 65 | # rekall removed the high part of the address (ignored in IA32e paging) 66 | # this will make virtual address comparison fail in radare2 67 | # small fix to restore that part 68 | syscall_addr |= 0xffff << 48 69 | # get some _OBJECT_ATTRIBUTES fields/subfields offsets & size 70 | win_types = rekall.get_winobj_fields() 71 | 72 | # init r2vmi 73 | r2_url = "vmi://{}:{}".format(vm_name,target) 74 | r2 = r2pipe.open(r2_url, ["-d", "-2"]) 75 | 76 | # make sure we use software breakpoints (faster) 77 | r2.cmd('e dbg.hwbp=false') 78 | 79 | logging.info('Setting breakpoint on %s @%s', syscall_name, hex(syscall_addr)) 80 | r2.cmd('db {}'.format(hex(syscall_addr))) 81 | logging.info('Waiting for breakpoint...') 82 | 83 | global interrupted 84 | while not interrupted: 85 | # continue 86 | r2.cmd('dc') 87 | # bp hit ! 88 | # get registers 89 | registers = r2.cmdj('drj') 90 | object_attributes_addr = registers['r8'] 91 | object_name_addr = read_field(r2, win_types['object_name'], object_attributes_addr) 92 | buffer_addr = read_field(r2, win_types['buffer'], object_name_addr) 93 | # read UNICODE_STRING buffer 94 | output = r2.cmd('psw @{}'.format(hex(buffer_addr))) 95 | logging.info('%s - @%s: %s', target, syscall_name, output) 96 | 97 | 98 | if __name__ == '__main__': 99 | args = docopt(__doc__, version='0.1') 100 | ret = main(args) 101 | sys.exit(ret) 102 | -------------------------------------------------------------------------------- /profile.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "profile.h" 4 | 5 | static addr_t get_kernel_base(vmi_instance_t vmi, json_object *root) 6 | { 7 | addr_t kernel_base; 8 | status_t status; 9 | 10 | // get $CONSTANTS 11 | json_object* constants = NULL; 12 | if (!json_object_object_get_ex(root, "$CONSTANTS", &constants)) 13 | goto outerr; 14 | 15 | // get PsActiveProcessHead 16 | json_object* process_head = NULL; 17 | if (!json_object_object_get_ex(constants, "PsActiveProcessHead", &process_head)) 18 | goto outerr; 19 | 20 | // get PsActiveProcessHead rva; 21 | addr_t process_head_rva = json_object_get_int64(process_head); 22 | 23 | // translate PsActiveProcessHead with vmi_translate 24 | addr_t process_head_addr; 25 | status = vmi_translate_ksym2v(vmi, "PsActiveProcessHead", &process_head_addr); 26 | if (status == VMI_FAILURE) 27 | goto outerr; 28 | 29 | // get kernel base address 30 | kernel_base = process_head_addr - process_head_rva; 31 | 32 | return kernel_base; 33 | 34 | outerr: 35 | eprintf("Fail to get kernel base\n"); 36 | return 0; 37 | } 38 | 39 | static bool load_symbols_section(RIO *io, json_object *root, 40 | const char *section_name, addr_t kernel_base) 41 | { 42 | json_object* section = NULL; 43 | if (!json_object_object_get_ex(root, section_name, §ion)) 44 | goto outerr; 45 | 46 | addr_t symbol_addr; 47 | char cmd[2048]; 48 | json_object_object_foreach(section, key, val) { 49 | // skip symbols starting with str: 50 | if (!strncmp(key, "str:", strlen("str:"))) 51 | continue; 52 | 53 | // skip symbols containing an @ 54 | if (strchr(key, '@')) 55 | continue; 56 | 57 | symbol_addr = kernel_base + json_object_get_int64(val); 58 | // printf("symbol: %s, addr: 0x%lx\n", key, symbol_addr); 59 | bzero(cmd, 2048); 60 | snprintf(cmd, 2048, "f rekall.%s = 0x%lx\n", key, symbol_addr); 61 | io->cb_printf(cmd); 62 | } 63 | 64 | return true; 65 | outerr: 66 | eprintf("Cannot load section %s\n", section_name); 67 | return false; 68 | } 69 | 70 | bool load_symbols(RIO *io, vmi_instance_t vmi) 71 | { 72 | const char* profile_path = NULL; 73 | json_object* root = NULL; 74 | addr_t kernel_base; 75 | 76 | // get rekall profile 77 | profile_path = vmi_get_rekall_path(vmi); 78 | if (!profile_path) 79 | { 80 | eprintf("Libvmi config has no Rekall profile path\n"); 81 | return false; 82 | } 83 | 84 | // load as json 85 | root = json_object_from_file(profile_path); 86 | if (!root) 87 | goto outerr; 88 | 89 | // get kernel base 90 | kernel_base = get_kernel_base(vmi, root); 91 | if (!kernel_base) 92 | goto outerr; 93 | 94 | if (!load_symbols_section(io, root, "$CONSTANTS", kernel_base)) 95 | goto outerr; 96 | 97 | if (!load_symbols_section(io, root, "$FUNCTIONS", kernel_base)) 98 | goto outerr; 99 | 100 | if (root) 101 | json_object_put(root); 102 | return true; 103 | 104 | outerr: 105 | eprintf("Error while parsing JSON Rekall profile\n"); 106 | if (root) 107 | json_object_put(root); 108 | return false; 109 | } 110 | -------------------------------------------------------------------------------- /io_vmi.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #include "io_vmi.h" 9 | #include "profile.h" 10 | 11 | #define URI_PREFIX "vmi://" 12 | 13 | extern RIOPlugin r_io_plugin_vmi; // forward declaration 14 | 15 | static void rio_vmi_destroy(RIOVmi *ptr) 16 | { 17 | eprintf("%s\n", __func__); 18 | if (ptr) 19 | { 20 | if (ptr->vm_name) 21 | { 22 | free(ptr->vm_name); 23 | ptr->vm_name = NULL; 24 | } 25 | if (ptr->vmi) 26 | { 27 | status_t status; 28 | // TODO dev 29 | // this resume VM should be done somewhere else 30 | status = vmi_resume_vm(ptr->vmi); 31 | if (status == VMI_FAILURE) 32 | { 33 | eprintf("Fail to resume VM\n"); 34 | } 35 | vmi_destroy(ptr->vmi); 36 | ptr->vmi = NULL; 37 | } 38 | if (ptr->bp_events_table) 39 | { 40 | g_hash_table_destroy(ptr->bp_events_table); 41 | } 42 | free(ptr); 43 | } 44 | } 45 | 46 | static bool __plugin_open(__attribute__((unused)) RIO *io, const char *pathname, __attribute__((unused)) bool many) { 47 | return (strncmp(pathname, URI_PREFIX, strlen(URI_PREFIX)) == 0); 48 | } 49 | 50 | static RIODesc *__open(RIO *io, const char *pathname, int flags, int mode) { 51 | eprintf("%s\n", __func__); 52 | RIODesc *ret = NULL; 53 | RIOVmi *rio_vmi = NULL; 54 | const char *delimiter = ":"; 55 | long pid = 0; 56 | char *uri_content = NULL; 57 | char *saveptr = NULL; 58 | 59 | if (!__plugin_open(io, pathname, 0)) 60 | return ret; 61 | 62 | // allocate RIOVmi 63 | rio_vmi = R_NEW0(RIOVmi); 64 | if (!rio_vmi) 65 | goto out; 66 | 67 | rio_vmi->current_vcpu = -1; 68 | rio_vmi->attached = false; 69 | // init breakpoint ghastable 70 | rio_vmi->bp_events_table = g_hash_table_new(g_direct_hash, g_direct_equal); 71 | // URI has the following format: vmi://vm_name:pid 72 | // parse URI 73 | uri_content = strdup(pathname + strlen(URI_PREFIX)); 74 | // get vm_name 75 | char *token = strtok_r(uri_content, delimiter, &saveptr); 76 | if (!token) 77 | { 78 | eprintf("strtok_r: Parsing vm_name failed\n"); 79 | goto out; 80 | } 81 | rio_vmi->vm_name = strdup(token); 82 | // get next token 83 | token = strtok_r(NULL, delimiter, &saveptr); 84 | if (!token) 85 | { 86 | eprintf("strtok_r: Parsing process identifier failed\n"); 87 | goto out; 88 | } 89 | rio_vmi->url_identify_by_name = false; 90 | // try to parse it as pid 91 | saveptr = NULL; 92 | pid = strtol(token, &saveptr, 10); 93 | if (!pid) 94 | { 95 | // token must be interpreted as a process name 96 | rio_vmi->url_identify_by_name = true; 97 | rio_vmi->proc_name = strdup(token); 98 | } 99 | else 100 | rio_vmi->pid = pid; 101 | if (rio_vmi->url_identify_by_name) 102 | eprintf("VM: %s, process name: %s\n", rio_vmi->vm_name, rio_vmi->proc_name); 103 | else 104 | eprintf("VM: %s, PID: %d\n", rio_vmi->vm_name, rio_vmi->pid); 105 | 106 | // init libvmi 107 | printf("Initializing LibVMI\n"); 108 | vmi_init_error_t error; 109 | status_t status = vmi_init_complete(&rio_vmi->vmi, rio_vmi->vm_name, VMI_INIT_DOMAINNAME | VMI_INIT_EVENTS, NULL, 110 | VMI_CONFIG_GLOBAL_FILE_ENTRY, NULL, &error); 111 | if (status == VMI_FAILURE) 112 | { 113 | eprintf("vmi_init_complete: Failed to initialize LibVMI, error: %d\n", error); 114 | goto out; 115 | } 116 | 117 | return r_io_desc_new (io, &r_io_plugin_vmi, pathname, flags | R_PERM_W, mode, rio_vmi); 118 | 119 | out: 120 | if (uri_content) 121 | free(uri_content); 122 | rio_vmi_destroy(rio_vmi); 123 | return ret; 124 | } 125 | 126 | static int __close(RIODesc *fd) { 127 | RIOVmi *rio_vmi = NULL; 128 | 129 | eprintf("%s\n", __func__); 130 | if (!fd || !fd->data) 131 | return -1; 132 | 133 | rio_vmi = fd->data; 134 | rio_vmi_destroy(rio_vmi); 135 | return true; 136 | } 137 | 138 | static ut64 __lseek(RIO *io, RIODesc *fd, ut64 offset, int whence) { 139 | // printf("%s, offset: %llx, io->off: %llx\n", __func__, offset, io->off); 140 | 141 | if (!fd || !fd->data) 142 | return -1; 143 | 144 | switch (whence) { 145 | case SEEK_SET: 146 | io->off = offset; 147 | break; 148 | case SEEK_CUR: 149 | io->off += (int)offset; 150 | break; 151 | case SEEK_END: 152 | io->off = UT64_MAX; 153 | break; 154 | } 155 | return io->off; 156 | } 157 | 158 | static int __read(RIO *io, RIODesc *fd, ut8 *buf, int len) { 159 | RIOVmi *rio_vmi = NULL; 160 | status_t status; 161 | size_t bytes_read = 0; 162 | access_context_t ctx; 163 | 164 | eprintf("%s, offset: %llx\n", __func__, io->off); 165 | 166 | if (!fd || !fd->data) 167 | return -1; 168 | 169 | rio_vmi = fd->data; 170 | // fill access context 171 | ctx.translate_mechanism = VMI_TM_PROCESS_PID; 172 | ctx.addr = io->off; 173 | ctx.pid = rio_vmi->pid; 174 | // read 175 | status = vmi_read(rio_vmi->vmi, &ctx, len, (void*)buf, &bytes_read); 176 | if (status == VMI_FAILURE) 177 | { 178 | eprintf("read %p: vmi_failure\n", (void*)io->off); 179 | return 0; 180 | } 181 | 182 | return bytes_read; 183 | } 184 | 185 | static int __write(RIO *io, RIODesc *fd, const ut8 *buf, int len) { 186 | RIOVmi *rio_vmi = NULL; 187 | status_t status; 188 | size_t bytes_written = 0; 189 | access_context_t ctx; 190 | 191 | eprintf("%s\n", __func__); 192 | 193 | // for (int i = 0; i < len; i++) 194 | // eprintf("%s buffer[%d]: %x\n", __func__, i, buf[i]); 195 | 196 | if (!fd || !fd->data) 197 | return -1; 198 | 199 | rio_vmi = fd->data; 200 | // fill access context 201 | ctx.translate_mechanism = VMI_TM_PROCESS_PID; 202 | ctx.addr = io->off; 203 | ctx.pid = rio_vmi->pid; 204 | // read 205 | status = vmi_write(rio_vmi->vmi, &ctx, len, (void*)buf, &bytes_written); 206 | if (status == VMI_FAILURE) 207 | { 208 | eprintf("write %p: vmi_failure\n", (void*)io->off); 209 | return 0; 210 | } 211 | return bytes_written; 212 | } 213 | 214 | static int __getpid(RIODesc *fd) { 215 | eprintf("%s\n", __func__); 216 | 217 | RIOVmi *rio_vmi = NULL; 218 | 219 | eprintf("%s\n", __func__); 220 | if (!fd || !fd->data) 221 | return -1; 222 | 223 | rio_vmi = fd->data; 224 | return rio_vmi->pid; 225 | } 226 | 227 | static int __gettid(__attribute__((unused)) RIODesc *fd) { 228 | eprintf("%s\n", __func__); 229 | return 0; 230 | } 231 | 232 | static char *__system(RIO *io, RIODesc *fd, const char *command) { 233 | RIOVmi *rio_vmi = NULL; 234 | eprintf("%s: command: %s\n", __func__, command); 235 | // io->cb_printf() 236 | 237 | if (!fd || !fd->data) 238 | return NULL; 239 | 240 | rio_vmi = fd->data; 241 | 242 | if (!strncmp(command, "symbols", strlen("symbols"))) 243 | { 244 | if (!load_symbols(io, rio_vmi->vmi)) 245 | eprintf("Cannot load symbols from Rekall profile\n"); 246 | } 247 | return NULL; 248 | } 249 | 250 | RIOPlugin r_io_plugin_vmi = { 251 | .name = "vmi", 252 | .desc = "VMI IO plugin for r2 vmi://[vm_name]:[pid]", 253 | .license = "LGPL", 254 | .check = __plugin_open, 255 | .open = __open, 256 | .close = __close, 257 | .lseek = __lseek, 258 | .read = __read, 259 | .write = __write, 260 | .getpid = __getpid, 261 | .gettid = __gettid, 262 | .system = __system, 263 | .isdbg = true, 264 | }; 265 | 266 | #ifndef CORELIB 267 | RLibStruct radare_plugin = { 268 | .type = R_LIB_TYPE_IO, 269 | .data = &r_io_plugin_vmi, 270 | .version = R2_VERSION 271 | }; 272 | #endif 273 | -------------------------------------------------------------------------------- /utils.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "utils.h" 4 | #include "io_vmi.h" 5 | 6 | #define LINEAR48(x) ((x) &= 0xffffffffffff) 7 | 8 | // hardcoded winxp, waiting for new libvmi rekall API 9 | // _EPROCESS.ThreadListHead 10 | #define EPROC_THREAD_HEAD_OFF 0x190 11 | // _ETHREAD.Win32StartAddress 12 | #define ETH_W32_START_OFF 0x228 13 | // _Ethread.ThreadListEntry 14 | #define ETH_THREAD_HEAD_OFF 0x22c 15 | 16 | /* 17 | // hardcoded win7, waiting for new libvmi rekall API 18 | // _EPROCESS.ThreadListHead 19 | #define EPROC_THREAD_HEAD_OFF 0x308 20 | // _ETHREAD.Win32StartAddress 21 | #define ETH_W32_START_OFF 0x418 22 | // _Ethread.ThreadListEntry 23 | #define ETH_THREAD_HEAD_OFF 0x428 24 | */ 25 | 26 | static bool interrupted = false; 27 | 28 | typedef struct 29 | { 30 | // store the mem_event, because we need to register it 31 | // when inside the single step callback 32 | vmi_event_t *mem_event; 33 | addr_t target_vaddr; 34 | addr_t target_gfn; 35 | RIOVmi *rio_vmi; 36 | } breakpoint_cb_data; 37 | 38 | // stolen from libvmi/examples/step-event-example 39 | void print_event(vmi_event_t *event) 40 | { 41 | eprintf("\tPAGE ACCESS: %c%c%c for GFN %"PRIx64" (offset %06"PRIx64") gla %016"PRIx64" (vcpu %u)\n", 42 | (event->mem_event.out_access & VMI_MEMACCESS_R) ? 'r' : '-', 43 | (event->mem_event.out_access & VMI_MEMACCESS_W) ? 'w' : '-', 44 | (event->mem_event.out_access & VMI_MEMACCESS_X) ? 'x' : '-', 45 | event->mem_event.gfn, 46 | event->mem_event.offset, 47 | event->mem_event.gla, 48 | event->vcpu_id 49 | ); 50 | } 51 | 52 | // compare 2 virtual addresses 53 | bool vaddr_equal(vmi_instance_t vmi, addr_t vaddr1, addr_t vaddr2) 54 | { 55 | page_mode_t page_mode = vmi_get_page_mode(vmi, 0); 56 | 57 | switch (page_mode) { 58 | case VMI_PM_IA32E: 59 | // only 48 bits are used by the MMU as linear address 60 | if (LINEAR48(vaddr1) == LINEAR48(vaddr2)) 61 | return true; 62 | break; 63 | case VMI_PM_PAE: 64 | if (vaddr1 == vaddr2) 65 | return true; 66 | break; 67 | default: 68 | eprintf("Unhandled page mode\n"); 69 | break; 70 | } 71 | return false; 72 | } 73 | 74 | char* dtb_to_pname(vmi_instance_t vmi, addr_t dtb) { 75 | addr_t ps_head = 0; 76 | addr_t flink = 0; 77 | addr_t start_proc = 0; 78 | addr_t pdb_offset = 0; 79 | addr_t tasks_offset = 0; 80 | addr_t name_offset = 0; 81 | addr_t value = 0; 82 | status_t status; 83 | 84 | 85 | status = vmi_get_offset(vmi, "win_tasks", &tasks_offset); 86 | if (VMI_FAILURE == status) 87 | { 88 | eprintf("failed\n"); 89 | return NULL; 90 | } 91 | 92 | status = vmi_get_offset(vmi, "win_pdbase", &pdb_offset); 93 | if (VMI_FAILURE == status) 94 | { 95 | eprintf("failed\n"); 96 | return NULL; 97 | } 98 | 99 | status = vmi_get_offset(vmi, "win_pname", &name_offset); 100 | if (VMI_FAILURE == status) 101 | { 102 | eprintf("failed\n"); 103 | return NULL; 104 | } 105 | status = vmi_translate_ksym2v(vmi, "PsActiveProcessHead", &ps_head); 106 | if (VMI_FAILURE == status) 107 | { 108 | eprintf("failed\n"); 109 | return NULL; 110 | } 111 | status = vmi_read_addr_ksym(vmi, "PsActiveProcessHead", &flink); 112 | if (VMI_FAILURE == status) 113 | { 114 | eprintf("failed\n"); 115 | return NULL; 116 | } 117 | 118 | while (flink != ps_head) 119 | { 120 | // get eprocess head 121 | start_proc = flink - tasks_offset; 122 | 123 | // get dtb value 124 | vmi_read_addr_va(vmi, start_proc + pdb_offset, 0, &value); 125 | if (value == dtb) 126 | { 127 | // read process name 128 | return vmi_read_str_va(vmi, start_proc + name_offset, 0); 129 | } 130 | // read new flink 131 | vmi_read_addr_va(vmi, flink, 0, &flink); 132 | } 133 | // idle process ? 134 | status = vmi_read_addr_ksym(vmi, "PsIdleProcess", &start_proc); 135 | if (VMI_FAILURE == status) 136 | { 137 | return NULL; 138 | } 139 | status = vmi_read_addr_va(vmi, start_proc + pdb_offset, 0, &value); 140 | if (VMI_FAILURE == status) 141 | { 142 | eprintf("fail to read CR3\n"); 143 | return NULL; 144 | } 145 | if (value == dtb) 146 | { 147 | return vmi_read_str_va(vmi, start_proc + name_offset, 0); 148 | } 149 | 150 | return NULL; 151 | } 152 | 153 | status_t vmi_dtb_to_pid_extended_idle(vmi_instance_t vmi, addr_t dtb, vmi_pid_t *pid) 154 | { 155 | status_t status; 156 | addr_t start_proc; 157 | addr_t pid_offset; 158 | 159 | 160 | status = vmi_dtb_to_pid(vmi, dtb, pid); 161 | if (VMI_FAILURE == status) 162 | { 163 | // Idle process ? 164 | status = vmi_read_addr_ksym(vmi, "PsIdleProcess", &start_proc); 165 | if (VMI_FAILURE == status) 166 | { 167 | return VMI_FAILURE; 168 | } 169 | status = vmi_get_offset(vmi, "win_pid", &pid_offset); 170 | if (VMI_FAILURE == status) 171 | { 172 | eprintf("fail to get offset\n"); 173 | return VMI_FAILURE; 174 | } 175 | status = vmi_read_32_va(vmi, start_proc + pid_offset, 0, (uint32_t*)pid); 176 | if (VMI_FAILURE == status) 177 | { 178 | eprintf("fail to read pid"); 179 | return VMI_FAILURE; 180 | } 181 | } 182 | return VMI_SUCCESS; 183 | } 184 | 185 | static event_response_t cb_on_cr3_load(vmi_instance_t vmi, vmi_event_t *event){ 186 | RIOVmi *rio_vmi = NULL; 187 | status_t status; 188 | pid_t pid = 0; 189 | char* proc_name = NULL; 190 | 191 | eprintf("%s\n", __func__); 192 | 193 | if(!event || event->type != VMI_EVENT_REGISTER || !event->data) { 194 | eprintf("ERROR (%s): invalid event encounted\n", __func__); 195 | return 0; 196 | } 197 | 198 | // get event data 199 | rio_vmi = (RIOVmi*) event->data; 200 | 201 | // process name 202 | proc_name = dtb_to_pname(vmi, event->reg_event.value); 203 | if (!proc_name) 204 | { 205 | eprintf("CR3: 0x%lx can't find process\n", event->reg_event.value); 206 | // stop monitoring 207 | interrupted = true; 208 | // pause the VM before we get out of main loop 209 | status = vmi_pause_vm(vmi); 210 | if (status == VMI_FAILURE) 211 | { 212 | eprintf("%s: Fail to pause VM\n", __func__); 213 | return 0; 214 | } 215 | // if we can't find the process in the list 216 | // it means we have intercepted a new CR3 217 | rio_vmi->attach_new_process = true; 218 | // set current VCPU 219 | rio_vmi->current_vcpu = event->vcpu_id; 220 | // save new CR3 value 221 | rio_vmi->pid_cr3 = event->reg_event.value; 222 | return 0; 223 | } 224 | 225 | status = vmi_dtb_to_pid_extended_idle(vmi, (addr_t) event->reg_event.value, &pid); 226 | if (status == VMI_FAILURE) 227 | { 228 | eprintf("ERROR (%s): fail to retrieve pid from cr3\n", __func__); 229 | return 0; 230 | } 231 | 232 | eprintf("Intercepted PID: %d, CR3: 0x%lx, Name: %s, RIP: 0x%lx\n", 233 | pid, event->reg_event.value, proc_name, event->x86_regs->rip); 234 | 235 | if (is_target_process(rio_vmi, proc_name, event->reg_event.value)) 236 | { 237 | // delete old and maybe partial name for the full proc name 238 | free(rio_vmi->proc_name); 239 | rio_vmi->proc_name = strdup(proc_name); 240 | eprintf("Found %s (%d)!\n", rio_vmi->proc_name, rio_vmi->pid); 241 | // stop monitoring 242 | interrupted = true; 243 | // pause the VM before we get out of main loop 244 | status = vmi_pause_vm(vmi); 245 | if (status == VMI_FAILURE) 246 | { 247 | eprintf("%s: Fail to pause VM\n", __func__); 248 | return 0; 249 | } 250 | // set current VCPU 251 | rio_vmi->current_vcpu = event->vcpu_id; 252 | // save new CR3 value 253 | rio_vmi->pid_cr3 = event->reg_event.value; 254 | } 255 | free(proc_name); 256 | 257 | return 0; 258 | } 259 | 260 | static event_response_t cb_on_sstep(vmi_instance_t vmi, vmi_event_t *event) 261 | { 262 | status_t status; 263 | breakpoint_cb_data *cb_data = NULL; 264 | char *proc_name = NULL; 265 | 266 | eprintf("%s\n", __func__); 267 | 268 | if(!event || event->type != VMI_EVENT_SINGLESTEP || !event->data) { 269 | eprintf("ERROR (%s): invalid event encounted\n", __func__); 270 | return 0; 271 | } 272 | 273 | // get event data 274 | cb_data = (breakpoint_cb_data*)(event->data); 275 | 276 | // same page ? 277 | if (event->ss_event.gfn != cb_data->target_gfn) 278 | { 279 | // out of the targeted page 280 | // reregister mem_event 281 | status = vmi_register_event(vmi, cb_data->mem_event); 282 | if (status == VMI_FAILURE) 283 | { 284 | eprintf("Fail to register event\n"); 285 | return VMI_EVENT_RESPONSE_NONE; 286 | } 287 | // toggle singlestep OFF 288 | eprintf("out of the targeted page\n"); 289 | return VMI_EVENT_RESPONSE_TOGGLE_SINGLESTEP; 290 | } 291 | 292 | // hit target ? 293 | if (!vaddr_equal(vmi, event->x86_regs->rip, cb_data->target_vaddr)) 294 | { 295 | return VMI_EVENT_RESPONSE_NONE; 296 | } 297 | 298 | // hit 299 | proc_name = dtb_to_pname(vmi, event->x86_regs->cr3); 300 | if (!proc_name) 301 | proc_name = "NEW_PROCESS.EXE"; 302 | 303 | eprintf("At KiStartUserThread: %s, CR3: 0x%lx\n", proc_name, event->x86_regs->cr3); 304 | if (is_target_process(cb_data->rio_vmi, proc_name, event->x86_regs->cr3)) 305 | { 306 | status = vmi_pause_vm(vmi); 307 | if (status == VMI_FAILURE) 308 | { 309 | eprintf("Fail to resume VM\n"); 310 | return false; 311 | } 312 | 313 | interrupted = true; 314 | // toggle singlestep OFF 315 | return VMI_EVENT_RESPONSE_TOGGLE_SINGLESTEP; 316 | } 317 | return VMI_EVENT_RESPONSE_NONE; 318 | } 319 | 320 | static event_response_t cb_on_continue_until_event(vmi_instance_t vmi, vmi_event_t *event) 321 | { 322 | status_t status; 323 | breakpoint_cb_data *cb_data; 324 | const char* proc_name = NULL; 325 | 326 | eprintf("%s\n", __func__); 327 | 328 | if(!event || event->type != VMI_EVENT_MEMORY || !event->data) { 329 | eprintf("ERROR (%s): invalid event encounted\n", __func__); 330 | return 0; 331 | } 332 | 333 | // get event data 334 | cb_data = (breakpoint_cb_data*)(event->data); 335 | 336 | // our address ? 337 | if (!vaddr_equal(vmi, event->x86_regs->rip, cb_data->target_vaddr)) 338 | { 339 | eprintf("Wrong RIP: 0x%lx\n", event->x86_regs->rip); 340 | // unregister mem_event to lift page permissions 341 | status = vmi_clear_event(vmi, event, NULL); 342 | if (status == VMI_FAILURE) 343 | { 344 | eprintf("Fail to clear event\n"); 345 | return VMI_EVENT_RESPONSE_NONE; 346 | } 347 | // toggle singlestep ON 348 | return VMI_EVENT_RESPONSE_TOGGLE_SINGLESTEP; 349 | } 350 | 351 | // it's a hit ! 352 | proc_name = dtb_to_pname(vmi, event->x86_regs->cr3); 353 | if (!proc_name) 354 | proc_name = "NEW_PROCESS.EXE"; 355 | 356 | eprintf("At KiStartUserThread: %s, CR3: 0x%lx\n", proc_name, event->x86_regs->cr3); 357 | if (is_target_process(cb_data->rio_vmi, proc_name, event->x86_regs->cr3)) 358 | { 359 | status = vmi_pause_vm(vmi); 360 | if (status == VMI_FAILURE) 361 | { 362 | eprintf("Fail to resume VM\n"); 363 | return false; 364 | } 365 | 366 | interrupted = true; 367 | } 368 | return VMI_EVENT_RESPONSE_EMULATE; 369 | } 370 | 371 | static bool continue_until(RIOVmi *rio_vmi, addr_t addr, bool kernel_translate) 372 | { 373 | eprintf("%s\n", __func__); 374 | status_t status; 375 | addr_t paddr; 376 | addr_t gfn; 377 | vmi_event_t mem_event = {0}; 378 | breakpoint_cb_data cb_data = {0}; 379 | 380 | // get nb vcpu 381 | int nb_vcpu = vmi_get_num_vcpus(rio_vmi->vmi); 382 | 383 | // build single step events 384 | vmi_event_t ss_events[nb_vcpu]; 385 | for (int i = 0; i < nb_vcpu; i++) 386 | { 387 | bzero(&ss_events[i], sizeof(vmi_event_t)); 388 | // prepare the event, but don't enable single step yet 389 | SETUP_SINGLESTEP_EVENT(&ss_events[i], 1u << i, cb_on_sstep, false); 390 | // assign event data 391 | ss_events[i].data = (void*)&cb_data; 392 | // register 393 | status = vmi_register_event(rio_vmi->vmi, &ss_events[i]); 394 | if (status == VMI_FAILURE) 395 | { 396 | eprintf("Fail to register event\n"); 397 | return false; 398 | } 399 | } 400 | 401 | // build memory event 402 | // get paddr 403 | if (kernel_translate) 404 | status = vmi_translate_kv2p(rio_vmi->vmi, addr, &paddr); 405 | else 406 | status = vmi_translate_uv2p(rio_vmi->vmi, rio_vmi->pid, addr, &paddr); 407 | if (status == VMI_FAILURE) 408 | { 409 | eprintf("Fail to get paddr for 0x%lx\n", addr); 410 | return false; 411 | } 412 | gfn = paddr >> 12; 413 | SETUP_MEM_EVENT(&mem_event, gfn, VMI_MEMACCESS_X, cb_on_continue_until_event, false); 414 | // assign data 415 | mem_event.data = (void*)&cb_data; 416 | // register 417 | status = vmi_register_event(rio_vmi->vmi, &mem_event); 418 | if (status == VMI_FAILURE) 419 | { 420 | eprintf("Fail to register event\n"); 421 | return false; 422 | } 423 | 424 | // fill callback_data 425 | cb_data.mem_event = &mem_event; 426 | cb_data.target_vaddr = addr; 427 | cb_data.target_gfn = gfn; 428 | cb_data.rio_vmi = rio_vmi; 429 | 430 | // resume vm execution 431 | status = vmi_resume_vm(rio_vmi->vmi); 432 | if (status == VMI_FAILURE) 433 | { 434 | eprintf("Fail to resume VM\n"); 435 | return false; 436 | } 437 | 438 | // listen 439 | interrupted = false; 440 | while (!interrupted) 441 | { 442 | int nb_events = vmi_are_events_pending(rio_vmi->vmi); 443 | eprintf("Listening on VMI events...%d events pending\n", nb_events); 444 | status = vmi_events_listen(rio_vmi->vmi, 1000); 445 | if (status == VMI_FAILURE) 446 | { 447 | interrupted = true; 448 | return false; 449 | } 450 | } 451 | 452 | // clear event buffer if any 453 | status = vmi_events_listen(rio_vmi->vmi, 0); 454 | if (status == VMI_FAILURE) 455 | { 456 | eprintf("fail to clear event buffer\n"); 457 | return false; 458 | } 459 | 460 | // clear mem_event 461 | status = vmi_clear_event(rio_vmi->vmi, &mem_event, NULL); 462 | if (status == VMI_FAILURE) 463 | { 464 | eprintf("Fail to clear event\n"); 465 | return false; 466 | } 467 | 468 | // clear single step 469 | for (int i = 0; i < nb_vcpu; i++) 470 | { 471 | status = vmi_clear_event(rio_vmi->vmi, &ss_events[i], NULL); 472 | if (status == VMI_FAILURE) 473 | { 474 | eprintf("Fail to clear event\n"); 475 | return false; 476 | } 477 | } 478 | return true; 479 | } 480 | 481 | static addr_t find_eprocess(vmi_instance_t vmi, uint64_t dtb) 482 | { 483 | addr_t ps_head = 0; 484 | addr_t flink = 0; 485 | addr_t start_proc = 0; 486 | addr_t pdb_offset = 0; 487 | addr_t tasks_offset = 0; 488 | addr_t value = 0; 489 | status_t status; 490 | 491 | 492 | status = vmi_get_offset(vmi, "win_tasks", &tasks_offset); 493 | if (VMI_FAILURE == status) 494 | { 495 | eprintf("failed\n"); 496 | return 0; 497 | } 498 | 499 | status = vmi_get_offset(vmi, "win_pdbase", &pdb_offset); 500 | if (VMI_FAILURE == status) 501 | { 502 | eprintf("failed\n"); 503 | return 0; 504 | } 505 | 506 | status = vmi_translate_ksym2v(vmi, "PsActiveProcessHead", &ps_head); 507 | if (VMI_FAILURE == status) 508 | { 509 | eprintf("failed\n"); 510 | return 0; 511 | } 512 | status = vmi_read_addr_ksym(vmi, "PsActiveProcessHead", &flink); 513 | if (VMI_FAILURE == status) 514 | { 515 | eprintf("failed\n"); 516 | return 0; 517 | } 518 | 519 | while (flink != ps_head) 520 | { 521 | // get eprocess head 522 | start_proc = flink - tasks_offset; 523 | 524 | // get dtb value 525 | vmi_read_addr_va(vmi, start_proc + pdb_offset, 0, &value); 526 | if (value == dtb) 527 | { 528 | // read process name 529 | return start_proc; 530 | } 531 | // read new flink 532 | vmi_read_addr_va(vmi, flink, 0, &flink); 533 | } 534 | return 0; 535 | } 536 | 537 | static addr_t find_ethread(vmi_instance_t vmi, addr_t eproc) 538 | { 539 | addr_t ethread; 540 | addr_t thread_list_head; 541 | status_t status; 542 | 543 | status = vmi_read_addr_va(vmi, eproc + EPROC_THREAD_HEAD_OFF, 0, &thread_list_head); 544 | if (status == VMI_FAILURE) 545 | { 546 | eprintf("Cannot read ethread\n"); 547 | return 0; 548 | } 549 | ethread = thread_list_head - ETH_THREAD_HEAD_OFF; 550 | return ethread; 551 | } 552 | 553 | static bool is_userland(uint64_t rflag) 554 | { 555 | // extract the IOPL field 556 | int iopl = rflag & ((1 << 13) | (1 << 12)); 557 | eprintf("iopl: %d\n", iopl); 558 | return (iopl == 3) ? true : false; 559 | } 560 | 561 | static event_response_t cb_on_sstep_until_userland(vmi_instance_t vmi, vmi_event_t *event) 562 | { 563 | uint64_t rflag; 564 | 565 | eprintf("%s\n", __func__); 566 | 567 | if(!event || event->type != VMI_EVENT_SINGLESTEP) { 568 | eprintf("ERROR (%s): invalid event encounted\n", __func__); 569 | return 0; 570 | } 571 | 572 | // check for mem event 573 | addr_t paddr = 0; 574 | vmi_translate_kv2p(vmi, event->x86_regs->rip, &paddr); 575 | addr_t gfn = paddr >> 12; 576 | eprintf("Checking for mem event on page: 0x%" PRIx64 "\n", gfn); 577 | vmi_event_t *mem_event = vmi_get_mem_event(vmi, gfn, VMI_MEMACCESS_X); 578 | if (mem_event != NULL) 579 | { 580 | eprintf("Mem event found !\n"); 581 | } 582 | 583 | rflag = event->x86_regs->rflags; 584 | eprintf("rflag: 0x%" PRIx64 ", rip: 0x%" PRIx64 "\n", rflag, event->x86_regs->rip); 585 | if (is_userland(rflag)) 586 | { 587 | vmi_pause_vm(vmi); 588 | interrupted = true; 589 | } 590 | return VMI_EVENT_RESPONSE_NONE; 591 | } 592 | 593 | bool attach_new_process(RDebug *dbg) 594 | { 595 | RIODesc *desc = NULL; 596 | RIOVmi *rio_vmi = NULL; 597 | status_t status; 598 | addr_t start_thread_addr; 599 | 600 | eprintf("%s\n", __func__); 601 | 602 | desc = dbg->iob.io->desc; 603 | rio_vmi = desc->data; 604 | 605 | status = vmi_translate_ksym2v(rio_vmi->vmi, "KiStartUserThread", &start_thread_addr); 606 | if (VMI_FAILURE == status) 607 | { 608 | // winxp ? KiStartThread 609 | status = vmi_translate_ksym2v(rio_vmi->vmi, "KiThreadStartup", &start_thread_addr); 610 | if (VMI_FAILURE == status) 611 | { 612 | eprintf("Fail to get KiStartUserThread | KiThreadStartup symbol\n"); 613 | return false; 614 | } 615 | } 616 | 617 | eprintf("KiStartUserThread: 0x%lx\n", start_thread_addr); 618 | continue_until(rio_vmi, start_thread_addr, true); 619 | 620 | addr_t eproc = find_eprocess(rio_vmi->vmi, rio_vmi->pid_cr3); 621 | if (!eproc) 622 | { 623 | eprintf("Cannot find EPROCESS\n"); 624 | return false; 625 | } 626 | eprintf("EPROCESS 0x%lx\n", eproc); 627 | addr_t ethread = find_ethread(rio_vmi->vmi, eproc); 628 | if (!ethread) 629 | { 630 | eprintf("Cannot find ETHREAD\n"); 631 | return false; 632 | } 633 | printf("ETHREAD 0x%lx\n", ethread); 634 | addr_t w32_start_addr; 635 | status = vmi_read_addr_va(rio_vmi->vmi, ethread + ETH_W32_START_OFF, 0, &w32_start_addr); 636 | if (status == VMI_FAILURE) 637 | { 638 | eprintf("Cannot read Win32StartAddress\n"); 639 | return false; 640 | } 641 | eprintf("Win32StartAddress 0x%lx\n", w32_start_addr); 642 | 643 | eprintf("mode: %d\n", VMI_PM_IA32E); 644 | 645 | // singlestep until userland 646 | vmi_event_t sstep_event; 647 | sstep_event.version = VMI_EVENTS_VERSION; 648 | sstep_event.type = VMI_EVENT_SINGLESTEP; 649 | sstep_event.callback = cb_on_sstep_until_userland; 650 | sstep_event.ss_event.enable = 1; 651 | SET_VCPU_SINGLESTEP(sstep_event.ss_event, rio_vmi->current_vcpu); 652 | 653 | // register 654 | status = vmi_register_event(rio_vmi->vmi, &sstep_event); 655 | if (status == VMI_FAILURE) 656 | { 657 | eprintf("Fail to register event\n"); 658 | return false; 659 | } 660 | 661 | // resume 662 | status = vmi_resume_vm(rio_vmi->vmi); 663 | if (status == VMI_FAILURE) 664 | { 665 | eprintf("Fail to resume vm\n"); 666 | return false; 667 | } 668 | 669 | interrupted = false; 670 | while (!interrupted) 671 | { 672 | vmi_events_listen(rio_vmi->vmi, 1000); 673 | } 674 | 675 | // process rest of event queue 676 | vmi_events_listen(rio_vmi->vmi, 0); 677 | 678 | // clear 679 | vmi_clear_event(rio_vmi->vmi, &sstep_event, NULL); 680 | 681 | // page_info_t pinfo; 682 | // status = vmi_pagetable_lookup_extended(rio_vmi->vmi, rio_vmi->pid_cr3, w32_start_addr, &pinfo); 683 | // if (status == VMI_FAILURE) 684 | // { 685 | // eprintf("Win32StartAddress is not mapped\n"); 686 | // return false; 687 | // } 688 | 689 | 690 | // addr_t ntcontinue_addr; 691 | // status = vmi_translate_ksym2v(rio_vmi->vmi, "NtContinue", &ntcontinue_addr); 692 | // if (status == VMI_FAILURE) 693 | // { 694 | // eprintf("fail to translate symbol\n"); 695 | // return false; 696 | // } 697 | 698 | // continue_until(rio_vmi, ntcontinue_addr, true); 699 | 700 | // addr_t win32startaddress_paddr; 701 | // status = vmi_pagetable_lookup(rio_vmi->vmi, rio_vmi->pid_cr3, w32_start_addr, &win32startaddress_paddr); 702 | // if (status == VMI_SUCCESS) 703 | // { 704 | // printf("Win32StartAddress is mapped\n"); 705 | // } 706 | 707 | 708 | // bool win32startaddress_mapped = false; 709 | // while (!win32startaddress_mapped) 710 | // { 711 | // printf("continue until MmAccessFault\n"); 712 | // addr_t mmaccessfault_vaddr; 713 | // // set breakpoint on MmAccessFault 714 | // status = vmi_translate_ksym2v(rio_vmi->vmi, "MmAccessFault", &mmaccessfault_vaddr); 715 | // if (status == VMI_FAILURE) 716 | // { 717 | // eprintf("fail to translate symbol\n"); 718 | // return false; 719 | // } 720 | // continue_until(rio_vmi, mmaccessfault_vaddr, true); 721 | // // test if win32startaddress is mapped 722 | // addr_t win32startaddress_paddr; 723 | // status = vmi_pagetable_lookup(rio_vmi->vmi, rio_vmi->pid_cr3, w32_start_addr, &win32startaddress_paddr); 724 | // if (status == VMI_SUCCESS) 725 | // { 726 | // printf("Win32StartAddress is mapped\n"); 727 | // win32startaddress_mapped = true; 728 | // } 729 | // } 730 | 731 | // continue 732 | // continue_until(rio_vmi, w32_start_addr, false); 733 | 734 | return true; 735 | } 736 | 737 | bool is_target_process(RIOVmi *rio_vmi, const char *proc_name, uint64_t dtb) 738 | { 739 | int pid; 740 | status_t status; 741 | 742 | status = vmi_dtb_to_pid_extended_idle(rio_vmi->vmi, dtb, &pid); 743 | if (status == VMI_FAILURE) 744 | { 745 | eprintf("Fail to get pid\n"); 746 | return false; 747 | } 748 | 749 | if (rio_vmi->url_identify_by_name && 750 | !strncasecmp(proc_name, rio_vmi->proc_name, strlen(rio_vmi->proc_name))) 751 | { 752 | rio_vmi->pid = pid; 753 | return true; 754 | } 755 | else if (!rio_vmi->url_identify_by_name && pid == rio_vmi->pid) 756 | { 757 | rio_vmi->proc_name = strdup(proc_name); 758 | return true; 759 | } 760 | return false; 761 | } 762 | 763 | bool intercept_process(RDebug *dbg, int pid) 764 | { 765 | RIODesc *desc = NULL; 766 | RIOVmi *rio_vmi = NULL; 767 | status_t status = 0; 768 | 769 | eprintf("Attaching to pid %d...\n", pid); 770 | 771 | desc = dbg->iob.io->desc; 772 | rio_vmi = desc->data; 773 | if (!rio_vmi) 774 | { 775 | eprintf("%s: Invalid RIOVmi\n", __func__); 776 | return 1; 777 | } 778 | 779 | status = vmi_pause_vm(rio_vmi->vmi); 780 | if (status == VMI_FAILURE) 781 | { 782 | eprintf("%s: Fail to pause VM\n", __func__); 783 | return 1; 784 | } 785 | 786 | vmi_event_t cr3_load_event = {0}; 787 | SETUP_REG_EVENT(&cr3_load_event, CR3, VMI_REGACCESS_W, 0, cb_on_cr3_load); 788 | 789 | // setting event data 790 | cr3_load_event.data = (void*) rio_vmi; 791 | 792 | status = vmi_register_event(rio_vmi->vmi, &cr3_load_event); 793 | if (status == VMI_FAILURE) 794 | { 795 | eprintf("%s: vmi event registration failure\n", __func__); 796 | return false; 797 | } 798 | 799 | status = vmi_resume_vm(rio_vmi->vmi); 800 | if (status == VMI_FAILURE) 801 | { 802 | eprintf("%s: Fail to resume VM\n", __func__); 803 | return false; 804 | } 805 | 806 | interrupted = false; 807 | while (!interrupted) 808 | { 809 | eprintf("%s: Listening on VMI events...\n", __func__); 810 | status = vmi_events_listen(rio_vmi->vmi, 1000); 811 | if (status == VMI_FAILURE) 812 | { 813 | interrupted = true; 814 | return false; 815 | } 816 | } 817 | 818 | // unregister cr3 event 819 | status = vmi_clear_event(rio_vmi->vmi, &cr3_load_event, NULL); 820 | if (status == VMI_FAILURE) 821 | { 822 | eprintf("%s Fail to clear event\n", __func__); 823 | return false; 824 | } 825 | 826 | // clear event buffer if any 827 | status = vmi_events_listen(rio_vmi->vmi, 0); 828 | if (status == VMI_FAILURE) 829 | { 830 | eprintf("%s: Fail to clear event buffer\n", __func__); 831 | return false; 832 | } 833 | 834 | return true; 835 | } 836 | -------------------------------------------------------------------------------- /debug_vmi.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "io_vmi.h" 6 | #include "utils.h" 7 | 8 | // vmi_events_listen loop 9 | static bool interrupted = false; 10 | 11 | // 12 | // callbacks 13 | // 14 | static event_response_t cb_on_mem_event(vmi_instance_t vmi, vmi_event_t *event){ 15 | status_t status; 16 | bp_event_data *event_data; 17 | const char *pname = NULL; 18 | RIOVmi *rio_vmi = NULL; 19 | 20 | eprintf("%s\n", __func__); 21 | 22 | if(!event || event->type != VMI_EVENT_MEMORY || !event->data) { 23 | eprintf("ERROR (%s): invalid event encounted\n", __func__); 24 | return VMI_EVENT_RESPONSE_NONE; 25 | } 26 | 27 | // get event_data 28 | event_data = (bp_event_data*) event->data; 29 | rio_vmi = event_data->rio_vmi; 30 | 31 | // did we catched a memory event while the user was simply asking for a singlestep ? 32 | if (rio_vmi->cmd_sstep) 33 | { 34 | // clear the memory event 35 | // so that the singlestep event can work 36 | status = vmi_clear_event(vmi, event, NULL); 37 | if (VMI_FAILURE == status) 38 | eprintf("%s: fail to clear event\n", __func__); 39 | return VMI_EVENT_RESPONSE_NONE; 40 | } 41 | 42 | pname = dtb_to_pname(vmi, event->x86_regs->cr3); 43 | 44 | // our pid ? 45 | if (event->x86_regs->cr3 != event_data->pid_cr3) 46 | { 47 | eprintf("%s: wrong cr3 (%s)(0x%lx)\n", __func__, pname, event->x86_regs->cr3); 48 | return VMI_EVENT_RESPONSE_EMULATE; 49 | } 50 | 51 | // at the right rip ? 52 | if (!vaddr_equal(vmi, event->x86_regs->rip, event_data->bp_vaddr)) 53 | { 54 | eprintf("%s: wrong rip: %"PRIx64" (bp_vaddr: %"PRIx64")\n", __func__, event->x86_regs->rip, event_data->bp_vaddr); 55 | return VMI_EVENT_RESPONSE_EMULATE; 56 | } 57 | 58 | eprintf("%s: RIP: %"PRIx64 " (%s)\n", __func__, event->x86_regs->rip, pname); 59 | print_event(event); 60 | 61 | // pause VM 62 | status = vmi_pause_vm(vmi); 63 | if (VMI_FAILURE == status) 64 | eprintf("%s: Fail to pause vm\n", __func__); 65 | 66 | // stop listen 67 | interrupted = true; 68 | 69 | return 0; 70 | } 71 | 72 | static event_response_t cb_on_sstep(vmi_instance_t vmi, vmi_event_t *event) { 73 | status_t status; 74 | bp_event_data *event_data = NULL; 75 | 76 | eprintf("%s\n", __func__); 77 | 78 | if(!event || event->type != VMI_EVENT_SINGLESTEP) { 79 | eprintf("ERROR (%s): invalid event encounted\n", __func__); 80 | return 0; 81 | } 82 | 83 | // event data ? 84 | if (event->data) 85 | { 86 | // coming from software breakpoint 87 | event_data = (bp_event_data*) event->data; 88 | // restore software breakpoint 89 | r_bp_restore_one(event_data->bp, event_data->bpitem, true); 90 | // null event data 91 | event->data = NULL; 92 | // toggle singlestep OFF 93 | return VMI_EVENT_RESPONSE_TOGGLE_SINGLESTEP; 94 | } 95 | else 96 | { 97 | // simple singlestep 98 | // stop monitoring 99 | interrupted = true; 100 | // pause the VM before exiting the callback 101 | status = vmi_pause_vm(vmi); 102 | if (status == VMI_FAILURE) 103 | eprintf("%s: Fail to pause VM\n", __func__); 104 | 105 | return VMI_EVENT_RESPONSE_NONE; 106 | } 107 | } 108 | 109 | static event_response_t cb_on_int3(vmi_instance_t vmi, vmi_event_t *event){ 110 | status_t status; 111 | bp_event_data *event_data; 112 | char *proc_name = NULL; 113 | RIOVmi *rio_vmi = NULL; 114 | 115 | eprintf("%s\n", __func__); 116 | 117 | if(!event || event->type != VMI_EVENT_INTERRUPT || !event->data) { 118 | eprintf("ERROR (%s): invalid event encounted\n", __func__); 119 | return VMI_EVENT_RESPONSE_NONE; 120 | } 121 | 122 | // get event_data 123 | event_data = (bp_event_data*) event->data; 124 | rio_vmi = event_data->rio_vmi; 125 | 126 | // process name 127 | proc_name = dtb_to_pname(vmi, event->x86_regs->cr3); 128 | 129 | // default reinject behavior 130 | // do not reinject interrupt in the guest* 131 | // TODO check list of breakpoints from r2 132 | event->interrupt_event.reinject = 0; 133 | 134 | // our targeted process ? 135 | if (event->x86_regs->cr3 != event_data->pid_cr3) 136 | { 137 | eprintf("%s: wrong process %s (0x%lx)\n", __func__, proc_name, event->x86_regs->cr3); 138 | 139 | // add event data to singlestep event already registered 140 | rio_vmi->sstep_event->data = event->data; 141 | 142 | // restore original opcode 143 | r_bp_restore_one(event_data->bp, event_data->bpitem, false); 144 | 145 | // toggle singlestep ON 146 | return VMI_EVENT_RESPONSE_TOGGLE_SINGLESTEP; 147 | } 148 | else 149 | { 150 | // pause VM 151 | status = vmi_pause_vm(vmi); 152 | if (VMI_FAILURE == status) 153 | { 154 | eprintf("%s: Fail to pause vm\n", __func__); 155 | } 156 | // stop listen 157 | interrupted = true; 158 | 159 | return VMI_EVENT_RESPONSE_NONE; 160 | } 161 | } 162 | 163 | 164 | // 165 | // R2 debug interface 166 | // 167 | static int __step(RDebug *dbg) { 168 | RIODesc *desc = NULL; 169 | RIOVmi *rio_vmi = NULL; 170 | status_t status; 171 | 172 | eprintf("%s\n", __func__); 173 | 174 | desc = dbg->iob.io->desc; 175 | rio_vmi = desc->data; 176 | if (!rio_vmi) 177 | { 178 | eprintf("%s: Invalid RIOVmi\n", __func__); 179 | return 1; 180 | } 181 | 182 | // enable singlestep 183 | // hack around lack of API in LibVMI 184 | // clear current event 185 | status = vmi_clear_event(rio_vmi->vmi, rio_vmi->sstep_event, NULL); 186 | if (VMI_FAILURE == status) 187 | { 188 | eprintf("%s: fail to clear event\n", __func__); 189 | return false; 190 | } 191 | 192 | // resetup singlestep event, enabled 193 | bzero(rio_vmi->sstep_event, sizeof(vmi_event_t)); 194 | SETUP_SINGLESTEP_EVENT(rio_vmi->sstep_event, 1u << 0, cb_on_sstep, true); 195 | // clear data field (not a software breakpoint) 196 | rio_vmi->sstep_event->data = NULL; 197 | // register event 198 | status = vmi_register_event(rio_vmi->vmi, rio_vmi->sstep_event); 199 | if (status == VMI_FAILURE) 200 | { 201 | eprintf("%s: fail to register event\n", __func__); 202 | return false; 203 | } 204 | 205 | status = vmi_resume_vm(rio_vmi->vmi); 206 | if (status == VMI_FAILURE) 207 | { 208 | eprintf("%s: Failed to resume VM execution\n", __func__); 209 | return false; 210 | } 211 | 212 | // we are in the command singlestep 213 | rio_vmi->cmd_sstep = true; 214 | 215 | return true; 216 | } 217 | 218 | 219 | // "dc" continue execution 220 | static int __continue(RDebug *dbg, __attribute__((unused)) int pid, __attribute__((unused)) int tid, __attribute__((unused)) int sig) { 221 | RIODesc *desc = NULL; 222 | RIOVmi *rio_vmi = NULL; 223 | status_t status; 224 | 225 | eprintf("%s, sig: %d\n", __func__, sig); 226 | 227 | desc = dbg->iob.io->desc; 228 | rio_vmi = desc->data; 229 | if (!rio_vmi) 230 | { 231 | eprintf("%s: Invalid RIOVmi\n", __func__); 232 | return 1; 233 | } 234 | 235 | status = vmi_resume_vm(rio_vmi->vmi); 236 | if (VMI_FAILURE == status) 237 | { 238 | eprintf("%s: Failed to resume VM execution\n", __func__); 239 | return 1; 240 | } 241 | 242 | return 0; 243 | } 244 | 245 | static int __attach(RDebug *dbg, int pid) { 246 | RIODesc *desc = NULL; 247 | RIOVmi *rio_vmi = NULL; 248 | status_t status = 0; 249 | 250 | eprintf("Attaching to pid %d...\n", pid); 251 | 252 | desc = dbg->iob.io->desc; 253 | rio_vmi = desc->data; 254 | if (!rio_vmi) 255 | { 256 | eprintf("%s: Invalid RIOVmi\n", __func__); 257 | return 1; 258 | } 259 | 260 | intercept_process(dbg, pid); 261 | 262 | // set attached to allow reg_read 263 | rio_vmi->attached = true; 264 | 265 | rio_vmi->sstep_event = R_NEW0(vmi_event_t); 266 | 267 | // did we attached to a new process ? 268 | if (rio_vmi->attach_new_process) 269 | { 270 | return attach_new_process(dbg); 271 | } 272 | else 273 | { 274 | eprintf("Attaching to existing process is not implemented\n"); 275 | } 276 | 277 | // init singlestep event (not enabled) 278 | SETUP_SINGLESTEP_EVENT(rio_vmi->sstep_event, 1u << 0, cb_on_sstep, false); 279 | // register event 280 | status = vmi_register_event(rio_vmi->vmi, rio_vmi->sstep_event); 281 | if (VMI_FAILURE == status) 282 | { 283 | eprintf("%s: fail to register event\n", __func__); 284 | return 1; 285 | } 286 | 287 | return 0; 288 | } 289 | 290 | static int __detach(__attribute__((unused)) RDebug *dbg, __attribute__((unused)) int pid) { 291 | eprintf("%s\n", __func__); 292 | 293 | return 1; 294 | } 295 | 296 | static RList* __threads(__attribute__((unused)) RDebug *dbg, __attribute__((unused)) int pid) { 297 | eprintf("%s\n", __func__); 298 | 299 | return NULL; 300 | } 301 | 302 | static RDebugReasonType __wait(RDebug *dbg, __attribute__((unused)) int pid) { 303 | RDebugReasonType reason = R_DEBUG_REASON_UNKNOWN; 304 | RIODesc *desc = NULL; 305 | RIOVmi *rio_vmi = NULL; 306 | status_t status; 307 | eprintf("%s\n", __func__); 308 | 309 | desc = dbg->iob.io->desc; 310 | rio_vmi = desc->data; 311 | if (!rio_vmi) 312 | { 313 | eprintf("%s: Invalid RIOVmi\n", __func__); 314 | return reason; 315 | } 316 | 317 | // handle CTRL-C 318 | r_cons_break_push(NULL, NULL); 319 | 320 | interrupted = false; 321 | while (!interrupted && !r_cons_is_breaked()) { 322 | eprintf("%s: Listen to VMI events...\n", __func__); 323 | status = vmi_events_listen(rio_vmi->vmi, 1000); 324 | if (status == VMI_FAILURE) 325 | { 326 | eprintf("%s: Fail to listen to events\n", __func__); 327 | return reason; 328 | } 329 | } 330 | // exit because of CTRL-C ? 331 | if (r_cons_is_breaked()) 332 | { 333 | eprintf("CTRL-C !\n"); 334 | intercept_process(dbg, rio_vmi->pid); 335 | } 336 | r_cons_break_pop(); 337 | 338 | // clear event buffer if any 339 | status = vmi_events_listen(rio_vmi->vmi, 0); 340 | if (status == VMI_FAILURE) 341 | { 342 | eprintf("%s: fail to clear event buffer\n", __func__); 343 | return reason; 344 | } 345 | 346 | // clear event if singlestep 347 | // breakpoint events are cleared in __breakpoint if unset 348 | // was it a single step ? 349 | if (rio_vmi->cmd_sstep) 350 | { 351 | // hack around lack of API in LibVMI 352 | status = vmi_clear_event(rio_vmi->vmi, rio_vmi->sstep_event, NULL); 353 | if (VMI_FAILURE == status) 354 | { 355 | eprintf("%s: fail to clear event\n", __func__); 356 | return false; 357 | } 358 | 359 | // set singlestep event, disabled 360 | bzero(rio_vmi->sstep_event, sizeof(vmi_event_t)); 361 | SETUP_SINGLESTEP_EVENT(rio_vmi->sstep_event, 1u << 0, cb_on_sstep, false); 362 | rio_vmi->sstep_event->data = NULL; 363 | // register it 364 | status = vmi_register_event(rio_vmi->vmi, rio_vmi->sstep_event); 365 | if (VMI_FAILURE == status) 366 | { 367 | eprintf("%s: fail to register event\n", __func__); 368 | return false; 369 | } 370 | 371 | rio_vmi->cmd_sstep = false; 372 | 373 | reason = R_DEBUG_REASON_STEP; 374 | } 375 | else 376 | { 377 | reason = R_DEBUG_REASON_BREAKPOINT; 378 | } 379 | 380 | return reason; 381 | } 382 | 383 | // "dm" get memory maps of target process 384 | static RList *__map_get(RDebug* dbg) { 385 | RIODesc *desc = NULL; 386 | RIOVmi *rio_vmi = NULL; 387 | status_t status; 388 | addr_t dtb = 0; 389 | page_mode_t page_mode; 390 | char unknown[] = "unknown_"; 391 | 392 | eprintf("%s\n", __func__); 393 | 394 | desc = dbg->iob.io->desc; 395 | rio_vmi = desc->data; 396 | if (!rio_vmi) 397 | { 398 | eprintf("%s: Invalid RIOVmi\n", __func__); 399 | return NULL; 400 | } 401 | 402 | status = vmi_pid_to_dtb(rio_vmi->vmi, rio_vmi->pid, &dtb); 403 | if (status == VMI_FAILURE) 404 | { 405 | eprintf("Fail to get dtb from pid\n"); 406 | return NULL; 407 | } 408 | 409 | page_mode = vmi_get_page_mode(rio_vmi->vmi, rio_vmi->current_vcpu); 410 | 411 | GSList *va_pages = vmi_get_va_pages(rio_vmi->vmi, dtb); 412 | if (!va_pages) 413 | { 414 | eprintf("Fail to get va pages\n"); 415 | return NULL; 416 | } 417 | 418 | 419 | RList *r_maps = r_list_newf((RListFree) r_debug_map_free); 420 | GSList *loop = va_pages; 421 | int nb = 0; 422 | while (loop) 423 | { 424 | addr_t pte_value = 0; 425 | page_info_t *page = loop->data; 426 | int permissions = R_PERM_R; 427 | int supervisor = 0; 428 | char str_nb[20]; 429 | 430 | // new map name 431 | int str_nb_size = sprintf(str_nb, "%d", nb); 432 | char *map_name = calloc(strlen(unknown) + str_nb_size + 1, 1); 433 | strncat(map_name, unknown, strlen(map_name)); 434 | strncat(map_name, str_nb, str_nb_size); 435 | // get permissions 436 | switch (page_mode) { 437 | case VMI_PM_LEGACY: 438 | pte_value = page->x86_legacy.pte_value; 439 | break; 440 | case VMI_PM_PAE: 441 | pte_value = page->x86_pae.pte_value; 442 | if (!VMI_GET_BIT(pte_value, 63)) 443 | permissions |= R_PERM_X; 444 | break; 445 | case VMI_PM_IA32E: 446 | pte_value = page->x86_ia32e.pte_value; 447 | break; 448 | default: 449 | eprintf("Unhandled page mode"); 450 | // TODO free 451 | return NULL; 452 | } 453 | supervisor = USER_SUPERVISOR(pte_value); 454 | if (READ_WRITE(pte_value)) 455 | permissions |= R_PERM_W; 456 | // build RDebugMap 457 | addr_t map_start = page->vaddr; 458 | addr_t map_end = page->vaddr + page->size; 459 | RDebugMap *r_debug_map = r_debug_map_new (map_name, map_start, map_end, permissions, supervisor); 460 | // append 461 | r_list_append (r_maps, r_debug_map); 462 | // loop 463 | loop = loop->next; 464 | nb +=1; 465 | } 466 | 467 | // free va_pages 468 | while (va_pages) 469 | { 470 | g_free(va_pages->data); 471 | va_pages = va_pages->next; 472 | } 473 | g_slist_free(va_pages); 474 | 475 | return r_maps; 476 | } 477 | 478 | static RList* __modules_get(__attribute__((unused)) RDebug *dbg) { 479 | eprintf("%s\n", __func__); 480 | 481 | return NULL; 482 | } 483 | 484 | static int __breakpoint (struct r_bp_t *bp, RBreakpointItem *b, bool set) { 485 | RIODesc *desc = NULL; 486 | RIOVmi *rio_vmi = NULL; 487 | status_t status; 488 | addr_t bp_vaddr = b->addr; 489 | gboolean ret; 490 | // return value of this function 491 | // whether our implementation handled the breakpoint 492 | // or if r2 should do it 493 | bool bp_handled = false; 494 | vmi_event_t *bp_event = NULL; 495 | eprintf("%s, set: %d, addr: %"PRIx64", hw: %d\n", __func__, set, bp_vaddr, b->hw); 496 | if (!bp) 497 | return false; 498 | 499 | desc = bp->iob.io->desc; 500 | rio_vmi = (RIOVmi*) desc->data; 501 | 502 | if (set) 503 | { 504 | // the breakpoint API will be called multiple times for the same breakpoint 505 | // in case of single stepping for example, radare2 still calls this API 506 | // for each breakpoint before the single-step 507 | // therefore, check if the breakpoint has already been inserted 508 | 509 | bp_event = (vmi_event_t*) g_hash_table_lookup(rio_vmi->bp_events_table, GINT_TO_POINTER(bp_vaddr)); 510 | if (!bp_event) 511 | { 512 | if (b->hw) 513 | { 514 | // hardware breakpoint 515 | // need to translate the virtual address to physical 516 | addr_t paddr; 517 | status = vmi_translate_uv2p(rio_vmi->vmi, bp_vaddr, rio_vmi->pid, &paddr); 518 | if (VMI_FAILURE == status) 519 | { 520 | eprintf("Fail to get physical addresss\n"); 521 | return 1; 522 | } 523 | 524 | // get guest frame number 525 | addr_t gfn = paddr >> 12; 526 | eprintf("%s: paddr: %016"PRIx64", gfn: %"PRIx64"\n", __func__, paddr, gfn); 527 | 528 | // prepare new vmi_event 529 | bp_event = R_NEW0(vmi_event_t); 530 | if (!bp_event) 531 | { 532 | eprintf("%s: Fail to allocate memory\n", __func__); 533 | return false; 534 | } 535 | SETUP_MEM_EVENT(bp_event, gfn, VMI_MEMACCESS_X, cb_on_mem_event, 0); 536 | bp_handled = true; 537 | } 538 | else 539 | { 540 | // software breakpoint 541 | // prepare new vmi_event 542 | bp_event = R_NEW0(vmi_event_t); 543 | if (!bp_event) 544 | { 545 | eprintf("%s: Fail to allocate memory\n", __func__); 546 | return false; 547 | } 548 | SETUP_INTERRUPT_EVENT(bp_event, cb_on_int3); 549 | // r2 has to write the software breakpoint by himself 550 | bp_handled = false; 551 | } 552 | // add event data 553 | bp_event_data *event_data = R_NEW0(bp_event_data); 554 | if (!event_data) 555 | { 556 | eprintf("%s: Fail to allocate memory\n", __func__); 557 | return false; 558 | } 559 | event_data->pid_cr3 = rio_vmi->pid_cr3; 560 | event_data->bp_vaddr = bp_vaddr; 561 | event_data->bp = bp; 562 | event_data->bpitem = b; 563 | event_data->rio_vmi = rio_vmi; 564 | bp_event->data = event_data; 565 | 566 | // add our breakpoint to the hashtable 567 | // [bp_vaddr] -> [vmi_event *] 568 | ret = g_hash_table_insert(rio_vmi->bp_events_table, GINT_TO_POINTER(bp_vaddr), bp_event); 569 | if (FALSE == ret) 570 | { 571 | eprintf("%s: Fail to insert event into ghashtable\n", __func__); 572 | return false; 573 | } 574 | 575 | // register breakpoint event 576 | // either interrupt or mem event 577 | status = vmi_register_event(rio_vmi->vmi, bp_event); 578 | if (VMI_FAILURE == status) 579 | { 580 | eprintf("%s: Fail to register event\n", __func__); 581 | return false; 582 | } 583 | } 584 | } else { 585 | // unset 586 | // get event from ghashtable 587 | bp_event = (vmi_event_t*) g_hash_table_lookup(rio_vmi->bp_events_table, GINT_TO_POINTER(bp_vaddr)); 588 | if (bp_event) 589 | { 590 | // unregister event 591 | status = vmi_clear_event(rio_vmi->vmi, bp_event, NULL); 592 | if (VMI_FAILURE == status) 593 | { 594 | eprintf("%s: Fail to clear event\n", __func__); 595 | return false; 596 | } 597 | if (bp_event->data) 598 | free(bp_event->data); 599 | free(bp_event); 600 | 601 | // remove key/value from table 602 | ret = g_hash_table_remove(rio_vmi->bp_events_table, GINT_TO_POINTER(bp_vaddr)); 603 | if (FALSE == ret) 604 | { 605 | eprintf("%s: Fail to remove key from breakpoint table\n", __func__); 606 | return false; 607 | } 608 | 609 | bp_handled = true; 610 | if (!b->hw) 611 | { 612 | // software breakpoint 613 | // r2 has to write back the original instruction 614 | bp_handled = false; 615 | } 616 | } 617 | else 618 | { 619 | eprintf("%s: Breakpoint already disabled\n", __func__); 620 | bp_handled = true; 621 | } 622 | } 623 | 624 | return bp_handled; 625 | } 626 | 627 | // "drp" register profile 628 | static const char *__reg_profile(RDebug *dbg) { 629 | eprintf("%s\n", __func__); 630 | int arch = r_sys_arch_id (dbg->arch); 631 | int bits = dbg->anal->bits; 632 | 633 | switch (arch) { 634 | case R_SYS_ARCH_X86: 635 | switch (bits) { 636 | case 32: 637 | return strdup ( 638 | #include "x86-32.h" 639 | ); 640 | break; 641 | case 64: 642 | return strdup ( 643 | #include "x86-64.h" 644 | ); 645 | break; 646 | default: 647 | eprintf("bit size not supported by vmi debugger\n"); 648 | return NULL; 649 | 650 | } 651 | break; 652 | default: 653 | eprintf("Architecture not supported by vmi debugger\n"); 654 | return NULL; 655 | } 656 | 657 | } 658 | 659 | // "dk" send signal 660 | static bool __kill(RDebug *dbg, __attribute__((unused)) int pid, __attribute__((unused)) int tid, int sig) { 661 | RIODesc *desc = NULL; 662 | RIOVmi *rio_vmi = NULL; 663 | eprintf("%s, sig: %d\n", __func__, sig); 664 | 665 | desc = dbg->iob.io->desc; 666 | rio_vmi = desc->data; 667 | if (!rio_vmi) 668 | { 669 | eprintf("%s: Invalid RIOVmi\n", __func__); 670 | return false; 671 | } 672 | 673 | if (sig < 0 || sig > 31) 674 | return false; 675 | return true; 676 | } 677 | 678 | static int __select(__attribute__((unused)) int pid, __attribute__((unused)) int tid) { 679 | eprintf("%s\n", __func__); 680 | 681 | return 1; 682 | } 683 | 684 | static RDebugInfo* __info(__attribute__((unused)) RDebug *dbg, __attribute__((unused)) const char *arg) { 685 | eprintf("%s\n", __func__); 686 | 687 | return NULL; 688 | } 689 | 690 | static RList* __frames(__attribute__((unused)) RDebug *dbg, __attribute__((unused)) ut64 at) { 691 | eprintf("%s\n", __func__); 692 | 693 | return NULL; 694 | } 695 | 696 | static int __reg_read(RDebug *dbg, int type, ut8 *buf, int size) { 697 | RIODesc *desc = NULL; 698 | RIOVmi *rio_vmi = NULL; 699 | status_t status = 0; 700 | int buf_size = 0; 701 | pid_t pid; 702 | uint64_t cr3 = 0; 703 | registers_t regs; 704 | 705 | eprintf("%s, type: %d, size:%d\n", __func__, type, size); 706 | 707 | desc = dbg->iob.io->desc; 708 | rio_vmi = desc->data; 709 | if (!rio_vmi) 710 | { 711 | eprintf("%s: Invalid RIOVmi\n", __func__); 712 | return 1; 713 | } 714 | 715 | if (!rio_vmi->attached) 716 | return 0; 717 | 718 | unsigned int nb_vcpus = vmi_get_num_vcpus(rio_vmi->vmi); 719 | 720 | bool found = false; 721 | for (unsigned int vcpu = 0; vcpu < nb_vcpus; vcpu++) 722 | { 723 | // get cr3 724 | // if we have just attached, we cannot rely on vcpu_reg() since the VCPU 725 | // state has not been synchronized with the new CR3 value from the attach event 726 | if (rio_vmi->pid_cr3) 727 | cr3 = rio_vmi->pid_cr3; 728 | else 729 | { 730 | // TODO: never reached, pid_cr3 is set since cb_on_cr3_load 731 | // and is always valid 732 | status = vmi_get_vcpureg(rio_vmi->vmi, &cr3, CR3, vcpu); 733 | if (status == VMI_FAILURE) 734 | { 735 | eprintf("Fail to get vcpu registers\n"); 736 | return 1; 737 | } 738 | } 739 | // convert to pid 740 | status = vmi_dtb_to_pid(rio_vmi->vmi, cr3, &pid); 741 | if (status == VMI_FAILURE) 742 | { 743 | eprintf("Fail to convert CR3 to PID\n"); 744 | return 1; 745 | } 746 | if (pid == rio_vmi->pid) 747 | { 748 | found = true; 749 | 750 | // get registers 751 | status = vmi_get_vcpuregs(rio_vmi->vmi, ®s, vcpu); 752 | if (status == VMI_FAILURE) 753 | { 754 | eprintf("Fail to get vcpu registers\n"); 755 | return 1; 756 | } 757 | break; 758 | } 759 | } 760 | if (!found) 761 | { 762 | eprintf("Cannot find CR3 !\n"); 763 | return 1; 764 | } 765 | 766 | int arch = r_sys_arch_id (dbg->arch); 767 | int bits = dbg->anal->bits; 768 | 769 | switch (arch) { 770 | case R_SYS_ARCH_X86: 771 | switch (bits) { 772 | case 32: 773 | memcpy(buf , &(regs.x86.rax), sizeof(uint32_t)); 774 | memcpy(buf + 4 , &(regs.x86.rcx), sizeof(uint32_t)); 775 | memcpy(buf + 8 , &(regs.x86.rdx), sizeof(uint32_t)); 776 | memcpy(buf + 12 , &(regs.x86.rbx), sizeof(uint32_t)); 777 | memcpy(buf + 16 , &(regs.x86.rsp), sizeof(uint32_t)); 778 | memcpy(buf + 20 , &(regs.x86.rbp), sizeof(uint32_t)); 779 | memcpy(buf + 24 , &(regs.x86.rsi), sizeof(uint32_t)); 780 | memcpy(buf + 28 , &(regs.x86.rdi), sizeof(uint32_t)); 781 | memcpy(buf + 32 , &(regs.x86.rip), sizeof(uint32_t)); 782 | break; 783 | case 64: 784 | memcpy(buf , &(regs.x86.rax), sizeof(regs.x86.rax)); 785 | memcpy(buf + 8 , &(regs.x86.rbx), sizeof(regs.x86.rbx)); 786 | memcpy(buf + 16 , &(regs.x86.rcx), sizeof(regs.x86.rcx)); 787 | memcpy(buf + 24 , &(regs.x86.rdx), sizeof(regs.x86.rdx)); 788 | memcpy(buf + 32 , &(regs.x86.rsi), sizeof(regs.x86.rsi)); 789 | memcpy(buf + 40 , &(regs.x86.rdi), sizeof(regs.x86.rdi)); 790 | memcpy(buf + 48 , &(regs.x86.rbp), sizeof(regs.x86.rbp)); 791 | memcpy(buf + 56 , &(regs.x86.rsp), sizeof(regs.x86.rsp)); 792 | memcpy(buf + 64 , &(regs.x86.r8), sizeof(regs.x86.r8)); 793 | memcpy(buf + 72 , &(regs.x86.r9), sizeof(regs.x86.r9)); 794 | memcpy(buf + 80 , &(regs.x86.r10), sizeof(regs.x86.r10)); 795 | memcpy(buf + 88 , &(regs.x86.r11), sizeof(regs.x86.r11)); 796 | memcpy(buf + 96 , &(regs.x86.r12), sizeof(regs.x86.r12)); 797 | memcpy(buf + 104 , &(regs.x86.r13), sizeof(regs.x86.r13)); 798 | memcpy(buf + 112 , &(regs.x86.r14), sizeof(regs.x86.r14)); 799 | memcpy(buf + 120 , &(regs.x86.r15), sizeof(regs.x86.r15)); 800 | memcpy(buf + 128, &(regs.x86.rip), sizeof(regs.x86.rip)); 801 | break; 802 | } 803 | break; 804 | default: 805 | eprintf("Architecture not supported\n"); 806 | return 1; 807 | } 808 | buf_size = 128 + sizeof(uint64_t); 809 | 810 | return buf_size; 811 | } 812 | 813 | RDebugPlugin r_debug_plugin_vmi = { 814 | .name = "vmi", 815 | .license = "LGPL3", 816 | .arch = "x86", 817 | .bits = R_SYS_BITS_32 | R_SYS_BITS_64, 818 | .canstep = 1, 819 | .info = &__info, 820 | .attach = &__attach, 821 | .detach = &__detach, 822 | .select = &__select, 823 | .threads = &__threads, 824 | .step = &__step, 825 | .cont = &__continue, 826 | .wait = &__wait, 827 | .kill = &__kill, 828 | .frames = &__frames, 829 | .reg_read = &__reg_read, 830 | .reg_profile = (void*) &__reg_profile, 831 | .map_get = &__map_get, 832 | .modules_get = &__modules_get, 833 | .breakpoint = &__breakpoint, 834 | }; 835 | 836 | #ifndef CORELIB 837 | RLibStruct radare_plugin = { 838 | .type = R_LIB_TYPE_DBG, 839 | .data = &r_debug_plugin_vmi, 840 | .version = R2_VERSION 841 | }; 842 | #endif 843 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU AFFERO GENERAL PUBLIC LICENSE 2 | Version 3, 19 November 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU Affero General Public License is a free, copyleft license for 11 | software and other kinds of works, specifically designed to ensure 12 | cooperation with the community in the case of network server software. 13 | 14 | The licenses for most software and other practical works are designed 15 | to take away your freedom to share and change the works. By contrast, 16 | our General Public Licenses are intended to guarantee your freedom to 17 | share and change all versions of a program--to make sure it remains free 18 | software for all its users. 19 | 20 | When we speak of free software, we are referring to freedom, not 21 | price. Our General Public Licenses are designed to make sure that you 22 | have the freedom to distribute copies of free software (and charge for 23 | them if you wish), that you receive source code or can get it if you 24 | want it, that you can change the software or use pieces of it in new 25 | free programs, and that you know you can do these things. 26 | 27 | Developers that use our General Public Licenses protect your rights 28 | with two steps: (1) assert copyright on the software, and (2) offer 29 | you this License which gives you legal permission to copy, distribute 30 | and/or modify the software. 31 | 32 | A secondary benefit of defending all users' freedom is that 33 | improvements made in alternate versions of the program, if they 34 | receive widespread use, become available for other developers to 35 | incorporate. Many developers of free software are heartened and 36 | encouraged by the resulting cooperation. However, in the case of 37 | software used on network servers, this result may fail to come about. 38 | The GNU General Public License permits making a modified version and 39 | letting the public access it on a server without ever releasing its 40 | source code to the public. 41 | 42 | The GNU Affero General Public License is designed specifically to 43 | ensure that, in such cases, the modified source code becomes available 44 | to the community. It requires the operator of a network server to 45 | provide the source code of the modified version running there to the 46 | users of that server. Therefore, public use of a modified version, on 47 | a publicly accessible server, gives the public access to the source 48 | code of the modified version. 49 | 50 | An older license, called the Affero General Public License and 51 | published by Affero, was designed to accomplish similar goals. This is 52 | a different license, not a version of the Affero GPL, but Affero has 53 | released a new version of the Affero GPL which permits relicensing under 54 | this license. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | TERMS AND CONDITIONS 60 | 61 | 0. Definitions. 62 | 63 | "This License" refers to version 3 of the GNU Affero General Public License. 64 | 65 | "Copyright" also means copyright-like laws that apply to other kinds of 66 | works, such as semiconductor masks. 67 | 68 | "The Program" refers to any copyrightable work licensed under this 69 | License. Each licensee is addressed as "you". "Licensees" and 70 | "recipients" may be individuals or organizations. 71 | 72 | To "modify" a work means to copy from or adapt all or part of the work 73 | in a fashion requiring copyright permission, other than the making of an 74 | exact copy. The resulting work is called a "modified version" of the 75 | earlier work or a work "based on" the earlier work. 76 | 77 | A "covered work" means either the unmodified Program or a work based 78 | on the Program. 79 | 80 | To "propagate" a work means to do anything with it that, without 81 | permission, would make you directly or secondarily liable for 82 | infringement under applicable copyright law, except executing it on a 83 | computer or modifying a private copy. Propagation includes copying, 84 | distribution (with or without modification), making available to the 85 | public, and in some countries other activities as well. 86 | 87 | To "convey" a work means any kind of propagation that enables other 88 | parties to make or receive copies. Mere interaction with a user through 89 | a computer network, with no transfer of a copy, is not conveying. 90 | 91 | An interactive user interface displays "Appropriate Legal Notices" 92 | to the extent that it includes a convenient and prominently visible 93 | feature that (1) displays an appropriate copyright notice, and (2) 94 | tells the user that there is no warranty for the work (except to the 95 | extent that warranties are provided), that licensees may convey the 96 | work under this License, and how to view a copy of this License. If 97 | the interface presents a list of user commands or options, such as a 98 | menu, a prominent item in the list meets this criterion. 99 | 100 | 1. Source Code. 101 | 102 | The "source code" for a work means the preferred form of the work 103 | for making modifications to it. "Object code" means any non-source 104 | form of a work. 105 | 106 | A "Standard Interface" means an interface that either is an official 107 | standard defined by a recognized standards body, or, in the case of 108 | interfaces specified for a particular programming language, one that 109 | is widely used among developers working in that language. 110 | 111 | The "System Libraries" of an executable work include anything, other 112 | than the work as a whole, that (a) is included in the normal form of 113 | packaging a Major Component, but which is not part of that Major 114 | Component, and (b) serves only to enable use of the work with that 115 | Major Component, or to implement a Standard Interface for which an 116 | implementation is available to the public in source code form. A 117 | "Major Component", in this context, means a major essential component 118 | (kernel, window system, and so on) of the specific operating system 119 | (if any) on which the executable work runs, or a compiler used to 120 | produce the work, or an object code interpreter used to run it. 121 | 122 | The "Corresponding Source" for a work in object code form means all 123 | the source code needed to generate, install, and (for an executable 124 | work) run the object code and to modify the work, including scripts to 125 | control those activities. However, it does not include the work's 126 | System Libraries, or general-purpose tools or generally available free 127 | programs which are used unmodified in performing those activities but 128 | which are not part of the work. For example, Corresponding Source 129 | includes interface definition files associated with source files for 130 | the work, and the source code for shared libraries and dynamically 131 | linked subprograms that the work is specifically designed to require, 132 | such as by intimate data communication or control flow between those 133 | subprograms and other parts of the work. 134 | 135 | The Corresponding Source need not include anything that users 136 | can regenerate automatically from other parts of the Corresponding 137 | Source. 138 | 139 | The Corresponding Source for a work in source code form is that 140 | same work. 141 | 142 | 2. Basic Permissions. 143 | 144 | All rights granted under this License are granted for the term of 145 | copyright on the Program, and are irrevocable provided the stated 146 | conditions are met. This License explicitly affirms your unlimited 147 | permission to run the unmodified Program. The output from running a 148 | covered work is covered by this License only if the output, given its 149 | content, constitutes a covered work. This License acknowledges your 150 | rights of fair use or other equivalent, as provided by copyright law. 151 | 152 | You may make, run and propagate covered works that you do not 153 | convey, without conditions so long as your license otherwise remains 154 | in force. You may convey covered works to others for the sole purpose 155 | of having them make modifications exclusively for you, or provide you 156 | with facilities for running those works, provided that you comply with 157 | the terms of this License in conveying all material for which you do 158 | not control copyright. Those thus making or running the covered works 159 | for you must do so exclusively on your behalf, under your direction 160 | and control, on terms that prohibit them from making any copies of 161 | your copyrighted material outside their relationship with you. 162 | 163 | Conveying under any other circumstances is permitted solely under 164 | the conditions stated below. Sublicensing is not allowed; section 10 165 | makes it unnecessary. 166 | 167 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 168 | 169 | No covered work shall be deemed part of an effective technological 170 | measure under any applicable law fulfilling obligations under article 171 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 172 | similar laws prohibiting or restricting circumvention of such 173 | measures. 174 | 175 | When you convey a covered work, you waive any legal power to forbid 176 | circumvention of technological measures to the extent such circumvention 177 | is effected by exercising rights under this License with respect to 178 | the covered work, and you disclaim any intention to limit operation or 179 | modification of the work as a means of enforcing, against the work's 180 | users, your or third parties' legal rights to forbid circumvention of 181 | technological measures. 182 | 183 | 4. Conveying Verbatim Copies. 184 | 185 | You may convey verbatim copies of the Program's source code as you 186 | receive it, in any medium, provided that you conspicuously and 187 | appropriately publish on each copy an appropriate copyright notice; 188 | keep intact all notices stating that this License and any 189 | non-permissive terms added in accord with section 7 apply to the code; 190 | keep intact all notices of the absence of any warranty; and give all 191 | recipients a copy of this License along with the Program. 192 | 193 | You may charge any price or no price for each copy that you convey, 194 | and you may offer support or warranty protection for a fee. 195 | 196 | 5. Conveying Modified Source Versions. 197 | 198 | You may convey a work based on the Program, or the modifications to 199 | produce it from the Program, in the form of source code under the 200 | terms of section 4, provided that you also meet all of these conditions: 201 | 202 | a) The work must carry prominent notices stating that you modified 203 | it, and giving a relevant date. 204 | 205 | b) The work must carry prominent notices stating that it is 206 | released under this License and any conditions added under section 207 | 7. This requirement modifies the requirement in section 4 to 208 | "keep intact all notices". 209 | 210 | c) You must license the entire work, as a whole, under this 211 | License to anyone who comes into possession of a copy. This 212 | License will therefore apply, along with any applicable section 7 213 | additional terms, to the whole of the work, and all its parts, 214 | regardless of how they are packaged. This License gives no 215 | permission to license the work in any other way, but it does not 216 | invalidate such permission if you have separately received it. 217 | 218 | d) If the work has interactive user interfaces, each must display 219 | Appropriate Legal Notices; however, if the Program has interactive 220 | interfaces that do not display Appropriate Legal Notices, your 221 | work need not make them do so. 222 | 223 | A compilation of a covered work with other separate and independent 224 | works, which are not by their nature extensions of the covered work, 225 | and which are not combined with it such as to form a larger program, 226 | in or on a volume of a storage or distribution medium, is called an 227 | "aggregate" if the compilation and its resulting copyright are not 228 | used to limit the access or legal rights of the compilation's users 229 | beyond what the individual works permit. Inclusion of a covered work 230 | in an aggregate does not cause this License to apply to the other 231 | parts of the aggregate. 232 | 233 | 6. Conveying Non-Source Forms. 234 | 235 | You may convey a covered work in object code form under the terms 236 | of sections 4 and 5, provided that you also convey the 237 | machine-readable Corresponding Source under the terms of this License, 238 | in one of these ways: 239 | 240 | a) Convey the object code in, or embodied in, a physical product 241 | (including a physical distribution medium), accompanied by the 242 | Corresponding Source fixed on a durable physical medium 243 | customarily used for software interchange. 244 | 245 | b) Convey the object code in, or embodied in, a physical product 246 | (including a physical distribution medium), accompanied by a 247 | written offer, valid for at least three years and valid for as 248 | long as you offer spare parts or customer support for that product 249 | model, to give anyone who possesses the object code either (1) a 250 | copy of the Corresponding Source for all the software in the 251 | product that is covered by this License, on a durable physical 252 | medium customarily used for software interchange, for a price no 253 | more than your reasonable cost of physically performing this 254 | conveying of source, or (2) access to copy the 255 | Corresponding Source from a network server at no charge. 256 | 257 | c) Convey individual copies of the object code with a copy of the 258 | written offer to provide the Corresponding Source. This 259 | alternative is allowed only occasionally and noncommercially, and 260 | only if you received the object code with such an offer, in accord 261 | with subsection 6b. 262 | 263 | d) Convey the object code by offering access from a designated 264 | place (gratis or for a charge), and offer equivalent access to the 265 | Corresponding Source in the same way through the same place at no 266 | further charge. You need not require recipients to copy the 267 | Corresponding Source along with the object code. If the place to 268 | copy the object code is a network server, the Corresponding Source 269 | may be on a different server (operated by you or a third party) 270 | that supports equivalent copying facilities, provided you maintain 271 | clear directions next to the object code saying where to find the 272 | Corresponding Source. Regardless of what server hosts the 273 | Corresponding Source, you remain obligated to ensure that it is 274 | available for as long as needed to satisfy these requirements. 275 | 276 | e) Convey the object code using peer-to-peer transmission, provided 277 | you inform other peers where the object code and Corresponding 278 | Source of the work are being offered to the general public at no 279 | charge under subsection 6d. 280 | 281 | A separable portion of the object code, whose source code is excluded 282 | from the Corresponding Source as a System Library, need not be 283 | included in conveying the object code work. 284 | 285 | A "User Product" is either (1) a "consumer product", which means any 286 | tangible personal property which is normally used for personal, family, 287 | or household purposes, or (2) anything designed or sold for incorporation 288 | into a dwelling. In determining whether a product is a consumer product, 289 | doubtful cases shall be resolved in favor of coverage. For a particular 290 | product received by a particular user, "normally used" refers to a 291 | typical or common use of that class of product, regardless of the status 292 | of the particular user or of the way in which the particular user 293 | actually uses, or expects or is expected to use, the product. A product 294 | is a consumer product regardless of whether the product has substantial 295 | commercial, industrial or non-consumer uses, unless such uses represent 296 | the only significant mode of use of the product. 297 | 298 | "Installation Information" for a User Product means any methods, 299 | procedures, authorization keys, or other information required to install 300 | and execute modified versions of a covered work in that User Product from 301 | a modified version of its Corresponding Source. The information must 302 | suffice to ensure that the continued functioning of the modified object 303 | code is in no case prevented or interfered with solely because 304 | modification has been made. 305 | 306 | If you convey an object code work under this section in, or with, or 307 | specifically for use in, a User Product, and the conveying occurs as 308 | part of a transaction in which the right of possession and use of the 309 | User Product is transferred to the recipient in perpetuity or for a 310 | fixed term (regardless of how the transaction is characterized), the 311 | Corresponding Source conveyed under this section must be accompanied 312 | by the Installation Information. But this requirement does not apply 313 | if neither you nor any third party retains the ability to install 314 | modified object code on the User Product (for example, the work has 315 | been installed in ROM). 316 | 317 | The requirement to provide Installation Information does not include a 318 | requirement to continue to provide support service, warranty, or updates 319 | for a work that has been modified or installed by the recipient, or for 320 | the User Product in which it has been modified or installed. Access to a 321 | network may be denied when the modification itself materially and 322 | adversely affects the operation of the network or violates the rules and 323 | protocols for communication across the network. 324 | 325 | Corresponding Source conveyed, and Installation Information provided, 326 | in accord with this section must be in a format that is publicly 327 | documented (and with an implementation available to the public in 328 | source code form), and must require no special password or key for 329 | unpacking, reading or copying. 330 | 331 | 7. Additional Terms. 332 | 333 | "Additional permissions" are terms that supplement the terms of this 334 | License by making exceptions from one or more of its conditions. 335 | Additional permissions that are applicable to the entire Program shall 336 | be treated as though they were included in this License, to the extent 337 | that they are valid under applicable law. If additional permissions 338 | apply only to part of the Program, that part may be used separately 339 | under those permissions, but the entire Program remains governed by 340 | this License without regard to the additional permissions. 341 | 342 | When you convey a copy of a covered work, you may at your option 343 | remove any additional permissions from that copy, or from any part of 344 | it. (Additional permissions may be written to require their own 345 | removal in certain cases when you modify the work.) You may place 346 | additional permissions on material, added by you to a covered work, 347 | for which you have or can give appropriate copyright permission. 348 | 349 | Notwithstanding any other provision of this License, for material you 350 | add to a covered work, you may (if authorized by the copyright holders of 351 | that material) supplement the terms of this License with terms: 352 | 353 | a) Disclaiming warranty or limiting liability differently from the 354 | terms of sections 15 and 16 of this License; or 355 | 356 | b) Requiring preservation of specified reasonable legal notices or 357 | author attributions in that material or in the Appropriate Legal 358 | Notices displayed by works containing it; or 359 | 360 | c) Prohibiting misrepresentation of the origin of that material, or 361 | requiring that modified versions of such material be marked in 362 | reasonable ways as different from the original version; or 363 | 364 | d) Limiting the use for publicity purposes of names of licensors or 365 | authors of the material; or 366 | 367 | e) Declining to grant rights under trademark law for use of some 368 | trade names, trademarks, or service marks; or 369 | 370 | f) Requiring indemnification of licensors and authors of that 371 | material by anyone who conveys the material (or modified versions of 372 | it) with contractual assumptions of liability to the recipient, for 373 | any liability that these contractual assumptions directly impose on 374 | those licensors and authors. 375 | 376 | All other non-permissive additional terms are considered "further 377 | restrictions" within the meaning of section 10. If the Program as you 378 | received it, or any part of it, contains a notice stating that it is 379 | governed by this License along with a term that is a further 380 | restriction, you may remove that term. If a license document contains 381 | a further restriction but permits relicensing or conveying under this 382 | License, you may add to a covered work material governed by the terms 383 | of that license document, provided that the further restriction does 384 | not survive such relicensing or conveying. 385 | 386 | If you add terms to a covered work in accord with this section, you 387 | must place, in the relevant source files, a statement of the 388 | additional terms that apply to those files, or a notice indicating 389 | where to find the applicable terms. 390 | 391 | Additional terms, permissive or non-permissive, may be stated in the 392 | form of a separately written license, or stated as exceptions; 393 | the above requirements apply either way. 394 | 395 | 8. Termination. 396 | 397 | You may not propagate or modify a covered work except as expressly 398 | provided under this License. Any attempt otherwise to propagate or 399 | modify it is void, and will automatically terminate your rights under 400 | this License (including any patent licenses granted under the third 401 | paragraph of section 11). 402 | 403 | However, if you cease all violation of this License, then your 404 | license from a particular copyright holder is reinstated (a) 405 | provisionally, unless and until the copyright holder explicitly and 406 | finally terminates your license, and (b) permanently, if the copyright 407 | holder fails to notify you of the violation by some reasonable means 408 | prior to 60 days after the cessation. 409 | 410 | Moreover, your license from a particular copyright holder is 411 | reinstated permanently if the copyright holder notifies you of the 412 | violation by some reasonable means, this is the first time you have 413 | received notice of violation of this License (for any work) from that 414 | copyright holder, and you cure the violation prior to 30 days after 415 | your receipt of the notice. 416 | 417 | Termination of your rights under this section does not terminate the 418 | licenses of parties who have received copies or rights from you under 419 | this License. If your rights have been terminated and not permanently 420 | reinstated, you do not qualify to receive new licenses for the same 421 | material under section 10. 422 | 423 | 9. Acceptance Not Required for Having Copies. 424 | 425 | You are not required to accept this License in order to receive or 426 | run a copy of the Program. Ancillary propagation of a covered work 427 | occurring solely as a consequence of using peer-to-peer transmission 428 | to receive a copy likewise does not require acceptance. However, 429 | nothing other than this License grants you permission to propagate or 430 | modify any covered work. These actions infringe copyright if you do 431 | not accept this License. Therefore, by modifying or propagating a 432 | covered work, you indicate your acceptance of this License to do so. 433 | 434 | 10. Automatic Licensing of Downstream Recipients. 435 | 436 | Each time you convey a covered work, the recipient automatically 437 | receives a license from the original licensors, to run, modify and 438 | propagate that work, subject to this License. You are not responsible 439 | for enforcing compliance by third parties with this License. 440 | 441 | An "entity transaction" is a transaction transferring control of an 442 | organization, or substantially all assets of one, or subdividing an 443 | organization, or merging organizations. If propagation of a covered 444 | work results from an entity transaction, each party to that 445 | transaction who receives a copy of the work also receives whatever 446 | licenses to the work the party's predecessor in interest had or could 447 | give under the previous paragraph, plus a right to possession of the 448 | Corresponding Source of the work from the predecessor in interest, if 449 | the predecessor has it or can get it with reasonable efforts. 450 | 451 | You may not impose any further restrictions on the exercise of the 452 | rights granted or affirmed under this License. For example, you may 453 | not impose a license fee, royalty, or other charge for exercise of 454 | rights granted under this License, and you may not initiate litigation 455 | (including a cross-claim or counterclaim in a lawsuit) alleging that 456 | any patent claim is infringed by making, using, selling, offering for 457 | sale, or importing the Program or any portion of it. 458 | 459 | 11. Patents. 460 | 461 | A "contributor" is a copyright holder who authorizes use under this 462 | License of the Program or a work on which the Program is based. The 463 | work thus licensed is called the contributor's "contributor version". 464 | 465 | A contributor's "essential patent claims" are all patent claims 466 | owned or controlled by the contributor, whether already acquired or 467 | hereafter acquired, that would be infringed by some manner, permitted 468 | by this License, of making, using, or selling its contributor version, 469 | but do not include claims that would be infringed only as a 470 | consequence of further modification of the contributor version. For 471 | purposes of this definition, "control" includes the right to grant 472 | patent sublicenses in a manner consistent with the requirements of 473 | this License. 474 | 475 | Each contributor grants you a non-exclusive, worldwide, royalty-free 476 | patent license under the contributor's essential patent claims, to 477 | make, use, sell, offer for sale, import and otherwise run, modify and 478 | propagate the contents of its contributor version. 479 | 480 | In the following three paragraphs, a "patent license" is any express 481 | agreement or commitment, however denominated, not to enforce a patent 482 | (such as an express permission to practice a patent or covenant not to 483 | sue for patent infringement). To "grant" such a patent license to a 484 | party means to make such an agreement or commitment not to enforce a 485 | patent against the party. 486 | 487 | If you convey a covered work, knowingly relying on a patent license, 488 | and the Corresponding Source of the work is not available for anyone 489 | to copy, free of charge and under the terms of this License, through a 490 | publicly available network server or other readily accessible means, 491 | then you must either (1) cause the Corresponding Source to be so 492 | available, or (2) arrange to deprive yourself of the benefit of the 493 | patent license for this particular work, or (3) arrange, in a manner 494 | consistent with the requirements of this License, to extend the patent 495 | license to downstream recipients. "Knowingly relying" means you have 496 | actual knowledge that, but for the patent license, your conveying the 497 | covered work in a country, or your recipient's use of the covered work 498 | in a country, would infringe one or more identifiable patents in that 499 | country that you have reason to believe are valid. 500 | 501 | If, pursuant to or in connection with a single transaction or 502 | arrangement, you convey, or propagate by procuring conveyance of, a 503 | covered work, and grant a patent license to some of the parties 504 | receiving the covered work authorizing them to use, propagate, modify 505 | or convey a specific copy of the covered work, then the patent license 506 | you grant is automatically extended to all recipients of the covered 507 | work and works based on it. 508 | 509 | A patent license is "discriminatory" if it does not include within 510 | the scope of its coverage, prohibits the exercise of, or is 511 | conditioned on the non-exercise of one or more of the rights that are 512 | specifically granted under this License. You may not convey a covered 513 | work if you are a party to an arrangement with a third party that is 514 | in the business of distributing software, under which you make payment 515 | to the third party based on the extent of your activity of conveying 516 | the work, and under which the third party grants, to any of the 517 | parties who would receive the covered work from you, a discriminatory 518 | patent license (a) in connection with copies of the covered work 519 | conveyed by you (or copies made from those copies), or (b) primarily 520 | for and in connection with specific products or compilations that 521 | contain the covered work, unless you entered into that arrangement, 522 | or that patent license was granted, prior to 28 March 2007. 523 | 524 | Nothing in this License shall be construed as excluding or limiting 525 | any implied license or other defenses to infringement that may 526 | otherwise be available to you under applicable patent law. 527 | 528 | 12. No Surrender of Others' Freedom. 529 | 530 | If conditions are imposed on you (whether by court order, agreement or 531 | otherwise) that contradict the conditions of this License, they do not 532 | excuse you from the conditions of this License. If you cannot convey a 533 | covered work so as to satisfy simultaneously your obligations under this 534 | License and any other pertinent obligations, then as a consequence you may 535 | not convey it at all. For example, if you agree to terms that obligate you 536 | to collect a royalty for further conveying from those to whom you convey 537 | the Program, the only way you could satisfy both those terms and this 538 | License would be to refrain entirely from conveying the Program. 539 | 540 | 13. Remote Network Interaction; Use with the GNU General Public License. 541 | 542 | Notwithstanding any other provision of this License, if you modify the 543 | Program, your modified version must prominently offer all users 544 | interacting with it remotely through a computer network (if your version 545 | supports such interaction) an opportunity to receive the Corresponding 546 | Source of your version by providing access to the Corresponding Source 547 | from a network server at no charge, through some standard or customary 548 | means of facilitating copying of software. This Corresponding Source 549 | shall include the Corresponding Source for any work covered by version 3 550 | of the GNU General Public License that is incorporated pursuant to the 551 | following paragraph. 552 | 553 | Notwithstanding any other provision of this License, you have 554 | permission to link or combine any covered work with a work licensed 555 | under version 3 of the GNU General Public License into a single 556 | combined work, and to convey the resulting work. The terms of this 557 | License will continue to apply to the part which is the covered work, 558 | but the work with which it is combined will remain governed by version 559 | 3 of the GNU General Public License. 560 | 561 | 14. Revised Versions of this License. 562 | 563 | The Free Software Foundation may publish revised and/or new versions of 564 | the GNU Affero General Public License from time to time. Such new versions 565 | will be similar in spirit to the present version, but may differ in detail to 566 | address new problems or concerns. 567 | 568 | Each version is given a distinguishing version number. If the 569 | Program specifies that a certain numbered version of the GNU Affero General 570 | Public License "or any later version" applies to it, you have the 571 | option of following the terms and conditions either of that numbered 572 | version or of any later version published by the Free Software 573 | Foundation. If the Program does not specify a version number of the 574 | GNU Affero General Public License, you may choose any version ever published 575 | by the Free Software Foundation. 576 | 577 | If the Program specifies that a proxy can decide which future 578 | versions of the GNU Affero General Public License can be used, that proxy's 579 | public statement of acceptance of a version permanently authorizes you 580 | to choose that version for the Program. 581 | 582 | Later license versions may give you additional or different 583 | permissions. However, no additional obligations are imposed on any 584 | author or copyright holder as a result of your choosing to follow a 585 | later version. 586 | 587 | 15. Disclaimer of Warranty. 588 | 589 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 590 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 591 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 592 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 593 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 594 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 595 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 596 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 597 | 598 | 16. Limitation of Liability. 599 | 600 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 601 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 602 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 603 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 604 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 605 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 606 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 607 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 608 | SUCH DAMAGES. 609 | 610 | 17. Interpretation of Sections 15 and 16. 611 | 612 | If the disclaimer of warranty and limitation of liability provided 613 | above cannot be given local legal effect according to their terms, 614 | reviewing courts shall apply local law that most closely approximates 615 | an absolute waiver of all civil liability in connection with the 616 | Program, unless a warranty or assumption of liability accompanies a 617 | copy of the Program in return for a fee. 618 | 619 | END OF TERMS AND CONDITIONS 620 | 621 | How to Apply These Terms to Your New Programs 622 | 623 | If you develop a new program, and you want it to be of the greatest 624 | possible use to the public, the best way to achieve this is to make it 625 | free software which everyone can redistribute and change under these terms. 626 | 627 | To do so, attach the following notices to the program. It is safest 628 | to attach them to the start of each source file to most effectively 629 | state the exclusion of warranty; and each file should have at least 630 | the "copyright" line and a pointer to where the full notice is found. 631 | 632 | 633 | Copyright (C) 634 | 635 | This program is free software: you can redistribute it and/or modify 636 | it under the terms of the GNU Affero General Public License as published 637 | by the Free Software Foundation, either version 3 of the License, or 638 | (at your option) any later version. 639 | 640 | This program is distributed in the hope that it will be useful, 641 | but WITHOUT ANY WARRANTY; without even the implied warranty of 642 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 643 | GNU Affero General Public License for more details. 644 | 645 | You should have received a copy of the GNU Affero General Public License 646 | along with this program. If not, see . 647 | 648 | Also add information on how to contact you by electronic and paper mail. 649 | 650 | If your software can interact with users remotely through a computer 651 | network, you should also make sure that it provides a way for users to 652 | get its source. For example, if your program is a web application, its 653 | interface could display a "Source" link that leads users to an archive 654 | of the code. There are many ways you could offer source, and different 655 | solutions will be better for different programs; see section 13 for the 656 | specific requirements. 657 | 658 | You should also get your employer (if you work as a programmer) or school, 659 | if any, to sign a "copyright disclaimer" for the program, if necessary. 660 | For more information on this, and how to apply and follow the GNU AGPL, see 661 | . 662 | --------------------------------------------------------------------------------