Linux에서 sudoedit를 사용하여 사용자가 파일을 안전하게 편집하도록 하는 방법
이 페이지에서
- Sudoedit 작동 방식
팀이 주로 제한된 권한으로 Linux에서 작업하는 회사의 시스템 관리자라고 가정해 보겠습니다. 이제 팀 중 하나의 구성원이 새로운 작업의 일부로 수퍼유저 권한이 필요한 파일을 자주 편집해야 하는 상황을 상상해 보십시오. 어떻게 하시겠습니까?
한 가지 옵션은 그들에게 sudo 액세스 권한을 부여하는 것이지만, 이는 낯선 사람이 귀하의 방 중 하나에만 있어야 할 때 귀하의 전체 집에 대한 액세스 권한을 부여하는 것과 같습니다. 즉, 기본 sudo 액세스 권한으로 무엇이든 할 수 있다는 것입니다. 루트 권한이 필요한 경우 특정 시스템 파일을 편집하는 기능만 있으면 됩니다.
sudo가 제공하는 유연성을 고려할 때 다른 옵션은 해당 파일을 편집할 수 있는 권한만 부여되는 방식으로 sudo 정책을 조정하는 것입니다. 예를 들면 다음과 같습니다.
%newsudo ALL = vim /path/to/file
이것이 완전한 sudo 액세스를 제공하는 것보다 훨씬 나은 솔루션이라는 것은 의심의 여지가 없지만 여전히 누군가 악용할 수 있는 허점이 있습니다.
내가 말하는 내용을 이해하려면 앞에서 언급한 제한된 액세스가 그룹에 제공되고 누군가가 sudo 명령을 사용하여 편집을 위해 해당 파일을 여는 시나리오를 고려하십시오.
이제 vim에 대한 지식이 있는 영리한 사람이라면 편집기 내에서 새 셸을 시작할 수 있다는 것을 알 것입니다. 다음 vim 명령을 실행하기만 하면 됩니다.
:shell
이렇게 하면 대화형 셸에 즉시 배치됩니다. 컴퓨터에서 이 단계를 시도하는 경우 whoami 명령을 실행하기만 하면 내가 말한 허점을 이해할 수 있습니다. 예, 루트로 셸에 있습니다.
예시 스크린샷은 다음과 같습니다.
말할 필요도 없이 단일 파일에 대한 편집 액세스를 제공하려는 목적이 무산되었고 사용자는 이제 무엇이든 할 수 있습니다.
우리가 가진 다른 옵션이 있는지 봅시다. sudoers 항목에서 사용할 수 있는 NOEXEC 태그가 있습니다.
%newsudo ALL = NOEXEC: vim /path/to/file
NOEXEC를 사용하면 기본적으로 sudo로 실행되는 프로그램이 다른 프로그램을 실행하지 못하도록 방지할 수 있습니다. 그렇다면 이것이 궁극적인 해결책일까요? 슬프게도 아니오. 그 이유는 sudoers 설명서에서 이 태그를 설명하는 방법입니다.
noexec
Many systems that support shared libraries have the ability to override default library functions by pointing an environment variable (usually LD_PRELOAD) to an alternate shared library. On such systems, sudo's noexec functionality can be used to prevent a program run by sudo from executing any other programs. Note, however, that this applies only to native dynamically-linked executables. Statically-linked executables and foreign executables running under binary emulation are not affected.
The noexec feature is known to work on SunOS, Solaris, *BSD, Linux, IRIX, Tru64 UNIX, MacOS X, HP-UX 11.x and AIX 5.3 and above.
...
...
...
To enable noexec for a command, use the NOEXEC tag as documented in the User Specification section above. Here is that example again:
aaron shanty = NOEXEC: /usr/bin/more, /usr/bin/vi
This allows user aaron to run /usr/bin/more and /usr/bin/vi with noexec enabled. This will prevent those two commands from executing other commands (such as a shell). If you are unsure whether or not your system is capable of supporting noexec you can always just try it out and check whether shell escapes work when noexec is enabled.
Note that restricting shell escapes is not a panacea. Programs running as root are still capable ofmany potentially hazardous operations (such as changing or overwriting files) that could lead to unintended privilege escalation.
...
...
...
굵게 강조 표시된 텍스트를 주의 깊게 읽으면 NOEXEC에 자체 제한 사항이 있음을 알 수 있습니다.
그렇다면 가장 안정적인 솔루션은 무엇입니까? 음, sudoedit입니다. sudoers 매뉴얼에서도 이 도구를 권장합니다.
In the specific case of an editor, a safer approach is to give the user permission to run sudoedit.
Sudoedit는 사용자가 파일을 안전하게 편집할 수 있도록 하는 기본 제공 명령어입니다. sudo 매뉴얼 페이지에 따르면 sudoedit는 -e 명령줄 옵션을 사용하여 sudo를 실행하는 것과 같습니다.
왜 더 나은가요?
sudoedit를 사용하면 사용자가 선호하는 편집기를 사용할 수 있습니다. 이는 사용자가 Vim 편집기를 사용해야 하는 이 튜토리얼의 시작 부분에서 논의한 솔루션과 달리 자신만의 맞춤설정을 즐길 수 있도록 합니다. 그리고 가장 큰 이유는 sudoedit를 사용하면 사용자가 파일을 루트가 아닌 자체적으로 편집하게 된다는 것입니다.
Sudoedit 작동 방식
sudoedit를 사용하려면 sudoers의 항목이 예를 들어 다음과 같아야 합니다.
%newsudo ALL = sudoedit /path/to/file
그리고 newsudo 그룹의 일부인 사용자는 다음 명령을 실행하여 파일을 편집할 수 있습니다.
sudoedit /path/to/file
따라서 이 명령이 수행하는 작업은 먼저 수정하려는 파일의 임시 사본을 생성하는 것입니다. 그런 다음 명령어는 SUDO_EDITOR, VISUAL 및 EDITOR 환경 변수를 검색하여 방금 생성된 임시 사본을 열기 위해 호출해야 하는 편집기를 결정합니다. 사용자가 수정 작업을 완료하면 변경사항이 원본 파일에 다시 복사됩니다.
다음은 sudo 명령 매뉴얼 페이지의 자세한 설명입니다.
-e, --edit
Edit one or more files instead of running a command. In lieu of a path name, the string "sudoedit" is used when consulting the security policy. If the user is authorized by the policy, the followingsteps are taken:
1. Temporary copies are made of the files to be edited with
the owner set to the invoking user.
2. The editor specified by the policy is run to edit the
temporary files. The sudoers policy uses the
SUDO_EDITOR, VISUAL and EDITOR environment variables (in
that order). If none of SUDO_EDITOR, VISUAL or EDITOR
are set, the first program listed in the editor
sudoers(5) option is used.
3. If they have been modified, the temporary files are
copied back to their original location and the temporary
versions are removed.
If the specified file does not exist, it will be created. Note that unlike most commands run by sudo, the editor is run with the invoking user's environment unmodified. If, for some reason, sudo is unable to update a file with its edited version, the user will receive a warning and the edited copy will remain in a temporary file.
지금쯤이면 sudoedit를 언제 사용해야 하는지, 파일 한 개 또는 몇 개만 수정하려는 경우 sudo를 사용하는 것보다 더 나은 이유를 포함하여 sudoedit에 대한 기본 개념을 이해하셨을 것입니다. 물론 다른 보안 관련 도구와 마찬가지로 sudoedit에도 악용 사례가 있었지만 여전히 많은 사용 사례에서 권장되는 솔루션입니다.