From 3dcb012e0520b69f1298f3d0b97d33f2c0bd62b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20H=C3=BCsers?= Date: Sun, 9 Jun 2024 21:51:20 +0200 Subject: [PATCH] upgpkg: discord-electron-openasar 0.0.55+827-1: refactor, replace krisp patcher * upstream release * Slight refactor of package function and launcher script * Replace Krisp patcher with more robust Python script * Add support for DISCORD_USER_DATA_DIR env var --- discord-electron-openasar/.SRCINFO | 14 ++-- discord-electron-openasar/PKGBUILD | 34 ++++---- .../discord-electron-openasar.install | 15 +++- discord-electron-openasar/discord-launcher.sh | 16 ++-- discord-electron-openasar/krisp-patcher.py | 83 +++++++++++++++++++ discord-electron-openasar/update-package.sh | 57 +++++++++++++ 6 files changed, 185 insertions(+), 34 deletions(-) create mode 100644 discord-electron-openasar/krisp-patcher.py create mode 100755 discord-electron-openasar/update-package.sh diff --git a/discord-electron-openasar/.SRCINFO b/discord-electron-openasar/.SRCINFO index 35dba20..b24b4e3 100644 --- a/discord-electron-openasar/.SRCINFO +++ b/discord-electron-openasar/.SRCINFO @@ -1,6 +1,6 @@ pkgbase = discord-electron-openasar pkgdesc = Discord packaged with OpenAsar using system provided electron (v30) for increased security and performance - pkgver = 0.0.54+827 + pkgver = 0.0.55+827 pkgrel = 1 url = https://discord.com install = discord-electron-openasar.install @@ -14,18 +14,22 @@ pkgbase = discord-electron-openasar depends = electron30 depends = libxss depends = unzip + depends = python optdepends = libpulse: Pulseaudio support optdepends = libappindicator-gtk3: Systray indicator support optdepends = xdg-utils: Open files - optdepends = rizin: Allow patching Krisp support + optdepends = python-pyelftools: Required for Krisp patcher + optdepends = python-capstone: Required for Krisp patcher provides = discord conflicts = discord options = !strip - source = https://dl.discordapp.net/apps/linux/0.0.54/discord-0.0.54.tar.gz + source = https://dl.discordapp.net/apps/linux/0.0.55/discord-0.0.55.tar.gz source = discord-launcher.sh + source = krisp-patcher.py source = git+https://github.com/goosemod/openasar.git#commit=4f264d860a5a6a32e1862ce26178b9cf6402335d - sha512sums = ceb6265e846fafd27e3c2941f00f0a2d4e045abe5471a2691b070bedb330ef345edee838a706b27ff65ecce40aec46b1a1dd9c4bb9763dadaa907ec181c1a222 - sha512sums = 9d00f9d2e05c2ba31c930c066f247954700bb0f96f2fc605e61c3973d7dacf962bf372659b71e05e5d8d4e152cc884bf12ec1fb5ecbfc4da55d22ab2591c4c40 + sha512sums = c12365109cb4db94033fda101073d2a3d6af7bdc9728e025494ee8d44ebf5f27623861879ec38bb7dd88d40de7cddede268e0f5e44bfeb586fc12713a3758c5b + sha512sums = fc25a6ac9ca2881a94158ed972039a4dcc082db91a8ba072f2f896b65a788c9decb4456c71da977ac2976e72d081c6c66aa4bb27b5d58cf946dfea6086b0d557 + sha512sums = 3c1021592fa856f3561072c76b5ee0b5a34a53bc230336e6d36827efb4866c9d801ef7abb24650d3a7210c61dd57f35e2812ae89226fc157cc8d9ffce032155f sha512sums = 055bbe5fbc63a715ab8357db8aabacad282e3d176b48e322d7133a5887291577687456bbfaf7b832d19f13b1a5a373e2c0f6f82664887509feb3c193ee4f1849 pkgname = discord-electron-openasar diff --git a/discord-electron-openasar/PKGBUILD b/discord-electron-openasar/PKGBUILD index 0e1ac1a..43f3145 100644 --- a/discord-electron-openasar/PKGBUILD +++ b/discord-electron-openasar/PKGBUILD @@ -2,7 +2,7 @@ pkgname=discord-electron-openasar _pkgname=discord -pkgver=0.0.54+827 +pkgver=0.0.55+827 _pkgver=${pkgver%%+*} pkgrel=1 _electronver=30 @@ -15,23 +15,24 @@ url='https://discord.com' license=('custom') options=('!strip') install="$pkgname.install" -depends=("${_electronname}" 'libxss' 'unzip') +depends=("${_electronname}" 'libxss' 'unzip' 'python') makedepends=('git' 'asar' 'nodejs' 'curl' 'python-html2text') optdepends=( 'libpulse: Pulseaudio support' 'libappindicator-gtk3: Systray indicator support' 'xdg-utils: Open files' - 'rizin: Allow patching Krisp support' + 'python-pyelftools: Required for Krisp patcher' + 'python-capstone: Required for Krisp patcher' ) source=("https://dl.discordapp.net/apps/linux/${_pkgver}/${_pkgname}-${_pkgver}.tar.gz" 'discord-launcher.sh' + 'krisp-patcher.py' # original: https://github.com/sersorrel/sys/blob/main/hm/discord/krisp-patcher.py "git+https://github.com/goosemod/openasar.git#commit=4f264d860a5a6a32e1862ce26178b9cf6402335d") -sha512sums=('ceb6265e846fafd27e3c2941f00f0a2d4e045abe5471a2691b070bedb330ef345edee838a706b27ff65ecce40aec46b1a1dd9c4bb9763dadaa907ec181c1a222' - '9d00f9d2e05c2ba31c930c066f247954700bb0f96f2fc605e61c3973d7dacf962bf372659b71e05e5d8d4e152cc884bf12ec1fb5ecbfc4da55d22ab2591c4c40' +sha512sums=('c12365109cb4db94033fda101073d2a3d6af7bdc9728e025494ee8d44ebf5f27623861879ec38bb7dd88d40de7cddede268e0f5e44bfeb586fc12713a3758c5b' + 'fc25a6ac9ca2881a94158ed972039a4dcc082db91a8ba072f2f896b65a788c9decb4456c71da977ac2976e72d081c6c66aa4bb27b5d58cf946dfea6086b0d557' + '3c1021592fa856f3561072c76b5ee0b5a34a53bc230336e6d36827efb4866c9d801ef7abb24650d3a7210c61dd57f35e2812ae89226fc157cc8d9ffce032155f' '055bbe5fbc63a715ab8357db8aabacad282e3d176b48e322d7133a5887291577687456bbfaf7b832d19f13b1a5a373e2c0f6f82664887509feb3c193ee4f1849') -_krisp_b2sum='c4e49b1a0be64cacd23474c5e474130084755f2de36af01686ce94c6eceea67426b6d6aa81889a418f9d08476476e99d63479b6131d1f7eaef987ecf4d779cf9' - # just in case I get the version wrong pkgver() { cd "${srcdir}/openasar" @@ -43,7 +44,6 @@ prepare() { sed -i -e "s|@PKGNAME@|${_pkgname}|g" \ -e "s|@PKGVER@|${_pkgver}|g" \ -e "s|@ELECTRON@|${_electronname}|g" \ - -e "s|@KRISPB2@|${_krisp_b2sum}|g" \ discord-launcher.sh # fix the .desktop file @@ -69,15 +69,14 @@ build() { package() { # create necessary directories - install -d "${pkgdir}"/usr/{lib/$_pkgname,bin} - install -d "${pkgdir}"/usr/share/{pixmaps,applications,licenses/$_pkgname} + install -d "${pkgdir}"/usr/lib/$_pkgname # copy relevant data cp -r ${_pkgname^}/resources "${pkgdir}"/usr/lib/$_pkgname/ - cp ${_pkgname^}/$_pkgname.png \ - "${pkgdir}"/usr/share/pixmaps/$_pkgname.png - cp ${_pkgname^}/$_pkgname.desktop \ - "${pkgdir}"/usr/share/applications/$_pkgname.desktop + + # intall icon and desktop file + install -Dm 644 ${_pkgname^}/$_pkgname.png "${pkgdir}"/usr/share/pixmaps/$_pkgname.png + install -Dm 644 ${_pkgname^}/$_pkgname.desktop "${pkgdir}"/usr/share/applications/$_pkgname.desktop # overwrite Discord asar install -Dm 644 openasar/app.asar "${pkgdir}"/usr/lib/$_pkgname/resources/ @@ -85,7 +84,10 @@ package() { # install the launch script install -Dm 755 discord-launcher.sh "${pkgdir}"/usr/bin/$_pkgname + # install krisp patcher + install -Dm 644 krisp-patcher.py "${pkgdir}"/usr/lib/${_pkgname}/ + # install licenses - install -Dm 644 LICENSE.md "${pkgdir}"/usr/share/licenses/$_pkgname/ - install -Dm 644 OSS-LICENSES.md "${pkgdir}"/usr/share/licenses/$_pkgname/ + install -Dm 644 LICENSE.md "${pkgdir}"/usr/share/licenses/$_pkgname/LICENSE.md + install -Dm 644 OSS-LICENSES.md "${pkgdir}"/usr/share/licenses/$_pkgname/OSS-LICENSES.md } diff --git a/discord-electron-openasar/discord-electron-openasar.install b/discord-electron-openasar/discord-electron-openasar.install index 7011466..1732135 100644 --- a/discord-electron-openasar/discord-electron-openasar.install +++ b/discord-electron-openasar/discord-electron-openasar.install @@ -1,4 +1,13 @@ post_upgrade() { + # return if old package version is greater than or equal to 0.0.55+827-1... + (( $(vercmp $2 '0.0.55+827-1') >= 0 )) && return + + cat < Instead of using 'rizin' to patch Krisp support a patcher written in Python + is now used. Consider installing the necessary optional dependencies to use + this feature. +EOF + # return if old package version is greater than or equal to 0.0.39+824-2... (( $(vercmp $2 '0.0.39+824-2') >= 0 )) && return @@ -26,9 +35,9 @@ EOF post_install() { cat < For Krisp noise suppression support consider installing 'rizin' and - setting 'PATCH_KRISP=true' in '~/.config/discord.conf'. Restart your - Discord client afterwards to apply the patch. +==> For Krisp noise suppression support consider installing the necessary optional + dependencies and setting 'PATCH_KRISP=true' in '~/.config/discord.conf'. + Restart your Discord client afterwards to apply the patch. ==> Additional Electron flags can be set in '~/.config/discord-flags.conf'. EOF } diff --git a/discord-electron-openasar/discord-launcher.sh b/discord-electron-openasar/discord-launcher.sh index c63b91c..e0604e2 100644 --- a/discord-electron-openasar/discord-launcher.sh +++ b/discord-electron-openasar/discord-launcher.sh @@ -1,22 +1,18 @@ #!/usr/bin/env bash -set -eo pipefail +set -euo pipefail declare -a flags - +declare -l PATCH_KRISP [[ -r "${XDG_CONFIG_HOME:-$HOME/.config}/@PKGNAME@.conf" ]] && source "${XDG_CONFIG_HOME:-$HOME/.config}/@PKGNAME@.conf" flags_file="${XDG_CONFIG_HOME:-$HOME/.config}/@PKGNAME@-flags.conf" -krisp_bin="${XDG_CONFIG_HOME:-$HOME/.config}/@PKGNAME@/@PKGVER@/modules/@PKGNAME@_krisp/@PKGNAME@_krisp.node" -krisp_b2=@KRISPB2@ +krisp_bin="${DISCORD_USER_DATA_DIR:-${XDG_CONFIG_HOME:-$HOME/.config}/@PKGNAME@}/@PKGVER@/modules/@PKGNAME@_krisp/@PKGNAME@_krisp.node" -if hash rizin &> /dev/null && [[ "${PATCH_KRISP}" == true ]]; then +if [[ "${PATCH_KRISP}" == true ]] && [[ -w "${krisp_bin}" ]] && python -c "import capstone; import elftools" &> /dev/null; then # Patch Krisp binary to ignore signature check - if [[ -w "${krisp_bin}" && $(b2sum "${krisp_bin}" | head -c 128) == $krisp_b2 ]]; then - addr=$(rz-find -x '4881ec00010000' "${krisp_bin}" | head -n1) - rizin -q -w -c "s $addr + 0x30 ; wao nop" "${krisp_bin}" &> /dev/null - fi + python /usr/lib/@PKGNAME@/krisp-patcher.py "${krisp_bin}" fi if [[ -r "${flags_file}" ]]; then @@ -33,7 +29,7 @@ for line in "${MAPFILE[@]}"; do fi done -unset flags_file krisp_bin krisp_b2 +unset flags_file krisp_bin exec /usr/lib/@ELECTRON@/electron \ diff --git a/discord-electron-openasar/krisp-patcher.py b/discord-electron-openasar/krisp-patcher.py new file mode 100644 index 0000000..5597290 --- /dev/null +++ b/discord-electron-openasar/krisp-patcher.py @@ -0,0 +1,83 @@ +import sys +import shutil + +from elftools.elf.elffile import ELFFile +from capstone import * +from capstone.x86 import * + +if len(sys.argv) < 2: + print(f"Usage: {sys.argv[0]} [path to discord_krisp.node]") + # "Unix programs generally use 2 for command line syntax errors and 1 for all other kind of errors." + sys.exit(2) + +executable = sys.argv[1] + +elf = ELFFile(open(executable, "rb")) +symtab = elf.get_section_by_name('.symtab') + +krisp_initialize_address = symtab.get_symbol_by_name("_ZN7discord15KrispInitializeEv")[0].entry.st_value +isSignedByDiscord_address = symtab.get_symbol_by_name("_ZN7discord4util17IsSignedByDiscordERKNSt4__Cr12basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEE")[0].entry.st_value + +text = elf.get_section_by_name('.text') +text_start = text['sh_addr'] +text_start_file = text['sh_offset'] +# This seems to always be zero (.text starts at the right offset in the file). Do it just in case? +address_to_file = text_start_file - text_start + +# Done with the ELF now. +# elf.close() + +krisp_initialize_offset = krisp_initialize_address - address_to_file +isSignedByDiscord_offset = krisp_initialize_address - address_to_file + +f = open(executable, "rb") +f.seek(krisp_initialize_offset) +krisp_initialize = f.read(64) +f.close() + +# States +found_issigned_by_discord_call = False +found_issigned_by_discord_test = False +found_issigned_by_discord_je = False +found_already_patched = False +je_location = None + +# We are looking for a call to IsSignedByDiscord, followed by a test, followed by a je. +# Then we patch the je into a two byte nop. + +md = Cs(CS_ARCH_X86, CS_MODE_64) +md.detail = True +for i in md.disasm(krisp_initialize, krisp_initialize_address): + if i.id == X86_INS_CALL: + if i.operands[0].type == X86_OP_IMM: + if i.operands[0].imm == isSignedByDiscord_address: + found_issigned_by_discord_call = True + + if i.id == X86_INS_TEST: + if found_issigned_by_discord_call: + found_issigned_by_discord_test = True + + if i.id == X86_INS_JE: + if found_issigned_by_discord_test: + found_issigned_by_discord_je = True + je_location = i.address + break + + if i.id == X86_INS_NOP: + if found_issigned_by_discord_test: + found_already_patched = True + break + +if je_location: + print(f"Found patch location: 0x{je_location:x}") + + shutil.copyfile(executable, executable + ".orig") + f = open(executable, 'rb+') + f.seek(je_location - address_to_file) + f.write(b'\x66\x90') # Two byte NOP + f.close() +else: + if found_already_patched: + print("Couldn't find patch location - already patched.") + else: + print("Couldn't find patch location - review manually. Sorry.") diff --git a/discord-electron-openasar/update-package.sh b/discord-electron-openasar/update-package.sh new file mode 100755 index 0000000..90cdc1e --- /dev/null +++ b/discord-electron-openasar/update-package.sh @@ -0,0 +1,57 @@ +#!/usr/bin/env bash + +set -euo pipefail + +readonly all_off="$(tput sgr0)" +readonly bold="${all_off}$(tput bold)" +readonly white="${bold}$(tput setaf 7)" +readonly blue="${bold}$(tput setaf 4)" +readonly red="${bold}$(tput setaf 1)" + +msg() { + printf "${blue}::${white} $1${all_off}\n" +} + +error() { + printf "${red}::${white} $1${all_off}\n" +} + +msgbegin() { + printf "${blue}::${white} $1" +} + +msgend() { + printf "$1${all_off}\n" +} + + +readonly krisp_zip='discord_krisp-1.zip' +readonly krisp_bin='discord_krisp.node' + +# head to directory of this script +cd $(dirname "$0") + +# update package to version used in PKGBUILD +source PKGBUILD + +msg "Running updpkgsums (Updating checksums)" +updpkgsums + +msg "Running mksrcinfo (Updating SRCINFO file)" +makepkg --printsrcinfo > .SRCINFO + +msg "Getting Krisp module" +curl -O "https://dl.discordapp.net/apps/linux/${_pkgver:-${pkgver}}/modules/${krisp_zip}" +unzip "${krisp_zip}" "${krisp_bin}" + +msg "Checking if Krisp module is patchable (watch output)" +python krisp-patcher.py "${krisp_bin}" + +#msg "Updating Krisp module checksum" +#readonly chcksm=$(b2sum "${krisp_bin}.orig" | head -c 128) +#sed -i "s/^_krisp_b2sum='.*'$/_krisp_b2sum='${chcksm}'/" PKGBUILD + +msgbegin "Cleaning up... " +rm -f "${krisp_zip}" "${krisp_bin}" "${krisp_bin}.orig" + +msgend "Done"