1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
extern crate nix;
extern crate libc;
#[macro_use] extern crate log;
extern crate env_logger;
#[cfg(unix)]
use libc::{open, c_uint, ptrace, waitpid, pid_t, c_int, pwrite};
use nix::errno;



#[cfg(target_pointer_width="64")]
pub struct Transaction{
    pub adress : u64,
    pub data : Vec<u8>,
}

#[cfg(target_pointer_width="32")]
pub struct Transaction{
    pub adress : u32,
    pub data : Vec<u8>,
}
#[cfg(unix)]
#[allow(dead_code)]
pub struct Transactions{
    pub transactions : Vec<Transaction>,
    targetpid : u32,
}

#[cfg(unix)]
#[allow(unused_assignments)]
#[allow(unused_variables)]
impl Transactions{
    pub fn make_transactions(&self) -> bool{
        let mut mem = "/proc/".to_string();
        mem.push_str(&self.targetpid.to_string());
        mem.push_str(&"/mem".to_string());
        let mut handle : Option<i32>;
        handle = None;
        unsafe{
            let ptrace_attach : c_uint =  0x10;
            let mut status : c_int = 0;
            let option : c_int = 0;
            ptrace(ptrace_attach, self.targetpid as pid_t);
            debug!("Val of ptrace {}", errno::errno());
            waitpid(self.targetpid as pid_t, &mut status, option);
            debug!("Val of waitpid {}", errno::errno());
            handle = Some(open(mem.as_ptr() as *const i8, 0x2));
            debug!("Val of open {}", errno::errno());
        }
        for transaction in &self.transactions{
                match handle{
                    Some(val) =>
                        unsafe{
                            pwrite(val, transaction.data.as_ptr() as *const libc::c_void,
                               transaction.data.len(), transaction.adress as i64);
                            debug!("Val of pwrite {}", errno::errno());
                            if errno::errno() != 0 {
                                debug!("Failed to write data");
                                return false;
                            }
                        },
                    None => {debug!("failed to write data"); return false;},
                }
        }
        debug!("Successfully written all data");
        return true;
    }
}

#[test]
fn mem_test() {
    env_logger::init().unwrap();
    debug!("Testing now");
    let test = Transaction{
        adress : 0x0,
        data : vec![0,0,0],
    };
    let test2 = Transactions{
        targetpid : 11211,
        transactions : vec![test]
    };
    assert!(test2.make_transactions());
}