├── README.md ├── apitool.sln └── apitool ├── apitool.cpp ├── apitool.vcxproj ├── apitool.vcxproj.filters └── apitool.vcxproj.user /README.md: -------------------------------------------------------------------------------- 1 | # apitool 2 |   Windows Api Tool 3 | 4 | 5 | # 功能: 6 | * 添加用户 7 | * 添加用户到组 8 | * 更改用户密码 9 | * 删除用户 10 | * 列出计算机上所有用户 11 | * 列出计算机上所有组 12 | 13 | 14 | ``` 15 | Name: 16 | WinApi Tools by Mking 17 | 18 | Usage: 19 | apitool.exe [options] [arguments...] 20 | 21 | Options: 22 | adduser (eg: apitool.exe adduser test 123456) 23 | addgroup (eg: apitool.exe addgroup test administrators) 24 | changepass (eg: apitool.exe changepass test test@123) 25 | deluser (eg: apitool.exe deluser test) 26 | listuser (eg: apitool.exe listuser) 27 | listgroup (eg: apitool.exe listgroup) 28 | ``` 29 | 30 | -------------------------------------------------------------------------------- /apitool.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 15 4 | VisualStudioVersion = 15.0.28307.852 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "apitool", "apitool\apitool.vcxproj", "{CF17A046-CE84-4A0E-815E-B80B6C89212C}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|x64 = Debug|x64 11 | Debug|x86 = Debug|x86 12 | Release|x64 = Release|x64 13 | Release|x86 = Release|x86 14 | EndGlobalSection 15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 16 | {CF17A046-CE84-4A0E-815E-B80B6C89212C}.Debug|x64.ActiveCfg = Debug|x64 17 | {CF17A046-CE84-4A0E-815E-B80B6C89212C}.Debug|x64.Build.0 = Debug|x64 18 | {CF17A046-CE84-4A0E-815E-B80B6C89212C}.Debug|x86.ActiveCfg = Debug|Win32 19 | {CF17A046-CE84-4A0E-815E-B80B6C89212C}.Debug|x86.Build.0 = Debug|Win32 20 | {CF17A046-CE84-4A0E-815E-B80B6C89212C}.Release|x64.ActiveCfg = Release|x64 21 | {CF17A046-CE84-4A0E-815E-B80B6C89212C}.Release|x64.Build.0 = Release|x64 22 | {CF17A046-CE84-4A0E-815E-B80B6C89212C}.Release|x86.ActiveCfg = Release|Win32 23 | {CF17A046-CE84-4A0E-815E-B80B6C89212C}.Release|x86.Build.0 = Release|Win32 24 | EndGlobalSection 25 | GlobalSection(SolutionProperties) = preSolution 26 | HideSolutionNode = FALSE 27 | EndGlobalSection 28 | GlobalSection(ExtensibilityGlobals) = postSolution 29 | SolutionGuid = {2DC1A966-5C5B-4D1D-B1BB-14FEED44780B} 30 | EndGlobalSection 31 | EndGlobal 32 | -------------------------------------------------------------------------------- /apitool/apitool.cpp: -------------------------------------------------------------------------------- 1 | #ifndef UNICODE 2 | #define UNICODE 3 | #endif 4 | #pragma comment(lib, "netapi32.lib") 5 | #define _CRT_SECURE_NO_DEPRECATE 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | USER_INFO_1 ui; 14 | DWORD dwLevel = 1; 15 | DWORD dwError = 0; 16 | NET_API_STATUS nStatus; 17 | 18 | 19 | bool EnableDebugPrivilege() 20 | { 21 | HANDLE hToken; 22 | LUID sedebugnameValue; 23 | TOKEN_PRIVILEGES tkp; 24 | 25 | if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) { 26 | //cout << "获取令牌句柄失败!" << endl; 27 | return false; 28 | } 29 | 30 | if (!LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &sedebugnameValue)) { 31 | //cout << "获取Luid失败" << endl; 32 | __try { 33 | if (hToken) { 34 | CloseHandle(hToken); 35 | } 36 | } 37 | __except (EXCEPTION_EXECUTE_HANDLER) {}; 38 | return false; 39 | } 40 | 41 | tkp.PrivilegeCount = 1; 42 | tkp.Privileges[0].Luid = sedebugnameValue; 43 | tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; 44 | if (!AdjustTokenPrivileges(hToken, FALSE, &tkp, sizeof(tkp), NULL, NULL)) { 45 | //cout << "修改特权不完全或失败!" << endl; 46 | __try { 47 | if (hToken) { 48 | CloseHandle(hToken); 49 | } 50 | } 51 | __except (EXCEPTION_EXECUTE_HANDLER) {}; 52 | return false; 53 | } 54 | else 55 | { 56 | //cout << "修改为system成功!" << endl; 57 | return true; 58 | } 59 | 60 | } 61 | 62 | 63 | void addUser(int argc, wchar_t* argv[]) { 64 | fwprintf(stderr, L"username:%s\npassword:%s\n", argv[2], argv[3]); 65 | ui.usri1_name = argv[2]; 66 | ui.usri1_password = argv[3]; 67 | ui.usri1_priv = USER_PRIV_USER; 68 | ui.usri1_home_dir = NULL; 69 | ui.usri1_comment = NULL; 70 | ui.usri1_flags = UF_DONT_EXPIRE_PASSWD; 71 | ui.usri1_script_path = NULL; 72 | 73 | nStatus = NetUserAdd(NULL, dwLevel, (LPBYTE)&ui, &dwError); 74 | if (nStatus == NERR_Success) { 75 | fwprintf(stderr, L"User %s has been successfully added\n", ui.usri1_name); 76 | } 77 | else { 78 | fprintf(stderr, "A system error has occurred: %d\n", nStatus); 79 | } 80 | 81 | exit(1); 82 | } 83 | 84 | void delUser(int argc, wchar_t* argv[]) { 85 | 86 | nStatus = NetUserDel(NULL, argv[2]); 87 | if (nStatus == NERR_Success) { 88 | fwprintf(stderr, L"User %s has been successfully delete\n", argv[2]); 89 | } 90 | else { 91 | fprintf(stderr, "A system error has occurred: %d\n", nStatus); 92 | } 93 | 94 | exit(1); 95 | } 96 | 97 | void changePass(int argc, wchar_t* argv[]) { 98 | 99 | DWORD dwLevel = 1003; 100 | USER_INFO_1003 ui_1003; 101 | NET_API_STATUS nStatus; 102 | LPWSTR wNewPassword; 103 | LPWSTR wComputerName; 104 | LPWSTR wUserName; 105 | 106 | wComputerName = NULL; 107 | wUserName = argv[2]; 108 | wNewPassword = argv[3]; 109 | 110 | ui_1003.usri1003_password = wNewPassword; 111 | 112 | nStatus = NetUserSetInfo(wComputerName, wUserName, dwLevel, (LPBYTE)&ui_1003, NULL); 113 | 114 | if (nStatus == NERR_Success) 115 | fwprintf(stderr, L"User %s's password has been successfully changed\n", argv[2]); 116 | else 117 | fprintf(stderr, "A system error has occurred: %d\n", nStatus); 118 | 119 | exit(0); 120 | } 121 | 122 | void addGroup(int argc, wchar_t* argv[]) { 123 | fwprintf(stderr, L"username:%s\ngroup:%s\n", argv[2], argv[3]); 124 | 125 | const wchar_t* name; 126 | name = (const wchar_t*)argv[2]; 127 | wchar_t lpszUser[30] = { 0 }; 128 | wcscpy(lpszUser, name); 129 | LOCALGROUP_MEMBERS_INFO_3 localgroup_members; 130 | localgroup_members.lgrmi3_domainandname = lpszUser; 131 | nStatus = NetLocalGroupAddMembers(NULL, // NULL 132 | argv[3], // group name 133 | 3, // name 134 | (LPBYTE)&localgroup_members, // buffer 135 | 1); 136 | switch (nStatus) 137 | { 138 | case 0: 139 | printf("User successfully added to %ls group.\n", argv[3]); 140 | break; 141 | case ERROR_MEMBER_IN_ALIAS: 142 | printf("User already in %ls group.\n", argv[3]); 143 | nStatus = 0; 144 | break; 145 | default: 146 | printf("Error adding user to %ls group: %d\n", argv[3], nStatus); 147 | break; 148 | } 149 | 150 | exit(1); 151 | } 152 | 153 | void listUser(int argc, wchar_t* argv[]) { 154 | LPUSER_INFO_0 pBuf = NULL; 155 | LPUSER_INFO_0 pTmpBuf; 156 | DWORD dwLevel = 0; 157 | DWORD dwPrefMaxLen = MAX_PREFERRED_LENGTH; 158 | DWORD dwEntriesRead = 0; 159 | DWORD dwTotalEntries = 0; 160 | DWORD dwResumeHandle = 0; 161 | DWORD i; 162 | DWORD dwTotalCount = 0; 163 | NET_API_STATUS nStatus; 164 | LPTSTR pszServerName = NULL; 165 | nStatus = NetUserEnum(NULL, 166 | dwLevel, 167 | FILTER_NORMAL_ACCOUNT, 168 | (LPBYTE*)&pBuf, 169 | dwPrefMaxLen, 170 | &dwEntriesRead, 171 | &dwTotalEntries, 172 | &dwResumeHandle); 173 | 174 | if ((nStatus == NERR_Success) || (nStatus == ERROR_MORE_DATA)) 175 | { 176 | if ((pTmpBuf = pBuf) != NULL) 177 | { 178 | 179 | for (i = 0; (i < dwEntriesRead); i++) 180 | { 181 | assert(pTmpBuf != NULL); 182 | if (pTmpBuf == NULL) 183 | { 184 | fwprintf(stderr, L"An access violation has occurred\n"); 185 | break; 186 | } 187 | fwprintf(stderr, L"--- %s\r\n", pTmpBuf->usri0_name); 188 | pTmpBuf++; 189 | } 190 | } 191 | } 192 | 193 | exit(1); 194 | } 195 | 196 | void listGroup(int argc, wchar_t* argv[]) { 197 | LPUSER_INFO_0 pBuf = NULL; 198 | LPUSER_INFO_0 pTmpBuf; 199 | DWORD dwLevel = 0; 200 | DWORD dwPrefMaxLen = MAX_PREFERRED_LENGTH; 201 | DWORD dwEntriesRead = 0; 202 | DWORD dwTotalEntries = 0; 203 | PDWORD_PTR dwResumeHandle = 0; 204 | DWORD i; 205 | DWORD dwTotalCount = 0; 206 | NET_API_STATUS nStatus; 207 | LPTSTR pszServerName = NULL; 208 | nStatus = NetLocalGroupEnum(NULL, dwLevel, (LPBYTE*)&pBuf, dwPrefMaxLen, &dwEntriesRead, &dwTotalEntries, dwResumeHandle); 209 | 210 | if ((nStatus == NERR_Success) || (nStatus == ERROR_MORE_DATA)) 211 | { 212 | if ((pTmpBuf = pBuf) != NULL) 213 | { 214 | 215 | for (i = 0; (i < dwEntriesRead); i++) 216 | { 217 | assert(pTmpBuf != NULL); 218 | if (pTmpBuf == NULL) 219 | { 220 | fwprintf(stderr, L"An access violation has occurred\n"); 221 | break; 222 | } 223 | fwprintf(stderr, L"--- %s\r\n", pTmpBuf->usri0_name); 224 | pTmpBuf++; 225 | } 226 | } 227 | } 228 | 229 | exit(1); 230 | } 231 | 232 | void printHelp() { 233 | TCHAR szPath[MAX_PATH] = { 0 }; 234 | GetModuleFileName(NULL, szPath, MAX_PATH); 235 | //(_tcsrchr(szPath, _T('\\')))[1] = 0; //pathName 236 | 237 | wchar_t* programName = &(_tcsrchr(szPath, _T('\\')))[1]; 238 | 239 | fwprintf(stderr, L"\ 240 | Name:\n\ 241 | WinApi Tools \n\n\ 242 | \ 243 | Usage:\n\ 244 | %ls [options] [arguments...]\n\n\ 245 | \ 246 | Options:\n\ 247 | adduser (eg: %ls adduser test 123456) \n\ 248 | addgroup (eg: %ls addgroup test administrators) \n\ 249 | changepass (eg: %ls changepass test test@123) \n\ 250 | deluser (eg: %ls deluser test) \n\ 251 | listuser (eg: %ls listuser) \n\ 252 | listgroup (eg: %ls listgroup) \n\ 253 | ", programName, programName, programName, programName, programName, programName, programName); 254 | 255 | exit(1); 256 | } 257 | 258 | int wmain(int argc, wchar_t* argv[]) 259 | { 260 | EnableDebugPrivilege(); 261 | 262 | if (argc == 4 && wcscmp(argv[1], TEXT("adduser")) == 0) 263 | { 264 | addUser(argc, argv); 265 | } 266 | 267 | else if (argc == 4 && wcscmp(argv[1], TEXT("addgroup")) == 0) 268 | { 269 | addGroup(argc, argv); 270 | } 271 | 272 | else if (argc == 4 && wcscmp(argv[1], TEXT("changepass")) == 0) 273 | { 274 | changePass(argc, argv); 275 | } 276 | 277 | else if (argc == 3 && wcscmp(argv[1], TEXT("deluser")) == 0) 278 | { 279 | delUser(argc, argv); 280 | } 281 | 282 | else if (argc == 2 && wcscmp(argv[1], TEXT("listuser")) == 0) 283 | { 284 | listUser(argc, argv); 285 | } 286 | 287 | else if (argc == 2 && wcscmp(argv[1], TEXT("listgroup")) == 0) 288 | { 289 | listGroup(argc, argv); 290 | } 291 | 292 | else { 293 | printHelp(); 294 | } 295 | 296 | return 0; 297 | 298 | } 299 | 300 | -------------------------------------------------------------------------------- /apitool/apitool.vcxproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | Debug 14 | x64 15 | 16 | 17 | Release 18 | x64 19 | 20 | 21 | 22 | 15.0 23 | {CF17A046-CE84-4A0E-815E-B80B6C89212C} 24 | Win32Proj 25 | apitool 26 | 10.0.17763.0 27 | 28 | 29 | 30 | Application 31 | true 32 | v141 33 | Unicode 34 | 35 | 36 | Application 37 | false 38 | v140 39 | true 40 | Unicode 41 | 42 | 43 | Application 44 | true 45 | v141 46 | Unicode 47 | 48 | 49 | Application 50 | false 51 | v140 52 | true 53 | Unicode 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | true 75 | 76 | 77 | true 78 | 79 | 80 | false 81 | $(SolutionDir)bin 82 | 83 | 84 | false 85 | $(SolutionDir)bin 86 | $(ProjectName)x64 87 | 88 | 89 | 90 | NotUsing 91 | Level3 92 | Disabled 93 | true 94 | WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) 95 | true 96 | pch.h 97 | 98 | 99 | Console 100 | true 101 | 102 | 103 | 104 | 105 | NotUsing 106 | Level3 107 | Disabled 108 | true 109 | _DEBUG;_CONSOLE;%(PreprocessorDefinitions) 110 | true 111 | pch.h 112 | 113 | 114 | Console 115 | true 116 | 117 | 118 | 119 | 120 | NotUsing 121 | Level3 122 | MaxSpeed 123 | true 124 | true 125 | true 126 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 127 | true 128 | pch.h 129 | 130 | 131 | Console 132 | true 133 | true 134 | false 135 | 136 | 137 | 138 | 139 | NotUsing 140 | Level3 141 | MaxSpeed 142 | true 143 | true 144 | true 145 | NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 146 | true 147 | pch.h 148 | 149 | 150 | Console 151 | true 152 | true 153 | false 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | -------------------------------------------------------------------------------- /apitool/apitool.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | 头文件 20 | 21 | 22 | 23 | 24 | 源文件 25 | 26 | 27 | -------------------------------------------------------------------------------- /apitool/apitool.vcxproj.user: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | --------------------------------------------------------------------------------