Skip to content

Commit

Permalink
MachMemoryKit: Updated package
Browse files Browse the repository at this point in the history
  • Loading branch information
pvieito committed Apr 1, 2024
1 parent ecd5ca3 commit 5ab7e1e
Show file tree
Hide file tree
Showing 14 changed files with 125 additions and 206 deletions.
24 changes: 0 additions & 24 deletions MachAttach/Info.plist

This file was deleted.

26 changes: 0 additions & 26 deletions MachKit/Info.plist

This file was deleted.

37 changes: 0 additions & 37 deletions MachKit/PID+ProcessName.swift

This file was deleted.

2 changes: 1 addition & 1 deletion MachKit/MachError.swift → MachMemoryKit/MachError.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// MachError.swift
// MachKit
// MachMemoryKit
//
// Created by Pedro José Pereira Vieito on 7/5/17.
// Copyright © 2017 Pedro José Pereira Vieito. All rights reserved.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// MachProcess.swift
// MachKit
// MachMemoryKit
//
// Created by Pedro José Pereira Vieito on 6/5/17.
// Copyright © 2017 Pedro José Pereira Vieito. All rights reserved.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// MachVirtualMemory.swift
// MachKit
// MachMemoryKit
//
// Created by Pedro José Pereira Vieito on 7/5/17.
// Copyright © 2017 Pedro José Pereira Vieito. All rights reserved.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// MachVirtualMemoryPatch.swift
// MachKit
// MachMemoryKit
//
// Created by Pedro José Pereira Vieito on 7/5/17.
// Copyright © 2017 Pedro José Pereira Vieito. All rights reserved.
Expand Down Expand Up @@ -32,7 +32,7 @@ extension MachVirtualMemory {
/// Modifies the specified expected bytes with some patched bytes at the patch address.
///
/// This function will check if the memory at the patch address is the expected one and then
/// wil try to modify it with the specified patch.
/// will try to modify it with the specified patch.
///
/// Note: the expected memory bytes and the patched memory bytes should be of the same length.
///
Expand All @@ -57,7 +57,7 @@ extension MachVirtualMemory {
/// Modifies the specified expected bytes with some patched bytes at the patch address.
///
/// This function will check if the memory at the patch address is the expected one and then
/// wil try to modify it with the specified patch.
/// will try to modify it with the specified patch.
///
/// Note: the expected memory bytes and the patched memory bytes should be of the same length.
///
Expand Down
22 changes: 22 additions & 0 deletions MachMemoryKit/PID+ProcessName.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// PID+ProcessName.swift
// MachMemoryKit
//
// Created by Pedro José Pereira Vieito on 7/5/17.
// Copyright © 2017 Pedro José Pereira Vieito. All rights reserved.
//

import Foundation
import FoundationKit

extension pid_t {
internal init?(processName: String) {
guard let process = try? Process(executableName: "pgrep", arguments: ["-n", "-i", "-x", processName]),
let output = try? process.runAndGetOutputString(),
let output = output.trimmingWhitespacesAndNewlines().components(separatedBy: .newlines).first,
let pid = pid_t(output) else {
return nil
}
self = pid
}
}
14 changes: 0 additions & 14 deletions MachMemoryTool/Info.plist

This file was deleted.

68 changes: 68 additions & 0 deletions MachMemoryTool/MachMemoryTool.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
//
// MachMemoryTool.swift
// MachMemoryTool
// Tool to patch the memory of a running process.
//
// Created by Pedro José Pereira Vieito on 7/5/17.
// Copyright © 2017 Pedro José Pereira Vieito. All rights reserved.
//

import Foundation
import FoundationKit
import MachMemoryKit
import ArgumentParser
import LoggerKit

@main
struct MachMemoryTool: ParsableCommand {
static var configuration: CommandConfiguration {
return CommandConfiguration(commandName: String(describing: Self.self))
}

@Option(name: .shortAndLong, help: "Input item.")
var input: String

@Option(name: .shortAndLong, help: "Memory offset to read (hex).")
var offset: String

@Option(name: [.customShort("z"), .long], help: "Memory size to read.")
var size: Int

@Flag(name: .shortAndLong, help: "Verbose mode.")
var verbose: Bool = false

func run() throws {
Logger.logMode = .commandLine
Logger.logLevel = verbose ? .debug : .info

do {
var process: MachProcess
if input == "-" {
try process = MachProcess(pid: ProcessInfo.processInfo.processIdentifier)
}
else if let pid = Int(input) {
try process = MachProcess(pid: pid)
}
else {
try process = MachProcess(processName: input)
}

Logger.log(important: "PID: \(process.pid)")

Logger.log(info: "ASLR Offset: \(process.memory.aslrOffset.hexString)")
Logger.log(info: "Base Address: \(process.memory.baseAddress.hexString)")

guard let offsetAddress = MachVirtualMemory.Address(hexString: offset) else {
throw NSError(description: "Input memory offset not valid.")
}

let addressRange = MachVirtualMemory.AddressRange(start: process.memory.baseAddress + offsetAddress, size: MachVirtualMemory.Size(size))
let data = try process.memory.readData(on: addressRange)
Logger.log(success: "Memory correctly read: 0x\(data.hexString)")
}
catch {
Logger.log(fatalError: error)
}
}
}

81 changes: 0 additions & 81 deletions MachMemoryTool/main.swift

This file was deleted.

39 changes: 25 additions & 14 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,42 +1,53 @@
// swift-tools-version:5.0
// swift-tools-version:5.9

import PackageDescription

let package = Package(
name: "MachKit",
name: "MachMemoryKit",
platforms: [
.macOS(.v13),
],
products: [
.executable(
name: "MachMemoryTool",
targets: ["MachMemoryTool"]
),
.library(
name: "MachKit",
targets: ["MachKit"]
name: "MachMemoryKit",
targets: ["MachMemoryKit"]
)
],
dependencies: [
.package(url: "git@github.com:pvieito/CommandLineKit.git", .branch("master")),
.package(url: "git@github.com:pvieito/LoggerKit.git", .branch("master")),
.package(url: "git@github.com:pvieito/FoundationKit.git", .branch("master"))
.package(url: "git@github.com:pvieito/LoggerKit.git", branch: "master"),
.package(url: "git@github.com:pvieito/FoundationKit.git", branch: "master"),
.package(url: "https://github.com/apple/swift-argument-parser", from: "1.0.0"),
],
targets: [
.target(
.executableTarget(
name: "MachMemoryTool",
dependencies: ["MachKit", "LoggerKit", "CommandLineKit"],
dependencies: [
"FoundationKit",
"LoggerKit",
"MachMemoryKit",
.product(name: "ArgumentParser", package: "swift-argument-parser"),
],
path: "MachMemoryTool"
),
.target(
name: "MachKit",
dependencies: ["MachAttach", "FoundationKit"],
path: "MachKit"
name: "MachMemoryKit",
dependencies: [
"MachAttach",
"FoundationKit"
],
path: "MachMemoryKit"
),
.target(
name: "MachAttach",
path: "MachAttach"
),
.testTarget(
name: "MachKitTests",
dependencies: ["MachKit"]
name: "MachMemoryKitTests",
dependencies: ["MachMemoryKit", "FoundationKit"]
)
]
)
Loading

0 comments on commit 5ab7e1e

Please sign in to comment.