Skip to content

Commit

Permalink
Merge pull request #664 from prey/factory-reset-with-options
Browse files Browse the repository at this point in the history
added a file with options to the taskscheduler
  • Loading branch information
SoraKenji authored Sep 7, 2022
2 parents 3e1a6ba + 9e8106d commit 6251c0d
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 18 deletions.
63 changes: 63 additions & 0 deletions lib/agent/actions/factoryreset/factory-reset-option.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
var getExecutionDate = () => {
var now = new Date();
now.setMinutes(now.getMinutes() + 2); //add two minuts
now = new Date(now);
return convertUTCDateToLocalDate(now).toISOString().slice(0, 19);

}

var getCreationDate = () => {
var now = new Date();
return convertUTCDateToLocalDate(now).toISOString().slice(0, 19);

}

function convertUTCDateToLocalDate(date) {
var newDate = new Date(date.getTime() - date.getTimezoneOffset()*60*1000);
return newDate;
}

exports.format_file = `<?xml version="1.0" encoding="UTF-16" ?>
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
<RegistrationInfo>
<Date>` + getCreationDate() + `</Date>
<URI>\\Prey\\Factory Reset</URI>
</RegistrationInfo>
<Triggers>
<TimeTrigger>
<StartBoundary>` + getExecutionDate() + `</StartBoundary>
<Enabled>true</Enabled>
</TimeTrigger>
</Triggers>
<Principals>
<Principal id="Author">
<UserId>S-1-5-18</UserId>
<RunLevel>LeastPrivilege</RunLevel>
</Principal>
</Principals>
<Settings>
<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
<DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
<StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
<AllowHardTerminate>true</AllowHardTerminate>
<StartWhenAvailable>false</StartWhenAvailable>
<RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
<IdleSettings>
<StopOnIdleEnd>true</StopOnIdleEnd>
<RestartOnIdle>false</RestartOnIdle>
</IdleSettings>
<AllowStartOnDemand>true</AllowStartOnDemand>
<Enabled>true</Enabled>
<Hidden>false</Hidden>
<RunOnlyIfIdle>false</RunOnlyIfIdle>
<WakeToRun>false</WakeToRun>
<ExecutionTimeLimit>PT72H</ExecutionTimeLimit>
<Priority>7</Priority>
</Settings>
<Actions Context="Author">
<Exec>
<Command>%SystemRoot%\\syswow64\\WindowsPowerShell\\v1.0\\powershell.exe</Command>
<Arguments>-NoProfile -ExecutionPolicy Bypass -File C:\\Windows\\Prey\\current\\lib\\agent\\actions\\factoryreset\\bin\\factory-reset.ps1</Arguments>
</Exec>
</Actions>
</Task>`
46 changes: 28 additions & 18 deletions lib/agent/actions/factoryreset/index.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
var os = require('os'),
path = require('path'),
join = path.join,
fs = require("fs"),
Emitter = require('events').EventEmitter,
common = require('../../common'),
logger = common.logger.prefix('factoryreset'),
token = require('./../../token'),
system = require('./../../../system'),
errors = require('./../../errors').status;
errors = require('./../../errors').status,
factory_reset_options = require('./factory-reset-option');

var emitter,
action,
node_bin = join(system.paths.current, 'bin', 'node'),
file_factory_reset = join(system.paths.current, 'lib', 'agent','actions','factoryreset','bin','factory-reset.ps1');
file_factory_reset = join(system.paths.current, 'lib', 'agent','actions','factoryreset','bin','factory-reset.ps1'),
directory_factory_reset = join(system.paths.current, 'lib', 'agent','actions','factoryreset','bin'),
file_factory_reset_xml = join(directory_factory_reset,'FactoryReset.xml');


var time_execution = () => {
var now = new Date();
now.setMinutes(now.getMinutes() + 2); //add two minuts
now = new Date(now);
datetext = now.toTimeString();
var datetext = now.toTimeString();
var time = datetext.split(' ')[0];
return time;

Expand Down Expand Up @@ -79,27 +83,33 @@ exports.start = function(id, opts, cb) {
key: "device-key",
token: "token",
logged: false,
dirs : [file_factory_reset, time_execution(), process.arch]
dirs : [file_factory_reset, time_execution(), process.arch, file_factory_reset_xml]
}

action = 'factory-reset';

emitter = new Emitter;
cb(null, emitter);

system.spawn_as_admin_user(node_bin, data, function(err, child) {
if(err){
logger.info('Error executing Factory Reset :' + JSON.stringify(err));
}
if (typeof child == 'function') { // only for windows
child(action, data, finished);
} else {
let error = new Error('Admin service not available');
error.code = 4;
error.name = errors.find( x => x.status_code == error.code).message;
return cb(error);
cb(null, emitter);

fs.writeFile(file_factory_reset_xml, factory_reset_options.format_file, (err) => {
if (err) {
err.code = 6;
return cb(err);
}
})
system.spawn_as_admin_user(node_bin, data, function(err, child) {
if(err){
logger.info('Error executing Factory Reset :' + JSON.stringify(err));
}
if (typeof child == 'function') { // only for windows
child(action, data, finished);
} else {
let error = new Error('Admin service not available');
error.code = 4;
error.name = errors.find( x => x.status_code == error.code).message;
return cb(error);
}
})
});
})
}

Expand Down
5 changes: 5 additions & 0 deletions lib/agent/errors/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,9 @@ exports.status = [
message : "token_error",
description : ""
},
{
status_code : 6,
message : "create_file_error",
description : "Error to create file XML"
},
]

0 comments on commit 6251c0d

Please sign in to comment.