Miscellaneous – other content that site is not based upon.

  • Remove unwanted admin rights from computers

    Remove unwanted admin rights from computers

    Intune and SCCM has remediation scripts available. The basic idea of these are that you do a detection script weather you should run a fix or not for the client. Idea is to gather all the local admin accounts and groups and whitelist the wanted groups.

    In my case I run the script with Intune and detect local admin accounts and output the admin accounts if detected.

    <#
    Script written by Tommi Voutilainen 
    
    Set whitelisted account so you allow some admins to stay in your computer. Whitelisted account on the base script are Domain admins group and local admin WindowsLAPS. Note there is no example of Intune admin groups. 
    
    #>
    # Specify the SID prefix for the built-in administrator account
    $AdminSIDPrefix = "S-1-5-21-*-500"  
    $adminAccounts = Get-WmiObject -Class Win32_UserAccount | Where-Object { $_.SID -like "$AdminSIDPrefix*" }
    # Whitelisted users who are allowed to remain in the local administrator group
    $whitelist = @("Domain Admins", "WindowsLAPS")
    $adminAccounts | ForEach-Object {
        if ($_ -notin $whitelist) {
            $whitelist += $_.Name
        }
    }
    
    
    # Specify the SID for the Administrators group
    $remediate = $false
    $AdminGroupSid = "S-1-5-32-544"
    $AdminGroup = New-Object System.Security.Principal.SecurityIdentifier($AdminGroupSid)
    $AdminGroupName = $AdminGroup.Translate([System.Security.Principal.NTAccount]).Value -replace '.+\\'
    
    
    $localAdmins = (([ADSI]"WinNT://./$AdminGroupName").psbase.Invoke('Members') | % {$_.GetType().InvokeMember('AdsPath','GetProperty',$null,$($_),$null)}) -match '^WinNT'| %{$_.Replace("WinNT://","")}
    
    foreach ($adminUser in $localAdmins) {
        $adminUser = $adminUser.Replace('/', '\')
        $whitelistUsername = ($adminUser -split '\\')[-1]
        if ($whitelist -notcontains $whitelistUsername) {
            write-output "Found $adminUser from local admin group. Going to Remediation."
            $Remediate = $true
        }
        else {
            #Log -Message "Whitelisted user: $adminUser was found." -Type "Warning"
        }
    }
    
    if ($remediate) {
     
     exit 1
    }
    else {
     exit 0
    }

    The above is detection script for local admin accounts present with whitelisted group of Domain admins and local user WindowsLAPS.

    <#
    Script written by Tommi Voutilainen 
    
    Set Log file according to SCCM or Something else...
    C:\Windows\CCM\Logs\AdminUsers.log"
    C:\ProgramData\Microsoft\IntuneManagementExtension\Logs\AdminUsers.log
    
    
    #>
    # Specify the SID prefix for the built-in administrator account
    $AdminSIDPrefix = "S-1-5-21-*-500"  
    $adminAccounts = Get-WmiObject -Class Win32_UserAccount | Where-Object { $_.SID -like "$AdminSIDPrefix*" }
    # Whitelisted users who are allowed to remain in the local administrator group
    $whitelist = @("Domain Admins", "WindowsLAPS")
    $adminAccounts | ForEach-Object {
        if ($_ -notin $whitelist) {
            $whitelist += $_.Name
        }
    }
    
    function Log {
        Param (
            [Parameter(Mandatory=$false)]
            $Message,
     
            [Parameter(Mandatory=$false)]
            $ErrorMessage,
     
            [Parameter(Mandatory=$false)]
            $Component,
     
            [Parameter(Mandatory=$false)]
            $Type,
            
            [Parameter(Mandatory=$false)]
            $LogFile
        )
        # Mapping string type values to integer values
        $typeMap = @{
            "Normal" = 1
            "Warning" = 2
            "Error" = 3
        }
        $Time = Get-Date -Format "HH:mm:ss.ffffff"
        $Date = Get-Date -Format "MM-dd-yyyy"
        $LogFile = "C:\ProgramData\Microsoft\IntuneManagementExtension\Logs\AdminUsers.log"   # Remember to set this one
        if ($ErrorMessage -ne $null) {
            $Type = "Error"
        }
        if ($Component -eq $null) {
            $Component = "Temporary Admin Rights"
        }
        if ($Type -eq $null) {
            $Type = "Normal"
        }
        $LogMessage = "<![LOG[$Message $ErrorMessage" + "]LOG]!><time=`"$Time`" date=`"$Date`" component=`"$Component`" context=`"`" type=`"$($typeMap[$Type])`" thread=`"`" file=`"`">"
        $LogMessage | Out-File -Append -Encoding UTF8 -FilePath $LogFile
    }
    
    #$adminGroupName = "Administrators"
    
    Log -Message "----------------------"
    Log -Message "Whitelisted accounts are: $whitelist"
    # Specify the SID for the Administrators group
    $AdminGroupSid = "S-1-5-32-544"
    $AdminGroup = New-Object System.Security.Principal.SecurityIdentifier($AdminGroupSid)
    $AdminGroupName = $AdminGroup.Translate([System.Security.Principal.NTAccount]).Value -replace '.+\\'
    
    $localAdmins = (([ADSI]"WinNT://./$AdminGroupName").psbase.Invoke('Members') | % {$_.GetType().InvokeMember('AdsPath','GetProperty',$null,$($_),$null)}) -match '^WinNT'| %{$_.Replace("WinNT://","")}
    
    foreach ($adminUser in $localAdmins) {
        $adminUser = $adminUser.Replace('/', '\')
        $whitelistUsername = ($adminUser -split '\\')[-1]
        if ($whitelist -notcontains $whitelistUsername) {
            Write-output "Removing $adminUser from local admin group."
            Log -Message "Removing $adminUser from local admin group."
            Remove-LocalGroupMember -Group $AdminGroupName -Member $adminUser -ErrorAction SilentlyContinue
        }
        else {
            Log -Message "Whitelisted user: $adminUser was found." -Type "Warning"
        }
    }
    
    $remainingAdmins = (([ADSI]"WinNT://./$AdminGroupName").psbase.Invoke('Members') | % {$_.GetType().InvokeMember('AdsPath','GetProperty',$null,$($_),$null)}) -match '^WinNT'| %{$_.Replace("WinNT://","")}
    
    foreach ($adminUser in $remainingAdmins) {
        $adminUser = $adminUser.Replace('/', '\')
        $whitelistUsername = ($adminUser -split '\\')[-1]
        if ($whitelist -notcontains $whitelistUsername) {
            Log -Message "$adminUser should not be local administrator anymore, but still is." -Type "Error"
        }
    }
    Log -Message "Script was run" (Get-Date -format "HH:mm d.M.yyyy")
    Log -Message "----------------------"
    exit 0

    This way you gain access to see what local admin accounts you have and are there problems that you didn’t know in your environment.

    Remember – There was no Intune admin rights here and to whitelist Intune admin rights you need to use graph.

  • Setting up Raspberry Pi webserver – How I build this site

    Setting up Raspberry Pi webserver – How I build this site

    Simple yet quite quick way. So I wanted to document it.

    Its no secret that I run Raspberry Pi as my server. So the following was done with

    So I customized the image so that I could set my server up with SSH and wireless connection. After the initial setup I made the following commands:

    nano wordpress-setup.sh

    And for the text editor I made the following script that automates the process:

    
    echo "Updating and upgrading system..."
    sudo apt update && sudo apt upgrade -y
    
    echo "Installing Apache, PHP, and MariaDB..."
    sudo apt install -y apache2 php mariadb-server php-mysql
    
    sudo systemctl restart apache2
    
    echo "Securing MariaDB installation..."
    sudo mysql_secure_installation
    
    echo "Creating WordPress database and user..."
    sudo mysql -u root -e "CREATE DATABASE wordpress;"
    sudo mysql -u root -e "GRANT ALL PRIVILEGES ON wordpress.* TO 'root'@'localhost';"
    sudo mysql -u root -e "FLUSH PRIVILEGES;"
    
    echo "Installing PHP extensions for WordPress..."
    sudo apt install -y php-curl php-dom php-imagick php-mbstring php-zip php-gd php-intl
    
    echo "Downloading WordPress..."
    cd /var/www/html/
    sudo rm -rf *
    sudo wget https://wordpress.org/latest.tar.gz
    sudo tar xzf latest.tar.gz
    sudo mv wordpress/* .
    sudo rm -rf wordpress latest.tar.gz
    
    echo "Setting permissions for WordPress files..."
    sudo chown -R www-data:www-data /var/www/html/
    
    echo "Enabling Apache rewrite module..."
    sudo a2enmod rewrite
    
    echo "Configuring Apache for WordPress..."
    sudo bash -c 'cat <<EOL > /etc/apache2/sites-available/000-default.conf
    <VirtualHost *:80>
        DocumentRoot /var/www/html
        <Directory /var/www/html>
            AllowOverride All
        </Directory>
    </VirtualHost>
    EOL'
    
    sudo systemctl restart apache2
    
    echo "Installing Certbot for HTTPS..."
    sudo apt install -y snapd
    sudo snap install core
    sudo snap install --classic certbot
    sudo ln -s /snap/bin/certbot /usr/bin/certbot
    sudo certbot --apache --non-interactive --agree-tos -m your_email@example.com -d your_domain.com
    sudo certbot renew --dry-run
    sudo systemctl restart apache2
    
    echo "WordPress installation completed! You can now access your site."
    

    After saving this file, it was then made executable

    chmod +x wordpress-setup.sh

    Then I just ran the script:

    sudo ./wordpress-setup.sh

    There are some sections that you have to answer like when setting up MariaDB

    Made a typo with the password so that’s included in the picture, if you missed that.

    This script updated, installed and did everything needed to have my site prepared to be built. Though I didn’t touch that I have DNS control, nor firewall.

    If you are to do a site based around this – please note that I am not an expert at this.

    So if in doubt, use another guide. But this was for my liking the best and the easiest as I wrote it into a automated format so I could do this once again if my server craps out again…