objective-see / DoNotDisturb

Detect Evil Maid Attacks
GNU General Public License v3.0
294 stars 43 forks source link

Sample script to snap a webcam image, then send an email alert with the image #19

Open tbbmz1907 opened 4 years ago

tbbmz1907 commented 4 years ago

This (snapperwmailf.app) is an applescript based app which snaps an image (in this case when the lid of the laptop is opened) and then sends an email alert along with the image. I'm not really a programmer (but have some background), so it took me a while to figure this out. But I hope it helps some other people...

set formattedDate to (do shell script "date +'%m%d%y-%H%M%S'")
set fullPath to "/Users/username/Desktop/ImageSnap/images/" & formattedDate & ".jpg"
set theScript to "/usr/local/bin/imagesnap -w 5 /Users/username/Desktop/ImageSnap/images/" & formattedDate & ".jpg"
do shell script (theScript)
delay 1
set recipientName to "John"
set recipientAddress to "john@doe.com"
set theSubject to "Alert on my Mac!"
set theContent to "Something is going on, maybe you want to check it"
tell application "Mail"
    set theMessage to make new outgoing message with properties {subject:theSubject, content:theContent, visible:true}
    tell theMessage
        make new to recipient with properties {name:recipientName, address:recipientAddress}
        make new attachment with properties {file name:fullPath}
        delay 1
    end tell
    send theMessage
end tell

This script uses: http://iharder.sourceforge.net/current/macosx/imagesnap/ You can find installation instructions and sample usage here: https://eyetea33.wordpress.com/2013/06/11/take-pictures-using-facetime-hd-or-isight-camera-on-mac-wakeup/

You should export the script as an app (using script editor), and then take some steps to make sure the system doesn't ask you for permission to use the camera and mail app each time. Read the comments of JayBrown how to achieve this here https://github.com/rharder/imagesnap/issues/26. But basically,

"Open the Info.plist (visible when you say show package contents of the app), add key LSUIElement with boolean true, change the BundleID, if you want, change the bundle icon"

Then You should also create a self-signed certificate, and then sign the app with the certificate.

  1. creating the certificate: https://developer.apple.com/library/archive/documentation/Security/Conceptual/CodeSigningGuide/Procedures/Procedures.html [under To obtain a self-signed certificate using Certificate Assistant] also https://eclecticlight.co/2019/01/16/code-signing-for-the-concerned-2-creating-a-personal-certificate/
    1. signign the certificate with terminal (when you're in the folder of the app): codesign --sign "OKs certifcate" --force --deep -i "com.apple.ScriptEditor.id.snapperwmailf" snapperwmailf.app
    2. while signing the certificate I encountered an error and found the fix here: https://stackoverflow.com/questions/39652867/code-sign-error-in-macos-high-sierra-xcode-resource-fork-finder-information

The system will ask the permission to use the camera and mail for the first time. But if you did the signing right it shouldn't ask again. Then on DND's preferences, you say execute action and provide: open ~/Desktop/ImageSnap/snapperwmailf.app

JayBrown commented 4 years ago

If you want to be stealthy, it would be prudent to tweak a couple of things: