-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMemory.swift
120 lines (99 loc) · 3.56 KB
/
Memory.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
//
// Memory.swift
// elenbot
//
// Created by Jascha Burmeister on 10.02.19.
// Copyright © 2019 Jascha Burmeister. All rights reserved.
//
import Foundation
import AppKit
enum MemoryError: Error {
case readFailed
case readSizeMismatch
case readDeallocateFailed
case allocateFailed
case deallocateFailed
case writeFailed
case protectFailed
}
class Memory {
private let client: Client!
init(client : Client) {
self.client = client
}
deinit {
}
func read<T>(address: UInt64, type: T.Type, count: Int) throws -> Array<T> {
var array: Array<T> = Array<T>()
for i in 0...(count-1) {
try array.append(read(address: address + UInt64(i * MemoryLayout<T>.stride) , type: type))
}
return array
}
func read<T>(address: UInt64, type: T.Type) throws -> T {
var data: vm_offset_t = 0
var dataCnt: mach_msg_type_number_t = 0;
let read_result = vm_read(client.process.remoteTask, UInt(address), UInt(MemoryLayout<T>.size), &data, &dataCnt)
if(read_result != KERN_SUCCESS) {
print("Error reading address: \(read_result)")
throw MemoryError.readFailed
}
let readDataPointer = UnsafeRawPointer(bitPattern: UInt(data))
let value : T = readDataPointer!.assumingMemoryBound(to: T.self).pointee
vm_deallocate(mach_task_self_, UInt(data), vm_size_t(dataCnt))
return value
}
func write(address: UInt64, data: UnsafeRawPointer, size: UInt64) throws {
let task = client.process.remoteTask
let pointerValue = UInt(bitPattern: data)
if(mach_vm_write(task, address, pointerValue, UInt32(size)) != 0) {
throw MemoryError.writeFailed
}
}
func allocate(size: UInt64, protection: vm_prot_t = VM_PROT_WRITE | VM_PROT_READ | VM_PROT_EXECUTE) throws -> UInt64 {
let task = client.process.remoteTask
var allocatedAddress = client.process.baseAddress
if(mach_vm_allocate(task, &allocatedAddress, size, 1) != 0) {
throw MemoryError.allocateFailed
}
if(mach_vm_protect(task, allocatedAddress, size, 0, protection) != 0) {
throw MemoryError.allocateFailed
}
return allocatedAddress
}
func deallocate(address: UInt64, size: UInt64) throws {
let task = client.process.remoteTask
if(mach_vm_deallocate(task, address, size) != 0) {
throw MemoryError.deallocateFailed
}
}
func protect(address: UInt64, size: UInt64, protection: vm_prot_t, maximum: Bool = false) throws {
let task = client.process.remoteTask
let max: UInt32 = maximum ? 1 : 0
if(mach_vm_protect(task, address, size, max, protection) != 0) {
throw MemoryError.protectFailed
}
}
private func getLowerBoundary(address : UInt64 ) -> UInt64 {
if(address % 4096 == 0) {
return address
}else {
return getLowerBoundary(address: address-1)
}
}
private func getUpperBoundary(address : UInt64 ) -> UInt64 {
return getUpperBoundaryRec(address: address + 4097)
}
private func getUpperBoundaryRec(address : UInt64 ) -> UInt64 {
if(address % 4096 == 0) {
return address
}else {
return getUpperBoundaryRec(address: address-1)
}
}
}
extension Data {
public func hex() -> String {
return self.map { String(format: "%02x", $0) }.joined().uppercased()
}
}