Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save viecode09/f0110ade7a1b1054faf72ff433c3e385 to your computer and use it in GitHub Desktop.
Save viecode09/f0110ade7a1b1054faf72ff433c3e385 to your computer and use it in GitHub Desktop.

Revisions

  1. @samhocevar samhocevar revised this gist Jun 28, 2019. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions gistfile1.sh
    Original file line number Diff line number Diff line change
    @@ -11,6 +11,11 @@
    # This script is a cleaned up and improved version of the procedure initially
    # found at https://ghc.haskell.org/trac/ghc/wiki/Building/Windows/SSHD
    #
    # Gotchas:
    # — the log file will be /var/log/msys2_sshd.log
    # — if you get error “sshd: fatal: seteuid XXX : No such device or address”
    # in the logs, try “passwd -R” (with admin privileges)
    #
    # Changelog:
    # 27 Jun 2019 — rename service to msys2_sshd to avoid conflicts with Windows OpenSSH
    # — use mkgroup.exe as suggested in the comments
  2. @samhocevar samhocevar revised this gist Jun 28, 2019. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions gistfile1.sh
    Original file line number Diff line number Diff line change
    @@ -13,7 +13,8 @@
    #
    # Changelog:
    # 27 Jun 2019 — rename service to msys2_sshd to avoid conflicts with Windows OpenSSH
    # — Use mkgroup.exe as suggested in the comments
    # — use mkgroup.exe as suggested in the comments
    # — fix a problem with CRLF and grep
    # 24 Aug 2015 — run server with -e to redirect logs to /var/log/sshd.log
    #

    @@ -69,7 +70,7 @@ fi

    # Add user to the Administrators group if necessary
    admingroup="$(mkgroup -l | awk -F: '{if ($2 == "S-1-5-32-544") print $1;}')"
    if ! (net localgroup "${admingroup}" | grep -q '^'"${PRIV_USER}"'$'); then
    if ! (net localgroup "${admingroup}" | grep -q '^'"${PRIV_USER}"'\>'); then
    if ! net localgroup "${admingroup}" "${PRIV_USER}" //add; then
    echo "ERROR: Unable to add user ${PRIV_USER} to group ${admingroup}"
    exit 1
  3. @samhocevar samhocevar revised this gist Jun 28, 2019. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions gistfile1.sh
    Original file line number Diff line number Diff line change
    @@ -12,6 +12,8 @@
    # found at https://ghc.haskell.org/trac/ghc/wiki/Building/Windows/SSHD
    #
    # Changelog:
    # 27 Jun 2019 — rename service to msys2_sshd to avoid conflicts with Windows OpenSSH
    # — Use mkgroup.exe as suggested in the comments
    # 24 Aug 2015 — run server with -e to redirect logs to /var/log/sshd.log
    #

    @@ -110,6 +112,7 @@ for u in "${PRIV_USER}" "${UNPRIV_USER}"; do
    mkpasswd -l -u "${u}" | sed -e 's/^[^:]*+//' | sed -ne "${SED}" \
    >> /etc/passwd
    done
    mkgroup.exe -l > /etc/group


    #
  4. @samhocevar samhocevar revised this gist Jun 28, 2019. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions gistfile1.sh
    Original file line number Diff line number Diff line change
    @@ -116,13 +116,13 @@ done
    # Finally, register service with cygrunsrv and start it
    #

    cygrunsrv -R sshd || true
    cygrunsrv -I sshd -d "MSYS2 sshd" -p \
    cygrunsrv -R msys2_sshd || true
    cygrunsrv -I msys2_sshd -d "MSYS2 sshd" -p \
    /usr/bin/sshd.exe -a "-D -e" -y tcpip -u "${PRIV_USER}" -w "${tmp_pass}"

    # The SSH service should start automatically when Windows is rebooted. You can
    # manually restart the service by running `net stop sshd` + `net start sshd`
    if ! net start sshd; then
    echo "ERROR: Unable to start sshd service"
    # manually restart the service by running `net stop msys2_sshd` + `net start msys2_sshd`
    if ! net start msys2_sshd; then
    echo "ERROR: Unable to start msys2_sshd service"
    exit 1
    fi
  5. @samhocevar samhocevar revised this gist Aug 25, 2015. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion gistfile1.sh
    Original file line number Diff line number Diff line change
    @@ -11,6 +11,9 @@
    # This script is a cleaned up and improved version of the procedure initially
    # found at https://ghc.haskell.org/trac/ghc/wiki/Building/Windows/SSHD
    #
    # Changelog:
    # 24 Aug 2015 — run server with -e to redirect logs to /var/log/sshd.log
    #

    set -e

    @@ -115,7 +118,7 @@ done

    cygrunsrv -R sshd || true
    cygrunsrv -I sshd -d "MSYS2 sshd" -p \
    /usr/bin/sshd.exe -a -D -y tcpip -u "${PRIV_USER}" -w "${tmp_pass}"
    /usr/bin/sshd.exe -a "-D -e" -y tcpip -u "${PRIV_USER}" -w "${tmp_pass}"

    # The SSH service should start automatically when Windows is rebooted. You can
    # manually restart the service by running `net stop sshd` + `net start sshd`
  6. @samhocevar samhocevar revised this gist Aug 14, 2015. 1 changed file with 2 additions and 5 deletions.
    7 changes: 2 additions & 5 deletions gistfile1.sh
    Original file line number Diff line number Diff line change
    @@ -8,11 +8,8 @@
    # — MSYS2 itself: http://sourceforge.net/projects/msys2/
    # — admin tools: pacman -S openssh cygrunsrv mingw-w64-x86_64-editrights
    #
    # This script is free software. It comes without any warranty, to
    # the extent permitted by applicable law. You can redistribute it
    # and/or modify it under the terms of the Do What the Fuck You Want
    # to Public License, Version 2, as published by the WTFPL Task Force.
    # See http://www.wtfpl.net/ for more details.
    # This script is a cleaned up and improved version of the procedure initially
    # found at https://ghc.haskell.org/trac/ghc/wiki/Building/Windows/SSHD
    #

    set -e
  7. @samhocevar samhocevar revised this gist Aug 14, 2015. No changes.
  8. @samhocevar samhocevar revised this gist Aug 14, 2015. 1 changed file with 17 additions and 2 deletions.
    19 changes: 17 additions & 2 deletions gistfile1.sh
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,19 @@
    #!/bin/sh
    #
    # msys2-sshd-setup.sh — configure sshd on MSYS2 and run it as a Windows service
    #
    # Please report issues and/or improvements to Sam Hocevar <[email protected]>
    #
    # Prerequisites:
    # — MSYS2 itself: http://sourceforge.net/projects/msys2/
    # — admin tools: pacman -S openssh cygrunsrv mingw-w64-x86_64-editrights
    #
    # This script is free software. It comes without any warranty, to
    # the extent permitted by applicable law. You can redistribute it
    # and/or modify it under the terms of the Do What the Fuck You Want
    # to Public License, Version 2, as published by the WTFPL Task Force.
    # See http://www.wtfpl.net/ for more details.
    #

    set -e

    @@ -7,8 +22,8 @@ set -e
    #

    PRIV_USER=sshd_server
    PRIV_NAME="Privileged server"
    UNPRIV_USER=sshd # Do not change this; it is hardcoded inside sshd
    PRIV_NAME="Privileged user for sshd"
    UNPRIV_USER=sshd # DO NOT CHANGE; this username is hardcoded in the openssh code
    UNPRIV_NAME="Privilege separation user for sshd"

    EMPTY_DIR=/var/empty
  9. @samhocevar samhocevar revised this gist Aug 14, 2015. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions gistfile1.sh
    Original file line number Diff line number Diff line change
    @@ -8,6 +8,8 @@ set -e

    PRIV_USER=sshd_server
    PRIV_NAME="Privileged server"
    UNPRIV_USER=sshd # Do not change this; it is hardcoded inside sshd
    UNPRIV_NAME="Privilege separation user for sshd"

    EMPTY_DIR=/var/empty

    @@ -74,8 +76,6 @@ done
    # The unprivileged sshd user (for privilege separation)
    #

    UNPRIV_USER=sshd # This username is hardcoded inside sshd
    UNPRIV_NAME="Privilege separation user for sshd"
    add="$(if ! net user "${UNPRIV_USER}" >/dev/null; then echo "//add"; fi)"
    if ! net user "${UNPRIV_USER}" ${add} //fullname:"${UNPRIV_NAME}" \
    //homedir:"$(cygpath -w ${EMPTY_DIR})" //active:no; then
  10. @samhocevar samhocevar revised this gist Aug 14, 2015. 1 changed file with 33 additions and 18 deletions.
    51 changes: 33 additions & 18 deletions gistfile1.sh
    Original file line number Diff line number Diff line change
    @@ -6,10 +6,8 @@ set -e
    # Configuration
    #

    PRIV_USER=cyg_server
    PRIV_USER=sshd_server
    PRIV_NAME="Privileged server"
    UNPRIV_USER=sshd
    UNPRIV_NAME="User for sshd privsep"

    EMPTY_DIR=/var/empty

    @@ -19,17 +17,17 @@ EMPTY_DIR=/var/empty
    #

    if ! /mingw64/bin/editrights -h >/dev/null; then
    echo "Missing 'editrights'. Try: pacman -S mingw-w64-x86_64-editrights."
    echo "ERROR: Missing 'editrights'. Try: pacman -S mingw-w64-x86_64-editrights."
    exit 1
    fi

    if ! cygrunsrv -v >/dev/null; then
    echo "Missing 'cygrunsrv'. Try: pacman -S cygrunsrv."
    echo "ERROR: Missing 'cygrunsrv'. Try: pacman -S cygrunsrv."
    exit 1
    fi

    if ! ssh-keygen -A; then
    echo "Missing 'ssh-keygen'. Try: pacman -S openssh."
    echo "ERROR: Missing 'ssh-keygen'. Try: pacman -S openssh."
    exit 1
    fi

    @@ -44,39 +42,53 @@ tmp_pass="$(tr -dc 'a-zA-Z0-9' < /dev/urandom | dd count=14 bs=1 2>/dev/null)"

    # Create user
    add="$(if ! net user "${PRIV_USER}" >/dev/null; then echo "//add"; fi)"
    net user "${PRIV_USER}" "${tmp_pass}" ${add} //fullname:"${PRIV_NAME}" \
    //homedir:"$(cygpath -w ${EMPTY_DIR})" //yes
    if ! net user "${PRIV_USER}" "${tmp_pass}" ${add} //fullname:"${PRIV_NAME}" \
    //homedir:"$(cygpath -w ${EMPTY_DIR})" //yes; then
    echo "ERROR: Unable to create Windows user ${PRIV_USER}"
    exit 1
    fi

    # Add user to the Administrators group if necessary
    admingroup="$(mkgroup -l | awk -F: '{if ($2 == "S-1-5-32-544") print $1;}')"
    if ! (net localgroup "${admingroup}" | grep -q '^'"${PRIV_USER}"'$'); then
    net localgroup "${admingroup}" "${PRIV_USER}" //add
    if ! net localgroup "${admingroup}" "${PRIV_USER}" //add; then
    echo "ERROR: Unable to add user ${PRIV_USER} to group ${admingroup}"
    exit 1
    fi
    fi

    # Infinite passwd expiry
    passwd -e "${PRIV_USER}"

    # set required privileges
    /mingw64/bin/editrights -a SeAssignPrimaryTokenPrivilege -u "${PRIV_USER}"
    /mingw64/bin/editrights -a SeCreateTokenPrivilege -u "${PRIV_USER}"
    /mingw64/bin/editrights -a SeTcbPrivilege -u "${PRIV_USER}"
    /mingw64/bin/editrights -a SeDenyRemoteInteractiveLogonRight -u "${PRIV_USER}"
    /mingw64/bin/editrights -a SeServiceLogonRight -u "${PRIV_USER}"
    for flag in SeAssignPrimaryTokenPrivilege SeCreateTokenPrivilege \
    SeTcbPrivilege SeDenyRemoteInteractiveLogonRight SeServiceLogonRight; do
    if ! /mingw64/bin/editrights -a "${flag}" -u "${PRIV_USER}"; then
    echo "ERROR: Unable to give ${flag} rights to user ${PRIV_USER}"
    exit 1
    fi
    done


    #
    # The unprivileged sshd user (for privilege separation)
    #

    UNPRIV_USER=sshd # This username is hardcoded inside sshd
    UNPRIV_NAME="Privilege separation user for sshd"
    add="$(if ! net user "${UNPRIV_USER}" >/dev/null; then echo "//add"; fi)"
    net user "${UNPRIV_USER}" ${add} //fullname:"${UNPRIV_NAME}" \
    //homedir:"$(cygpath -w ${EMPTY_DIR})" //active:no
    if ! net user "${UNPRIV_USER}" ${add} //fullname:"${UNPRIV_NAME}" \
    //homedir:"$(cygpath -w ${EMPTY_DIR})" //active:no; then
    echo "ERROR: Unable to create Windows user ${PRIV_USER}"
    exit 1
    fi


    #
    # Add or update /etc/passwd entries
    #

    touch /etc/passwd
    for u in "${PRIV_USER}" "${UNPRIV_USER}"; do
    sed -i -e '/^'"${u}"':/d' /etc/passwd
    SED='/^'"${u}"':/s?^\(\([^:]*:\)\{5\}\).*?\1'"${EMPTY_DIR}"':/bin/false?p'
    @@ -91,8 +103,11 @@ done

    cygrunsrv -R sshd || true
    cygrunsrv -I sshd -d "MSYS2 sshd" -p \
    /usr/bin/sshd -a -D -y tcpip -u "${PRIV_USER}" -w "${tmp_pass}"
    /usr/bin/sshd.exe -a -D -y tcpip -u "${PRIV_USER}" -w "${tmp_pass}"

    # The SSH service should start automatically when Windows is rebooted. You can
    # manually restart the service by running `net stop sshd` + `net start sshd`
    net start sshd
    if ! net start sshd; then
    echo "ERROR: Unable to start sshd service"
    exit 1
    fi
  11. @samhocevar samhocevar revised this gist May 5, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.sh
    Original file line number Diff line number Diff line change
    @@ -39,7 +39,7 @@ fi
    #

    # Some random password; this is only needed internally by cygrunsrv and
    # is limited to 14 characters (lol)
    # is limited to 14 characters by Windows (lol)
    tmp_pass="$(tr -dc 'a-zA-Z0-9' < /dev/urandom | dd count=14 bs=1 2>/dev/null)"

    # Create user
  12. @samhocevar samhocevar revised this gist May 5, 2015. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions gistfile1.sh
    Original file line number Diff line number Diff line change
    @@ -43,14 +43,14 @@ fi
    tmp_pass="$(tr -dc 'a-zA-Z0-9' < /dev/urandom | dd count=14 bs=1 2>/dev/null)"

    # Create user
    add=""; if ! net user "${PRIV_USER}" >/dev/null; then add="//add"; fi
    add="$(if ! net user "${PRIV_USER}" >/dev/null; then echo "//add"; fi)"
    net user "${PRIV_USER}" "${tmp_pass}" ${add} //fullname:"${PRIV_NAME}" \
    //homedir:"$(cygpath -w ${EMPTY_DIR})" //yes

    # Add user to the Administrators group if necessary
    admingroup="$(mkgroup -l | awk -F: '{if ($2 == "S-1-5-32-544") print $1;}')"
    if ! (net localgroup "${admingroup}" | grep -q '^'"${PRIV_USER}"'$'); then
    net localgroup "${admingroup}" "${PRIV_USER}" ${add}
    net localgroup "${admingroup}" "${PRIV_USER}" //add
    fi

    # Infinite passwd expiry
    @@ -68,7 +68,7 @@ passwd -e "${PRIV_USER}"
    # The unprivileged sshd user (for privilege separation)
    #

    add=""; if ! net user "${UNPRIV_USER}" >/dev/null; then add="//add"; fi
    add="$(if ! net user "${UNPRIV_USER}" >/dev/null; then echo "//add"; fi)"
    net user "${UNPRIV_USER}" ${add} //fullname:"${UNPRIV_NAME}" \
    //homedir:"$(cygpath -w ${EMPTY_DIR})" //active:no

  13. @samhocevar samhocevar created this gist May 4, 2015.
    98 changes: 98 additions & 0 deletions gistfile1.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,98 @@
    #!/bin/sh

    set -e

    #
    # Configuration
    #

    PRIV_USER=cyg_server
    PRIV_NAME="Privileged server"
    UNPRIV_USER=sshd
    UNPRIV_NAME="User for sshd privsep"

    EMPTY_DIR=/var/empty


    #
    # Check installation sanity
    #

    if ! /mingw64/bin/editrights -h >/dev/null; then
    echo "Missing 'editrights'. Try: pacman -S mingw-w64-x86_64-editrights."
    exit 1
    fi

    if ! cygrunsrv -v >/dev/null; then
    echo "Missing 'cygrunsrv'. Try: pacman -S cygrunsrv."
    exit 1
    fi

    if ! ssh-keygen -A; then
    echo "Missing 'ssh-keygen'. Try: pacman -S openssh."
    exit 1
    fi


    #
    # The privileged cyg_server user
    #

    # Some random password; this is only needed internally by cygrunsrv and
    # is limited to 14 characters (lol)
    tmp_pass="$(tr -dc 'a-zA-Z0-9' < /dev/urandom | dd count=14 bs=1 2>/dev/null)"

    # Create user
    add=""; if ! net user "${PRIV_USER}" >/dev/null; then add="//add"; fi
    net user "${PRIV_USER}" "${tmp_pass}" ${add} //fullname:"${PRIV_NAME}" \
    //homedir:"$(cygpath -w ${EMPTY_DIR})" //yes

    # Add user to the Administrators group if necessary
    admingroup="$(mkgroup -l | awk -F: '{if ($2 == "S-1-5-32-544") print $1;}')"
    if ! (net localgroup "${admingroup}" | grep -q '^'"${PRIV_USER}"'$'); then
    net localgroup "${admingroup}" "${PRIV_USER}" ${add}
    fi

    # Infinite passwd expiry
    passwd -e "${PRIV_USER}"

    # set required privileges
    /mingw64/bin/editrights -a SeAssignPrimaryTokenPrivilege -u "${PRIV_USER}"
    /mingw64/bin/editrights -a SeCreateTokenPrivilege -u "${PRIV_USER}"
    /mingw64/bin/editrights -a SeTcbPrivilege -u "${PRIV_USER}"
    /mingw64/bin/editrights -a SeDenyRemoteInteractiveLogonRight -u "${PRIV_USER}"
    /mingw64/bin/editrights -a SeServiceLogonRight -u "${PRIV_USER}"


    #
    # The unprivileged sshd user (for privilege separation)
    #

    add=""; if ! net user "${UNPRIV_USER}" >/dev/null; then add="//add"; fi
    net user "${UNPRIV_USER}" ${add} //fullname:"${UNPRIV_NAME}" \
    //homedir:"$(cygpath -w ${EMPTY_DIR})" //active:no


    #
    # Add or update /etc/passwd entries
    #

    for u in "${PRIV_USER}" "${UNPRIV_USER}"; do
    sed -i -e '/^'"${u}"':/d' /etc/passwd
    SED='/^'"${u}"':/s?^\(\([^:]*:\)\{5\}\).*?\1'"${EMPTY_DIR}"':/bin/false?p'
    mkpasswd -l -u "${u}" | sed -e 's/^[^:]*+//' | sed -ne "${SED}" \
    >> /etc/passwd
    done


    #
    # Finally, register service with cygrunsrv and start it
    #

    cygrunsrv -R sshd || true
    cygrunsrv -I sshd -d "MSYS2 sshd" -p \
    /usr/bin/sshd -a -D -y tcpip -u "${PRIV_USER}" -w "${tmp_pass}"

    # The SSH service should start automatically when Windows is rebooted. You can
    # manually restart the service by running `net stop sshd` + `net start sshd`
    net start sshd