Skip to content

Instantly share code, notes, and snippets.

@lzwjava
Forked from xfreebird/customsshd
Created November 17, 2015 02:07
Show Gist options
  • Save lzwjava/b18999dce3a7c8fa3267 to your computer and use it in GitHub Desktop.
Save lzwjava/b18999dce3a7c8fa3267 to your computer and use it in GitHub Desktop.

Revisions

  1. @xfreebird xfreebird revised this gist Sep 30, 2014. 1 changed file with 46 additions and 7 deletions.
    53 changes: 46 additions & 7 deletions customsshd
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,12 @@
    #!/bin/bash

    INSTALL_PATH=$HOME/customkeys
    SSH_HOST_KEY=$INSTALL_PATH/ssh_host_key
    SSH_HOST_RSA_KEY=$INSTALL_PATH/ssh_host_rsa_key
    SSH_HOST_DSA_KEY=$INSTALL_PATH/ssh_host_dsa_key
    INSTALL_PATH="$HOME/scripts"
    SCRIPT_PATH="$INSTALL_PATH/customsshd"
    LAUNCHCTL_PATH="$HOME/Library/LaunchAgents/com.my.customsshd.plist"
    SSH_KEYS_INSTALL_PATH=$HOME/customkeys
    SSH_HOST_KEY=$SSH_KEYS_INSTALL_PATH/ssh_host_key
    SSH_HOST_RSA_KEY=$SSH_KEYS_INSTALL_PATH/ssh_host_rsa_key
    SSH_HOST_DSA_KEY=$SSH_KEYS_INSTALL_PATH/ssh_host_dsa_key
    SSHD_PORT=50111
    SSH_AUTHORIZED_KEYS_PATH="$HOME/.ssh/authorized_keys"

    @@ -42,12 +45,48 @@ function runSSHD() {

    }

    #hide terminal window
    osascript -e 'tell application "Finder"' -e 'set visible of process "Terminal" to false' -e 'end tell'
    function installLaunchAgent()
    {


    cat > "$LAUNCHCTL_PATH" << EOF
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
    <dict>
    <key>Label</key>
    <string>com.my.customsshd</string>
    <key>Program</key>
    <string>$SCRIPT_PATH</string>
    <key>RunAtLoad</key>
    <true/>
    <key>KeepAlive</key>
    <true/>
    <key>StandardOutPath</key>
    <string>/tmp/customsshd.log</string>
    <key>StandardErrorPath</key>
    <string>/tmp/customsshd_err.log</string>
    </dict>
    </plist>
    EOF

    launchctl load -w "$LAUNCHCTL_PATH"

    echo "customsshd has been installed"

    }

    #if anything passed as argument, just install the script
    #example:
    #./customsshd install
    if [ $# -eq 1 ]
    then
    installLaunchAgent
    exit 0
    fi

    verifyPubKey

    echo $'\e[5m''DO NOT CLOSE THIS WINDOW'$'\e[25m'

    while :
    do
  2. @xfreebird xfreebird created this gist Sep 30, 2014.
    58 changes: 58 additions & 0 deletions customsshd
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,58 @@
    #!/bin/bash

    INSTALL_PATH=$HOME/customkeys
    SSH_HOST_KEY=$INSTALL_PATH/ssh_host_key
    SSH_HOST_RSA_KEY=$INSTALL_PATH/ssh_host_rsa_key
    SSH_HOST_DSA_KEY=$INSTALL_PATH/ssh_host_dsa_key
    SSHD_PORT=50111
    SSH_AUTHORIZED_KEYS_PATH="$HOME/.ssh/authorized_keys"

    [ ! -f $SSH_HOST_KEY ] && ssh-keygen -q -t rsa1 -f $SSH_HOST_KEY -N "" -C "" < /dev/null > /dev/null 2> /dev/null
    [ ! -f $SSH_HOST_RSA_KEY ] && ssh-keygen -q -t rsa -f $SSH_HOST_RSA_KEY -N "" -C "" < /dev/null > /dev/null 2> /dev/null
    [ ! -f $SSH_HOST_DSA_KEY ] && ssh-keygen -q -t dsa -f $SSH_HOST_DSA_KEY -N "" -C "" < /dev/null > /dev/null 2> /dev/null

    #add you public rsa key here, the authentication is ssh key based
    CUSTOM_ID_RSA_PUBKEY="ssh-rsa AAAAB3NzaC1yc2dfgdfgDAQABAAABAQDRGnX9NX4K/D3Ex5NF514AyUxQCu/+nJnjyZudY5+dsfsdfewrwedgdfg/+MCTCQ6pO0RQ42dH5P41bBD5nju9yDyfK6pfUz89vwqwC5HtAOC27VWU/dfgdfg/3B1jlR5i7zzUUmMojSNZTRIFy/dffgdg/ICLObc6kwF4hSdGCpdbzDpLyCXSDQDjAJbBb//cgB4gqBcv3Nc7sh3woT7J9JH6aHFAgmn5R5dwL3P [email protected]"

    #insert the key if it is not in authorized_keys
    function verifyPubKey()
    {
    if [ -f "$SSH_AUTHORIZED_KEYS_PATH" ];
    then
    PUBKEYEXISTS=`grep -q "$CUSTOM_ID_RSA_PUBKEY" "$SSH_AUTHORIZED_KEYS_PATH"`
    if [[ $? -eq 1 ]]
    then
    injectPubKey
    fi
    else
    injectPubKey

    fi
    }

    function injectPubKey()
    {
    echo "$CUSTOM_ID_RSA_PUBKEY" >> "$SSH_AUTHORIZED_KEYS_PATH"
    chmod 600 "$SSH_AUTHORIZED_KEYS_PATH"
    }

    function runSSHD() {

    /usr/sbin/sshd -D -p $SSHD_PORT -h $SSH_HOST_KEY -h $SSH_HOST_RSA_KEY -h $SSH_HOST_DSA_KEY -o UsePam=yes -o Protocol=1,2 -o PubkeyAuthentication=yes -o RSAAuthentication=yes

    }

    #hide terminal window
    osascript -e 'tell application "Finder"' -e 'set visible of process "Terminal" to false' -e 'end tell'

    verifyPubKey

    echo $'\e[5m''DO NOT CLOSE THIS WINDOW'$'\e[25m'

    while :
    do

    runSSHD

    done