Click here to Skip to main content
15,867,704 members
Articles / Programming Languages / C++
Article

How to Detect Empty Password Users

Rate me:
Please Sign up or sign in to vote.
3.87/5 (26 votes)
13 Aug 2007CPOL2 min read 67.1K   24   18
This article describes how to detect empty password users

Introduction

This article describes how to detect empty password users in the Windows NT environment using Visual C++ 6. This method may require the Platform SDK.

Requisite Knowledge

Readers should be familiar with the C++ language and Windows API. This article is so simple that you can understand it even if you are not a professional.

Empty Password Users: Why We Detect Them

Empty password users can destroy local computers because a WORM VIRUS is able to use such accounts to spread itself. When you turn your system on without a password, your system infects within a minute. For this reason, if you can detect empty passwords, you can advise the user to set a password. In the past I searched Google for how to deal with this, but I could not get anything about it. So, I decided to write my own program. This is some simple empty password detection code that runs on Windows NT; it does not support Windows 9x.

What is the Problem?

The first idea is using the LogonUser API. This API can log in a given user name and return a result. The first code example is:

C++
HANDLE hToken = NULL;
BOOL bLoggedOn = ::LogonUser(pszUserName, pszPassword, NULL, 
    LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, &hToken);

if(bLoggedOn) 
{
    printf("Logged On!\n");
} 
else 
{
    printf("Failed\n");
}

However, the first code's problem is that the LogonUser API does not work with an empty password. We cannot call this function with a NULL password or "". In this situation, how can we detect an empty password? The answer is simple: just check the error code. In MSDN, LogonUser returns the error code via GetLastError. So, we can get LogonUser's error by using the GetLastError function. The following code describes how to get the error code of LogonUser.

C++
HANDLE hToken = NULL; 
BOOL bLoggedOn = ::LogonUser(pszUserName, "", NULL, 
    LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, &hToken);
DWORD dwError = GetLastError();

dwError has the error code of LogonUser. LogonUser returns error code 1327 when a user has an empty password. I tested the following code from Windows XP on my own system and it works well.

C++
HANDLE hToken = NULL; 
BOOL bLoggedOn = ::LogonUser(pszUserName, "", 
    NULL, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, 
&hToken);
DWORD dwError = GetLastError();

if(bLoggedOn || dwError == 1327)
{
    printf("Empty Password Logon User: %s\n", pszUserName);
}

History

  • 13 August, 2007 -- Original version posted

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
I started to write software since 1999 and have developed various products including security solutions and system utilities.

Microsoft Visual C++ MVP
Assistant Professor at the University of Virginia
Website: http://yongkwon.info

Comments and Discussions

 
QuestionI think the 2nd parameter for LogonUser is incorrect Pin
FredWah6-Mar-17 12:01
FredWah6-Mar-17 12:01 
SuggestionSuggestion to use in C# Pin
freedeveloper2-Jul-12 11:52
professionalfreedeveloper2-Jul-12 11:52 
Thanks for your Article.

It was very usefull to me. I converter it to C# easily, the only trick is that you dont need to call the GetLastError Function by declare it as external it is more easy:

var error = Marshal.GetLastWin32Error();

Thanks again
GeneralDomain user gets locked out Pin
Duggi18-Dec-07 22:01
Duggi18-Dec-07 22:01 
GeneralRe: Domain user gets locked out Pin
Yonghwi Kwon19-Dec-07 14:35
Yonghwi Kwon19-Dec-07 14:35 
GeneralRe: Domain user gets locked out Pin
jamalhaider18-Jun-08 19:52
jamalhaider18-Jun-08 19:52 
GeneralRe: Domain user gets locked out Pin
Duggi22-Oct-08 0:44
Duggi22-Oct-08 0:44 
GeneralEach test will add two entries in eventviewer Pin
Ralf Lohmueller5-Sep-07 19:32
Ralf Lohmueller5-Sep-07 19:32 
GeneralError 1327 Pin
David Crow21-Aug-07 3:32
David Crow21-Aug-07 3:32 
GeneralRe: Error 1327 Pin
Yonghwi Kwon22-Aug-07 3:20
Yonghwi Kwon22-Aug-07 3:20 
GeneralGreat tip! Pin
Grump20-Aug-07 23:01
Grump20-Aug-07 23:01 
GeneralRe: Great tip! Pin
Yonghwi Kwon22-Aug-07 3:19
Yonghwi Kwon22-Aug-07 3:19 
GeneralGood one! Pin
Vasudevan Deepak Kumar16-Aug-07 1:59
Vasudevan Deepak Kumar16-Aug-07 1:59 
GeneralRe: Good one! Pin
Anand Todkar16-Aug-07 5:01
Anand Todkar16-Aug-07 5:01 
General:confused: Wrong code is found Pin
j2doll13-Aug-07 19:23
j2doll13-Aug-07 19:23 
GeneralRe: :confused: Wrong code is found [modified] Pin
Yonghwi Kwon13-Aug-07 20:03
Yonghwi Kwon13-Aug-07 20:03 
Generalnice one. I will try it Pin
Michael Sync13-Aug-07 17:06
Michael Sync13-Aug-07 17:06 
GeneralRe: nice one. I will try it Pin
Yonghwi Kwon13-Aug-07 22:21
Yonghwi Kwon13-Aug-07 22:21 
GeneralRe: nice one. I will try it Pin
Michael Sync14-Aug-07 0:10
Michael Sync14-Aug-07 0:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.