Skip to content

Instantly share code, notes, and snippets.

View macshome's full-sized avatar

Josh Wisenbaker macshome

View GitHub Profile
@macshome
macshome / GetProcessList.swift
Last active May 8, 2024 08:48
Swift Playground to get all running processes on macOS
import Darwin
func getProcessList() {
// Requesting the pid of 0 from systcl will return all pids
var mib = [CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0]
var bufferSize = 0
// To find the needed buffer size you call sysctl with a nil results pointer.
// This sets the size of the buffer needed in the bufferSize pointer.
if sysctl(&mib, UInt32(mib.count), nil, &bufferSize, nil, 0) < 0 {
@macshome
macshome / EnvironmentVariables.swift
Created May 2, 2024 12:48
A playground to see different ways to get environment variables in Swift
import Foundation
// A playground to see different ways to get environment variables in Swift
// Foundation is the easiest way using the awesome ProcessInfo class.
// Get all of the environment variables for your running process in a Dictionary.
let foundationEnv = ProcessInfo().environment
print("********** ProcessInfo Environment **********")
@macshome
macshome / IOKit.swift
Last active April 29, 2024 14:42
This playground shows you a few different ways to get device info via IOKit.
// This playground shows you a few different ways to get device info via IOKit.
// If you want to explore IOKit and look for interesting data I would download
// the Additional Developer Tools from Apple and use the IORegistryExplorer app.
// It makes it super easy to poke around in the IOKit planes.
import IOKit
import Foundation
// For convient access we can make a computed getter for the PlatformExpert.
// Traditionally this has been where you go to find all sorts of data about the
@macshome
macshome / MobileGestalt.swift
Created April 24, 2024 17:44
Get crazy and hook MobileGestalt in a Swift Playground!
// Get crazy and hook MobileGestalt in a Swift Playground!
//
// If you are a LONG time Mac developer you know that the Gestalt system
// used to be a way to get info about your Mac. These days it's a private
// thing that Apple locks away and you shouldn't really touch. Most of the info
// that you need can probaby be found in IOKit, but some values, like
// the provisioningUDID are not avaliable any other way.
//
// That said, it's a fun exersize to see how to do some various things in Swift.
// Things like loading a dylib or calling private C functions.
@macshome
macshome / defang.md
Last active April 24, 2024 18:31
How to defang system protections on macOS

How to Defang macOS System Protections

If you want to change things on the root drive of a Mac you will need to take some steps to disable the built in security of the system. Most of these steps are the same regardless if you are on Intel or Apple Silicon. If there is a difference it is noted.

Note that all of these things put a Mac into an unsupported and less secure state.

Make sure you either perform these steps in a VM or that you reset the protections after you are done poking around

Protections and Terms

(This list is not exahustive on the details of each. Check the links at the end for more info.)

@macshome
macshome / TimeInterval+Calendar.swift
Last active February 29, 2024 17:06
A simple extension to TimeInterval that allows you to just use the standard units of time rather than all that 60 * 60 jazz with APIs that use TimeInterval.
import Foundation
extension TimeInterval {
var seconds: TimeInterval { self }
var minutes: TimeInterval { self * 60 }
var hours: TimeInterval { self * 3_600 }
var days: TimeInterval { self * 86_400 }
var weeks: TimeInterval { self * 604_800 }
var months: TimeInterval { self * 2_628_000 }
@macshome
macshome / Activity.swift
Created February 15, 2024 18:49 — forked from zwaldowski/Activity.swift
os_activity_t for Swift 3
//
// Activity.swift
//
// Created by Zachary Waldowski on 8/21/16.
// Copyright © 2016 Zachary Waldowski. Licensed under MIT.
//
import os.activity
private final class LegacyActivityContext {
@macshome
macshome / IsScreenLocked.swift
Created October 30, 2023 19:05
Paste into a Swift playground on macOS to see if your screen is locked
import Cocoa
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
var isScreenLocked: Bool {
guard let wssProperties = CGSessionCopyCurrentDictionary() as? [String : Any], (wssProperties["CGSSessionScreenIsLocked"] != nil) else {
return false
}
return true
@macshome
macshome / Sessions.swift
Created October 30, 2023 18:55
Paste into a macOS Swift Playground to explore SecuritySession and CGSession attributes.
import Cocoa
// Security Session
//
// You can access bits about the security session to get some basic info. You can only get info about your own session.
// Other than the session id, the values are all Bools.
var sessionID = SecuritySessionId()
var sessionAttrs = SessionAttributeBits()
let result = SessionGetInfo(callerSecuritySession,
@macshome
macshome / MetadataSearching.swift
Last active October 30, 2023 18:43
Paste into a Swift playground to find all the executable binaries and shell scripts on your system with blinding speed.
import Foundation
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
// Simple class to search for things with `NSMetadataQuery`.
class Searcher {
// This is just for timing things
let startTime = Date()