Microsoft MVP성태의 닷넷 이야기
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일

C# - Win32 API를 이용한 윈도우 계정 정보 (예: 마지막 로그온 시간)

아래의 글에 자세하게 설명하고 있습니다.

How can I find out the last time a user logged on from C++?
; https://devblogs.microsoft.com/oldnewthing/20230622-00/?p=108369

간단하게는 PowerShell로 가능하고,

PS C:\Users\testusr> echo $env:USERNAME
testusr

PS C:\Users\testusr> Get-LocalUser "testusr" | Format-List
AccountExpires         :
Description            :
Enabled                : True
FullName               :
PasswordChangeableDate : 2021-11-19 오전 7:49:48
PasswordExpires        :                           // 리눅스라면 chage 명령어로 제어
UserMayChangePassword  : True
PasswordRequired       : False
PasswordLastSet        : 2021-11-19 오전 7:49:48
LastLogon              : 2023-06-23 오후 6:02:07  // 리눅스의 경우 "who -u" 명령어로 조회
Name                   : testusr
SID                    : S-1-5-....[생략]...
PrincipalSource        : Local
ObjectClass            : User

윈도우 Shell에서는 net.exe를 이용해 구할 수 있습니다.

C:\testusr> net user "testusr"
User name                    testusr
Full Name
Comment
User's comment
Country/region code          000 (System Default)
Account active               Yes
Account expires              Never

Password last set            2021-11-19 오전 7:49:48
Password expires             Never
Password changeable          2021-11-19 오전 7:49:48
Password required            No
User may change password     Yes

Workstations allowed         All
Logon script
User profile
Home directory
Last logon                   2023-06-23 오후 6:02:07

Logon hours allowed          All

Local Group Memberships      *Administrators       *docker-users
                             *Performance Log Users
Global Group memberships     *None
The command completed successfully.

코드로는 NetUserGetInfo 함수를 이용해 구할 수 있습니다. (문서에 예제 코드가 포함돼 있습니다.)

C#으로 구해보면, 대략 다음과 같은 코드로 나옵니다. ^^

using System.Runtime.InteropServices;
using System.Text;

namespace ConsoleApp1;

internal class Program
{
    [DllImport("netapi32.dll", CharSet = CharSet.Unicode)] // lmaccess.h
    public extern static NERR NetUserGetInfo(string servername, string username, NET_USER_GET_TYPE level, out IntPtr bufptr);

    [DllImport("netapi32.dll", CharSet = CharSet.Unicode)]
    public extern static uint NetApiBufferFree(IntPtr buffer); // lmapibuf.h

    static void Main(string[] args)
    {
        string? userName = Environment.GetEnvironmentVariable("USERNAME");
        string? serverName = Environment.GetEnvironmentVariable("USERDOMAIN");
        if (userName == null || serverName == null)
        {
            Console.WriteLine("USERNAME or USERDOMAIN is not set");
            return;
        }

        NET_USER_GET_TYPE infoType = NET_USER_GET_TYPE.USER_INFO_2;

        NERR result = NetUserGetInfo(serverName, userName, infoType, out IntPtr buffer);
        if (result != NERR.NERR_Success || buffer == IntPtr.Zero)
        {
            return;
        }

        switch (infoType)
        {
            case NET_USER_GET_TYPE.USER_INFO_2:
                var userInfo = USER_INFO_2.LoadFromPtr(buffer);
                Console.WriteLine(userInfo);
                break;
        }

        NetApiBufferFree(buffer);
    }
}

public struct USER_INFO_2 // lmaccess.h
{
    public string usri2_name;
    public string usri2_password;
    public uint usri2_password_age;
    public uint usri2_priv;
    public string usri2_home_dir;
    public string usri2_comment;
    public uint usri2_flags;
    public string usri2_script_path;
    public uint usri2_auth_flags;
    public string usri2_full_name;
    public string usri2_usr_comment;
    public string usri2_parms;
    public string usri2_workstations;
    public uint usri2_last_logon;
    public uint usri2_last_logoff;
    public uint usri2_acct_expires;
    public uint usri2_max_storage;
    public uint usri2_units_per_week;
    public byte[] usri2_logon_hours;
    public uint usri2_bad_pw_count;
    public uint usri2_num_logons;
    public string usri2_logon_server;
    public uint usri2_country_code;
    public uint usri2_code_page;
    public uint usri2_user_id;
    public uint usri2_primary_group_id;
    public string usri2_profile;
    public string usri2_home_dir_drive;
    public uint usri2_password_expired;

    public static USER_INFO_2 LoadFromPtr(IntPtr ptr)
    {
        if (IntPtr.Size == 4)
        {
            throw new NotSupportedException();
        }

        USER_INFO_2 data = new USER_INFO_2();

        data.usri2_name = ReadString(ref ptr);
        data.usri2_password = ReadString(ref ptr);

        data.usri2_password_age = ReadUInt32(ref ptr);
        data.usri2_priv = ReadUInt32(ref ptr);

        data.usri2_home_dir = ReadString(ref ptr);
        data.usri2_comment = ReadString(ref ptr);

        data.usri2_flags = (uint)ReadUInt32(ref ptr, 8); // 8 bytes alignment
        data.usri2_script_path = ReadString(ref ptr);
        data.usri2_auth_flags = (uint)ReadUInt32(ref ptr, 8); // 8 bytes alignment

        data.usri2_full_name = ReadString(ref ptr);
        data.usri2_usr_comment = ReadString(ref ptr);
        data.usri2_parms = ReadString(ref ptr);
        data.usri2_workstations = ReadString(ref ptr);

        data.usri2_last_logon = ReadUInt32(ref ptr);
        data.usri2_last_logoff = ReadUInt32(ref ptr);
        data.usri2_acct_expires = ReadUInt32(ref ptr);
        data.usri2_max_storage = ReadUInt32(ref ptr);
        
        data.usri2_units_per_week = ReadUInt32(ref ptr, 8); // 8 bytes alignment
        data.usri2_logon_hours = ReadBytes(ref ptr, 21);
        data.usri2_bad_pw_count = ReadUInt32(ref ptr);
        data.usri2_num_logons = ReadUInt32(ref ptr);

        data.usri2_logon_server = ReadString(ref ptr);
        data.usri2_country_code = ReadUInt32(ref ptr);
        data.usri2_code_page = ReadUInt32(ref ptr);

        return data;
    }

    public static byte[] ReadBytes(ref IntPtr ptr, int count)
    {
        IntPtr ptrAddr = Marshal.ReadIntPtr(ptr);
        ptr = new IntPtr(ptr + IntPtr.Size);

        byte[] buffer = new byte[count];
        for (int i = 0; i < count; i ++)
        {
            buffer[i] = Marshal.ReadByte(ptrAddr, i);
        }

        return buffer;
    }

    public static string ReadString(ref IntPtr ptr)
    {
        IntPtr ptrAddr = Marshal.ReadIntPtr(ptr);
        ptr = new IntPtr(ptr + IntPtr.Size);
        return Marshal.PtrToStringUni(ptrAddr) ?? "";
    }

    public static uint ReadUInt32(ref IntPtr ptr, int offset = 4)
    {
        uint value = (uint)Marshal.ReadInt32(ptr);
        ptr = new IntPtr(ptr + offset);
        return value;
    }

    public override string ToString()
    {
        StringBuilder sb = new();
        sb.AppendLine($"User account name: {usri2_name}");
        sb.AppendLine($"Password: {usri2_password}");

        sb.AppendLine($"Password age (seconds): {usri2_password_age}");

        sb.AppendLine($"Privilege level: {usri2_priv}");
        sb.AppendLine($"Home directory: {usri2_home_dir}");
        sb.AppendLine($"Comment: {usri2_comment}");
        sb.AppendLine($"Flags (in hex): {usri2_flags:x}");
        sb.AppendLine($"Script path: {usri2_script_path}");
        sb.AppendLine($"Auth flags (in hex): {usri2_auth_flags:x}");

        sb.AppendLine($"Full name: {usri2_full_name}");
        sb.AppendLine($"User comment: {usri2_usr_comment}");
        sb.AppendLine($"Parameters: {usri2_parms}");
        sb.AppendLine($"Workstations: {usri2_workstations}");

        sb.AppendLine($"Last logon (seconds since January 1, 1970 GMT): {usri2_last_logon}");
        sb.AppendLine($"Last logoff (seconds since January 1, 1970 GMT): {usri2_last_logoff}");
        sb.AppendLine($"Account expires (seconds since January 1, 1970 GMT): {(int)usri2_acct_expires}");
        sb.AppendLine($"Max storage: {(int)usri2_max_storage}");
        sb.AppendLine($"Units per week: {usri2_units_per_week}");

        sb.Append("Logon hours:");
        for (int i = 0; i < 21; i ++)
        {
            sb.Append($" {usri2_logon_hours[i]:x2}");
        }
        sb.AppendLine();

        sb.AppendLine($"Bad password count: {usri2_bad_pw_count}");
        sb.AppendLine($"Number of logons: {usri2_num_logons}");

        sb.AppendLine($"Logon server: {usri2_logon_server}");
        sb.AppendLine($"Country code: {usri2_country_code}");
        sb.AppendLine($"Code page: {usri2_code_page}");

        return sb.ToString();
    }
}

public enum NET_USER_GET_TYPE : uint // lmaccess.h
{
    USER_INFO_2 = 2,
}

public enum NERR // lmerr.h
{
    NERR_Success = 0,
    ERROR_ACCESS_DENIED = 5,
    ERROR_BAD_NETPATH = 53,
    ERROR_INVALID_LEVEL = 124,
    NERR_UserNotFound = 2221,
    NERR_InvalidComputer = 2351,
}

C++에 맞춰진 구조체의 마샬링 코드가 좀 귀찮게 들어갔을 뿐 코드 자체는 단순합니다. 단지, 한 가지 주의할 점이 있는데요, C++의 구조체에 별도로 #pragma pack이 지정된 것은 아니어서,

typedef struct _USER_INFO_2 {
    LPWSTR   usri2_name;
    LPWSTR   usri2_password;
    DWORD    usri2_password_age;
    DWORD    usri2_priv;
    LPWSTR   usri2_home_dir;
    LPWSTR   usri2_comment;
    DWORD    usri2_flags;
    LPWSTR   usri2_script_path;
    DWORD    usri2_auth_flags;
    LPWSTR   usri2_full_name;
    LPWSTR   usri2_usr_comment;
    LPWSTR   usri2_parms;
    LPWSTR   usri2_workstations;
    DWORD    usri2_last_logon;
    DWORD    usri2_last_logoff;
    DWORD    usri2_acct_expires;
    DWORD    usri2_max_storage;
    DWORD    usri2_units_per_week;
    PBYTE    usri2_logon_hours;
    DWORD    usri2_bad_pw_count;
    DWORD    usri2_num_logons;
    LPWSTR   usri2_logon_server;
    DWORD    usri2_country_code;
    DWORD    usri2_code_page;
}USER_INFO_2, *PUSER_INFO_2, *LPUSER_INFO_2;

64비트의 경우 8바이트 정렬로 인해 위의 3개 필드(usri2_flags, usri2_auth_flags, usri2_units_per_week)의 데이터 자체는 4바이트 크기이지만 패딩을 위해 각각 4바이트를 더 건너뛰어야 합니다.

자... 그래서 이제 실행을 하면 이런 출력을 보게 될 것입니다. ^^

User account name: testusr
Password:
Password age (seconds): 50253110
Privilege level: 2
Home directory:
Comment:
Flags (in hex): 10221
Script path:
Auth flags (in hex): 0
Full name:
User comment:
Parameters:
Workstations:
Last logon (seconds since January 1, 1970 GMT): 1687523407
Last logoff (seconds since January 1, 1970 GMT): 0
Account expires (seconds since January 1, 1970 GMT): -1
Max storage: -1
Units per week: 168
Logon hours: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
Bad password count: 0
Number of logons: 1282
Logon server: \\*
Country code: 0
Code page: 0

(첨부 파일은 이 글의 예제 코드를 포함합니다.)

참고로, Get과 대응해 Set에 해당하는 NetUserSetInfo API도 있는데, 해당 문서에는 계정을 비활성화하는 예제 코드를 담고 있습니다.




[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]







[최초 등록일: ]
[최종 수정일: 6/25/2023]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 




1  2  [3]  4  5  6  7  8  9  10  11  12  13  14  15  ...
NoWriterDateCnt.TitleFile(s)
13571정성태3/1/20241946닷넷: 2223. C# - await 호출과 WPF의 Dispatcher Queue 동작 확인파일 다운로드1
13570정성태2/29/20241895닷넷: 2222. C# - WPF의 Dispatcher Queue 동작 확인파일 다운로드1
13569정성태2/28/20241802닷넷: 2221. C# - LoadContext, LoadFromContext 그리고 GAC파일 다운로드1
13568정성태2/27/20241932닷넷: 2220. C# - .NET Framework 프로세스의 LoaderOptimization 설정을 확인하는 방법파일 다운로드1
13567정성태2/27/20241882오류 유형: 898. .NET Framework 3.5 이하에서 mscoree.tlb 참조 시 System.BadImageFormatException파일 다운로드1
13566정성태2/27/20241949오류 유형: 897. Windows 7 SDK 설치 시 ".NET Development" 옵션이 비활성으로 선택이 안 되는 경우
13565정성태2/23/20241824닷넷: 2219. .NET CLR2 보안 모델에서의 개별 System.Security.Permissions 제어
13564정성태2/22/20242019Windows: 259. Hyper-V Generation 1 유형의 VM을 Generation 2 유형으로 바꾸는 방법
13563정성태2/21/20241985디버깅 기술: 196. windbg - async/await 비동기인 경우 메모리 덤프 분석의 어려움
13562정성태2/21/20242031오류 유형: 896. ASP.NET - .NET Framework 기본 예제에서 System.Web에 대한 System.IO.FileNotFoundException 예외 발생
13561정성태2/20/20242109닷넷: 2218. C# - (예를 들어, Socket) 비동기 I/O에 대한 await 호출 시 CancellationToken을 이용한 취소파일 다운로드1
13560정성태2/19/20242149디버깅 기술: 195. windbg 분석 사례 - Semaphore 잠금으로 인한 Hang 현상 (닷넷)
13559정성태2/19/20242978오류 유형: 895. ASP.NET - System.Security.SecurityException: 'Requested registry access is not allowed.'
13558정성태2/18/20242232닷넷: 2217. C# - 최댓값이 1인 SemaphoreSlim 보다 Mutex 또는 lock(obj)를 선택하는 것이 나은 이유
13557정성태2/18/20241971Windows: 258. Task Scheduler의 Author 속성 값을 변경하는 방법
13556정성태2/17/20242024Windows: 257. Windows - Symbolic (hard/soft) Link 및 Junction 차이점
13555정성태2/15/20242190닷넷: 2216. C# - SemaphoreSlim 사용 시 주의점
13554정성태2/15/20241897VS.NET IDE: 189. Visual Studio - 닷넷 소스코드 디컴파일 찾기가 안 될 때
13553정성태2/14/20241994닷넷: 2215. windbg - thin/fat lock 없이 동작하는 Monitor.Wait + Pulse
13552정성태2/13/20241934닷넷: 2214. windbg - Monitor.Enter의 thin lock과 fat lock
13551정성태2/12/20242126닷넷: 2213. ASP.NET/Core 웹 응용 프로그램 - 2차 스레드의 예외로 인한 비정상 종료
13550정성태2/11/20242285Windows: 256. C# - Server socket이 닫히면 Accept 시켰던 자식 소켓이 닫힐까요?
13549정성태2/3/20242833개발 환경 구성: 706. C# - 컨테이너에서 실행하기 위한 (소켓) 콘솔 프로젝트 구성
13548정성태2/1/20242674개발 환경 구성: 705. "Docker Desktop for Windows" - ASP.NET Core 응용 프로그램의 소켓 주소 바인딩(IPv4/IPv6 loopback, Any)
13547정성태1/31/20242449개발 환경 구성: 704. Visual Studio - .NET 8 프로젝트부터 dockerfile에 추가된 "USER app" 설정
13546정성태1/30/20242255Windows: 255. (디버거의 영향 등으로) 대상 프로세스가 멈추면 Socket KeepAlive로 연결이 끊길까요?
1  2  [3]  4  5  6  7  8  9  10  11  12  13  14  15  ...