GNU/Linux >> Linux の 問題 >  >> Linux

Linuxでsudoeditを使用してユーザーがファイルを安全に編集できるようにする方法

あなたが会社のシステム管理者であり、チームが主に制限付きの権限でLinuxを使用しているとします。ここで、チームの1つのメンバーが(新しい作業の一部として)スーパーユーザー特権を必要とするファイルを頻繁に編集する必要がある状況を想像してみてください。どうしますか?

1つのオプションは、「sudo」アクセスを許可することですが、これは、必要なのが1つの部屋だけにいる場合に、見知らぬ人に完全な家へのアクセスを許可するようなものです。つまり、デフォルトの「sudo」アクセスは特定のシステムファイルを編集する機能だけが必要な場合は、root権限が必要なことは何でもできるようにします。

'sudo'が提供する柔軟性の種類を考えると、他のオプションは、問題のファイルを編集する権限のみが付与されるように'sudo'ポリシーを微調整することです。たとえば、次のようになります。

%newsudo ALL = vim /path/to/file

これは、完全なsudoアクセスを提供するよりもはるかに優れたソリューションであることは間違いありませんが、誰かが悪用する可能性のある抜け穴がまだあります。

私が話していることを理解するために、前述の制限付きアクセスがグループに提供され、誰かが「sudo」コマンドを使用して編集のために問題のファイルを開くシナリオを考えてみましょう。

これで、vimに関する十分な知識を持った賢い人は、エディター内から新しいシェルを起動できることを知っているでしょう。彼らがしなければならないのは、次のvimコマンドを実行することだけです。

:shell

これにより、即座にインタラクティブシェルに配置されます。マシンでこれらの手順を試している場合は、「whoami」コマンドを実行するだけで、私が話していた抜け穴を理解できます-はい、ルートとしてシェルにいます。

スクリーンショットの例を次に示します。

言うまでもなく、1つのファイルにのみ編集アクセスを提供するという目的は無効になり、ユーザーは今何でもできるようになりました。

他に選択肢があるかどうか見てみましょう。 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」を使用すると、ユーザーは「root」ではなく、自分自身としてファイルを編集することになります。

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」についての基本的な考え方を理解できたはずです。これには、いつ使用するか、1つまたは複数のファイルを編集するだけの場合に「sudo」を使用するよりも優れている理由が含まれます。もちろん、他のセキュリティ関連ツールと同様に、「sudoedit」はエクスプロイトのシェアを持っていますが、それでも多くのユースケースで推奨されるソリューションです。


Linux
  1. diffコマンドを使用してLinuxでファイルを行ごとに比較する方法

  2. LinuxでLogrotateを使用してログファイルを管理する方法

  3. LinuxでLog2ramを使用してRAMにログファイルを書き込む方法

  1. LinuxですべてのSudoユーザーを見つける方法

  2. SCPを使用してファイルを安全に転送する方法

  3. Linux で SCP コマンドを使用してファイルを安全に転送する方法

  1. SCPとSFTPを使用してLinuxホスト間でファイルを安全にコピーする方法

  2. 3 つの方法を使用して Linux で複数のファイルの名前をまとめて変更する方法

  3. Sort コマンドを使用して Linux でファイルをソートする方法