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

Linux –現在のプロセスでネットワークの共有を解除する方法は?

unshare -r -nを使用すると、root以外のネットワークにアクセスせずに新しいコマンドを実行できます。 、例:

$ unshare -r -n ls
a.txt  b.txt

ネットワークアクセスを必要とするコマンドは、予想どおりに失敗します。

$ unshare -r -n curl unix.stackexchange.com
curl: (6) Could not resolve host: unix.stackexchange.com

/sysの魔法のファイルに書き込むことで、現在のプロセスのネットワークアクセスを削除できるかどうか疑問に思っています。 または同様のもの。

次のようなことができるようになりたいです

$ /bin/sh -c 'echo 1 > /sys/unsharethis; curl unix.stackexchange.com'

straceからの抜粋 -ing unshare -r -n ls unshareを表示します システムコール

open("/usr/lib/locale/locale-archive", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=4759040, ...}) = 0
mmap(NULL, 4759040, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f7ec6968000
close(3)                                = 0
unshare(CLONE_NEWUSER|CLONE_NEWNET)     = 0
open("/proc/self/setgroups", O_WRONLY)  = 3
write(3, "deny", 4)                     = 4

これは、現在のプロセスからネットワークアクセスを共有解除することが、実際には共有解除を実現する唯一の方法であることを示唆しています(つまり、spawnへの引数として渡すことはできません。 またはその同等物)。また、シェルがunshareのラッパーを公開するように特別に拡張されていない限り、シェルスクリプトからの共有解除は機能しないことも示唆しています。 。

承認された回答:

これは、gdbを使用して行うことができます。 デバッガー、および実行中のプロセスをアタッチできるかどうか(ダンプ可能な状態を変更するプログラム、またはsetgidなどのプログラムは、ルートからでなければアタッチできません)。

一部のオプションファイルは、libc6のデバッグシンボルのようなgdbの使用に役立ちます。また、Linux関連のいくつかのインクルードファイルを使用して、後でいくつかのシンボルの実際の値を取得できます(Debianの場合:(おそらく)libc6-dbglibc6-dev およびlinux-libc-dev パッケージ)ですが、実際には「レシピ」が作成されると、おそらくもう必要なくなります。

まず、unshare()以上のものです unshare -r やってるの?これがないと、新しいユーザーはnobodyに留まります 最初のユーザーとして書くことすらできません:

$ id
uid=1000(user) gid=1000(user) groups=1000(user)
$ strace unshare -r -n /bin/sleep 1 2>&1 |sed -n '/^unshare/,/^execve/p'
unshare(CLONE_NEWNET|CLONE_NEWUSER)     = 0
open("/proc/self/setgroups", O_WRONLY)  = 3
write(3, "deny", 4)                     = 4
close(3)                                = 0
open("/proc/self/uid_map", O_WRONLY)    = 3
write(3, "0 1000 1", 8)                 = 8
close(3)                                = 0
open("/proc/self/gid_map", O_WRONLY)    = 3
write(3, "0 1000 1", 8)                 = 8
close(3)                                = 0
execve("/bin/sleep", ["/bin/sleep", "1"], [/* 18 vars */]) = 0

後で使用します。

$ ip -4 -br a
lo               UNKNOWN        127.0.0.1/8 
[email protected]        UP             10.0.3.66/24 
$ ping -c1 10.0.3.1
PING 10.0.3.1 (10.0.3.1) 56(84) bytes of data.
64 bytes from 10.0.3.1: icmp_seq=1 ttl=64 time=0.167 ms

--- 10.0.3.1 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms

rtt min/avg/max/mdev = 0.167/0.167/0.167/0.000 ms
$ id
uid=1000(user) gid=1000(user) groups=1000(user)
$ echo $$
338
$

他の端末の場合:

$ gdb --pid=338
Reading symbols from /bin/bash...(no debugging symbols found)...done.
Reading symbols from /lib/x86_64-linux-gnu/libtinfo.so.5...(no debugging symbols found)...done.
Reading symbols from /lib/x86_64-linux-gnu/libdl.so.2...Reading symbols from /usr/lib/debug/.build-id/b8/95f0831f623c5f23603401d4069f9f94c24761.debug...done.
done.
Reading symbols from /lib/x86_64-linux-gnu/libc.so.6...Reading symbols from /usr/lib/debug/.build-id/aa/889e26a70f98fa8d230d088f7cc5bf43573163.debug...done.
done.

[…]

(gdb)

それでは、最初の関数を呼び出しましょう:

(gdb) call unshare(CLONE_NEWNET|CLONE_NEWUSER)
No symbol "CLONE_NEWNET" in current context.

わかりました。gdbにそれを知らせる方法があるかもしれませんが、私は教祖ではありません:

(gdb) !
$ grep CLONE_NEW /usr/include/linux/sched.h # man 2 unshare
#define CLONE_NEWNS 0x00020000  /* New mount namespace group */
#define CLONE_NEWCGROUP     0x02000000  /* New cgroup namespace */
#define CLONE_NEWUTS        0x04000000  /* New utsname namespace */
#define CLONE_NEWIPC        0x08000000  /* New ipc namespace */
#define CLONE_NEWUSER       0x10000000  /* New user namespace */
#define CLONE_NEWPID        0x20000000  /* New pid namespace */
#define CLONE_NEWNET        0x40000000  /* New network namespace */
$ find /usr/include/ -name fcntl.h |xargs grep O_WRONLY # man 2 open
/usr/include/asm-generic/fcntl.h:#define O_WRONLY   00000001
$ exit
exit
(gdb) call unshare(0x50000000)
$1 = 0
(gdb) call open("/proc/self/setgroups", 1)
$2 = 3
(gdb) call write($2,"deny",4)
$3 = 4
(gdb) call close($2)
$4 = 0
(gdb) call open("/proc/self/uid_map", 1)
$5 = 3
(gdb) call write($5, "0 1000 1", 8)
$6 = 8
(gdb) call close($5)
$7 = 0
(gdb) call open("/proc/self/gid_map", 1)
$8 = 3
(gdb) call write($8, "0 1000 1", 8)
$9 = 8
(gdb) call close($8)
$10 = 0
(gdb) quit
A debugging session is active.

    Inferior 1 [process 338] will be detached.

Quit anyway? (y or n) y
Detaching from program: /bin/bash, process 338

変更されたプロセスでは、eth0を確認できます インターフェイスが消えました:

$ ip -br a
lo               DOWN           127.0.0.1/8 
$ echo $$
338
$ id
uid=0(root) gid=0(root) groupes=0(root)
$ touch /
touch: setting times of '/': Permission denied
$ touch ~/test1
$ ls ~/test1
/home/user/test1
$ ping 10.0.3.1
connect: Network is unreachable

戻ることはできません。新しいユーザー名前空間を最初の名前空間に戻すことはできません。プロセスが十分な特権で実行されている場合(たとえば、機能が失われていないrootやSELinux)、それは可能です(unshare(CLONE_NEWNET)のみを使用します / setns(savedopenedfd)

関連:新しいユーザーのデスクトップのデフォルトを設定する方法は??

もちろん、ファイルにスクリプトを記述して、許可されている実行中のプロセスを変更したり、シェルをgdbサブプロセスから変更したりすることもできます。 removenetwork.gdbの内容 、ここではpid:gidを使用してプロセスを変更する場合にのみ有効です ==1000:1000

更新:以下のsyscallの(おおよその)リターンタイプを追加しました。これにより、一部のバージョンのgdbが非開発環境で文句を言うのを回避できます:

call (int)unshare(0x50000000)
call (int)open("/proc/self/setgroups", 1)
call (long)write($2,"deny",4)
call (int)close($2)
call (int)open("/proc/self/uid_map", 1)
call (long)write($5, "0 1000 1", 8)
call (int)close($5)
call (int)open("/proc/self/gid_map", 1)
call (long)write($8, "0 1000 1", 8)
call (int)close($8)
quit

例:

$ sh -c 'id; gdb --pid=$$ < removenetwork.gdb >/dev/null 2>&1; id; curl unix.stackexchange.com'
uid=1000(user) gid=1000(user) groups=1000(user)
uid=0(root) gid=0(root) groups=0(root)
curl: (6) Could not resolve host: unix.stackexchange.com

更新 :この質問に示されているように、rootがまったく必要ない場合は、rootにマップする必要はまったくありません。 write($XX, "0 1000 1", 8)の出現箇所を置き換えるだけです write($XX, "1000 1000 1", 11)を使用 (uid:gidの場合 ==1000:1000 場合)。補足グループは依然として不可避的に失われますが、uid / gidは変更されません(それ自体にマップされます)。


Linux
  1. Linuxでゾンビプロセスを強制終了する方法

  2. 効果的なプロセス管理のための8つのLinuxコマンド

  3. Linuxにvtopをインストールする方法

  1. LinuxでプロセスのCPU使用率を制限する方法

  2. プロセス監視のために Linux に Monit をインストールして構成する方法

  3. サンドボックス化のために、Linux プロセスのソケット作成を無効にする方法は?

  1. ネットワーク診断用の10のLinuxコマンド

  2. Linux –プロセスのネットワークアクセスをブロックしますか?

  3. ネットワーク インターフェイス HA の Linux Etherchannel ボンディングをセットアップする方法