错误?在codesign中 - 删除签名功能

use*_*477 6 macos digital-signature

我想从已使用codesign签名的Mac应用中删除数字签名.有一个未记录的选项来编码, - 删除签名,它的名字似乎就是我所需要的.但是,我无法让它发挥作用.我意识到它没有记录,但我真的可以使用这个功能.也许我做错了什么?

 codesign -s MyIdentity foo.app
Run Code Online (Sandbox Code Playgroud)

正常工作,签署应用程序

 codesign --remove-signature foo.app
Run Code Online (Sandbox Code Playgroud)

然后说磁盘活动几秒钟

 foo.app: invalid format for signature
Run Code Online (Sandbox Code Playgroud)

和foo.app已经增长到1.9 GB!(具体来说,它是foo.app/Contents/Resources/MacOS中的可执行文件,从1.1 MB增加到1.9 GB.)

当我尝试签名/取消签名二进制支持工具而不是.app时,会发生同样的事情.

有任何想法吗?


背景:这是我自己的应用程序; 我不打算打败版权保护或类似的东西.

我想分发一个已签名的应用程序,以便每次更新应用程序都不需要用户批准来读取/写入应用程序在钥匙串中的条目.但是,有些人需要通过将自己的文件夹添加到/ Resources来修改应用程序.如果他们这样做,签名将变为无效,并且应用程序无法使用自己的Keychain条目.

该应用程序可以轻松检测是否发生了这种情况.如果应用程序可以删除它的签名,一切都会好的.那些进行此修改的人需要给修改后的,现在未签名的应用程序许可才能使用Keychain,但这对我来说没问题.

小智 8

有点晚了,但我更新了一个名为的公共域工具unsign,它修改了可执行文件以清除签名.

https://github.com/steakknife/unsign

  • 只要您清楚地详细说明OP如何使用它来执行他们想要做的事情,那么推广您编写的代码(甚至是部分代码)也没问题.否则,它被认为是垃圾邮件. (2认同)

Geo*_*xon 5

我今天遇到了这个问题。我可以确认--remove-signatureApple 的选项codesign(并且在 OP 提出这个问题六年后仍然存在)严重错误。

对于一些背景知识,Xcode(和 Apple 的命令行开发人员工具)包含该codesign实用程序,但不包含用于删除签名的工具。但是,由于在某些情况下需要经常执行此操作,因此包含一个完全未记录的选项:

codesign --remove-signature哪个(由于缺乏文档而假设)应该是相当不言自明的,但不幸的是,如果不付出一些努力,它很少能按预期工作。所以我最终编写了一个脚本来解决 OP 的问题,我的和类似的问题。如果有足够多的人在这里找到它并觉得它有用,请告诉我,我会把它放在 GitHub 或其他什么地方。


#!/bin/sh # codesign_remove_for_real -- working `codesign --remove-signature`
# (c) 2018 G. Nixon. BSD 2-clause minus retain/reproduce license requirements.

total_size(){
  # Why its so damn hard to get decent recursive filesize total in the shell?
  # - Darwin `du` doesn't do *bytes* (or anything less than 512B blocks)
  # - `find` size options are completely non-standardized and doesn't recurse
  # - `stat` is not in POSIX at all, and its options are all over the map...
  # - ... etc. 
  # So: here we just use `find` for a recursive list of *files*, then wc -c
  # and total it all up. Which sucks, because we have to read in every bit
  # of every file. But its the only truly portable solution I think.
  find "$@" -type f -print0 | xargs -0n1 cat | wc -c | tr -d '[:space:]'
}

# Get an accurate byte count before we touch anything. Zero would be bad.
size_total=$(total_size "$@") && [ $size_total -gt 0 ] || exit 1

recursively_repeat_remove_signature(){
  # `codesign --remove-signature` randomly fails in a few ways.
  # If you're lucky, you'll get an error like:
  # [...]/codesign_allocate: can't write output file: [...] (Invalid argument)
  # [...] the codesign_allocate helper tool cannot be found or used
  # or something to that effect, in which case it will return non-zero.
  # So we'll try it (suppressing stderr), and if it fails we'll just try again.
  codesign --remove-signature --deep "$@" 2>/dev/null ||
    recursively_repeat_remove_signature "$@"

  # Unfortunately, the other very common way it fails is to do something? that
  # hugely increases the binary size(s) by a seemingly arbitrary amount and
  # then exits 0. `codesign -v` will tell you that there's no signature, but
  # there are other telltale signs its not completely removed. For example,
  # if you try stripping an executable after this, you'll get something like
  # strip: changes being made to the file will invalidate the code signature
  # So, the solution  (well, my solution) is to do a file size check; once
  # we're finally getting the same result, we've probably been sucessful.
  # We could of course also use checksums, but its much faster this way.
  [ $size_total == $(total_size "$@") ] ||
    recursively_repeat_remove_signature "$@"

  # Finally, remove any leftover _CodeSignature directories.
  find "$@" -type d -name _CodeSignature -print0 | xargs -0n1 rm -rf
} 

signature_info(){
  # Get some info on code signatures. Not really required for anything here.
  for info in "-dr-" "-vv"; do codesign $info "$@";  done # "-dvvvv" 
}

# If we want to be be "verbose", check signature before. Un/comment out:
# echo >&2; echo "Current Signature State:" >&2; echo >&2; signature_info "$@"


# So we first remove any extended attributes and/or ACLs (which are common,
# and tend to interfere with the process here) then run our repeat scheme.
xattr -rc "$@" && chmod -RN "$@" && recursively_repeat_remove_signature "$@"

# Done!


# That's it; at this point, the executable or bundle(s) should sucessfully
# have truly become stripped of any code-signing. To test, one could
# try re-signing it again with an ad-hoc signature, then removing it again:
# (un/comment out below, as you see fit)

# echo >&2 && echo "Testing..." >&2; codesign -vvvvs - "$@" &&
  # signature_info "$@" && recursively_repeat_remove_signature "$@"

# And of course, while it sometimes returns false positives, lets at least:
codesign -dvvvv "$@" || echo "Signature successfully removed!" >&2 && exit 0
Run Code Online (Sandbox Code Playgroud)


Yuj*_*uji 1

我同意当你这样做时发生了一些奇怪的事情--remove-signature

但是,您不应该尝试取消代码签名,而应该更改用户将额外文件放入Resources. 相反,指定一条特定的路径,通常

~/Library/Application Support/Name_Of_Your_App/
Run Code Online (Sandbox Code Playgroud)

或者可能

~/Library/Application Support/Name_Of_Your_App/Resources/
Run Code Online (Sandbox Code Playgroud)

并要求用户在那里放置额外的文件。Resources然后,在您的代码中,当您需要读取文件时,除了文件之外,还要始终检查目录。