邪恶八进制信息安全团队技术讨论组's Archiver

fbleach 2008-3-16 14:40

[转载]软件编写基础知识锦囊(一)

文章作者:恶魔之子
信息来源:黑客手册技术论坛
原文链接:[url]http://www.nohack.cn/bbs/thread-68813-1-1.html[/url]

确定Windows和windows系统目录

有两个SDK函数可以完成该功能。GetWindowsDirectory和GetSystemDirectory,下例说明了如何使用这两个函数:

TCHAR szDir [MAX_PATH];

//Get the full path of the windows directory.

:: GetWindowsDirectory (szDir, MAX_PATH);

TRACE ("Windows directory %s\n", szDir);

//Get the full path of the windows system directory.

:: GetSystemDirectory (szDir, MAX_PATH);

TRACE ("Windows system directory %s\n", szDir);

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



***********************************************************************************

让程序从CTRL+ATL+DEL消失

使用Win32 API函数RegisterServiceProcess这里我们使用了汇编。

#i nclude <windows.h>

HINSTANCE hLibrary;
void *regproc;

void CADInit(void);
void HideApp(void);
void ShowApp(void);
void CADClean(void);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
CADInit(); //加载 DLL 并创建一指向它指针
HideApp(); //隐藏程序
//ShowApp(); //显示程序

//其他处理或调用

CADClean(); //卸载 DLL
return 0; //retrun 0 因为没有进入消息循环
}

void CADInit(void)
{
//加载 kernel32.dll
hLibrary = LoadLibrary("kernel32.dll");
//获取函数RegisterServiceProcess的地址
regproc = GetProcAddress(hLibrary, "RegisterServiceProcess");
}

void HideApp(void)
{
//实现程序的隐藏
__asm
{
push 1
push 0
call regproc
}
return;
}

void ShowApp(void)
{
//恢复状态
__asm
{
push 0
push 0
call regproc
}
return;
}

void CADClean(void)
{
//卸载 DLL
FreeLibrary(hLibrary);
return;
}
本程序在W2K和Win9x测试通过。

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



***********************************************************************************

程序自杀(进程自己结束自己)

HMODULE module = GetModuleHandle(0);
CHAR buf[MAX_PATH];
GetModuleFileName(module, buf, sizeof buf);
CloseHandle(HANDLE(4));

 __asm
{
lea eax, buf
push 0
push 0
push eax
push ExitProcess
push module
push DeleteFile
push UnmapViewOfFile
ret
}
return;

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



***********************************************************************************
操作系统信息

//结构OSVERSIONINFO包含操作系统的版本信息
OSVERSIONINFO osvi;
CString winver,os;

osvi.dwOSVersionInfoSize=sizof(OSVERSIONINFO);
GetVersionEx(&osvi);

switch(osvi.dwPlatformId)
{
case 0:
os="Win 3.X";
break;
case 1:
os="Win 9X";
break;
case 2:
os="Win NT/2000/XP";
break;
default:
os="Other OS";
break;
}

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



***********************************************************************************

隐藏你的鼠标


(注意:注销或重新启动就可以恢复)

一、建立一个单文档的应用程序框架
二、为隐藏主窗口,将OnCreate 删除。
并在App类里修改m_pMainWnd指向ShowWindow(SW_HIDE)
三、现在在mainframe的实现文件里添加如下内容:

POINT mp,cursorNew;
/////////////////////////////////////
// CMainFrame construction/destruction
UINT FMouse(LPVOID param)
{
int flag=0;

WINDOWPLACEMENT wp;///窗口位置
wp.length=sizeof(WINDOWPLACEMENT);
HWND hWnd;
char tmp[20];
RECT rt;
hWnd=GetDesktopWindow();////GetForegroundWindow();
GetWindowPlacement(hWnd,&wp);
GetWindowRect(hWnd,&rt);
GetWindowText(hWnd,tmp,20);

HDC dc=GetDC((HWND)param);

int iResult;
iResult=AfxMessageBox("确实要隐藏吗?",MB_OKCANCEL);
if(iResult==IDOK)
{
while(1)
{
hWnd=GetForegroundWindow();//GetDesktopWindow();
GetWindowRect(hWnd,&rt);
GetWindowText(hWnd,tmp,20);
GetWindowPlacement(hWnd,&wp);
GetCursorPos(&cursorNew);
while(1){
::mouse_event(MOUSEEVENTF_MOVE,cursorNew.x,cursorNew.y,0,0);
}
}
}
return 0;
}
在构造函数里启动线程CMainFrame::CMainFrame()
{
HWND hWnd=::GetParent(NULL);
GetCursorPos(&mp);
AfxBeginThread(FMouse,hWnd,0);
}

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



***********************************************************************************

系统的定时关机

TOKEN_PRIVILEGES tkp;
HANDLE hToken;

if (!OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
{
MessageBox("OpenProcessToken failed!");
}

LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,&tkp.Privileges[0].Luid); //获得本地机唯一的标识
tkp.PrivilegeCount = 1;
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,(PTOKEN_PRIVILEGES) NULL, 0); //调整获得的权限

if (GetLastError() != ERROR_SUCCESS)
{
MessageBox("AdjustTokenPrivileges enable failed!");
}

fResult =InitiateSystemShutdown(
NULL, // 要关的计算机用户名,可在局域网网中关掉对方的机器,NULL表示关本机
"由于系统不稳定,WINDOWS将在上面的时间内关机,请做好保存工作!", // 显示的消息
10, // 关机所需的时间
TRUE,
TRUE); //设为TRUE为重起,设为FALSE为关机

if(!fResult)
{
MessageBox("InitiateSystemShutdown failed.");
}

tkp.Privileges[0].Attributes = 0;
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,(PTOKEN_PRIVILEGES) NULL, 0);

if (GetLastError() != ERROR_SUCCESS)
{
MessageBox("AdjustTokenPrivileges disable failed.");
}

ExitWindowsEx(EWX_SHUTDOWN,0); //开始关机

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



***********************************************************************************


程序只运行一个实例,并激活前一个实例

具体实现:

1、在程序初始化的时候 (InitInstance()) 枚举所有的窗口,查找本程序的实例是否存在
2、在主窗口初始化的时候在本窗口的属性列表中添加一个标记,以便程序查找.

部分关键代码

1、在App的InitInstance()中枚举所有窗口,查找本程序实例

HWND oldHWnd = NULL;
EnumWindows(EnumWndProc,(LPARAM)&oldHWnd); //枚举所有运行的窗口
if(oldHWnd != NULL)
{
AfxMessageBox("本程序已经在运行了");
::showWindow(oldHWnd,SW_SHOWNORMAL); //激活找到的前一个程序
::setForegroundWindow(oldHWnd); //把它设为前景窗口
return false; //退出本次运行
}
2、添加EnumWndProc窗口过程函数://添加的标识只运行一次的属性名
CString g_szPropName = "Your Prop Name"; //自己定义一个属性名
HANDLE g_hValue = (HANDLE)1; //自己定义一个属性值

BOOL CALLBACK EnumWndProc(HWND hwnd,LPARAM lParam)
{
HANDLE h = GetProp(hwnd,g_szPropName);
if( h == g_hValue)
{
*(HWND*)lParam = hwnd;
return false;
}
return true;
}
3、在主窗口的 OnInitDialog()中添加属性 //设置窗口属性
SetProp(m_hWnd,g_szPropName,g_hValue);

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



***********************************************************************************

得到计算机的主机名和IP地址

#i nclude<winsock2.h>

链接库:Wsock32.lib

{
WORD wVersionRequested;
WSADATA wsaData;
char name[255];
CString ip;
PHOSTENT hostinfo;
wVersionRequested = MAKEWORD( 2, 0 );

if ( WSAStartup( wVersionRequested, &wsaData ) == 0 )
{

if( gethostname ( name, sizeof(name)) == 0)
{
if((hostinfo = gethostbyname(name)) != NULL)
{
ip = inet_ntoa (*(struct in_addr *)*hostinfo->h_addr_list);
}
}

WSACleanup( );
}
}


***********************************************************************************

用VC++读取网卡MAC地址的程序

运行VC++6.0,选择创建一个Win32 Console程序,然后输入以下代码:

#i nclude "stdafx.h"
#i nclude <windows.h>
#i nclude <wincon.h>
#i nclude <stdlib.h>
#i nclude <stdio.h>
#i nclude <time.h>

nb30.h #i nclude < nb30.h >
typedef struct _ASTAT_
{
ADAPTER_STATUS adapt;
NAME_BUFFER NameBuff [30];
}ASTAT, * PASTAT;

ASTAT Adapter;

void getmac_one (int lana_num)
{
NCB ncb;
UCHAR uRetCode;
memset( &ncb, 0, sizeof(ncb) );
ncb.ncb_command = NCBRESET;
ncb.ncb_lana_num = lana_num;
uRetCode = Netbios( &ncb );
printf( "The NCBRESET return code is:
0x%x \n", uRetCode );
memset( &ncb, 0, sizeof(ncb) );
ncb.ncb_command = NCBASTAT;
ncb.ncb_lana_num = lana_num;
strcpy( (char *)ncb.ncb_callname,
"* " );
ncb.ncb_buffer = (unsigned char *) &Adapter;
ncb.ncb_length = sizeof(Adapter);
uRetCode = Netbios( &ncb );
printf( "The NCBASTAT
return code is: 0x%x \n", uRetCode );
if ( uRetCode == 0 )
{
printf( "The Ethernet Number[%d]
is: %02X%02X-%02X%02X-%02X%02X\n",
lana_num,
Adapter.adapt.adapter_address[0],
Adapter.adapt.adapter_address[1],
Adapter.adapt.adapter_address[2],
Adapter.adapt.adapter_address[3],
Adapter.adapt.adapter_address[4],
Adapter.adapt.adapter_address[5]);
}
}
int main(int argc, char* argv[])
{
NCB ncb;
UCHAR uRetCode;
LANA_ENUM lana_enum;
memset( &ncb, 0, sizeof(ncb) );
ncb.ncb_command = NCBENUM;
ncb.ncb_buffer = (unsigned char *) &lana_enum;
ncb.ncb_length = sizeof(lana_enum);
uRetCode = Netbios( &ncb );
printf( "The NCBENUM return
code is:
0x%x \n", uRetCode );
if ( uRetCode == 0 )
{
printf( "Ethernet Count is : %d\n\n", lana_enum.length);
for ( int i=0; i< lana_enum.length; ++i)
getmac_one( lana_enum.lana);
}
return 0;
}

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



***********************************************************************************

解决VC++语言程序中的2000年问题

PROCTIME.C源程序清单:

#i nclude <stdio.h>
#i nclude <bios.h>
#i nclude <time.h>

void goto_ xy(int x,int y)
{
union REGS r;
r.h.ah=2; r.h.bh=0;
r.h.dh=(char)x;
r.h.dl=(char)y;
int86(0x10,&r,&r);
}
get_key()
{
union REGS r;
r.h.ah=0;
return int86(0x16,&r,&r);
}
int wherex()
{int x;
union REGS r;
r.h.ah=3; r.h.bh=0;
int86(0x10,&r,&r);
(char)x=r.h.dh;
return x;
}
int wherey()
{ int y;
union REGS r;
r.h.ah=3; r.h.bh=0;
int86(0x10,&r,&r);
(char)y=r.h.dl;
return y;
}
void write_video(int x,int y,char *p,int attrib)
{
register int I;
union REGS r;
for (I=y;*p;I++) {
if (*p==\n)
{ goto_ xy(x+1,2);break; }
goto_ xy(x,I);
r.h.ah=9; r.h.bh=0;
r.x.cx=1; r.h.al=*p++;
r.h.bl=(char)attrib;
int86(0x10,&r,&r);
}
}
void disptime(int x,int y)
{
struct tm *newtime;
time_t aclock;
long startsec,currsec;
int dqx,dqy,YEAR;
char sj[50],week[3];
time(&aclock);
newtime=localtime(&aclock);
YEAR=newtime->tm_year+1990
startsec=newtime->tm_sec;
switch(newtime->tm_wday)
{
case 0: strcpy(week,"日"); break;
case 1: strcpy(week,"一"); break;
case 2: strcpy(week,"二"); break;
case 3: strcpy(week,"三"); break;
case 4: strcpy(week,"四"); break;
case 5: strcpy(week,"五"); break;
case 6: strcpy(week,"六"); break;
}
while(!kbhit())
{
time(&aclock);
newtime=localtime(&aclock);
sprintf(sj,"%d.%2d.%2d 星期%s %d:%2d:%2d",YEAR,newtime->tm_mon+1,newtime- >tm_mday,week,newtime->tm_hour,newtime->tm_min,newtime->tm_sec);
currsec=newtime->tm_sec;
if(startsec!=currsec)
{
dqx=wherex(); dqy=wherey();
write_video(x,y,sj,0x0a);
goto_ xy(dqx,dqy);
return;
}
}
}
main ()
{
union inkey { char ch[2]; int I; }c;
for(;;) {
for(;;) {
if (kbhit()) { c.I=get_key(); break; }
disptime(0,54);
if (c.ch[0]==27) exit(0);
}
}
}

页: [1]
© 1999-2008 EvilOctal Security Team