Last active
February 20, 2016 02:51
-
-
Save twlz0ne/5479944 to your computer and use it in GitHub Desktop.
Securely auto login Mac OS X ## Usage: curl -O https://raw.github.com/gist/5479944/safe-autologin.sh && sh ./safe-autologin.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
# Gavin Brock (http://brock-family.org/gavin/perl) - June 2007 | |
#==============================================================================# | |
use strict; | |
use warnings; | |
use Foundation; | |
#==============================================================================# | |
sub kcpassword_xor { | |
my ($pass) = @_; | |
### The magic 11 bytes - these are just repeated | |
# 0x7D 0x89 0x52 0x23 0xD2 0xBC 0xDD 0xEA 0xA3 0xB9 0x1F | |
my @key = qw( 125 137 82 35 210 188 221 234 163 185 31 ); | |
my $key = pack "C*", @key; | |
my $key_len = length $key; | |
for (my $n=0; $n<length($pass); $n+=$key_len) { | |
substr($pass,$n,$key_len) ^= $key; | |
} | |
return $pass; | |
} | |
#==============================================================================# | |
sub write_pass { | |
my ($enc) = @_; | |
my $file = '/etc/kcpassword'; | |
open(my $kcp, '>', $file) || die; | |
print $kcp $enc; | |
close($kcp); | |
chmod(0600, $file); | |
} | |
#==============================================================================# | |
sub set_loginwindow { | |
my ($user) = @_; | |
my $uid = getpwnam($user) || die; | |
my $file = '/Library/Preferences/com.apple.loginwindow.plist'; | |
$user = NSString->stringWithCString_($user); | |
$uid = NSNumber->numberWithLong_($uid); | |
my $plist = NSMutableDictionary->dictionaryWithContentsOfFile_($file); | |
die unless $plist && $$plist; | |
$plist->setObject_forKey_($user, "lastUserName"); | |
$plist->setObject_forKey_($user, "autoLoginUser"); | |
$plist->setObject_forKey_($uid, "autoLoginUserUID"); | |
$plist->writeToFile_atomically_($file, 0); | |
} | |
#==============================================================================# | |
if ( $#ARGV != 1 ) { | |
printf "Usage: $0 user pass\n"; | |
die; | |
} | |
my $user = $ARGV[0]; | |
my $pass = $ARGV[1]; | |
my $enc = kcpassword_xor($pass); | |
write_pass($enc); | |
set_loginwindow($user); | |
#system('killall','loginwindow'); | |
exit 0; | |
#==============================================================================# |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# @brief enable auto login with automatic screen lock for mac os x | |
# @version 0.1 | |
# @auther gongqijian | |
# @date 2013/04/29 | |
set -e | |
# enable autolgin | |
read -p "auto-login username:" username | |
read -s -p "auto-login user's password:" password | |
echo | |
curl -O https://raw.github.com/gist/5479944/kcpassword.pl | |
sudo -k | |
printf "sudo " | |
sudo perl ./kcpassword.pl $username $password | |
# create plist | |
cat >~/Library/LaunchAgents/com.$username.loginhook.plist<<EOF | |
<?xml version="1.0" encoding="UTF-8"?> | |
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | |
<plist version="1.0"> | |
<dict> | |
<key>AbandonProcessGroup</key> | |
<true/> | |
<key>Label</key> | |
<string>com.$username.loginhook</string> | |
<key>ProgramArguments</key> | |
<array> | |
<string>/System/Library/CoreServices/Menu Extras/User.menu/Contents/Resources/CGSession</string> | |
<string>-suspend</string> | |
</array> | |
<key>RunAtLoad</key> | |
<true/> | |
</dict> | |
</plist> | |
EOF | |
echo "Now, you can reboot your system to have a check." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment