<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/macos-code-injection-for-fun-and-no-profit-0c9d9lwbb" -->

---
title: macOS code injection for fun and no profit | daily.dev
description: A hands-on walkthrough of macOS process code injection using Mach APIs. Covers attaching to a running process via task_for_pid(), suspending/resuming it,...
canonical: https://daily.dev/posts/macos-code-injection-for-fun-and-no-profit-0c9d9lwbb
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: macOS code injection for fun and no profit | daily.dev
og:description: A hands-on walkthrough of macOS process code injection using Mach APIs. Covers attaching to a running process via task_for_pid(), suspending/resuming it,...
og:url: https://daily.dev/posts/macos-code-injection-for-fun-and-no-profit-0c9d9lwbb
og:image: https://api.daily.dev/og/posts/0c9D9lWbB.png
og:image:alt: macOS code injection for fun and no profit
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# macOS code injection for fun and no profit

**[Hacker News](https://daily.dev/sources/hn)** · 17 min read · 0 upvotes · 0 comments

## Summary

A hands-on walkthrough of macOS process code injection using Mach APIs. Covers attaching to a running process via task_for_pid(), suspending/resuming it, reading and writing process memory with vm_read_overwrite() and vm_write(), allocating executable memory with vm_allocate(), and patching functions at runtime using a trampoline technique on both x86_64 and ARM64. The -fpatchable-function-entry compiler flag is used to prepend NOP sleds to functions, making room for the 16-byte ARM64 trampoline. Full source code is provided on GitHub.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://mariozechner.at/posts/2024-07-20-macos-code-injection-fun/>

## Similar posts on daily.dev

- [Redefining Go Functions](https://daily.dev/posts/redefining-go-functions-xw2qyqen0) · Lobsters · 2 upvotes · 1 comments
- [Process Explorer for macOS](https://daily.dev/posts/process-explorer-for-macos-5ifesanlg) · Hacker News · 3 upvotes · 1 comments
- [To study how chips really work, MIT researchers built their own operating system](https://daily.dev/posts/to-study-how-chips-really-work-mit-researchers-built-their-own-operating-system-pggnrpyef) · MIT News · 0 upvotes · 0 comments
- [We rebuilt the Linux microVM stack on Apple Silicon](https://daily.dev/posts/we-rebuilt-the-linux-microvm-stack-on-apple-silicon-2hmxgetth) · Encore · 21 upvotes · 2 comments

---

Tags: [#c++](https://daily.dev/tags/c++), [#hardware](https://daily.dev/tags/hardware), [#mac](https://daily.dev/tags/mac)

[View this post on daily.dev](https://daily.dev/posts/macos-code-injection-for-fun-and-no-profit-0c9d9lwbb)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"macOS code injection for fun and no profit","url":"https://daily.dev/posts/macos-code-injection-for-fun-and-no-profit-0c9d9lwbb","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/macos-code-injection-for-fun-and-no-profit-0c9d9lwbb"},"datePublished":"2026-03-08T04:16:45.766Z","dateModified":"2026-10-14T02:25:13.339Z","description":"A hands-on walkthrough of macOS process code injection using Mach APIs. Covers attaching to a running process via task_for_pid(), suspending/resuming it,...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/fba13edd70c54fd859b3983f94e1cbfe?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/fba13edd70c54fd859b3983f94e1cbfe?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"Hacker News","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Hacker News","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/hn","url":"https://daily.dev/sources/hn"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/macos-code-injection-for-fun-and-no-profit-0c9d9lwbb","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"c++,hardware,mac","timeRequired":"PT17M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Hacker News","item":"https://daily.dev/sources/hn"},{"@type":"ListItem","position":3,"name":"macOS code injection for fun and no profit"}]}
```

