-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathfind_token.c
165 lines (143 loc) · 4.68 KB
/
find_token.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
Software License Agreement (BSD License)
Copyright (c) 2006, Luke Jennings ([email protected])
All rights reserved.
Redistribution and use of this software in source and binary forms, with or without modification, are
permitted provided that the following conditions are met:
* Redistributions of source code must retain the above
copyright notice, this list of conditions and the
following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the
following disclaimer in the documentation and/or other
materials provided with the distribution.
* Neither the name of Luke Jennings nor the names of its
contributors may be used to endorse or promote products
derived from this software without specific prior
written permission of Luke Jennings.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#define _CRT_SECURE_NO_DEPRECATE 1
#include <stdio.h>
#include <assert.h>
#include <windows.h>
#include <lm.h>
void enumerate_logged_on_users(char *server, char *username, char *password);
int main(int argc, char *argv[])
{
FILE *fp;
char *username, *password, file_line[100], temp_server_name[100];
BOOL bFileMode = FALSE;
if (argc < 2)
{
fprintf(stderr, "usage: \n\n%s <server_name_or_ip> | -f <server_list_file> [username] [password]\n", argv[0]);
exit(1);
}
if (!_stricmp(argv[1], "-f") && argc > 2)
bFileMode = TRUE;
if (argc == 5)
{
username = argv[3];
password = argv[4];
}
else if (argc == 4)
{
username = argv[2];
password = argv[3];
}
else
{
username = NULL;
password = NULL;
}
printf("[*] Scanning for logged on users...\n\n");
printf("Server Name\t\tUsername\n");
printf("------------------------------------------------------\n");
if (bFileMode)
{
fp = fopen(argv[2], "r");
while (fgets(file_line, sizeof(file_line)-1, fp))
{
sscanf(file_line, "%s\n", temp_server_name);
enumerate_logged_on_users(temp_server_name, username, password);
}
}
else
enumerate_logged_on_users(argv[1], username, password);
return 0;
}
void enumerate_logged_on_users(char *server, char *username, char *password)
{
LPWKSTA_USER_INFO_1 pBuf = NULL;
LPWKSTA_USER_INFO_1 pTmpBuf;
DWORD dwLevel = 1;
DWORD dwPrefMaxLen = MAX_PREFERRED_LENGTH;
DWORD dwEntriesRead = 0;
DWORD dwTotalEntries = 0;
DWORD dwResumeHandle = 0;
DWORD i;
DWORD dwTotalCount = 0;
NET_API_STATUS nStatus;
wchar_t pszServerName[100];
char ascii_server_name[100];
NETRESOURCE nr;
strcpy(ascii_server_name, "\\\\");
strncat(ascii_server_name, server, 97);
ascii_server_name[99] = '\0';
mbstowcs(pszServerName, ascii_server_name, strlen(ascii_server_name)+1);
if (username && password)
{
nr.dwType = RESOURCETYPE_DISK;
nr.lpLocalName = NULL;
nr.lpProvider = NULL;
nr.lpRemoteName = ascii_server_name;
WNetCancelConnection2A(nr.lpRemoteName, 0, TRUE);
WNetAddConnection2A(&nr, password, username, 0);
}
do // begin do
{
nStatus = NetWkstaUserEnum((LPWSTR)pszServerName,
dwLevel,
(LPBYTE*)&pBuf,
dwPrefMaxLen,
&dwEntriesRead,
&dwTotalEntries,
&dwResumeHandle);
if ((nStatus == NERR_Success) || (nStatus == ERROR_MORE_DATA))
{
if ((pTmpBuf = pBuf) != NULL)
{
for (i = 0; (i < dwEntriesRead); i++)
{
assert(pTmpBuf != NULL);
if (pTmpBuf == NULL)
{
fprintf(stderr, "An access violation has occurred\n");
break;
}
if (!wcschr((wchar_t*)(pTmpBuf)->wkui1_username, L'$'))
{
printf("%s", server);
wprintf(L"\t\t%s\\%s\n", pTmpBuf->wkui1_logon_domain, pTmpBuf->wkui1_username);
}
pTmpBuf++;
dwTotalCount++;
}
}
}
else
fprintf(stderr, "%s\t\tError: %d\n", server, nStatus);
} while (nStatus == ERROR_MORE_DATA);
if (pBuf != NULL)
{
NetApiBufferFree(pBuf);
pBuf = NULL;
}
}