루트가 아닌 사용자로 iptables를 나열할 수 있습니까? 왜?

루트가 아닌 사용자로 iptables를 나열할 수 있습니까? 왜?

iptables --list … 루트 권한 없이 이 명령을 실행할 수 있습니까 ?

루트가 아닌 사용자로 실행하면 다음이 인쇄됩니다.

$ iptables --list
iptables v1.4.21: can't initialize iptables table `filter': Permission denied (you must be root)
Perhaps iptables or your kernel needs to be upgraded.

iptables를 나열하기 위해 루트여야 한다면 그 이유는 무엇입니까? 보안 문제에 대한 규칙을 확인하시겠습니까? iptables --list 루트 액세스가 필요한 리소스나 서비스 가 있나요 ?

분명히 iptables 방화벽 규칙을 수정하려면 권한이 필요합니다. 시청해도 되는지 묻고 싶습니다.


규칙을 나열할 수 있는 루트 이외의 기능이 있습니까? iptables는 netlink를 사용하여 커널과 상호 작용합니까? 왜냐하면웹 링크(7)말하는

유효한 UID만 0이거나 CAP_NET_ADMIN Netlink 멀티캐스트 그룹을 보내거나 듣는 기능.

어쩌면 이것은 iptables에서는 작동하지 않을 수도 있습니다.

이것이 올바른 접근 방식인지는 확실하지 않지만 함수를 추가해도  iptables 규칙을 나열할 수는 없습니다.

bash-4.1$ echo $UID
2000
bash-4.1$ getcap /sbin/iptables-multi-1.4.7
/sbin/iptables-multi-1.4.7 = cap_net_admin+ep
bash-4.1$ /sbin/iptables-multi-1.4.7 main --list
FATAL: Could not load /lib/modules/3.10.0-514.21.1.el7.x86_64/modules.dep: No such file or directory
iptables v1.4.7: can't initialize iptables table `filter': Permission denied (you must be root)
Perhaps iptables or your kernel needs to be upgraded. 

다음은 몇 가지 관련 질문입니다.

제 생각에는 둘 다 해결 방법을 제공하지만 제한 사항 뒤에 숨은 근본적인 이유에 대해서는 논의하지 않습니다.

답변1

iptables테이블을 읽으려면 CAP_NET_RAW 및 CAP_NET_ADMIN이 필요한 것 같습니다 . 나는 노력했다

$ cp /usr/sbin/iptables ~/iptables  # note, it may be a symbolic link
$ sudo setcap CAP_NET_RAW,CAP_NET_ADMIN+ep  ~/iptables
$ ~/iptables  -nvL

그것은 중요하지 않습니다.

답변2

실제로 iptablesnetlink 인터페이스는 커널과 통신하는 데 사용됩니다. netlink 소켓을 연 xtables다음 소켓을 통해 명령을 실행합니다. 개별 명령이 아닌 소켓이 열릴 때 액세스 제어가 수행되므로 규칙을 나열하고 수정하려면 동일한 권한이 필요합니다. 사용자가 규칙을 나열할 수 있지만 수정하지 못하게 하는 유일한 방법은 잘 작성된 setuid(또는 setcap) 실행 파일을 제공하는 것입니다.

netfilter에 대한 인터페이스가 있으면 좋겠지만 /proc, 내가 아는 한 이를 구현하는 작업은 완료되지 않았습니다.

답변3

이것이 내가 한 일입니다:

$ cat iptables-list.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main()
{
    if (setuid(0) != 0)
    {
        perror("setuid");
        return EXIT_FAILURE;
    }
    execl("/sbin/iptables", "iptables", "-L", "-n", NULL);
    perror("execl");
}
$ gcc -Wall -Werror -o iptables-list -s iptables-list.c
$ sudo mv iptables-list /sbin/iptables-list
$ sudo chown 0.0 /sbin/iptables-list
$ sudo chmod 4755 /sbin/iptables-list
$ ls -l /sbin/iptables-list
-rwsr-xr-x 1 root root 5552 Feb 23 08:14 /sbin/iptables-list
$ iptables-list
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
...

답변4

Ubuntu 20.04.5 LTS Focal에서는 설정하고 CAP_DAC_READ_SEARCH작동 해야 합니다 .CAP_NET_RAWCAP_NET_ADMIN

CAP_NET_RAW합계 만 설정하면 CAP_NET_ADMIN다음과 같은 오류가 표시됩니다.

Fatal: can't open lock file /run/xtables.lock: Permission denied

어떻게 작동하는지 살펴보겠습니다.

$ sudo cp /usr/sbin/ip6tables ~/ip6tables

$ sudo setcap cap_net_admin,cap_net_raw+ep ./ip6tables # not enough caps
$ ./ip6tables -nvL
Fatal: can't open lock file /run/xtables.lock: Permission denied

# CAP_NET_RAW + CAP_NET_ADMIN + CAP_DAC_READ_SEARCH capabilities
$ sudo setcap cap_dac_read_search,cap_net_admin,cap_net_raw+ep ./ip6tables 
$ ./ip6tables -nvL # works fine
Chain INPUT (policy ACCEPT 1813K packets, 4509M bytes)
=== /// ===

또한 프로그램을 실행하고 ip6tables를 하위 프로세스로 실행하려는 경우 해당 하위 프로세스에 필요한 기능을 전달해야 합니다.

Golang에 대해서는 다음을 참조하세요.이번 홍보그리고이 코드

관련 정보