Last active
February 4, 2018 15:21
-
-
Save paulc/14f3aac984f9c196165a0cc86267f144 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ts = (Get-Date).toString("yyyyMMdd-HHmmss") | |
$logfile = "C:\Users\Administrator\log-metadata-$ts.txt" | |
$server = "http://169.254.1.1:8080" | |
$name = $env:computername | |
$if = Get-WmiObject win32_networkadapterconfiguration | where -property ipenabled | |
$q = $server + "/mac/" + $if.macaddress | |
Function Log-Write { | |
Param ([string]$msg) | |
Add-Content $logfile -value $msg | |
} | |
Log-Write "[+] Requesting instance metatada: $q" | |
try { | |
$config = (Invoke-WebRequest $q).content | ConvertFrom-Json | |
if (Get-Member -InputObject $config -Name name -MemberType Properties) { | |
if ($config.name -eq $name) { | |
Log-Write "[+] Hostname ok: $name" | |
} else { | |
Log-Write "[+] Hostname incorrect: $name / $($config.name)" | |
Log-Write "[+] Resetting hostname" | |
Rename-Computer -ComputerName $name -NewName $config.name | |
Log-Write "[+] Restarting" | |
Restart-Computer -Force | |
} | |
} elseif (Get-Member -InputObject $config -Name error -MemberType Properties) { | |
Log-Write "[-] ERROR: $($config.error)" | |
} else { | |
Log-Write "[-] ERROR: Invalid response" | |
} | |
} catch { | |
Log-Write "[-] ERROR: $($error[0]) [$q]" | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
New-Item -Path "C:\Program Files\VMBoot" -ItemType Directory -Force | |
@" | |
`$ts = (Get-Date).toString("yyyyMMdd-HHmmss") | |
`$logfile = "C:\log-vmboot-`$ts.txt" | |
`$server = "http://169.254.1.1:8080" | |
`$name = `$env:computername | |
`$if = Get-WmiObject win32_networkadapterconfiguration | where -property ipenabled | |
`$q = `$server + "/mac/" + `$if.macaddress | |
Function Log-Write { | |
Param ([string]`$msg) | |
Add-Content `$logfile -value `$msg | |
} | |
Log-Write "[+] Requesting instance metatada: `$q" | |
try { | |
`$config = (Invoke-WebRequest -UseBasicParsing `$q).content | ConvertFrom-Json | |
if (Get-Member -InputObject `$config -Name name -MemberType Properties) { | |
if (`$config.name -eq `$name) { | |
Log-Write "[+] Hostname ok: `$name" | |
} else { | |
Log-Write "[+] Hostname incorrect: `$name / `$(`$config.name)" | |
Log-Write "[+] Resetting hostname" | |
Rename-Computer -ComputerName `$name -NewName `$config.name | |
Log-Write "[+] Restarting" | |
Restart-Computer -Force | |
} | |
} elseif (Get-Member -InputObject `$config -Name error -MemberType Properties) { | |
Log-Write "[-] ERROR: `$(`$config.error)" | |
} else { | |
Log-Write "[-] ERROR: Invalid response" | |
} | |
} catch { | |
Log-Write "[-] ERROR: `$(`$error[0]) [`$q]" | |
} | |
"@ | Out-File "C:\Program Files\VMBoot\boot.ps1" | |
$name = "VMBoot" | |
$action = New-ScheduledTaskAction -Execute powershell.exe -Argument '-executionpolicy bypass -f "C:\Program Files\VMBoot\boot.ps1"' | |
$trigger = New-ScheduledTaskTrigger -AtStartup | |
$principal = New-ScheduledTaskPrincipal -UserId SYSTEM -LogonType ServiceAccount -RunLevel Highest | |
$settings = New-ScheduledTaskSettingsSet -RunOnlyIfNetworkAvailable | |
Register-ScheduledTask -TaskName $name -Action $action -Trigger $trigger -Settings $settings -Principal $principal | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from bottle import Bottle | |
from tinydb import TinyDB,Query,JSONStorage | |
class ReadOnlyJSONStorage(JSONStorage): | |
def __init__(self, path, **kwargs): | |
super(JSONStorage, self).__init__() | |
self.kwargs = kwargs | |
self._handle = open(path, 'r') | |
def write(self, data): | |
raise NotImplementedError('TinyDB Instance Read Only') | |
db = TinyDB('/vm/.metadata/vm.json',storage=ReadOnlyJSONStorage) | |
vm = Query() | |
app = Bottle() | |
@app.route('/') | |
def index(): | |
return "Index" | |
@app.route('/mac/<address>') | |
def name(address): | |
meta = db.get(vm.config.network0_mac == address.lower()) | |
if meta: | |
return meta | |
else: | |
return { 'error': 'MAC address not found' } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
set -e | |
tmux new-session -s vm-metadata -d \ | |
'python /root/vm/vmconf.py | |
while :; do | |
echo /vm | entr -pd python /root/vm/vmconf.py | |
done' | |
tmux split-window -t vm-metadata -v \ | |
'su -c daemon -m nobody -c "python -mbottle -b 169.254.1.1:8080 metadata:app"' | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import glob,os.path,re,shlex,sys,time | |
from tinydb import TinyDB,Query | |
os.umask(int('022',8)) | |
db = TinyDB('/vm/.metadata/vm.json',indent=2) | |
vm = Query() | |
print(time.asctime() + " [+] Updating /vm/.metadata/vm.json") | |
for config_file in glob.glob('/vm/*/*.conf'): | |
config = {} | |
name = os.path.splitext(os.path.basename(config_file))[0] | |
with open(config_file) as f: | |
for l in f: | |
if re.match('^[a-z]\w+=',l): | |
option,value = l.split('=',1) | |
value = shlex.split(value)[0] | |
config[option] = value | |
db.upsert({'name':name,'config':config},vm.name == name) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment