Post

Kali Linux 渗透测试 (OSCP) 笔记 0x01

认识 Kali

Linux 文件系统

  • /bin - 基础命令(ls, cd, cat, 等)
  • /sbin - 系统应用(fdisk, mkfs, sysctl, 等)
  • /etc - 配置文件
  • /tmp - 临时文件(在引导 boot 时被删除)
  • /usr/bin - 一般应用(apt, ncat, nmap, 等)
  • /usr/share - 存放应用共享文件和数据的目录

基础 Linux 命令

man 命令

man 显示命令的使用手册(manual pages),比如查看 ls 命令的使用手册:

1
$ man ls

在使用手册页面,命令右边会有一个角标,表示应用的级别,命令的级别分别为:

级别内容
1用户命令
2系统内核调用的程序接口
3C 语言库调用的程序接口
4特殊文件比如设备节点和驱动
5文件格式和配置
6游戏
7杂项
8管理员命令

使用 man -k passwd 参数来搜索 passwd 关键词相关的内容:

1
2
3
4
5
6
$ man -k passwd
checkPasswdAccess (3) - query the SELinux policy database in the kernel
chgpasswd (8) - update group passwords in batch mode
chpasswd (8) - update passwords in batch mode
passwd (5) - the password file
...

使用正则来匹配 passwd 单词:

1
2
3
4
$ man -k '^passwd$'
passwd (1) - change user password
passwd (1ssl) - compute password hashes
passwd (5) - the password file

根据搜索结果后面括号里的序号,使用 man 5 passwd 来查看 passwd 文件的帮助:

1
$ man 5 passwd

apropos

apropos 命令和 man -k 命令是一样的

列出文件

ls 命令输出基本的文件列表,使用 -a 参数输出所有文件(包括隐藏文件),使用 -l 参数输出文件更多信息:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ ls
Desktop Documents Downloads Music PentestEnv Pictures Public Templates Videos
$ ls /etc/apache2/sites-available/*.conf
/etc/apache2/sites-available/000-default.conf
/etc/apache2/sites-available/default-ssl.conf
$ ls -al
.
..
.bash_history
.bashrc
.cache
.config
Desktop
Documents

定位到不同目录

Linux 不使用 Windows 风格的盘符,所有文件、文件夹、设备等,全部在根目录 / 下的子目录中。使用 cd 命令更改当前所在目录,使用 pwd 命令输出当前所在目录,使用 cd ~ 命令回到用户根目录:

1
2
3
4
5
$ cd /usr/share/metasploit-framework/
/usr/share/metasploit-framework$ pwd
/usr/share/metasploit-framework
$ cd ~
/home/kali

新建目录

使用 mkdir+目录名新建目录:

1
2
3
4
5
6
7
8
9
$ mkdir notes
$ cd notes/
~/notes$ mkdir module one
~/notes$ ls
module one
~/notes$ rm -rf module/ one/
~/notes$ mkdir "module one"
$ cd module\ one/
~/notes/module one$

使用 mkdir -p 命令时,会创建需要的父目录。如创建 recon, exploit, report 三个目录的同时,自动创建父目录 test

1
2
3
4
5
$ mkdir -p test/{recon,exploit,report}
$ ls -l test/
exploit
recon
report

查找文件

在 Kali Linux 中查找文件或文件夹的三个常用命令为:find, locate, which

which

which 命令在 $PATH 定义的环境中查找文件,返回完整路径:

1
2
3
4
5
$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

$ which sbd
/usr/bin/sbd

locate

locate命令查找文件或文件夹最快,实际上是通过搜索内置的数据库 locate.db 查找的,这个数据库自动更新,也可以使用 updatedb 命令手动更新。

1
2
3
4
$ sudo updatedb

$ locate sbd.exe
/usr/share/windows-resources/sbd/sbd.exe

find

find 命令是这三个里最复杂和灵活的搜索工具。如在根目录中搜索开头是“sbd”的文件:

1
2
3
4
5
6
7
$ sudo find / -name sbd*
/usr/bin/sbd
/usr/share/doc/sbd
/usr/share/windows-resources/sbd
/usr/share/windows-resources/sbd/sbd.exe
/usr/share/windows-resources/sbd/sbdbg.exe
...

find 对比 locate 的优势在于,find 不仅可以搜索文件和目录名,还可以搜索文件时间、大小、所属、文件类型、权限等…

Exercises

  1. man ls
  2. man -k compression 或者 apropos compression
  3. which pwd
  4. locate wce32.exe
  5. find . -type f -mtime -24 ! -user root -exec ls -l {} +

    -type f # 类型为文件 -mtime -24 # 之前 24 小时有修改过 ! -user root # 文件归属非 root 用户 -exec ls -la {} + # 执行 ls -l 命令

管理 Kali Linux 服务

SSH 服务

SSH 服务基于 TCP,默认监听 22 端口。开启 SSH 服务的命令为:

1
$ sudo systemctl start ssh

该命令不输出结果,但是可以通过查找监听 TCP 22 端口的服务来确认已经启动:

1
2
3
$ sudo ss -antlp | grep sshd
LISTEN 0 128 *:22 *:* users:(("sshd",pid=472,fd=3))
LISTEN 0 128 :::22 :::* users:(("sshd",pid=472,fd=4))

如果希望 SSH 服务在 Kali 开启时自动启动,使用 systemctl 命令,该命令可以启用(systemctl enable)或禁用(systemctl disable)大部分服务。

1
2
3
4
$ sudo systemctl enable ssh
Synchronizing state of ssh.service with SysV service script with /lib/systemd/systemd-
Executing: /lib/systemd/systemd-sysv-install enable ssh
Created symlink /etc/systemd/system/sshd.service → /lib/systemd/system/ssh.service.

HTTP 服务

HTTP 服务基于 TCP 协议,默认监听 80  端口。启动 HTTP 服务(apache2)和启用 SSH 服务相似:

1
$ sudo systemctl start apache2

同样的方法确认 HTTP 服务已经启动:

1
2
$ sudo ss -antlp | grep apache
LISTEN 0 128 :::80 :::* users:(("apache2",pid=2434,fd=4),("apache2",pid=2433,fd=4),("apache2",pid=2431,fd=4))

让 HTTP 服务开机时启动的方法和设置 SSH 服务的方法一样,使用 systemctl

1
2
3
$ sudo systemctl enable apache2
Synchronizing state of apache2.service with SysV service script with /lib/systemd/syst
Executing: /lib/systemd/systemd-sysv-install enable apache2

在 Kali 中大多数服务的操作和操作 SSH 以及 HTTP 服务一样,查看所有可用的服务,运行 systemctl list-unit-files

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ systemctl list-unit-files
...
UNIT FILE STATE
proc-sys-fs-binfmt_misc.automount static
-.mount generated
dev-hugepages.mount static
dev-mqueue.mount static
media-cdrom0.mount generated
proc-sys-fs-binfmt_misc.mount static
run-vmblock\x2dfuse.mount disabled
sys-fs-fuse-connections.mount static
sys-kernel-config.mount static
sys-kernel-debug.mount static
...

搜索、安装、移除工具

APT 是 Debian-based 系统中管理包和应用的工具,可以安装、移除应用,升级包,甚至升级整个系统。

apt update

为了加快查找 APT 数据库相关操作的速度,APT 包是缓存在本地的。使用 apt update升级可用包和相关信息,包括版本等信息列表:

1
2
3
4
5
6
7
$ sudo apt update
Hit:1 http://kali.mirror.globo.tech/kali kali-rolling InRelease
Reading package lists... Done
Building dependency tree
Reading state information... Done
699 packages can be upgraded.
Run 'apt list --upgradable' to see them.

apt upgrade

升级完 APT 数据库,就可以升级以安装的包或者系统。比如升级 metasploit-framework:

1
$ apt upgrade metasploit-framework

apt-cache searchapt show

apt-cache search 命令查找内置缓存数据库(apt update)并提供相关信息。比如要安装 pure-ftpd,第一件事就是查找该工具是否包含在 Kali 源里面:

1
2
3
4
5
6
7
8
9
$ apt-cache search pure-ftpd
ftpd - File Transfer Protocol (FTP) server
mysqmail-pure-ftpd-logger - real-time logging system in MySQL - Pure-FTPd traffic-logger
pure-ftpd - Secure and efficient FTP server
pure-ftpd-common - Pure-FTPd FTP server (Common Files)
pure-ftpd-ldap - Secure and efficient FTP server with LDAP user authentication
pure-ftpd-mysql - Secure and efficient FTP server with MySQL user authentication
pure-ftpd-postgresql - Secure and efficient FTP server with PostgreSQL user authentication
resource-agents - Cluster Resource Agents

搜索结果中出现了一个 resource-agents,是因为 apt-cache search 会搜索整个描述文档。

使用 apt-show 来确认 resource-agents 描述中是否包含 pure-ftpd

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
$ apt show resource-agents
Package: resource-agents
Version: 1:4.2.0-2
...
Description: Cluster Resource Agents
This package contains cluster resource agents (RAs) compliant with the Open
Cluster Framework (OCF) specification, used to interface with various services
in a High Availability environment managed by the Pacemaker resource manager.
.
Agents included:
AoEtarget: Manages ATA-over-Ethernet (AoE) target exports
AudibleAlarm: Emits audible beeps at a configurable interval
...
NodeUtilization: Node Utilization

Pure-FTPd: Manages a Pure-FTPd FTP server instance

Raid1: Manages Linux software RAID (MD) devices on shared storage
...

apt install

使用 apt install 安装包:

1
2
3
4
5
6
7
8
9
10
11
12
$ sudo apt install pure-ftpd
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
  openbsd-inetd pure-ftpd-common
The following NEW packages will be installed:
  openbsd-inetd pure-ftpd pure-ftpd-common
0 upgraded, 3 newly installed, 0 to remove and 0 not upgraded.
Need to get 341 kB of archives.
After this operation, 883 kB of additional disk space will be used.
...

使用 apt remove --purge 来移除包。

apt remove –purge

apt remove --purge 完全从系统中移除一个包。需要注意的是 apt remove 也可以移除安装包但是会留下一些文件(通常是用户修改过的配置文件)。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
$ sudo apt remove --purge pure-ftpd
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
  openbsd-inetd pure-ftpd-common
Use 'sudo apt autoremove' to remove them.
The following packages will be REMOVED:
  pure-ftpd*
0 upgraded, 0 newly installed, 1 to remove and 0 not upgraded.
After this operation, 507 kB disk space will be freed.
Do you want to continue? [Y/n] y
(Reading database ... 135806 files and directories currently installed.)
Removing pure-ftpd (1.0.43-3) ...
Processing triggers for man-db (2.7.6.1-2) ...
(Reading database ... 135793 files and directories currently installed.)
Purging configuration files for pure-ftpd (1.0.43-3) ...
Processing triggers for systemd (232-25+deb9u12) ...

dpkg

dpkg是用来安装包的工具,无论是否直接通过 APT,它也可以很方便的在离线环境下使用。需要注意的是 dpkg 不会 安装任何依赖环境。使用 dpkg-i 或者 --install 选项来安装一个 .deb 包文件,这需要提前获取到 .deb 文件。

1
2
3
4
5
6
7
8
$ sudo dpkg -i man-db_2.7.0.2-5_amd64.deb
(Reading database ... 86425 files and directories currently installed.)
Preparing to unpack man-db_2.7.0.2-5_amd64.deb ...
Unpacking man-db (2.7.0.2-5) over (2.7.0.2-4) ...
Setting up man-db (2.7.0.2-5) ...
Updating database of manual pages ...
Processing triggers for mime-support (3.58) ...
...
This post is licensed under CC BY 4.0 by the author.