Page 1 of 1 1
Topic Options
#207887 - 2013-10-08 09:12 PM Add user to local admins group and also connect to SQL database...
Thrasonic Offline
Fresh Scripter

Registered: 2011-08-15
Posts: 26
Loc: GA
Hi all. A while back someone in our IT dept. created a group policy in which the Domain Users group is added to the Local Administrators group on any PC that you log into that belongs to our domain. This was done because there are some apps that require elevated priviliges. We are now looking at ways to remediate this security issue. A stop-gap measure is to disable that policy and have the actual user of a PC be in the local administrators group. At least that way not anyone who logs into a PC automatically has local admin privileges.

I was wondering if it would be possible to use kix to accomplish this task. We have an app that has an inventory of PC's and who uses what PC in a SQL database. I was wondering if a kix script could be used to access the database, compare the computer name of the PC on which the script is being run with the database, and when the computer name is found in the database (along with a username linked to that computer name) add that useranme to the loca admins group.

So I'm thinking the order this needs to be done via a kix script is as follows:

1 - Grab the compture name being logged into.
2 - Connect with a SQL database that has the computer names and associated user names.
3 - Compare that computer name with the data in the SQL database.
4 - If a match is found grab the user name associated with the computer name and add that user name to the local admins group.

Is something like that possible with a kix script? If so can you get me started because I don't know where to begin. I'm a bit of a kix scripting newbie...

Top
#207896 - 2013-10-09 12:54 AM Re: Add user to local admins group and also connect to SQL database... [Re: Thrasonic]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4403
Loc: New Jersey
Now that you've fired the admin that created that GPO... ;\)

Rather than disable the GPO, why not remove the Domain Users group from it and let it reapply, applying the settings without the offending group?

If you really want to script it, here's something I used in pre-GPO days to modify groups from a central admin workstation. You can wrap it in a loop that enumerates the computer names.. It requires PSEXEC.EXE and running using an account that has admin rights on the remote system.
 Code:

$aComputers = {get a list of computer names to put in the array using your favorite method}
$Group  = 'Administrators'     ; name of group to modify
$Remove = 'Domain Users'     ; name of object to remove
For Each $Computer in $aComputers
  $Cmd = '%COMSPEC% /c psexec.exe ' + $Computer + ' Net LocalGroup "' + $Group + '" "' + $Remove + ' /delete ">>groupadd.log')
  If $DEBUG
    $Cmd @CRLF
  Else
    Shell $Cmd
  EndIf
Next
This should get you started. You could also use tcLib and use it to push a scheduled task and immediately execute it.. a little more coding (4 lines to use tcLib) but avoids PSExec. I'd have the user log where they log in, then perform this remotely via an admin task, removing domain admins and adding the specific user.

The same method can be used locally to remove the Domain Admins and add the user.. possible only because the user currently has local admin rights. Two lines calling Net LocalGroup - one to remove and one to add, no remote exec needed.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#207899 - 2013-10-09 03:30 PM Re: Add user to local admins group and also connect to SQL database... [Re: Glenn Barnas]
Thrasonic Offline
Fresh Scripter

Registered: 2011-08-15
Posts: 26
Loc: GA
Hi Glenn. Thanks for the response. I misspoke (or mistyped, actually). We won't be removing the GPO but instead just remove the part that tells the GPO to place the Domain Users group in the Local Admins group of the PC. So we'll leave the GPO in place since it does other things we want to retain.

However, before doing that we need to somehow get the individual user's name, the user of each individual PC, in the Local Admin group. Should I be able to do that using your script above as a starting point? I know I need to get a list of computer names to user names for reference. Can that be done via an ODBC connector in the script or something similar?

Top
#207902 - 2013-10-09 04:37 PM Re: Add user to local admins group and also connect to SQL database... [Re: Thrasonic]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4403
Loc: New Jersey
There are UDFs to connect to SQL that work well - I'd use those. I assume you have some type of table that matches computers with authorized local admins. You can also use a simple INI file, something like
 Code:
[AUTHORIZED]
computer1=joe,mary
Computer2=tom,harry
computer3=jane
Basically, computer name = user list.

Here's some sample logic using the INI file, run during logon (assuming everyone's a local admin):
 Code:
; Get a list of auth admins for this computer
; can be gathered from SQL or an INI file as desired
; The 'Allowed' list should begin and end with a comma, and be comma-delimited
$Allowed = ',' + ReadProfileString('\\server\share\ladmins.ini', 'AUTHORIZED', @WKSTA) + ','

If InStr($Allowed, ',' + @USERID + ',')
  'User is allowed as admin!' @CRLF
  $Cmd = '%COMSPEC% /c '
  $Cmd = $Cmd + ' Net LocalGroup "Administrators" "Domain Users" /delete >>groupadd.log'
  Shell $Cmd
  $Cmd = '%COMSPEC% /c '
  $Cmd = $Cmd + 'Net LocalGroup "Administrators" ' + @USERID + ' /add >>groupadd.log'
  Shell $Cmd
EndIf
You might want to add the user before removing the group...

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#207930 - 2013-10-21 08:29 PM Re: Add user to local admins group and also connect to SQL database... [Re: Glenn Barnas]
Thrasonic Offline
Fresh Scripter

Registered: 2011-08-15
Posts: 26
Loc: GA
Hi Glenn. So your suggestion is to pull the data from the SQL database that maps user's to PC's first and then have the kix script reference that data (in an INI file, for example). Does that sound correct?

Would it be too much for me to ask you to explain to me how the sample logic you wrote works, line by line? I'm looking at it but I'm a scripting newbie so much of it doesn't make sense to me. Sorry.

Top
#207931 - 2013-10-21 10:14 PM Re: Add user to local admins group and also connect to SQL database... [Re: Thrasonic]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4403
Loc: New Jersey
Well, it would be either-or.. Use SQL or use an INI. the INI solution is easier and uses only built-in Kix commands but you'd have to build the INI file from a SQL query.

Bottom line - you have your data in a format of "computer_name" and "list of admin users" format. The list of allowed users is placed into the $Allowed variable, either through the ReadProfileString command as shown in my example or via a SQL query UDF call.

There's one important thing on that line - it puts a comma before and after the list of user names, so "John,Mary,Tommy,Sue" becomes ",John,Mary,Tommy,Sue,".

The If statement uses InStr to determine if @USERID is in the $Allowed variable. Before checking, the @USERID is also wrapped in commas. This forces an exact match. Consider this - if you simply checked for @USERID and "Tom" logged in, it would match "Tommy" because it's a sub-string search. By wrapping @USERID in commas, you get ",Tom,", which doesn't match. When Tommy logs in, you get ",Tommy,", which does.

The InStr doesn't check position, just whether a match was found. Some position greater than zero (found) or zero (not found). The result is treated as a Boolean value. So - if a match is found, we run the commands in the If / EndIf section. There are two Shell commands, one to remove the generic "Domain Users" group and one to add the current user. I haven't tested this specific code, so you may want to add the current user before removing Domain Users, although the rights should not change until next process.

There's an inherent flaw in this in that if more than one person should have local admin rights on a workstation - the first user removes the rights of all other users, preventing the second (authorized, but no longer a local admin) from updating the group. With slightly more complex logic, the first user could enumerate the list of domain accounts that are permitted and repeat the /Add command for all allowed users.

Personally, I'd use PSExec.exe to run the commands needed from a central server. Get a list of workstations and the users allowed to log in with local admin rights.. you'll need this anyway to make the above work. Then, simply call the two commands using PSEXEC (Shell PSExec.exe \\target $Cmd) to update the remote workstations. Again, you'd run the /Delete syntax once and the /Add syntax once per allowed user. If you run the script as a domain admin from a server or admin workstation, you can process quite a number of workstations per hour - do it once and be done.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#207932 - 2013-10-22 04:36 AM Re: Add user to local admins group and also connect to SQL database... [Re: Glenn Barnas]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
what is wrong with something simple as:
 Code:
shell "net localgroup administrators /add @userid"
shell 'net localgroup administrators /delete "mydomain\domain users"'


it does not make sure the user logged in is the user of the record but it will get rid of the everyone has full control permission and 99% of PCs have correct admin.
_________________________
!

download KiXnet

Top
#207933 - 2013-10-22 04:55 AM Re: Add user to local admins group and also connect to SQL database... [Re: Lonkero]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
oh. and I see Glenn already had the same principal in his script with one downside. it would not work.
removing yourself from administrators and then trying to add yourself back. oops. no go.
_________________________
!

download KiXnet

Top
#207934 - 2013-10-22 01:41 PM Re: Add user to local admins group and also connect to SQL database... [Re: Lonkero]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4403
Loc: New Jersey
Yeah - I mentioned that the sequence should be reversed for optimal success.. ;\)

I had pulled the example from an admin script that wasn't run by the local user, which I believe is a better process than trying to run it locally.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
1 registered (Henriques) and 2516 anonymous users online.
Newest Members
fgarcia, Viginette, ManuvdWielNL, Sir_Barrington, batdk82
17889 Registered Users

Generated in 0.039 seconds in which 0.017 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org