larsmue
Goto Top

Netzwerklaufwerk per Script verbinden

Hallo,

ich hoffe sehr, nicht gleich bei meiner ersten Frage als Anfänger der sich in das Thema Netzwerk einarbeiten möchte (für den privaten gebrauch) hier gesteinigt zu werden. Aber ich habe über die Suche nichts gefunden, zumal ich auch gar nicht so recht weiss nach was für einem Begriff ich suchen soll.....

Nun aber zu meiner Frage:

Ich stelle z.Zt. an meinem Windows Laptop ein Zugriff auf mein NAS (WD MYCloud - schon älter und nicht aus dem Internet erreichbar und schon gar nicht mit irgendwelchen MyCloud Diensten - ist ein interner Datenspeicher), beim Windows Systemstart per bat-Script her die im AutoStart liegt.
Aufgebaut habe ich das Script auf folgende Art:

@echo off
net use * /delete /yes
net use L: \\192.168.0.75\daten_1 /user:XX XXXX /persistent:no
net use M: \\192.168.0.75\daten_2 /user:XX XXXX /persistent:no
exit

Das funktioniert recht gut, aber... da in dem Script das Passwort im Klartext steht, finde ich das nicht wirklich so toll.
Daher meine Frage, was kann ich verändern um es besser, eleganter zu lösen ohne das Passwort im Klartext in dem Script zu hinterlegen?
Ich hoffe wirklich wenn ihr mir helfen könntet
LG
Lars

Content-Key: 4942548943

Url: https://administrator.de/contentid/4942548943

Printed on: June 25, 2024 at 06:06 o'clock

Member: michi1983
Solution michi1983 Jun 16, 2024 at 10:03:29 (UTC)
Goto Top
Hallo,

Kollege @13034433319 wird dir sicherlich gleich was Schönes bereitstellen.

Hier dennoch mal ein Auszug aus ChatGPT:

To enhance the security of your batch script for mapping network drives without storing the password in plaintext, you can use the following approaches:

### Approach 1: Using Windows Credential Manager

1. **Store the Credentials in the Windows Credential Manager:**
   - Open the Credential Manager from the Control Panel or search for "Credential Manager" in the start menu.  
   - Add a Windows Credential for the network location. Provide the network address (e.g., `\\192.168.0.75`), the username, and the password.

2. **Modify Your Script:**
   - Update your script to use the stored credentials. The Credential Manager will automatically use the stored password when you specify the username.

Here's how you can modify your script:  

```batch
@echo off
net use * /delete /yes
net use L: \\192.168.0.75\daten_1 /user:XX /persistent:no
net use M: \\192.168.0.75\daten_2 /user:XX /persistent:no
exit
```

### Approach 2: Using a Secure Encrypted Password File

1. **Create an Encrypted Password File:**
   - Use PowerShell to create an encrypted password file that only your user account can decrypt.

   ```powershell
   $Password = Read-Host -AsSecureString
   $Password | ConvertFrom-SecureString | Out-File "C:\path\to\password.txt"  
   ```

2. **Modify Your Script to Use the Encrypted Password:**
   - Use PowerShell within your batch script to read the encrypted password file and map the network drives.

Here's an example of the updated script:  

```batch
@echo off
net use * /delete /yes

rem Use PowerShell to decrypt the password and store it in a variable
for /f "delims=" %%i in ('powershell -command "Get-Content 'C:\path\to\password.txt' | ConvertTo-SecureString | ConvertFrom-SecureString -AsPlainText"') do set MyPassword=%%i  

rem Map the network drives using the decrypted password
net use L: \\192.168.0.75\daten_1 /user:XX %MyPassword% /persistent:no
net use M: \\192.168.0.75\daten_2 /user:XX %MyPassword% /persistent:no

exit
```

### Approach 3: Using Group Policy Preferences

1. **Create a Group Policy Object:**
   - Open the Group Policy Management Console (gpmc.msc).
   - Create a new Group Policy Object (GPO) or edit an existing one.

2. **Configure Drive Mapping:**
   - Navigate to `User Configuration -> Preferences -> Windows Settings -> Drive Maps`.
   - Create a new drive map entry for each network drive.
   - Set the action to "Create", specify the drive letter, and provide the path to the network location.  
   - Under the "Common" tab, you can configure item-level targeting to apply the drive mapping to specific users or groups.  

By using Group Policy Preferences, you can manage network drive mappings centrally without exposing passwords in scripts.

### Summary

Using Windows Credential Manager or creating an encrypted password file with PowerShell are practical methods to avoid storing plaintext passwords in your batch script. Alternatively, for a more centralized approach, using Group Policy Preferences can help manage drive mappings securely within a domain environment.

Gruß
Member: LarsMue
LarsMue Jun 16, 2024 at 10:07:26 (UTC)
Goto Top
Klasse,

vielen Dank.. an ChatGPT hatte ich noch gar nicht gedacht...
Lösung 2 scheint in meinen Augen eine gute zu sein face-smile
Member: kreuzberger
kreuzberger Jun 16, 2024 at 10:11:16 (UTC)
Goto Top
@LarsMue

wenn der User auf dem NAS den gleichen Namen und Passwort haben sollte es auch ohne weitere Passwortübergabe gehen.

https://de.wikipedia.org/wiki/Single_Sign-on


Kreuzberger
Member: LarsMue
LarsMue Jun 16, 2024 at 10:56:11 (UTC)
Goto Top
Es sind in meinem Fall unterschiedliche Namen und Passwörter.

Ich habe es jetzt mittels der verschlüsselten Passwortdatei lösen können.

Nochmals vielen Dank.. und ich habe in Zukunft sicher noch die eine oder andere Frage um etwas mehr von dem ganzen Thema zu erlernen.
LG
Lars
Member: ThePinky777
ThePinky777 Jun 17, 2024 updated at 06:27:51 (UTC)
Goto Top
wer glaub das das secure ist, irrt.
wenn ein hacker kontrolle von deinem system erlangt muss er das script nur modifizieren und eine Zeile anfügen:

echo %MyPassword%
(einfach in Zeile 47 eingeben)

nachdem das PW in die Variable geladen wurde.

kannst es gleich im Klartext reinschreiben und dir das ganze sparen.

wollte ich nur mal erwähnen falls sich jemand in falscher sicherheit wiegt bei solchen scripts.

einiger nutzen ist eventuell, das normale user - die absolut keinen schimmer von nix haben, dann nicht das PW im klartext sehen können.
Member: LarsMue
LarsMue Jun 17, 2024 at 06:35:05 (UTC)
Goto Top
Gut zu wissen.. Danke

ich hatte es jetzt so gemacht.
Ich habe zuerst eine verschlüsselte Passwort Datei erstellt (passwort.txt)

dann folgendes erstellt:

$encrypted = Get-Content "C:\\Netstart\\Passwort.txt" | ConvertTo-SecureString  
$credential = New-Object System.Management.Automation.PsCredential ("USERX", $encrypted)  
New-PSDrive -name "M" -PSProvider FileSystem -Root "\\192.168.0.75\media" -Persist -Credential $credential  

und dann ein batch-Script im Autostart abgelegt

@echo off
net use * /delete /yes
powershell.exe -ExecutionPolicy Bypass -File "C://Netstart/netstart.ps1"  
exit

das funktioniert, aber in wie weit das auch sicher ist kann ich leide nicht beurteilen.
Für mich ist es aber schon besser als das Passwort im Klartext auf der Festplatte zu hinterlegen