Skip to content

Instantly share code, notes, and snippets.

@Alkenso
Alkenso / ObserveKEXT+kern_event.c
Created April 29, 2020 18:38
Observe KEXT: kern_event
// Common code
// used in both client and kernel parts.
// Common.h
enum TestKEXTEventType
{
TestKextEventStart = 1,
TestKextEventStop = 2
};
@Alkenso
Alkenso / ObserveKEXT+IOKit.swift
Last active April 29, 2020 18:39
Observe KEXT: IOKit driver
import IOKit
let serviceName = "FSGuardService"
let watcher = IOKitKEXTWatcher(ioServiceName: serviceName)
watcher.updateHandler = {
if $0 != nil {
NSLog("IOKit service loaded: \(serviceName).")
} else {
NSLog("IOKit service unloaded: \(serviceName).")
@Alkenso
Alkenso / ObserveKEXT+polling.swift
Last active April 29, 2020 18:39
Observe KEXT: polling
import IOKit.kext // #import <IOKit/kext/KextManager.h>
func LocateKEXT(_ identifier: String) -> URL? {
KextManagerCreateURLForBundleIdentifier(kCFAllocatorDefault, identifier as CFString)?.takeRetainedValue() as URL?
}
func IsKEXTLoaded(_ identifier: String) -> Bool {
LocateKEXT(identifier) != nil
}
@Alkenso
Alkenso / ObserveKEXT+NSDistributedNotificationCenter.swift
Last active April 29, 2020 18:38
Observe KEXT: NSDistributedNotificationCenter
import Cocoa
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
private var _token: NSObjectProtocol?
func applicationDidFinishLaunching(_ notification: Notification) {
_token = DistributedNotificationCenter.default().addObserver(forName: NSNotification.Name(rawValue: "Loaded Kext Notification"),
object: nil,
@Alkenso
Alkenso / Atomic.swift
Created October 20, 2019 21:34
Swift atomic property using @propertyWrapper
// Requires Swift 5.1 (Xcode 11.1+).
//
// Using pthread_rw_lock allows to minimize system resources
// and prevent creation of DispatchQueue per each @Atomic property.
//
@propertyWrapper
final class Atomic<Value> {
var value: Value