关于域普通用户权限中寻找域中域管登陆记录的问题及解决

前言:作为普通域用户如何进行定位域用户登录的笔记

看了很多篇都说啥脚本可以直接来定位高权限的登录记录,我都试了其实发现都是不行的,作为普通域用户根本没有权限来进行相关的查询。

注册表

最后折中的办法,因为这种方法在PC系统是无法查询的!

import re
import sys
import os

abs_path = os.getcwd() + os.path.sep

content_regexp = "\s+"  # 多个空格进行字符串拆分到数组
# 首先把net group "Domain Computers" /domain得到的域机器名字放在computer.txt文本中
with open(abs_path + "computer.txt", 'r') as f:
    content = f.read()
    computer_content_array = re.split(content_regexp, content)
    for computer in computer_content_array:
        #这个是批量跑每个机器上的登陆情况的
        print("reg query \\\\" + computer[:-1] + "\\HKEY_USERS")

PsLoggedon

原理跟上面的一样还是通过注册表来进行查询共享会话

通过工具PsLoggedon64能够实现查询注册表来进行定位,所以以后可以使用这个来进行高权限会话的定位。

https://docs.microsoft.com/en-us/sysinternals/downloads/psloggedon

sloggedon.exe [-] [-l] [-x] [\\computername|username]

-   显示支持的选项和用于输出值的单位。
-l  仅显示本地登录,不显示本地和网络资源登录。
-x  不显示登录时间。

域控机器上进行 dir \\WIN-SKE-PC\c$

WIN-SKE-PC 机器上进行PsLoggedon64查询如下

代码实现

// ConsoleApplication28.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
// ConsoleApplication25.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>

#ifndef UNICODE
#define UNICODE
#endif
#pragma comment(lib, "Netapi32.lib")
#pragma warning(disable:4996)
#include <stdio.h>
#include <assert.h>
#include <windows.h> 
#include <lm.h>
#include <ctime>

int session_enum(LPTSTR pszServerName) {
    NET_API_STATUS nStatus;
    LPSESSION_INFO_10 pBuf = NULL;
    LPSESSION_INFO_10 pTmpBuf;
    DWORD dwLevel = 10;
    DWORD dwPrefMaxLen = MAX_PREFERRED_LENGTH;
    DWORD dwEntriesRead = 0;
    DWORD dwTotalEntries = 0;
    DWORD dwResumeHandle = 0;
    DWORD i;
    DWORD dwTotalCount = 0;
    //LPTSTR pszServerName = NULL;
    do // begin do
    {
        nStatus = NetSessionEnum(pszServerName,
            NULL,
            NULL,
            dwLevel,
            (LPBYTE*)&pBuf,
            dwPrefMaxLen,
            &dwEntriesRead,
            &dwTotalEntries,
            &dwResumeHandle);
        //
        // If the call succeeds,
        //
        if ((nStatus == NERR_Success) || (nStatus == ERROR_MORE_DATA))
        {
            if ((pTmpBuf = pBuf) != NULL)
            {
                //
                // Loop through the entries.
                //
                for (i = 0; (i < dwEntriesRead); i++)
                {
                    assert(pTmpBuf != NULL);

                    if (pTmpBuf == NULL)
                    {
                        fprintf(stderr, "An access violation has occurred\n");
                        break;
                    }
                    //
                    // Print the retrieved data. 
                    //

                    //sprintf(url, "%ws -> %ws\n", pTmpBuf->sesi10_cname,pTmpBuf->sesi10_username);


                    SYSTEMTIME sys;
                    GetLocalTime(&sys);
                    char current_time[64] = { NULL };
                    sprintf(current_time, "%4d-%02d-%02d %02d:%02d:%02d ", sys.wYear, sys.wMonth, sys.wDay, sys.wHour, sys.wMinute, sys.wSecond);




                    printf("[%s]  [%ws]  [%ws]  [%ws]\n", current_time, pszServerName, pTmpBuf->sesi10_cname, pTmpBuf->sesi10_username);
                    /*
                    wprintf(L"\n\tClient: %s\n", pTmpBuf->sesi10_cname);
                    wprintf(L"\tUser:   %s\n", pTmpBuf->sesi10_username);
                    printf("\tActive: %d\n", pTmpBuf->sesi10_time);
                    printf("\tIdle:   %d\n", pTmpBuf->sesi10_idle_time);
                    */
                    pTmpBuf++;
                    dwTotalCount++;
                }
            }
        }
        //
        // Otherwise, indicate a system error.
        //
        else
            fprintf(stderr, "A system error has occurred: %d\n", nStatus);
        //
        // Free the allocated memory.
        //
        if (pBuf != NULL)
        {
            NetApiBufferFree(pBuf);
            pBuf = NULL;
        }
    }
    // 
    // Continue to call NetSessionEnum while 
    //  there are more entries. 
    // 
    while (nStatus == ERROR_MORE_DATA); // end do

    // Check again for an allocated buffer.
    //
    if (pBuf != NULL)
        NetApiBufferFree(pBuf);
    return 0;
}
int wmain(int argc, wchar_t* argv[])
{



    //
    // Check command line arguments.
    //

    if (argc == 1) {
        printf("\nUsing:\n\t session_enum.exe \\\\dc1 \\\\dc2 \n");
        return 0;
    }

    while (true)
    {
        for (size_t i = 0; i < argc; i++)
        {
            if (i == 0) {
                continue;
            }
            //printf("%ws\n",argv[i]);
            session_enum(argv[i]);
        }
        Sleep(5000);
    }




    return 0;
}

posted @ 2020-05-24 15:45  zpchcbd  阅读(402)  评论(0)    收藏  举报