黑客編寫軟件基礎知識集合

--------------------------------------------------------------------------------
http://www.hackbase.com 閱讀: 時間:2004-9-29 20:05:05 來源:黑客基地 
確定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這裏我們使用了彙編。

#include <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);


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

探測Windows主機的NetBIOS信息

NetBIOS信息
在我們和遠程Windows2000/XP主機建立了空會話之後,我們就有權枚舉系統裏的各項NetBIOS信息了。
當然在某些選項中需要較高的權利,不過我們只執行那些匿名用戶可以獲得的絕大多數系統信息。

時間:探測遠程主機的當前日期和時間信息。它會返回一個數據結構,包括年,月,日,星期,時,分,秒等等。
不過得到的是GMT標準時間,當然對於我們來說就應該換算爲GMT+8:00了。由此可以判斷出主機所在的時區信息。

操作系統指紋:探測遠程主機的操作系統指紋信息。一共有三種級別的探測(100,101,102),
我們使用的是101級,它會返回一個數據結構,可以獲取遠程主機的平臺標識,服務器名稱,操作系統的主次版本
(Windows2000爲5.0,WindowsXP爲5.1,而最新操作系統Longhorn的版本爲6.0),
服務器類型(每臺主機可能同時包含多種類型信息)和註釋。

共享列表:探測遠程主機的共享列表。我們可以獲得一個數據結構指針,枚舉遠程主機的所有共享信息
(隱藏的共享列表在內)。其中包括共享名稱,類型與備註。類型可分爲:磁盤驅動器,打印隊列,通訊設備,
進程間通訊與特殊設備。

用戶列表: 探測遠程主機的用戶列表,返回一個數據結構指針,枚舉所有用戶信息。可獲取用戶名,全名,
用戶標識符,說明與標識信息。標識信息可以探測用戶的訪問權限。

本地組列表: 探測遠程主機的本地組列表信息。枚舉所有本地組信息,包含本地組名稱和註釋信息。

組列表: 探測遠程主機的組列表信息。枚舉所有的組信息,包括組名稱,註釋,組標識符與屬性。在此基礎上,
我們可以枚舉組內的所有用戶信息。

組用戶列表: 探測特定組內的用戶信息。我們可以獲得組內所有用戶的名稱。當我門獲得了所有的用戶列表,
下一步就應該很清楚了,那就是掛一個字典進行破解了。

傳輸協議列表: 探測遠程主機的傳輸協議信息,枚舉所有的傳輸列表。可以獲得每個傳輸協議的名稱,地址,
網絡地址和當前與本傳輸協議連接的用戶數目。

會話列表: 探測遠程主機的當前會話列表。枚舉每個會話的相關信息,包括客戶端主機的名稱,當前用戶的名稱,
活動時間和空閒時間。這可以幫助我們瞭解遠程主機用戶的喜好等等。

主要函數與相關數據結構分析

1. 建立空會話
WNetAddConnection2(&nr,username,password,0);
//nr爲NETRESOURCE數據結構的對象;
//username爲建立空會話的用戶名,在此將用戶名設置爲NULL;
//password爲登陸密碼,在此將密碼設置爲NULL;
2. 撤消空會話
WNetCancelConnection2(ipc,0,TRUE);
//ipc爲TCHAR的指針,我們可以這樣獲得:
//swprintf(ipc,_T("////%s//ipc$"),argv[1]),argv[1]爲主機名或地址;

3. 探測主機時間
nStatus=NetRemoteTOD(server,(PBYTE*)&pBuf);
//參數server爲主機的名稱或地址;
//pBuf爲TIME_OF_DAY_INFO數據結構的指針;
//nStatus爲NET_API_STATUS成員;

4. 探測操作系統指紋
NetServerGetInfo(server,dwLevel,(PBYTE *)&pBuf);
//dwLevel爲等級數,我們選擇的是101級;
//pBuf是SERVER_INFO_101數據結構的指針;

5. 探測共享列表
NetShareEnum(server,dwLevel,(PBYTE *)&pBuf,MAX_PREFERRED_LENGTH,&er,&tr,&resume);
//dwLevel的等級數爲1級;
//pBuf是SHARE_INFO_1數據結構的指針;
//MAX_PREFERRED_LENGTH指定返回數據的長度;
//er指明返回的實際可以枚舉的成員數目;
//tr返回所有的成員數目;
//resume用於繼續進行共享搜索;

6. 探測用戶列表
NetQueryDisplayInformation(server,dwLevel,i,100,0xFFFFFFFF,&dwRec,(PVOID *)&pBuf);
//dwLevel的等級數爲1級;
//i爲枚舉的索引;
//dwRec返回獲取的信息數目;
//pBuf爲NET_DISPLAY_USER數據結構的指針;

7. 探測本地組列表
NetLocalGroupEnum(server,dwLevel,(PBYTE *)&pBuf,-1,&er,&tr,&resume);
//dwLevel的等級是1;
//pBuf返回LOCALGROUP_INFO_1數據結構的指針;

8. 探測組列表
NetQueryDisplayInformation(server,dwLevel,i,100,0xFFFFFFFF,&dwRec,(PVOID*)&pGBuf);
//dwLevel的等級爲3;
//pGBuf返回NET_DISPLAY_GROUP的數據結構指針;

9. 探測組內的用戶
NetGroupGetUsers(server,pGBuffer->grpi3_name,0,(PBYTE *)&pUBuf,MAX_PREFERRED_LENGTH,&er,&tr,&resume);
//pGBuffer->grpi3_name爲組的名稱;
//pUBuf返回GROUP_USERS_INFO_0數據結構的指針;

10.探測傳輸協議列表
NetServerTransportEnum(server,dwLevel,(PBYTE *)&pBuf,MAX_PREFERRED_LENGTH,&er,&tr,&resume);
//dwLevel的等級爲0級;
//pBuf返回SERVER_TRANSPORT_INFO_0數據結構的指針;

11.探測會話列表
NetSessionEnum(server,pszClient,pszUser,dwLevel,(PBYTE *)&pBuf,MAX_PREFERRED_LENGTH,&er,&tr,&resume);
//pszClient指定客戶的地址;
//pszUser指定用戶名;
//dwLevel的等級是10級;
//pBuf返回SESSION_INFO_10數據結構的指針;

12.釋放內存
NetApiBufferFree(pBuf);
//釋放由系統分配的內存空間。


源代碼

#define UNICODE
#define _UNICODE

#include <windows.h>
#include <winnetwk.h>
#include <tchar.h>
#include "include/lmaccess.h"
#include "include/lmserver.h"
#include "include/lmshare.h"
#include <lm.h>

#pragma comment (lib,"mpr")
#pragma comment (lib,"netapi32")

void start();
void usage();
int datetime(PTSTR server);
int fingerprint(PTSTR server);
int netbios(PTSTR server);
int users(PTSTR server);
int localgroup(PTSTR server);
int globalgroup(PTSTR server);
int transport(PTSTR server);
int session(PTSTR server);

int wmain(int argc,TCHAR *argv[])
{
NETRESOURCE nr;
DWORD ret;
TCHAR username[100]=_T("");
TCHAR password[100]=_T("");
TCHAR ipc[100]=_T("");

system("cls.exe");
start();
if(argc!=2)
{
usage();
return -1;
}
swprintf(ipc,_T("////%s//ipc$"),argv[1]);
nr.lpLocalName=NULL;
nr.lpProvider=NULL;
nr.dwType=RESOURCETYPE_ANY;
nr.lpRemoteName=ipc;
ret=WNetAddConnection2(&nr,username,password,0);
if(ret!=ERROR_SUCCESS)
{
_tprintf(_T("/nIPC$ Connect Failed./n"));
return -1;
}

datetime(argv[1]);
fingerprint(argv[1]);
netbios(argv[1]);
users(argv[1]);
localgroup(argv[1]);
globalgroup(argv[1]);
transport(argv[1]);
session(argv[1]);

ret=WNetCancelConnection2(ipc,0,TRUE);
if(ret!=ERROR_SUCCESS)
{
_tprintf(_T("IPC$ Disconnect Failed./n"));
return -1;
}
return 0;
}

void start()
{
_tprintf(_T("=====[ T-SMB Scan, by TOo2y ]=====/n"));
_tprintf(_T("=====[ E-mail: [email protected] ]=====/n"));
_tprintf(_T("=====[ HomePage: www.safechina.net ]=====/n"));
_tprintf(_T("=====[ Date: 12-12-2002 ]=====/n"));
}

void usage()
{
_tprintf(_T("/nUsage:/t T-SMB Remoteip"));
_tprintf(_T("/nRequest: Remote host must be opening port 445/tcp of Microsoft-DS./n"));
}

int datetime(PTSTR server)
{
PTIME_OF_DAY_INFO pBuf=NULL;
NET_API_STATUS nStatus;
DWORD lerror;

_tprintf(_T("/n*** Date and Time ***/n"));
nStatus=NetRemoteTOD(server,(PBYTE*)&pBuf);
if(nStatus==NERR_Success)
{
if(pBuf!=NULL)
{
_tprintf(_T("/nCurrent date:/t%.2d-%.2d-%d"),pBuf->tod_month,pBuf->tod_day,pBuf->tod_year);
_tprintf(_T("/nCurrent time:/t%.2d:%.2d:%.2d.%.2d (GMT)"),pBuf->tod_hours,pBuf->tod_mins,pBuf->tod_secs,pBuf->tod_hunds);
pBuf->tod_hours=(pBuf->tod_hours+8)%24;
_tprintf(_T("/nCurrent time:/t%.2d:%.2d:%.2d.%.2d (GMT+08:00)/n"),pBuf->tod_hours,pBuf->tod_mins,pBuf->tod_secs,pBuf->tod_hunds);
}
}
else
{
lerror=GetLastError();
if(lerror==997)
{
_tprintf(_T("/nDateTime:/tOverlapped I/O operation is in progress. /n"));
}
else
{
_tprintf(_T("/nDatetime Error:/t%d/n"),lerror);
}
}
if(pBuf!=NULL)
{
NetApiBufferFree(pBuf);
}
return 0;
}

int fingerprint(PTSTR server)
{
DWORD dwlength;
DWORD dwLevel;
NET_API_STATUS nStatus;
PSERVER_INFO_101 pBuf;
DWORD lerror;

dwLevel=101;
pBuf=NULL;
dwlength=_tcslen(server);

_tprintf(_T("/n**** Fingerprint ****/n"));
nStatus=NetServerGetInfo(server,dwLevel,(PBYTE *)&pBuf);
if(nStatus==NERR_Success)
{
_tprintf(_T("/nComputername:/t%s"),pBuf->sv101_name);
_tprintf(_T("/nComment:/t%s"),pBuf->sv101_comment);
_tprintf(_T("/nPlatform:/t%d"),pBuf->sv101_platform_id);
_tprintf(_T("/nVersion:/t%d.%d"),pBuf->sv101_version_major,pBuf->sv101_version_minor);
_tprintf(_T("/nType:"));
if(pBuf->sv101_type & SV_TYPE_NOVELL)
{
_tprintf(_T("/t/tNovell server./n"));
}
if(pBuf->sv101_type & SV_TYPE_XENIX_SERVER)
{
_tprintf(_T("/t/tXenix server./n"));
}
if(pBuf->sv101_type & SV_TYPE_DOMAIN_ENUM)
{
_tprintf(_T("/t/tPrimary domain ./n"));
}
if(pBuf->sv101_type & SV_TYPE_TERMINALSERVER)
{
_tprintf(_T("/t/tTerminal Server./n"));
}
if(pBuf->sv101_type & SV_TYPE_WINDOWS)
{
_tprintf(_T("/t/tWindows 95 or later./n"));
}
if(pBuf->sv101_type & SV_TYPE_SERVER)
{
_tprintf(_T("/t/tA LAN Manager server./n"));
}
if(pBuf->sv101_type & SV_TYPE_WORKSTATION)
{
_tprintf(_T("/t/tA LAN Manager workstation./n"));
}
if(pBuf->sv101_type & SV_TYPE_PRINTQ_SERVER)
{
_tprintf(_T("/t/tServer sharing print queue./n"));
}
if(pBuf->sv101_type & SV_TYPE_DOMAIN_CTRL)
{
_tprintf(_T("/t/tPrimary domain controller./n"));
}
if(pBuf->sv101_type & SV_TYPE_DOMAIN_BAKCTRL)
{
_tprintf(_T("/t/tBackup domain controller./n"));
}
if(pBuf->sv101_type & SV_TYPE_AFP)
{
_tprintf(_T("/t/tApple File Protocol server./n"));
}
if(pBuf->sv101_type & SV_TYPE_DOMAIN_MEMBER)
{
_tprintf(_T("/t/tLAN Manager 2.x domain member./n"));
}
if(pBuf->sv101_type & SV_TYPE_LOCAL_LIST_ONLY)
{
_tprintf(_T("/t/tServers maintained by the browser./n"));
}
if(pBuf->sv101_type & SV_TYPE_DIALIN_SERVER)
{
_tprintf(_T("/t/tServer running dial-in service./n"));
}
if(pBuf->sv101_type & SV_TYPE_TIME_SOURCE)
{
_tprintf(_T("/t/tServer running the Timesource service./n"));
}
if(pBuf->sv101_type & SV_TYPE_SERVER_MFPN)
{
_tprintf(_T("/t/tMicrosoft File and Print for NetWare./n"));
}
if(pBuf->sv101_type & SV_TYPE_NT)
{
_tprintf(_T("/t/tWindows NT/2000/XP workstation or server./n"));
}
if(pBuf->sv101_type & SV_TYPE_WFW)
{
_tprintf(_T("/t/tServer running Windows for Workgroups./n"));
}
if(pBuf->sv101_type & SV_TYPE_POTENTIAL_BROWSER)
{
_tprintf(_T("/t/tServer that can run the browser service./n"));
}
if(pBuf->sv101_type & SV_TYPE_BACKUP_BROWSER)
{
_tprintf(_T("/t/tServer running a browser service as backup./n"));
}
if(pBuf->sv101_type & SV_TYPE_MASTER_BROWSER)
{
_tprintf(_T("/t/tServer running the master browser service./n"));
}
if(pBuf->sv101_type & SV_TYPE_DOMAIN_MASTER)
{
_tprintf(_T("/t/tServer running the domain master browser./n"));
}
if(pBuf->sv101_type & SV_TYPE_CLUSTER_NT)
{
_tprintf(_T("/t/tServer clusters available in the domain./n"));
}
if(pBuf->sv101_type & SV_TYPE_SQLSERVER)
{
_tprintf(_T("/t/tAny server running with Microsoft SQL Server./n"));
}
if(pBuf->sv101_type & SV_TYPE_SERVER_NT)
{
_tprintf(_T("/t/tWindows NT/2000 server that is not a domain controller./n"));
}
}
else
{
lerror=GetLastError();
if(lerror==997)
{
_tprintf(_T("/nFingerprint:/tOverlapped I/O operation is in progress./n"));
}
else
{
_tprintf(_T("/nFingerprint Error:/t%d/n"),lerror);
}
}
if(pBuf!=NULL)
{
NetApiBufferFree(pBuf);
}
return 0;
}

int netbios(PTSTR server)
{
DWORD er,tr,resume;
DWORD i,dwLength,dwLevel;
PSHARE_INFO_1 pBuf,pBuffer;
NET_API_STATUS nStatus;
DWORD lerror;

er=0;
tr=0;
resume=1;
dwLevel=1;
dwLength=_tcslen(server);

_tprintf(_T("/n****** Netbios ******/n"));
do
{
nStatus=NetShareEnum(server,dwLevel,(PBYTE *)&pBuf,MAX_PREFERRED_LENGTH,&er,&tr,&resume);
if((nStatus==ERROR_SUCCESS) || (nStatus==ERROR_MORE_DATA))
{
pBuffer=pBuf;
for(i=1;i<=er;i++)
{
_tprintf(_T("/nName:/t/t%s"),pBuffer->shi1_netname);
_tprintf(_T("/nRemark:/t/t%s"),pBuffer->shi1_remark);
_tprintf(_T("/nType:/t/t"));
if(pBuffer->shi1_type==STYPE_DISKTREE)
{
_tprintf(_T("Disk drive./n"));
}
else if(pBuffer->shi1_type==STYPE_PRINTQ)
{
_tprintf(_T("Print queue./n"));
}
else if(pBuffer->shi1_type==STYPE_DEVICE)
{
_tprintf(_T("Communication device./n"));
}
else if(pBuffer->shi1_type==STYPE_IPC)
{
_tprintf(_T("Interprocess communication (IPC)./n"));
}
else if(pBuffer->shi1_type==STYPE_SPECIAL)
{
_tprintf(_T("Special share reserved for interprocess communication (IPC$) or remote administration of the server (ADMIN$)./n"));
}
else
{
_tprintf(_T("/n"));
}
pBuffer++;
}
}
else
{
lerror=GetLastError();
if(lerror==997)
{
_tprintf(_T("/nNetbios:/tOverlapped I/O operation is in progress./n"));
}
else
{
_tprintf(_T("/nNetbios Error:/t%d/n"),lerror);
}
}
if(pBuf!=NULL)
{
NetApiBufferFree(pBuf);
}
}
while(nStatus==ERROR_MORE_DATA);
return 0;
}

int users(PTSTR server)
{
PNET_DISPLAY_USER pBuf,pBuffer;
DWORD nStatus;
DWORD dwRec;
DWORD i=0;
DWORD lerror;
DWORD dwLevel;

dwLevel=1;

_tprintf(_T("/n******* Users *******/n"));
do
{
nStatus=NetQueryDisplayInformation(server,dwLevel,i,100,0xFFFFFFFF,&dwRec,(PVOID *)&pBuf);
if((nStatus==ERROR_SUCCESS) || (nStatus==ERROR_MORE_DATA))
{
pBuffer=pBuf;
for(;dwRec>0;dwRec--)
{
_tprintf(_T("/nName:/t/t%s"),pBuffer->usri1_name);
_tprintf(_T("/nFull Name:/t%s"),pBuffer->usri1_full_name);
_tprintf(_T("/nUser ID:/t%u"),pBuffer->usri1_user_id);
_tprintf(_T("/nComment: /t%s"),pBuffer->usri1_comment);
_tprintf(_T("/nFlag:"));
if(pBuffer->usri1_flags & UF_ACCOUNTDISABLE)
{
_tprintf(_T("/t/tThe users account is disabled./n"));
}
if(pBuffer->usri1_flags & UF_TRUSTED_FOR_DELEGATION)
{
_tprintf(_T("/t/tThe account is enabled for delegation. /n"));
}
if(pBuffer->usri1_flags & UF_LOCKOUT)
{
_tprintf(_T("/t/tThe account is currently locked out (blocked)./n"));
}
if(pBuffer->usri1_flags & UF_SMARTCARD_REQUIRED)
{
_tprintf(_T("/t/tRequires the user to log on to the user account with a smart card. /n"));
}
if(pBuffer->usri1_flags & UF_DONT_REQUIRE_PREAUTH)
{
_tprintf(_T("/t/tThis account does not require Kerberos preauthentication for logon./n"));
}
if(pBuffer->usri1_flags & UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED)
{
_tprintf(_T("/t/tThe users password is stored under reversible encryption in the Active Directory. /n"));
}
if(pBuffer->usri1_flags & UF_NOT_DELEGATED)
{
_tprintf(_T("/t/tMarks the account as /"sensitive/"; other users cannot act as delegates of this user account./n"));
}
if(pBuffer->usri1_flags & UF_USE_DES_KEY_ONLY)
{
_tprintf(_T("/t/tRestrict this principal to use only Data Encryption Standard (DES) encryption types for keys./n"));
}
if(pBuffer->usri1_flags & UF_HOMEDIR_REQUIRED)
{
_tprintf(_T("/t/tThe home directory is required. Windows NT/Windows 2000/Windows XP ignores this value./n"));
}
if(pBuffer->usri1_flags & UF_SCRIPT)
{
_tprintf(_T("/t/tThe logon script executed. This value must be set for LAN Manager 2.0 and Windows NT/2000/XP./n"));
}
i=pBuffer->usri1_next_index;
pBuffer++;
}
}
else
{
lerror=GetLastError();
if(lerror==997)
{
_tprintf(_T("/nUsers:/t/tOverlapped I/O operation is in progress./n"));
}
else
{
_tprintf(_T("/nUsers Error:/t%d/n"),lerror);
}
}
if(pBuf!=NULL)
{
NetApiBufferFree(pBuf);
}
}while(nStatus==ERROR_MORE_DATA);
return 0;
}

int localgroup(PTSTR server)
{
NET_API_STATUS nStatus;
PLOCALGROUP_INFO_1 pBuf,pBuffer;
DWORD i,dwLevel;
DWORD er,tr,resume;
DWORD lerror;

resume=0;
dwLevel=1;

_tprintf(_T("/n**** Local Group ****/n"));
do
{
nStatus=NetLocalGroupEnum(server,dwLevel,(PBYTE *)&pBuf,MAX_PREFERRED_LENGTH,&er,&tr,&resume);
if((nStatus==NERR_Success) || (nStatus==ERROR_MORE_DATA))
{
pBuffer=pBuf;
for(i=1;i<=er;i++)
{
_tprintf(_T("/nName:/t/t%s"),pBuffer->lgrpi1_name);
_tprintf(_T("/nComment:/t%s"),pBuffer->lgrpi1_comment);
_tprintf(_T("/n"));
pBuffer++;
}
}
else
{
lerror=GetLastError();
if(lerror==997)
{
_tprintf(_T("/nLocal Group:/tOverlapped I/O operation is in progress./n"));
}
else
{
_tprintf(_T("/nLocal Group Error:/t%d/n"),lerror);
}
}
if(pBuf!=NULL)
{
NetApiBufferFree(pBuf);
}
}while(nStatus==ERROR_MORE_DATA);
return 0;
}

int globalgroup(PTSTR server)
{
PNET_DISPLAY_GROUP pGBuf,pGBuffer;
PGROUP_USERS_INFO_0 pUBuf,pUBuffer;
DWORD nGStatus,nUStatus;
DWORD i;
DWORD dwLevel,dwRec;
DWORD k;
DWORD er,tr,resume;
DWORD lerror;

i=0;
dwLevel=3;
er=0;
tr=0;
resume=0;
_tprintf(_T("/n**** Global group ****/n"));
do
{
nGStatus=NetQueryDisplayInformation(server,dwLevel,i,100,0xFFFFFFFF,&dwRec,(PVOID*)&pGBuf);
if((nGStatus==ERROR_SUCCESS) || (nGStatus==ERROR_MORE_DATA))
{
pGBuffer=pGBuf;
for(;dwRec>0;dwRec--)
{
_tprintf(_T("/nName:/t/t%s"),pGBuffer->grpi3_name);
_tprintf(_T("/nComment:/t%s"),pGBuffer->grpi3_comment);
_tprintf(_T("/nGroup ID:/t%u"),pGBuffer->grpi3_group_id);
_tprintf(_T("/nAttributs:/t%u"),pGBuffer->grpi3_attributes);
_tprintf(_T("/nMembers:/t"));

nUStatus=NetGroupGetUsers(server,pGBuffer->grpi3_name,0,(PBYTE *)&pUBuf,MAX_PREFERRED_LENGTH,&er,&tr,&resume);
if(nUStatus==NERR_Success)
{
pUBuffer=pUBuf;
for(k=1;k<=er;k++)
{
_tprintf(_T("%s "),pUBuffer->grui0_name);
pUBuffer++;
}
if(pUBuf!=NULL)
{
NetApiBufferFree(pUBuf);
}
}
_tprintf(_T("/n"));
i=pGBuffer->grpi3_next_index;
pGBuffer++;
}
}
else
{
lerror=GetLastError();
if(lerror==997)
{
_tprintf(_T("/nGlobal Group:/tOverlapped I/O operation is in progress./n"));
}
else
{
_tprintf(_T("/nGlobal Group Error:/t%d/n"),lerror);
}
}
if(pGBuf!=NULL)
{
NetApiBufferFree(pGBuf);
}
}while(nGStatus==ERROR_MORE_DATA);
return 0;
}

int transport(PTSTR server)
{
NET_API_STATUS nStatus;
PSERVER_TRANSPORT_INFO_0 pBuf,pBuffer;
DWORD dwLevel;
DWORD i;
DWORD er,tr,resume;
DWORD dwTotalCount;
DWORD dwLength;
DWORD lerror;

er=0;
tr=0;
resume=0;
dwLevel=0;
dwTotalCount=0;

_tprintf(_T("/n***** Transport *****/n"));
dwLength=_tcslen(server);
do
{
nStatus=NetServerTransportEnum(server,dwLevel,(PBYTE *)&pBuf,MAX_PREFERRED_LENGTH,&er,&tr,&resume);
if((nStatus==NERR_Success) || (nStatus==ERROR_MORE_DATA))
{
pBuffer=pBuf;
for(i=0;i<er;i++)
{
_tprintf(_T("/nTransport:/t%s"),pBuffer->svti0_transportname);
_tprintf(_T("/nNetworkAddr:/t%s"),pBuffer->svti0_networkaddress);
_tprintf(_T("/nActiveClient:/t%d User(s)/n"),pBuffer->svti0_numberofvcs);
pBuffer++;
dwTotalCount++;
}
}
else
{
lerror=GetLastError();
if(lerror==997)
{
_tprintf(_T("/nTransport:/tOverlapped I/O operation is in progress./n"));
}
else
{
_tprintf(_T("/nTransport Error:/t%d/n"),lerror);
}
}
if(pBuf!=NULL)
{
NetApiBufferFree(pBuf);
}
}while(nStatus==ERROR_MORE_DATA);
_tprintf(_T("/nTotal of %d entrie(s) enumerated./n"),dwTotalCount);
return 0;
}

int session(PTSTR server)
{
PSESSION_INFO_10 pBuf,pBuffer;
NET_API_STATUS nStatus;
DWORD i,dwLevel;
DWORD er,tr,resume;
DWORD dwTotalCount;
DWORD dwLength;
PTSTR pszClient;
PTSTR pszUser;
DWORD lerror;

_tprintf(_T("/n****** Session ******/n"));
dwLevel=10;
dwTotalCount=0;
pszClient=NULL;
pszUser=NULL;
er=0;
tr=0;
resume=0;
dwLength=_tcslen(server);

do
{
nStatus=NetSessionEnum(server,pszClient,pszUser,dwLevel,(PBYTE *)&pBuf,MAX_PREFERRED_LENGTH,&er,&tr,&resume);
if((nStatus==NERR_Success) || (nStatus==ERROR_MORE_DATA))
{
pBuffer=pBuf;
for(i=0;i<er;i++)
{
if(pBuffer==NULL)
{
_tprintf(_T("An access violation has occurred./n"));
break;
}
_tprintf(_T("/nClient:/t/t%s"),pBuffer->sesi10_cname);
_tprintf(_T("/nUser:/t/t%s"),pBuffer->sesi10_username);
_tprintf(_T("/nSeconds Active:/t%d"),pBuffer->sesi10_time);
_tprintf(_T("/nSeconds Idle:/t%d/n"),pBuffer->sesi10_idle_time);
pBuffer++;
dwTotalCount++;
}
}
else
{
lerror=GetLastError();
if(lerror==997)
{
_tprintf(_T("/nSession:/tOverlapped I/O operation is in progress./n"));
}
else
{
_tprintf(_T("/nSession Error:/t%d/n"),lerror);
}
}
if(pBuf!=NULL)
{
NetApiBufferFree(pBuf);
}
}while(nStatus==ERROR_MORE_DATA);
_tprintf(_T("/nTotal of %d entrie(s) enumerated./n"),dwTotalCount);
return 0;
}


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

得到計算機的主機名和IP地址

#include<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程序,然後輸入以下代碼:

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

nb30.h #include < 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;
}

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章