nano-tetra-modules/DNSspoof/api/module.php

301 lines
10 KiB
PHP
Raw Normal View History

2017-11-16 05:42:22 +00:00
<?php namespace pineapple;
2018-08-27 00:32:32 +00:00
2017-11-16 05:42:22 +00:00
class DNSspoof extends Module
{
2018-08-27 00:32:32 +00:00
public function route()
2017-11-16 05:42:22 +00:00
{
switch ($this->request->action) {
2018-08-27 00:32:32 +00:00
case 'refreshInfo':
$this->refreshInfo();
break;
case 'refreshOutput':
2017-11-16 05:42:22 +00:00
$this->refreshOutput();
break;
case 'refreshStatus':
$this->refreshStatus();
break;
case 'toggleDNSspoof':
$this->toggleDNSspoof();
break;
case 'handleDependencies':
$this->handleDependencies();
break;
case 'handleDependenciesStatus':
$this->handleDependenciesStatus();
break;
case 'refreshHistory':
$this->refreshHistory();
break;
case 'viewHistory':
$this->viewHistory();
break;
case 'deleteHistory':
$this->deleteHistory();
break;
2018-08-27 00:32:32 +00:00
case 'downloadHistory':
$this->downloadHistory();
break;
case 'toggleDNSspoofOnBoot':
$this->toggleDNSspoofOnBoot();
break;
case 'getInterfaces':
$this->getInterfaces();
break;
case 'saveAutostartSettings':
$this->saveAutostartSettings();
break;
case 'saveLandingPageData':
$this->saveLandingPageData();
break;
case 'getLandingPageData':
$this->getLandingPageData();
break;
case 'saveHostsData':
$this->saveHostsData();
break;
case 'getHostsData':
$this->getHostsData();
break;
2017-11-16 05:42:22 +00:00
}
}
protected function checkDep($dependencyName)
2018-08-27 00:32:32 +00:00
{
return ($this->checkDependency($dependencyName) && ($this->uciGet("dnsspoof.module.installed")));
2018-08-27 00:32:32 +00:00
}
2017-11-16 05:42:22 +00:00
2018-08-27 00:32:32 +00:00
protected function getDevice()
{
return trim(exec("cat /proc/cpuinfo | grep machine | awk -F: '{print $2}'"));
}
2017-11-16 05:42:22 +00:00
2018-08-27 00:32:32 +00:00
protected function refreshInfo()
{
$moduleInfo = @json_decode(file_get_contents("/pineapple/modules/DNSspoof/module.info"));
$this->response = array('title' => $moduleInfo->title, 'version' => $moduleInfo->version);
}
2017-11-16 05:42:22 +00:00
private function handleDependencies()
{
if (!$this->checkDep("dnsspoof")) {
2018-08-27 00:32:32 +00:00
$this->execBackground("/pineapple/modules/DNSspoof/scripts/dependencies.sh install ".$this->request->destination);
$this->response = array('success' => true);
} else {
$this->execBackground("/pineapple/modules/DNSspoof/scripts/dependencies.sh remove");
$this->response = array('success' => true);
}
}
2017-11-16 05:42:22 +00:00
private function handleDependenciesStatus()
{
2018-08-27 00:32:32 +00:00
if (!file_exists('/tmp/DNSspoof.progress')) {
2017-11-16 05:42:22 +00:00
$this->response = array('success' => true);
2018-08-27 00:32:32 +00:00
} else {
2017-11-16 05:42:22 +00:00
$this->response = array('success' => false);
}
}
private function toggleDNSspoofOnBoot()
{
2018-08-27 00:32:32 +00:00
if (exec("cat /etc/rc.local | grep DNSspoof/scripts/autostart_dnsspoof.sh") == "") {
exec("sed -i '/exit 0/d' /etc/rc.local");
exec("echo /pineapple/modules/DNSspoof/scripts/autostart_dnsspoof.sh >> /etc/rc.local");
exec("echo exit 0 >> /etc/rc.local");
} else {
exec("sed -i '/DNSspoof\/scripts\/autostart_dnsspoof.sh/d' /etc/rc.local");
}
}
2017-11-16 05:42:22 +00:00
private function toggleDNSspoof()
{
2018-08-27 00:32:32 +00:00
if (!$this->checkRunning("dnsspoof")) {
$this->uciSet("dnsspoof.run.interface", $this->request->interface);
2017-11-16 05:42:22 +00:00
2018-08-27 00:32:32 +00:00
$this->execBackground("/pineapple/modules/DNSspoof/scripts/dnsspoof.sh start");
} else {
$this->uciSet("dnsspoof.run.interface", '');
2017-11-16 05:42:22 +00:00
2018-08-27 00:32:32 +00:00
$this->execBackground("/pineapple/modules/DNSspoof/scripts/dnsspoof.sh stop");
}
}
2017-11-16 05:42:22 +00:00
2018-08-27 00:32:32 +00:00
private function getInterfaces()
{
exec("cat /proc/net/dev | tail -n +3 | cut -f1 -d: | sed 's/ //g'", $interfaceArray);
2017-11-16 05:42:22 +00:00
2018-08-27 00:32:32 +00:00
$this->response = array("interfaces" => $interfaceArray, "selected" => $this->uciGet("dnsspoof.run.interface"));
}
2017-11-16 05:42:22 +00:00
private function refreshStatus()
{
2018-08-27 00:32:32 +00:00
if (!file_exists('/tmp/DNSspoof.progress')) {
if (!$this->checkDependency("dnsspoof")) {
$installed = false;
$install = "Not installed";
$installLabel = "danger";
$processing = false;
$status = "Start";
$statusLabel = "success";
$bootLabelON = "default";
$bootLabelOFF = "danger";
} else {
$installed = true;
$install = "Installed";
$installLabel = "success";
$processing = false;
if ($this->checkRunning("dnsspoof")) {
$status = "Stop";
$statusLabel = "danger";
} else {
$status = "Start";
$statusLabel = "success";
}
if (exec("cat /etc/rc.local | grep DNSspoof/scripts/autostart_dnsspoof.sh") == "") {
$bootLabelON = "default";
$bootLabelOFF = "danger";
} else {
$bootLabelON = "success";
$bootLabelOFF = "default";
}
}
} else {
$installed = false;
$install = "Installing...";
$installLabel = "warning";
$processing = true;
$status = "Not running";
$statusLabel = "danger";
$bootLabelON = "default";
$bootLabelOFF = "danger";
2017-11-16 05:42:22 +00:00
}
2018-08-27 00:32:32 +00:00
$device = $this->getDevice();
$sdAvailable = $this->isSDAvailable();
$this->response = array("device" => $device, "sdAvailable" => $sdAvailable, "status" => $status, "statusLabel" => $statusLabel, "installed" => $installed, "install" => $install, "installLabel" => $installLabel, "bootLabelON" => $bootLabelON, "bootLabelOFF" => $bootLabelOFF, "processing" => $processing);
2017-11-16 05:42:22 +00:00
}
2018-08-27 00:32:32 +00:00
private function refreshOutput()
{
if ($this->checkDependency("dnsspoof")) {
if ($this->checkRunning("dnsspoof")) {
$path = "/pineapple/modules/DNSspoof/log";
$latest_ctime = 0;
$latest_filename = '';
$d = dir($path);
while (false !== ($entry = $d->read())) {
$filepath = "{$path}/{$entry}";
if (is_file($filepath) && filectime($filepath) > $latest_ctime) {
$latest_ctime = filectime($filepath);
$latest_filename = $entry;
}
}
if ($latest_filename != "") {
$log_date = gmdate("F d Y H:i:s", filemtime("/pineapple/modules/DNSspoof/log/".$latest_filename));
if ($this->request->filter != "") {
$filter = $this->request->filter;
$cmd = "cat /pineapple/modules/DNSspoof/log/".$latest_filename." | ".$filter;
} else {
$cmd = "cat /pineapple/modules/DNSspoof/log/".$latest_filename;
}
exec($cmd, $output);
if (!empty($output)) {
$this->response = implode("\n", array_reverse($output));
} else {
$this->response = "Empty log...";
}
}
} else {
$this->response = "DNSspoof is not running...";
}
} else {
$this->response = "DNSspoof is not installed...";
}
}
private function refreshHistory()
{
$this->streamFunction = function () {
$log_list = array_reverse(glob("/pineapple/modules/DNSspoof/log/*"));
echo '[';
for ($i=0;$i<count($log_list);$i++) {
$info = explode("_", basename($log_list[$i]));
$entryDate = gmdate('Y-m-d H-i-s', $info[1]);
$entryName = basename($log_list[$i]);
echo json_encode(array($entryDate, $entryName));
if ($i!=count($log_list)-1) {
echo ',';
}
}
echo ']';
};
}
private function viewHistory()
{
$log_date = gmdate("F d Y H:i:s", filemtime("/pineapple/modules/DNSspoof/log/".$this->request->file));
exec("cat /pineapple/modules/DNSspoof/log/".$this->request->file, $output);
if (!empty($output)) {
$this->response = array("output" => implode("\n", $output), "date" => $log_date);
} else {
$this->response = array("output" => "Empty log...", "date" => $log_date);
}
}
private function deleteHistory()
{
exec("rm -rf /pineapple/modules/DNSspoof/log/".$this->request->file);
}
private function downloadHistory()
{
$this->response = array("download" => $this->downloadFile("/pineapple/modules/DNSspoof/log/".$this->request->file));
}
2017-11-16 05:42:22 +00:00
2018-08-27 00:32:32 +00:00
private function saveAutostartSettings()
{
$settings = $this->request->settings;
$this->uciSet("dnsspoof.autostart.interface", $settings->interface);
}
private function saveLandingPageData()
{
$filename = '/www/index.php';
file_put_contents($filename, $this->request->configurationData);
}
private function getLandingPageData()
{
$configurationData = file_get_contents('/www/index.php');
$this->response = array("configurationData" => $configurationData);
}
private function saveHostsData()
{
$filename = '/etc/pineapple/spoofhost';
file_put_contents($filename, $this->request->configurationData);
}
private function getHostsData()
{
$configurationData = file_get_contents('/etc/pineapple/spoofhost');
$this->response = array("configurationData" => $configurationData);
}
2017-11-16 05:42:22 +00:00
}