(+84) 236.3827111 ex. 402

Chủ đề OSCP (Tăng cấp quyền Linux - Phần 1)


17.1. Liệt kê Linux

Đơn vị Học tập này bao gồm các Mục tiêu Học tập sau:

  • Hiểu về quyền của tệp và người dùng trên Linux
  • Thực hiện liệt kê thủ công
  • Thực hiện liệt kê tự động

Trong Đơn vị Học tập này, chúng ta sẽ bắt đầu với phần ôn lại về Linux sơ đồ đặc quyền, sau đó chuyển sang thực hiện thủ công và tự động kỹ thuật liệt kê.

17.1.1. Hiểu về quyền của tệp và người dùng trên Linux

Trước khi thảo luận về các kỹ thuật leo thang đặc quyền cụ thể, hãy cùng tìm hiểu tóm tắt các quyền và kiểm soát truy cập trên Linux.

Một trong những đặc điểm nổi bật của Linux và các biến thể UNIX khác là hầu hết các tài nguyên, bao gồm tập tin, thư mục, thiết bị, và thậm chí truyền thông mạng cũng được đại diện trong hệ thống tập tin.1 Nói một cách thông tục, "mọi thứ đều là một tập tin".

Mỗi tệp (và theo đó là mọi thành phần của hệ thống Linux) đều tồn tại bởi quyền của người dùng và nhóm dựa trên ba thuộc tính chính: đọc (ký hiệu bằng r), viết (ký hiệu bằng w), và thực thi (ký hiệu bằng x). Mỗi tệp hoặc thư mục có các mục cụ thể quyền cho ba nhóm người dùng: chủ sở hữu, chủ sở hữu nhóm và nhóm khác.

Mỗi quyền (rwx) cho phép tập hợp người dùng được chỉ định thực hiện các hành động khác nhau tùy thuộc vào việc tài nguyên là tệp hay Thư mục.

Đối với tệp, r cho phép đọc nội dung tệp, w cho phép thay đổi Nội dung và X cho phép chạy tệp. Một thư mục được xử lý khác với một tệp. Truy cập đọc mang lại quyền tham khảo danh sách nội dung của nó (tệp và thư mục). Truy cập ghi cho phép tạo hoặc xóa tệp. Cuối cùng, thực thi truy cập cho phép vượt qua thư mục để truy cập nội dung của nó (ví dụ sử dụng lệnh cd). Có khả năng vượt qua một thư mục mà không thể đọc nó cho phép người dùng truy cập các mục đã biết, nhưng chỉ khi biết chính xác tên của chúng.

Hãy cùng xem xét một sự kết hợp đơn giản của các quyền tệp đó bằng cách sử dụng một ví dụ thực tế trên máy Kali địa phương của chúng tôi, vì nó dựa trên Bản phân phối Debian trên Linux.

kali@kali:~$ ls -l /etc/shadow

-rw-r----- 1 root shadow 1751 May  2 09:31 /etc/shadow

Danh sách 1 - Kiểm tra quyền tệp và quyền sở hữu của người dùng

Đối với mỗi danh mục người dùng, ba quyền truy cập khác nhau được hiển thị. Dấu gạch nối đầu tiên chúng ta gặp mô tả loại tệp. Vì nó không hoàn toàn liên quan đến file các quyền truy cập,2 Chúng ta có thể yên tâm bỏ qua nó.

Ba ký tự tiếp theo hiển thị quyền chủ sở hữu tệp (root), mà là RW-, nghĩa là chủ sở hữu đã đọc và ghi, nhưng chưa thực thi đặc quyền. Tiếp theo, chủ sở hữu nhóm bóng chỉ được đọc truy cập, vì cờ ghi và thực thi không được đặt. Cuối cùng, những người khác Nhóm chưa được cấp quyền truy cập nào cho tệp này.

Giờ đây chúng ta có thể áp dụng kiến thức nhập môn về tệp Linux này quyền khi thực hiện liệt kê nâng cấp đặc quyền trong Phần tiếp theo.

1(Arch Linux, 2022), https://wiki.archlinux.org/index.php/users_and_groups ↩︎

2(Wikipedia, 2022), https://en.wikipedia.org/wiki/File-system_permissions ↩︎
 

17.1.2. Liệt kê thủ công

Việc liệt kê thủ công các hệ thống Linux có thể tốn nhiều thời gian. Tuy nhiên, Cách tiếp cận này cho phép kết quả kiểm soát tốt hơn vì nó hỗ trợ xác định các phương pháp nâng cấp đặc quyền kỳ lạ hơn thường bị bỏ qua bởi các công cụ tự động.

Hơn nữa, việc liệt kê tự động không thể thay thế việc điều tra thủ công bởi vì các thiết lập tùy chỉnh của môi trường mục tiêu của chúng tôi có khả năng cao chính xác là những người bị cấu hình sai.

Một số lệnh trong Module này có thể cần chỉnh sửa nhỏ tùy thuộc vào phiên bản hệ điều hành mục tiêu. Ngoài ra, không tất cả các lệnh trình bày trong phần này sẽ có thể tái tạo trên Khách hàng tận tâm.

Khi tiếp cận mục tiêu ban đầu, một trong những lần đầu tiên Những điều chúng ta nên xác định là ngữ cảnh người dùng. Chúng ta có thể dùng id1 lệnh để thu thập ngữ cảnh người dùng thông tin. Chúng ta có thể làm điều đó bằng cách kết nối qua SSH với tư cách là người dùng joe đến máy phòng thí nghiệm Debian của chúng tôi.

joe@debian-privesc:~$ id

uid=1000(joe) gid=1000(joe) groups=1000(joe),24(cdrom),25(floppy),29(audio),30(dip),44(video),46(plugdev),109(netdev),112(bluetooth),116(lpadmin),117(scanner)

Danh sách 2 - Thu thập thông tin về người dùng hiện tại

Kết quả cho thấy chúng ta đang hoạt động như người dùng joe, điều này đã một Định danh Người dùng (UID)2 và Định danh Nhóm (GID) của 1000. Người dùng joe cũng là thành viên của các nhóm khác nằm ngoài phạm vi cho Mô-đun này.

Để liệt kê tất cả người dùng, chúng ta chỉ cần đọc nội dung của tệp /etc/passwd.

joe@debian-privesc:~$ cat /etc/passwd

root:x:0:0:root:/root:/bin/bash

daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin

bin:x:2:2:bin:/bin:/usr/sbin/nologin

sys:x:3:3:sys:/dev:/usr/sbin/nologin

...

www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin

...

dnsmasq:x:106:65534:dnsmasq,,,:/var/lib/misc:/usr/sbin/nologin

usbmux:x:107:46:usbmux daemon,,,:/var/lib/usbmux:/usr/sbin/nologin

rtkit:x:108:114:RealtimeKit,,,:/proc:/usr/sbin/nologin

sshd:x:109:65534::/run/sshd:/usr/sbin/nologin

...

Debian-gdm:x:117:124:Gnome Display Manager:/var/lib/gdm3:/bin/false

joe:x:1000:1000:joe,,,:/home/joe:/bin/bash

systemd-coredump:x:999:999:systemd Core Dumper:/:/usr/sbin/nologin

eve:x:1001:1001:,,,:/home/eve:/bin/bash

Danh sách 3 - Nhận được Thông tin về người dùng

Tệp passwd liệt kê một số tài khoản người dùng, bao gồm Các tài khoản được sử dụng bởi nhiều dịch vụ trên máy đích như www-data và SSHD. Điều này chỉ ra rằng một máy chủ web và một SSH Máy chủ có thể đã được cài đặt trên hệ thống.

Giờ đây chúng ta có thể phóng to dữ liệu của người dùng hiện tại:

  • Tên đăng nhập: "joe" - Chỉ tên người dùng được sử dụng để đăng nhập.
  • Mật khẩu mã hóa: "x" - Trường này thường chứa phiên bản mật khẩu đã băm của người dùng. Trong trường hợp này, giá trị X có nghĩa là toàn bộ hàm băm mật khẩu nằm trong tệp /etc/shadow (sẽ nói thêm về điều này ngay sau).
  • UID: "1000" - Ngoài người dùng gốc luôn có UID bằng 0, Linux bắt đầu đếm ID người dùng thông thường từ 1000. Giá trị này là còn gọi là ID người dùng thực.
  • GID: "1000" - Đại diện cho ID Nhóm cụ thể của người dùng.
  • Bình luận: "joe,,," - Trường này thường chứa một mô tả về người dùng, thường chỉ lặp lại tên người dùng thông tin.
  • Thư mục chính: "/home/joe" - Mô tả ngôi nhà của người dùng thư mục được nhắc khi đăng nhập.
  • Shell đăng nhập: "/bin/bash" - Chỉ định tương tác mặc định Shell, nếu có.

Ngoài người dùng joe, chúng ta còn nhận thấy một người dùng khác tên là eve, và có thể suy ra đây là người dùng tiêu chuẩn vì nó có Thư mục chính /home/eve đã cấu hình. Mặt khác, hệ thống các dịch vụ được cấu hình với /usr/sbin/nologin làm shell đăng nhập, trong đó câu lệnh nologin được sử dụng để chặn bất kỳ câu lệnh từ xa hoặc cục bộ nào Đăng nhập bằng tài khoản dịch vụ.

Liệt kê tất cả người dùng trên một máy mục tiêu có thể giúp xác định tiềm năng các tài khoản người dùng có đặc quyền cao mà chúng ta có thể nhắm đến nhằm nâng cao đặc quyền của chúng ta.

Tiếp theo, tên máy chủ của một máy thường có thể cung cấp manh mối về nó vai trò chức năng. Thường thì các tên máy chủ sẽ bao gồm các chữ viết tắt có thể nhận diện như web cho máy chủ web, db cho a Máy chủ cơ sở dữ liệu, DC cho bộ điều khiển miền, v.v.

Trên hầu hết các bản phân phối Linux, chúng ta có thể tìm thấy tên máy chủ được nhúng trong Dấu nhắc lệnh. Tuy nhiên, chúng ta chỉ nên dựa vào các lệnh hệ thống để lấy thông tin của mục tiêu, vì đôi khi văn bản của lời nhắc có thể gây hiểu lầm.

Chúng ta có thể phát hiện tên máy chủ với tên máy chủ rất phù hợp3 chỉ huy.

joe@debian-privesc:~$ hostname

debian-privesc

Danh sách 4 - Lấy thông tin về tên máy chủ

Enterprises often enforce a naming convention scheme for hostnames, so they can be categorized by location, description, operating system, and service level. In our case, the hostname is comprised of only two parts: the OS type and the description.

Identifying the role of a machine can help us focus our information gathering efforts by increasing the context surrounding the host.

At some point during the privilege escalation process, we may need to rely on kernel4 exploits that specifically exploit vulnerabilities in the core of a target's operating system. These types of exploits are built for a very specific type of target, specified by a particular operating system and version combination. Since attacking a target with a mismatched kernel exploit can lead to system instability or even a crash, we must gather precise information about the target.

Any system instability caused by our penetration testing activity would likely alert system administrators prior to any SOC team. For this reason, we should be twice as careful when dealing with kernel exploits and, when possible, test the exploits in a local environment beforehand.

The /etc/issue and /etc/*-release files contain information about the operating system release and version. We can also run the uname -a5 command:

joe@debian-privesc:~$ cat /etc/issue

Debian GNU/Linux 10 \n \l

 

joe@debian-privesc:~$ cat /etc/os-release

PRETTY_NAME="Debian GNU/Linux 10 (buster)"

NAME="Debian GNU/Linux"

VERSION_ID="10"

VERSION="10 (buster)"

VERSION_CODENAME=buster

ID=debian

HOME_URL="https://www.debian.org/"

SUPPORT_URL="https://www.debian.org/support"

BUG_REPORT_URL="https://bugs.debian.org/"

 

joe@debian-privesc:~$ uname -a

Linux debian-privesc 4.19.0-21-amd64 #1 SMP Debian 4.19.249-2 (2022-06-30)

x86_64 GNU/Linux

Listing 5 - Getting the version of the running operating system and architecture

The issue and os-release files located in the /etc directory contain the operating system version (Debian 10) and release-specific information, including the distribution codename (buster). The command uname -a outputs the kernel version (4.19.0) and architecture (x86_64).

Next, let's explore which running processes and services may allow us to elevate our privileges. For this to occur, the process must run in the context of a privileged account and must either have insecure permissions or allow us to interact with it in unintended ways.

We can list system processes (including those run by privileged users) with the ps6 command. We'll use the a and x flags to list all processes with or without a tty7 and the u flag to list the processes in a user-readable format.

joe@debian-privesc:~$ ps aux

USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND

root         1  0.0  0.4 169592 10176 ?        Ss   Aug16   0:02 /sbin/init

...

colord     752  0.0  0.6 246984 12424 ?        Ssl  Aug16   0:00 /usr/lib/colord/colord

Debian-+   753  0.0  0.2 157188  5248 ?        Sl   Aug16   0:00 /usr/lib/dconf/dconf-service

root       477  0.0  0.5 179064 11060 ?        Ssl  Aug16   0:00 /usr/sbin/cups-browsed

root       479  0.0  0.4 236048  9152 ?        Ssl  Aug16   0:00 /usr/lib/policykit-1/polkitd --no-debug

root       486  0.0  1.0 123768 22104 ?        Ssl  Aug16   0:00 /usr/bin/python3 /usr/share/unattended-upgrades/unattended-upgrade-shutdown --wait-for-signal

root       510  0.0  0.3  13812  7288 ?        Ss   Aug16   0:00 /usr/sbin/sshd -D

root       512  0.0  0.3 241852  8080 ?        Ssl  Aug16   0:00 /usr/sbin/gdm3

root       519  0.0  0.4 166764  8308 ?        Sl   Aug16   0:00 gdm-session-worker [pam/gdm-launch-environment]

root       530  0.0  0.2  11164  4448 ?        Ss   Aug16   0:03 /usr/sbin/apache2 -k start

root      1545  0.0  0.0      0     0 ?        I    Aug16   0:00 [kworker/1:1-events]

root      1653  0.0  0.3  14648  7712 ?        Ss   01:03   0:00 sshd: joe [priv]

root      1656  0.0  0.0      0     0 ?        I    01:03   0:00 [kworker/1:2-events_power_efficient]

joe       1657  0.0  0.4  21160  8960 ?        Ss   01:03   0:00 /lib/systemd/systemd --user

joe       1658  0.0  0.1 170892  2532 ?        S    01:03   0:00 (sd-pam)

joe       1672  0.0  0.2  14932  5064 ?        S    01:03   0:00 sshd: joe@pts/0

joe       1673  0.0  0.2   8224  5020 pts/0    Ss   01:03   0:00 -bash

root      1727  0.0  0.0      0     0 ?        I    03:00   0:00 [kworker/0:0-ata_sff]

root      1728  0.0  0.0      0     0 ?        I    03:06   0:00 [kworker/0:2-ata_sff]

joe       1730  0.0  0.1  10600  3028 pts/0    R+   03:10   0:00 ps axu

Danh sách 6 - Lấy danh sách chạy các tiến trình trên Linux

Kết quả đầu ra liệt kê một số tiến trình đang chạy dưới dạng gốc và có giá trị nghiên cứu các lỗ hổng có thể có. Chúng ta sẽ nhận thấy lệnh ps mà chúng ta chạy cũng được liệt kê trong đầu ra, thuộc sở hữu của người hiện tại người dùng. Chúng ta cũng có thể lọc quy trình do người dùng sở hữu cụ thể từ xuất ra với tên người dùng phù hợp.

Bước tiếp theo trong phân tích máy chủ mục tiêu của chúng tôi là xem xét lại Các giao diện mạng, tuyến đường và cổng mở có sẵn. Thông tin này có thể giúp chúng tôi xác định liệu mục tiêu bị xâm nhập có liên quan đến hay không nhiều mạng lưới và do đó có thể được sử dụng như một điểm trung tâm. Sự hiện diện của các giao diện ảo cụ thể cũng có thể chỉ ra sự tồn tại của Phần mềm ảo hóa hoặc phần mềm diệt virus.

Kẻ tấn công có thể sử dụng mục tiêu bị xâm nhập để xoay chuyển hoặc di chuyển giữa các mục tiêu mạng kết nối. Điều này sẽ tăng cường khả năng quan sát mạng và cho phép kẻ tấn công nhắm vào các máy chủ không thể truy cập trực tiếp từ bản gốc Máy tấn công.

Chúng ta cũng có thể kiểm tra các binding cổng để xem có dịch vụ đang chạy hay không chỉ khả dụng trên địa chỉ loopback, thay vì trên địa chỉ định tuyến Một. Điều tra một chương trình hoặc dịch vụ đặc quyền nghe lén trên giao diện loopback có thể mở rộng bề mặt tấn công và tăng cường Xác suất thành công của một cuộc tấn công leo thang đặc quyền.

Tùy thuộc vào phiên bản Linux, chúng ta có thể liệt kê Cấu hình TCP/IP của mọi bộ điều hợp mạng với một trong hai IFCONFIG8 Hoặc IP.9 Trong khi lệnh đầu hiển thị thống kê giao diện, lệnh sau hiển thị thống kê giao diện cung cấp phiên bản cô đọng của cùng thông tin. Cả hai chỉ huy Chấp nhận cờ A để hiển thị tất cả thông tin có sẵn.

joe@debian-privesc:~$ ip a

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000

    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

    inet 127.0.0.1/8 scope host lo

       valid_lft forever preferred_lft forever

    inet6 ::1/128 scope host

       valid_lft forever preferred_lft forever

2: ens192: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000

    link/ether 00:50:56:8a:b9:fc brd ff:ff:ff:ff:ff:ff

    inet 192.168.50.214/24 brd 192.168.50.255 scope global ens192

       valid_lft forever preferred_lft forever

    inet6 fe80::250:56ff:fe8a:b9fc/64 scope link

       valid_lft forever preferred_lft forever

3: ens224: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000

    link/ether 00:50:56:8a:72:64 brd ff:ff:ff:ff:ff:ff

    inet 172.16.60.214/24 brd 172.16.60.255 scope global ens224

       valid_lft forever preferred_lft forever

    inet6 fe80::250:56ff:fe8a:7264/64 scope link

       valid_lft forever preferred_lft forever

Danh sách 7 - Liệt kê toàn bộ Cấu hình TCP/IP trên tất cả các bộ điều hợp có sẵn trên Linux

Dựa trên kết quả trên, ứng dụng Linux cũng được kết nối với nhiều hơn hơn là một mạng lưới.

Chúng ta có thể hiển thị bảng định tuyến mạng với bất kỳ tuyến nào10 hoặc routel,11 tùy thuộc vào bản phân phối Linux và phiên bản. Cả hai lệnh đều cung cấp Thông tin tương tự.

joe@debian-privesc:~$ routel

         target            gateway          source    proto    scope    dev tbl

/usr/bin/routel: 48: shift: can't shift that many

        default     192.168.50.254                   static          ens192

    172.16.60.0 24                   172.16.60.214   kernel     link ens224

   192.168.50.0 24                  192.168.50.214   kernel     link ens192

      127.0.0.0          broadcast       127.0.0.1   kernel     link     lo local

      127.0.0.0 8            local       127.0.0.1   kernel     host     lo local

      127.0.0.1              local       127.0.0.1   kernel     host     lo local

127.255.255.255          broadcast       127.0.0.1   kernel     link     lo local

    172.16.60.0          broadcast   172.16.60.214   kernel     link ens224 local

  172.16.60.214              local   172.16.60.214   kernel     host ens224 local

  172.16.60.255          broadcast   172.16.60.214   kernel     link ens224 local

   192.168.50.0          broadcast  192.168.50.214   kernel     link ens192 local

 192.168.50.214              local  192.168.50.214   kernel     host ens192 local

 192.168.50.255          broadcast  192.168.50.214   kernel     link ens192 local

            ::1                                      kernel              lo

         fe80:: 64                                   kernel          ens224

         fe80:: 64                                   kernel          ens192

            ::1              local                   kernel              lo local

fe80::250:56ff:fe8a:7264              local                   kernel          ens224 local

fe80::250:56ff:fe8a:b9fc              local                   kernel          ens192 local

Danh sách 8 - In các tuyến trên Linux

Cuối cùng, chúng ta có thể hiển thị các kết nối mạng đang hoạt động và nghe lén Cổng sử dụng một trong hai Netstat12 hoặc SS,13 cả hai đều chấp nhận cùng một tranh luận.

Ví dụ, ta có thể liệt kê tất cả các kết nối với -a, tránh giải quyết hostname (có thể làm chậm quá trình thực thi lệnh) với -n, và liệt kê tên tiến trình mà kết nối thuộc về với -p. Ta có thể kết hợp các đối số và đơn giản chạy ss -anp:

joe@debian-privesc:~$ ss -anp

Netid      State       Recv-Q      Send-Q                                        Local Address:Port                     Peer Address:Port

nl         UNCONN      0           0                                                         0:461                                  *

nl         UNCONN      0           0                                                         0:323                                  *

nl         UNCONN      0           0                                                         0:457                                  *

...

udp        UNCONN      0           0                                                      [::]:47620                            [::]:*

tcp        LISTEN      0           128                                                 0.0.0.0:22                            0.0.0.0:*

tcp        LISTEN      0           5                                                 127.0.0.1:631                           0.0.0.0:*

tcp        ESTAB       0           36                                           192.168.50.214:22                      192.168.118.2:32890

tcp        LISTEN      0           128                                                       *:80                                  *:*

tcp        LISTEN      0           128                                                    [::]:22                               [::]:*

tcp        LISTEN      0           5                                                     [::1]:631                              [::]:*

Danh sách 9 - Liệt kê tất cả kết nối mạng đang hoạt động trên Linux

Đầu ra liệt kê các cổng nghe và phiên đang hoạt động khác nhau, bao gồm cả kết nối SSH đang hoạt động của riêng chúng tôi và ổ nghe của nó.

Tiếp tục với việc liệt kê cơ sở, hãy tập trung tiếp theo vào tường lửa quy tắc.

Nói chung, chúng tôi chủ yếu quan tâm đến trạng thái, hồ sơ của tường lửa, và các quy tắc trong giai đoạn khai thác từ xa của một đánh giá. Tuy nhiên, thông tin này cũng có thể hữu ích trong quá trình đặc quyền leo thang. Ví dụ, nếu một dịch vụ mạng không được đặt từ xa có thể truy cập vì bị chặn bởi tường lửa, thường thì truy cập cục bộ qua giao diện loopback. Nếu chúng ta có thể tương tác với Các dịch vụ này tại địa phương, chúng tôi có thể tận dụng chúng để nâng cấp các dịch vụ của chúng tôi đặc quyền trên hệ thống cục bộ.

Trong giai đoạn này, chúng tôi cũng có thể thu thập thông tin về các luồng vào và lọc cổng đi để hỗ trợ chuyển tiếp cổng và tunneling Khi đến lúc chuyển sang mạng nội bộ.

Trên các hệ thống dựa trên Linux, chúng ta phải có quyền root để liệt kê Quy tắc tường lửa với iptables.14 Tuy nhiên, tùy thuộc vào cách cấu hình tường lửa, chúng ta có thể để thu thập thông tin về các quy tắc với tư cách là người dùng tiêu chuẩn.

Ví dụ, iptables-persistent15 gói trên Debian Linux lưu các quy tắc tường lửa trong các tệp cụ thể dưới /etc/iptables theo mặc định. Các tệp này được hệ thống sử dụng để khôi phục netfilter16 Quy tắc khi khởi động. Những tập tin này thường được để lại với quyền yếu, cho phép bất kỳ người dùng cục bộ nào đọc trên hệ thống mục tiêu.

Chúng ta cũng có thể tìm kiếm các tệp được tạo bằng lệnh iptables-save, được sử dụng để dump cấu hình tường lửa vào một tệp được chỉ định bởi người dùng. Tệp này thường được sử dụng làm đầu vào cho lệnh iptables-restore và dùng để khôi phục các quy tắc tường lửa Vào giờ khởi động. Nếu một quản trị viên hệ thống từng chạy lệnh này, chúng ta có thể tìm kiếm thư mục cấu hình (/etc) hoặc grep Hệ thống tệp cho các lệnh iptables để xác định vị trí tệp. Nếu tệp có quyền không an toàn, chúng ta có thể dùng nội dung để suy luận tường lửa Các quy tắc cấu hình đang chạy trên hệ thống.

joe@debian-privesc:~$ cat /etc/iptables/rules.v4

# Generated by xtables-save v1.8.2 on Thu Aug 18 12:53:22 2022

*filter

:INPUT ACCEPT [0:0]

:FORWARD ACCEPT [0:0]

:OUTPUT ACCEPT [0:0]

-A INPUT -p tcp -m tcp --dport 1999 -j ACCEPT

COMMIT

# Completed on Thu Aug 18 12:53:22 2022

Danh sách 10 - Kiểm tra bảng IP tùy chỉnh

Vì tệp này chỉ được đọc bởi bất kỳ người dùng nào ngoài root, chúng ta có thể Kiểm tra nội dung bên trong. Chúng ta sẽ nhận thấy một quy tắc không mặc định mà rõ ràng cho phép cổng đích 1999. Chi tiết cấu hình này được giữ nguyên và sẽ được ghi chú để điều tra sau.

Tiếp theo, hãy cùng xem xét các nhiệm vụ đã lên lịch mà kẻ tấn công thường tận dụng trong các cuộc tấn công leo thang đặc quyền. Các hệ thống thường hoạt động như máy chủ định kỳ thực hiện các tác vụ tự động, được lên lịch khác nhau. Khi những điều này hệ thống bị cấu hình sai, hoặc các tệp do người dùng tạo ra bị bỏ lại với quyền không an toàn, chúng ta có thể chỉnh sửa các tệp sẽ được thực thi bởi hệ thống lập lịch ở mức đặc quyền cao.

Trình lập lịch công việc dựa trên Linux được gọi là cron.17 Các tác vụ đã lên lịch được liệt kê dưới các thư mục /etc/cron.*, trong đó * biểu thị tần suất mà tác vụ diễn ra sẽ chạy. Ví dụ, các tác vụ sẽ được chạy hàng ngày có thể được tìm thấy dưới /etc/cron.daily. Mỗi script được liệt kê riêng thư mục con.

joe@debian-privesc:~$ ls -lah /etc/cron*

-rw-r--r-- 1 root root 1.1K Oct 11  2019 /etc/crontab

 

/etc/cron.d:

total 24K

drwxr-xr-x   2 root root 4.0K Aug 16 04:25 .

drwxr-xr-x 120 root root  12K Aug 18 12:37 ..

-rw-r--r--   1 root root  102 Oct 11  2019 .placeholder

-rw-r--r--   1 root root  285 May 19  2019 anacron

 

/etc/cron.daily:

total 60K

drwxr-xr-x   2 root root 4.0K Aug 18 09:05 .

drwxr-xr-x 120 root root  12K Aug 18 12:37 ..

-rw-r--r--   1 root root  102 Oct 11  2019 .placeholder

-rwxr-xr-x   1 root root  311 May 19  2019 0anacron

-rwxr-xr-x   1 root root  539 Aug  8  2020 apache2

-rwxr-xr-x   1 root root 1.5K Dec  7  2020 apt-compat

-rwxr-xr-x   1 root root  355 Dec 29  2017 bsdmainutils

-rwxr-xr-x   1 root root  384 Dec 31  2018 cracklib-runtime

-rwxr-xr-x   1 root root 1.2K Apr 18  2019 dpkg

-rwxr-xr-x   1 root root 2.2K Feb 10  2018 locate

-rwxr-xr-x   1 root root  377 Aug 28  2018 logrotate

-rwxr-xr-x   1 root root 1.1K Feb 10  2019 man-db

-rwxr-xr-x   1 root root  249 Sep 27  2017 passwd

 

/etc/cron.hourly:

total 20K

drwxr-xr-x   2 root root 4.0K Aug 16 04:17 .

drwxr-xr-x 120 root root  12K Aug 18 12:37 ..

-rw-r--r--   1 root root  102 Oct 11  2019 .placeholder

 

/etc/cron.monthly:

total 24K

drwxr-xr-x   2 root root 4.0K Aug 16 04:25 .

drwxr-xr-x 120 root root  12K Aug 18 12:37 ..

-rw-r--r--   1 root root  102 Oct 11  2019 .placeholder

-rwxr-xr-x   1 root root  313 May 19  2019 0anacron

 

/etc/cron.weekly:

total 28K

drwxr-xr-x   2 root root 4.0K Aug 16 04:26 .

drwxr-xr-x 120 root root  12K Aug 18 12:37 ..

-rw-r--r--   1 root root  102 Oct 11  2019 .placeholder

-rwxr-xr-x   1 root root  312 May 19  2019 0anacron

-rwxr-xr-x   1 root root  813 Feb 10  2019 man-db

joe@debian-privesc:~$

Danh sách 11 - Danh sách tất cả cron công việc

Khi liệt kê nội dung thư mục, chúng tôi nhận thấy một số tác vụ được lên lịch để Chạy hàng ngày.

Cần lưu ý rằng quản trị viên hệ thống thường thêm các công cụ của riêng họ Các tác vụ đã lên lịch trong tệp /etc/crontab. Những nhiệm vụ này nên được kiểm tra kỹ lưỡng để phát hiện quyền truy cập tệp không an toàn, vì hầu hết Các công việc trong tệp cụ thể này sẽ chạy dưới dạng root. Để xem hiện tại Các công việc đã lên lịch của người dùng, chúng ta có thể chạy CronTab kèm theo tham số -l.

joe@debian-privesc:~$ crontab -l

# Edit this file to introduce tasks to be run by cron.

#

# Each task to run has to be defined through a single line

# indicating with different fields when the task will be run

# and what command to run for the task

#

# To define the time you can provide concrete values for

# minute (m), hour (h), day of month (dom), month (mon),

# and day of week (dow) or use '*' in these fields (for 'any').

#

# Notice that tasks will be started based on the cron's system

# daemon's notion of time and timezones.

#

# Output of the crontab jobs (including errors) is sent through

# email to the user the crontab file belongs to (unless redirected).

#

# For example, you can run a backup of all your user accounts

# at 5 a.m every week with:

# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/

#

# For more information see the manual pages of crontab(5) and cron(8)

#

# m h  dom mon dow   command

 

Danh sách 12 - Đăng các công việc cron cho người dùng hiện tại

Trong đầu ra trên, chỉ có các lệnh được chú thích, Có nghĩa là chưa có công việc CRON nào được cấu hình cho người dùng Joe. Tuy nhiên, Nếu ta thử chạy cùng lệnh với tiền tố sudo, ta phát hiện rằng một script sao lưu được lên lịch chạy mỗi phút.

joe@debian-privesc:~$ sudo crontab -l

[sudo] password for joe:

# Edit this file to introduce tasks to be run by cron.

...

# m h  dom mon dow   command

 

* * * * * /bin/bash /home/joe/.scripts/user_backups.sh

Danh sách 13 - Đăng các công việc cron cho người dùng gốc

Liệt kê các công việc cron bằng sudo sẽ tiết lộ các công việc do người dùng gốc chạy. Trong Ví dụ này, nó hiển thị một script sao lưu chạy dưới dạng root. Nếu tệp này có quyền hạn yếu, chúng ta có thể tận dụng nó để leo thang đặc quyền.

Như chúng ta sẽ biết sau trong Module này, người dùng joe đã được chỉ cấp quyền SUDO cụ thể để liệt kê các công việc cron đang chạy dưới dạng Người dùng root. Chỉ riêng quyền này không thể bị lạm dụng để lấy một gốc Vỏ sò.

Đến một lúc nào đó, chúng ta có thể cần tận dụng một lỗ hổng để leo thang đặc quyền địa phương. Nếu đúng như vậy, việc tìm kiếm một lỗ hổng hoạt động của chúng tôi bắt đầu với liệt kê tất cả các ứng dụng đã cài đặt, ghi chú phiên bản của mỗi người. Chúng ta có thể sử dụng thông tin này để tìm kiếm một lỗ hổng phù hợp.

Tìm kiếm thông tin này thủ công có thể rất tốn thời gian và không hiệu quả, vì vậy chúng ta sẽ học cách tự động hóa quy trình này trong Phần tiếp theo. Tuy nhiên, chúng ta nên biết cách truy vấn thủ công khi đã cài đặt các gói này cần thiết để xác nhận thông tin thu thập được trong quá trình Các bước liệt kê trước đó.

Các hệ thống dựa trên Linux sử dụng nhiều trình quản lý gói khác nhau. Vì ví dụ, các bản phân phối Linux dựa trên Debian, như bản trong phòng thí nghiệm, sử dụng dpkg,18 trong khi các hệ thống dựa trên Red Hat sử dụng vòng tua (rpm).19

Để liệt kê các ứng dụng được dpkg cài đặt trên hệ thống Debian của chúng tôi, chúng tôi có thể dùng dpkg -l.

joe@debian-privesc:~$ dpkg -l

Desired=Unknown/Install/Remove/Purge/Hold

| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend

|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)

||/ Name                                  Version                                      Architecture Description

+++-=====================================-============================================-============-===============================================================================

ii  accountsservice                       0.6.45-2                                     amd64        query and manipulate user account information

ii  acl                                   2.2.53-4                                     amd64        access control list - utilities

ii  adduser                               3.118                                        all          add and remove users and groups

ii  adwaita-icon-theme                    3.30.1-1                                     all          default icon theme of GNOME

ii  aisleriot                             1:3.22.7-2                                   amd64        GNOME solitaire card game collection

ii  alsa-utils                            1.1.8-2                                      amd64        Utilities for configuring and using ALSA

ii  anacron                               2.3-28                                       amd64        cron-like program that doesn't go by time

ii  analog                                2:6.0-22                                     amd64        web server log analyzer

ii  apache2                               2.4.38-3+deb10u7                             amd64        Apache HTTP Server

ii  apache2-bin                           2.4.38-3+deb10u7                             amd64        Apache HTTP Server (modules and other binary files)

ii  apache2-data                          2.4.38-3+deb10u7                             all          Apache HTTP Server (common files)

ii  apache2-doc                           2.4.38-3+deb10u7                             all          Apache HTTP Server (on-site documentation)

ii  apache2-utils                         2.4.38-3+deb10u7                             amd64        Apache HTTP Server (utility programs for web servers)

...

Listing 14 - Listing all installed packages on a Debian Linux operating system

This confirms what we expected earlier from enumerating listening ports: the Debian 10 machine is, in fact, running a web server. In this case, it is running Apache2.

As we previously mentioned, files with insufficient access restrictions can create a vulnerability that may grant an attacker elevated privileges. This most often happens when an attacker can modify scripts or binary files that are executed under the context of a privileged account.

Sensitive files that are readable by an unprivileged user may also contain important information such as hard-coded credentials for a database or a service account running with higher privileges.

Since it is not feasible to manually check the permissions of each file and directory, we need to automate this task as much as possible. As a start, we can use find20 to identify files with insecure permissions.

In the example below, we are searching for every directory writable by the current user on the target system. We'll search the whole root directory (/) and use the -writable argument to specify the attribute we are interested in. We can also use -type d to locate directories, and filter errors with 2>/dev/null:

joe@debian-privesc:~$ find / -writable -type d 2>/dev/null

..

/home/joe

/home/joe/Videos

/home/joe/Templates

/home/joe/.local

/home/joe/.local/share

/home/joe/.local/share/sounds

/home/joe/.local/share/evolution

/home/joe/.local/share/evolution/tasks

/home/joe/.local/share/evolution/tasks/system

/home/joe/.local/share/evolution/tasks/trash

/home/joe/.local/share/evolution/addressbook

/home/joe/.local/share/evolution/addressbook/system

/home/joe/.local/share/evolution/addressbook/system/photos

/home/joe/.local/share/evolution/addressbook/trash

/home/joe/.local/share/evolution/mail

/home/joe/.local/share/evolution/mail/trash

/home/joe/.local/share/evolution/memos

/home/joe/.local/share/evolution/memos/system

/home/joe/.local/share/evolution/memos/trash

/home/joe/.local/share/evolution/calendar

/home/joe/.local/share/evolution/calendar/system

/home/joe/.local/share/evolution/calendar/trash

/home/joe/.local/share/icc

/home/joe/.local/share/gnome-shell

/home/joe/.local/share/gnome-settings-daemon

/home/joe/.local/share/keyrings

/home/joe/.local/share/tracker

/home/joe/.local/share/tracker/data

/home/joe/.local/share/folks

/home/joe/.local/share/gvfs-metadata

/home/joe/.local/share/applications

/home/joe/.local/share/nano

/home/joe/Downloads

/home/joe/.scripts

/home/joe/Pictures

/home/joe/.cache

 

...

Listing 15 - Listing all world writable directories

As shown above, several directories seem to be world-writable, including the /home/joe/.scripts directory, which is the location of the cron script we found earlier. This certainly warrants further investigation.

Let's move further with our enumeration. On most systems, drives are automatically mounted at boot time. Because of this, it's easy to forget about unmounted drives that could contain valuable information. We should always look for unmounted drives, and if they exist, check the mount permissions.

On Linux-based systems, we can use mount21 to list all mounted filesystems. In addition, the /etc/fstab22 file lists all drives that will be mounted at boot time.

joe@debian-privesc:~$ cat /etc/fstab

...

UUID=60b4af9b-bc53-4213-909b-a2c5e090e261 /               ext4    errors=remount-ro 0       1

# swap was on /dev/sda5 during installation

UUID=86dc11f3-4b41-4e06-b923-86e78eaddab7 none            swap    sw              0       0

/dev/sr0        /media/cdrom0   udf,iso9660 user,noauto     0       0

 

joe@debian-privesc:~$ mount

sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime)

proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)

udev on /dev type devtmpfs (rw,nosuid,relatime,size=1001064k,nr_inodes=250266,mode=755)

devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000)

tmpfs on /run type tmpfs (rw,nosuid,noexec,relatime,size=204196k,mode=755)

/dev/sda1 on / type ext4 (rw,relatime,errors=remount-ro)

securityfs on /sys/kernel/security type securityfs (rw,nosuid,nodev,noexec,relatime)

tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev)

tmpfs on /run/lock type tmpfs (rw,nosuid,nodev,noexec,relatime,size=5120k)

tmpfs on /sys/fs/cgroup type tmpfs (ro,nosuid,nodev,noexec,mode=755)

cgroup2 on /sys/fs/cgroup/unified type cgroup2 (rw,nosuid,nodev,noexec,relatime,nsdelegate)

cgroup on /sys/fs/cgroup/systemd type cgroup (rw,nosuid,nodev,noexec,relatime,xattr,name=systemd)

pstore on /sys/fs/pstore type pstore (rw,nosuid,nodev,noexec,relatime)

bpf on /sys/fs/bpf type bpf (rw,nosuid,nodev,noexec,relatime,mode=700)

...

systemd-1 on /proc/sys/fs/binfmt_misc type autofs (rw,relatime,fd=25,pgrp=1,timeout=0,minproto=5,maxproto=5,direct,pipe_ino=10550)

mqueue on /dev/mqueue type mqueue (rw,relatime)

debugfs on /sys/kernel/debug type debugfs (rw,relatime)

hugetlbfs on /dev/hugepages type hugetlbfs (rw,relatime,pagesize=2M)

tmpfs on /run/user/117 type tmpfs (rw,nosuid,nodev,relatime,size=204192k,mode=700,uid=117,gid=124)

tmpfs on /run/user/1000 type tmpfs (rw,nosuid,nodev,relatime,size=204192k,mode=700,uid=1000,gid=1000)

binfmt_misc on /proc/sys/fs/binfmt_misc type binfmt_misc (rw,relatime)

Listing 16 - Listing content of /etc/fstab and all mounted drives

The output reveals a swap partition and the primary ext4 disk of this Linux system.

Keep in mind that the system administrator might have used custom configurations or scripts to mount drives that are not listed in the /etc/fstab file. Because of this, it's good practice to not only scan /etc/fstab, but to also gather information about mounted drives using mount.

Furthermore, we can use lsblk23 to view all available disks.

joe@debian-privesc:~$ lsblk

NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT

sda      8:0    0   32G  0 disk

|-sda1   8:1    0   31G  0 part /

|-sda2   8:2    0    1K  0 part

`-sda5   8:5    0  975M  0 part [SWAP]

sr0     11:0    1 1024M  0 rom

 

Listing 17 - Listing all available drives using lsblk

We'll notice that the sda drive consists of three different numbered partitions. In some situations, showing information for all local disks on the system might reveal partitions that are not mounted. Depending on the system configuration (or misconfiguration), we then might be able to mount those partitions and search for interesting documents, credentials, or other information that could allow us to escalate our privileges or get a better foothold in the network.

Another common privilege escalation technique involves exploitation of device drivers and kernel modules. We will explore actual exploitation tactics later in this Module, but first let's examine some important enumeration techniques.

Since this technique relies on matching vulnerabilities with corresponding exploits, we'll need to gather a list of drivers and kernel modules that are loaded on the target. We can enumerate the loaded kernel modules using lsmod without any additional arguments.

joe@debian-privesc:~$ lsmod

Module                  Size  Used by

binfmt_misc            20480  1

rfkill                 28672  1

sb_edac                24576  0

crct10dif_pclmul       16384  0

crc32_pclmul           16384  0

ghash_clmulni_intel    16384  0

vmw_balloon            20480  0

...

drm                   495616  5 vmwgfx,drm_kms_helper,ttm

libata                270336  2 ata_piix,ata_generic

vmw_pvscsi             28672  2

scsi_mod              249856  5 vmw_pvscsi,sd_mod,libata,sg,sr_mod

i2c_piix4              24576  0

button                 20480  0

Listing 18 - Listing loaded drivers

Once we've collected the list of loaded modules and identified those we want more information about, such as libata in the above example, we can use modinfo to find out more about the specific module. We should note that this tool requires the full path to run.

joe@debian-privesc:~$ /sbin/modinfo libata

filename:       /lib/modules/4.19.0-21-amd64/kernel/drivers/ata/libata.ko

version:        3.00

license:        GPL

description:    Library module for ATA devices

author:         Jeff Garzik

srcversion:     00E4F01BB3AA2AAF98137BF

depends:        scsi_mod

retpoline:      Y

intree:         Y

name:           libata

vermagic:       4.19.0-21-amd64 SMP mod_unload modversions

sig_id:         PKCS#7

signer:         Debian Secure Boot CA

sig_key:        4B:6E:F5:AB:CA:66:98:25:17:8E:05:2C:84:66:7C:CB:C0:53:1F:8C

...

Listing 19 - Displaying additional information about a module

Once we've obtained a list of drivers and their versions, we are better positioned to find any relevant exploits.

Later in this Module, we will explore various methods of privilege escalation. However, there are a few specific enumerations we should cover in this section that could reveal interesting "shortcuts" to privilege escalation.

Aside from the rwx file permissions described previously, two additional special rights pertain to executable files: setuid and setgid. These are symbolized with the letter "s".

If these two rights are set, either an uppercase or lowercase "s" will appear in the permissions. This allows the current user to execute the file with the rights of the owner (setuid) or the owner's group (setgid).

When running an executable, it normally inherits the permissions of the user that runs it. However, if the SUID permissions are set, the binary will run with the permissions of the file owner. This means that if a binary has the SUID bit set and the file is owned by root, any local user will be able to execute that binary with elevated privileges.

When a user or a system-automated script launches a SUID application, it inherits the UID/GID of its initiating script: this is known as effective UID/GID (eUID, eGID), which is the actual user that the OS verifies to grant permissions for a given action.

Any user who manages to subvert a setuid root program to call a command of their choice can effectively impersonate the root user and gains all rights on the system. Penetration testers regularly search for these types of files when they gain access to a system as a way of escalating their privileges.

We can use find to search for SUID-marked binaries. In this case, we are starting our search at the root directory (/), searching for files (-type f) with the SUID bit set, (-perm -u=s) and discarding all error messages (2>/dev/null):

joe@debian-privesc:~$ find / -perm -u=s -type f 2>/dev/null

/usr/bin/chsh

/usr/bin/fusermount

/usr/bin/chfn

/usr/bin/passwd

/usr/bin/sudo

/usr/bin/pkexec

/usr/bin/ntfs-3g

/usr/bin/gpasswd

/usr/bin/newgrp

/usr/bin/bwrap

/usr/bin/su

/usr/bin/umount

/usr/bin/mount

/usr/lib/policykit-1/polkit-agent-helper-1

/usr/lib/xorg/Xorg.wrap

/usr/lib/eject/dmcrypt-get-device

/usr/lib/openssh/ssh-keysign

/usr/lib/spice-gtk/spice-client-glib-usb-acl-helper

/usr/lib/dbus-1.0/dbus-daemon-launch-helper

/usr/sbin/pppd

Listing 20 - Searching for SUID files

In this case, the command found several SUID binaries. Exploitation of SUID binaries will vary based on several factors. For example, if /bin/cp (the copy command) were SUID, we could copy and overwrite sensitive files such as /etc/passwd.

A comprehensive list of Linux privilege escalation techniques can be found in a compendium24 by g0tmi1k as well as in other,25 more up-to-date,26 resources.

Having covered the main concepts behind manually enumerating Linux systems for privilege escalation techniques, we are going to learn how to automate the process in bulk in the upcoming section.

1(Linux man-pages project, 2019), http://man7.org/linux/man-pages/man1/id.1.html ↩︎

2(Wikipedia, 2022), https://en.wikipedia.org/wiki/User_identifier ↩︎

3(Peter Tobias, 2003), https://linux.die.net/man/1/hostname ↩︎

4(Wikipedia, 2022), https://en.wikipedia.org/wiki/Kernel_(operating_system) ↩︎

5(David MacKenzie, 2003), https://linux.die.net/man/1/uname ↩︎

6(Linux man-pages project, 2018), http://man7.org/linux/man-pages/man1/ps.1.html ↩︎

7(Linus Åkesson, 2018), https://www.linusakesson.net/programming/tty/ ↩︎

8(Fred N. van Kempen, 2003), https://linux.die.net/man/8/ifconfig ↩︎

9(Linux man-pages project, 2011), http://man7.org/linux/man-pages/man8/ip.8.html ↩︎

10(Phil Blundell, 2003), https://linux.die.net/man/8/route ↩︎

11(Linux man-pages project, 2008), http://man7.org/linux/man-pages/man8/routel.8.html ↩︎

12(Bernd Eckenfels, 2003), https://linux.die.net/man/8/netstat ↩︎

13(Linux man-pages project, 2019), http://man7.org/linux/man-pages/man8/ss.8.html ↩︎

14(Herve Eychenne, 2003), https://linux.die.net/man/8/iptables ↩︎

15(Debian, SPI Inc., 2023), https://packages.debian.org/sid/iptables-persistent ↩︎

(Carlos Polop, 2022), https://book.hacktricks.xyz/linux-hardening/privilege-escalation ↩︎

Resources

Some of the labs require you to start the target machine(s) below.

Please note that the IP addresses assigned to your target machines may not match those referenced in the Module text and video.

Linux Privilege Escalation - Manual Enumeration - VM #1

Linux Privilege Escalation - Manual Enumeration - VM #2

Labs

  1. Connect to VM 1 with the provided credentials and replicate the manual enumeration techniques covered in this section. Inspect the target's OS information and its release details. What is the Linux distribution codename?


(Còn tiếp ... )