iRule modification - rdp

I currently use the below iRule with our Terminal Services pool. It strips everything after the “@” in the username so that the persistence entry is only the username. I would like to modify it so that usernames that are entered in the form “domain\username” are also entered in the table with just the username. In toehr words I weould like the iRule to strip out everything before the “/”. How would I do this? Can someone help?

when CLIENT_ACCEPTED {

TCP::collect

}

when CLIENT_DATA {

TCP::collect 25

binary scan [TCP::payload] x11a* msrdp

log local0. “Contents after binary scan: $msrdp”

if { [string equal -nocase -length 17 $msrdp “cookie: mstshash=”] } {

set msrdp [string range $msrdp 17 end]

set len [string first “\n” $msrdp]

if { $len == -1 } {

TCP::collect

return

}

if { $msrdp contains “@” } {

if { $len > 5 } {

incr len -1

log local0. “Data Persisting on: [getfield $msrdp “@” 1]”

persist uie [getfield $msrdp “@” 1] 10800

}

} else { persist uie $msrdp 10800}

}

TCP::release

}

UnRuleY and citizen_elah were the ones that wrtoe the iRule for me so if you guys are still around can you help me out? Thanks!

Yep, I’m still lurking…can you post some examples (sanitized) of the contents of msrdp immediately after the binary scan?

here is whats in the ltm log:

Feb 2 13:36:29 tmm tmm[31729]: Rule sso_username : Contents after binary scan: Cookie: mstshash=jmasgalas

Feb 2 13:41:20 tmm tmm[31729]: Rule sso_username : Contents after binary scan: Cookie: mstshash=jmasgalas@Summithealth.local

Feb 2 13:41:20 tmm tmm[31729]: Rule sso_username : Data Persisting on: jmasgalas

Feb 2 14:23:09 tmm tmm[31729]: Rule sso_username : Contents after binary scan: Cookie: mstshash=CHBGHOSP\

Feb 2 14:23:33 tmm tmm[31729]: Rule sso_username : Contents after binary scan: Cookie: mstshash=danaip@Summithealth.local

Feb 2 14:23:33 tmm tmm[31729]: Rule sso_username : Data Persisting on: danaip

See how one of the entries stroes only chbghosp\. I want to strip that part so that we can get the username instead of the domain. Another issue we are having is that it persists usernames stripped of @summithealth.local as seperate entries than usernames entered with just the username. For example, if I log into a 2X thin client I am logged in as jmasgalas@summithealth.local. The iRule srtips it down to jmasgalas and created a persistence table entry. I then rdp to the VIP from my desktop which sends the username as jmasgalas. The F5 is creating two different persistence table entries names jmasgalas. If you need it I can send you the contents of the persistence table.

You should be able to check for the presence of the backslash, and if present, split on it and take everything in the second field as your persistence value. If you just want the username, and not the @…, you could do a secondary split on the @ and keep the first field.

So I could do that in the same iRule? How would I do it. I’m not very good with iRules.

If the string you’re trying to parse can have three forms:

jmasgalas@example.local

EXAMPLE\jmasgalas

jmasgalas

You can use something like this to parse them down to the account name:

if {$account contains "@"}{ 
    set account [getfield $account "@" 1] 
 } elseif {$account contains "\\"}{ 
     Need to escape the backslash with a backslash 
    set account [getfield $account "\\" 1] 
 }

If I’ve missed the issue, let me know.

Thanks,

Aaron

OK. So I took hoolio’s idea and came up with the below iRule. However it strips everything after the \ instead of before. So a username of domain\username becomes domain.

when CLIENT_ACCEPTED {

TCP::collect

}

when CLIENT_DATA {

TCP::collect 25

binary scan [TCP::payload] x11a* msrdp

log local0. “Contents after binary scan: $msrdp”

if { [string equal -nocase -length 17 $msrdp “cookie: mstshash=”] } {

set msrdp [string range $msrdp 17 end]

set len [string first “\n” $msrdp]

if { $len == -1 } {

TCP::collect

return

}

if { $msrdp contains “@” } {

if { $len > 5 } {

incr len -1

log local0. “Data Persisting on: [getfield $msrdp “@” 1]”

persist uie [getfield $msrdp “@” 1] 10800

}

}

if { $msrdp contains “\\” } {

if { $len > 5 } {

incr len -1

log local0. “Data Persisting on: [getfield $msrdp “\\” 1]”

persist uie [getfield $msrdp “\\” 1] 10800

}

} else { persist uie $msrdp 10800}

}

TCP::release

}

Can you log the $msrdp value before you modify it?

if { [string equal -nocase -length 17 $msrdp "cookie: mstshash="] } {

→

if { [string equal -nocase -length 17 $msrdp "cookie: mstshash="] } {  
    log local0. "\$msrdp: $msrdp"

Aaron

It does log it after the scan. Adding the log command just duplicates it. It does correctly recognize the usernames.

Here is the output of the other versions of the username input.

Logging in with username@domain.local:

Feb 6 09:57:59 tmm tmm[31729]: Rule ssotest : Contents after binary scan: Cookie: mstshash=nursetest@summithealth.local

Feb 6 09:57:59 tmm tmm[31729]: Rule ssotest : Data Persisting on: nursetest

Logging in with just the username:

Feb 6 09:59:28 tmm tmm[31729]: Rule ssotest : Contents after binary scan: Cookie: mstshash=nursetest

Thanks for that.

If the original string has two backslashes, then you’d need to use four for getfield. The first and third slashes escape the second and fourth.

[getfield $msrdp “\\\\” 2]

You could try something like this which assumes that the msrdp string won’t have both an @ and backslashes. It also handles one backslash:

if { $msrdp contains "@" } { 
       log local0. "Data Persisting on: [getfield $msrdp "@" 1]" 
       persist uie [getfield $msrdp "@" 1] 10800 
    } elseif { $msrdp contains "\\\\" } { 
       log local0. "Data Persisting on: [getfield $msrdp "\\\\" 2]" 
       persist uie [getfield $msrdp "\\\\" 2] 10800 
    } elseif { $msrdp contains "\\" } { 
       log local0. "Data Persisting on: [getfield $msrdp "\\" 2]" 
       persist uie [getfield $msrdp "\\" 2] 10800 
    } else { 
       persist uie $msrdp 10800 
       log local0. "Data Persisting on: $msrdp" 
    } 
 }

Aaron

So how would that incorporate to my entire iRule. Which parts would I keep and which would I change?

Can you try this?

when CLIENT_ACCEPTED { 
    TCP::collect 
 } 
 when CLIENT_DATA { 
  
    TCP::collect 25 
  
    binary scan [TCP::payload] x11a* msrdp 
    log local0. "Contents after binary scan: $msrdp" 
  
    if { [string equal -nocase -length 17 $msrdp "cookie: mstshash="] } { 
       set msrdp [string range $msrdp 17 end] 
       set len [string first "\n" $msrdp] 
       if { $len == -1 } { 
          TCP::collect 
          return 
       } 
       if { $msrdp contains "@" } {  
          log local0. "Data Persisting on: [getfield $msrdp "@" 1]"  
          persist uie [getfield $msrdp "@" 1] 10800  
       } elseif { $msrdp contains "\\\\" } {  
          log local0. "Data Persisting on: [getfield $msrdp "\\\\" 2]"  
          persist uie [getfield $msrdp "\\\\" 2] 10800  
       } elseif { $msrdp contains "\\" } {  
          log local0. "Data Persisting on: [getfield $msrdp "\\" 2]"  
          persist uie [getfield $msrdp "\\" 2] 10800  
       } else {  
          persist uie $msrdp 10800  
          log local0. "Data Persisting on: $msrdp"  
       }  
    } 
    TCP::release 
 }

I’m not sure I understand why the length was being decremented by 1 if $len is greater than 5. Citizen, can you elaborate on this?

Thanks,

Aaron

It seems that with this rule it persists the value but as seperate entries. Also the stripping everything before \ is not creating a persistence table entry. When I ssh to our F5 here is what the output of b persist show all | grep doctest is:

[root@bigbrother:Active] config b persist show all | grep doctest

Mode: universal Value: doctest

Mode: universal Value: doctest

[root@bigbrother:Active] config

Here is the log file:

Feb 6 13:01:45 tmm tmm[31729]: Rule ssotest2 : Contents after binary scan: Cookie: mstshash=doctest

Feb 6 13:01:45 tmm tmm[31729]: Rule ssotest2 : Data Persisting on: doctest

Feb 6 13:02:31 tmm tmm[31729]: Rule ssotest2 : Contents after binary scan: Cookie: mstshash=doctest@Summithealth.local

Feb 6 13:02:31 tmm tmm[31729]: Rule ssotest2 : Data Persisting on: doctest

Feb 6 13:05:54 tmm tmm[31729]: Rule ssotest2 : Contents after binary scan: Cookie: mstshash=chbghosp\

Feb 6 13:05:54 tmm tmm[31729]: Rule ssotest2 : Data Persisting on:

@Hoolio, if I recall correctly the len variable includes the “=”, so it is decremented to remove the additional character that would otherwise be included in the persist string.

Hey guys,

I got it to work. Here is the iRule as I have it:

when CLIENT_ACCEPTED {

TCP::collect

}

when CLIENT_DATA {

TCP::collect 25

binary scan [TCP::payload] x11a* msrdp

log local0. “Contents after binary scan: $msrdp”

if { [string equal -nocase -length 17 $msrdp “cookie: mstshash=”] } {

set msrdp [string range $msrdp 17 end]

set len [string first “\n” $msrdp]

if { $len == -1 } {

TCP::collect

return

}

if { $msrdp contains “@” } {

log local0. “Setting data to: [getfield $msrdp “@” 1]”

set username [getfield $msrdp “@” 1]

} elseif { $msrdp contains “\\” } {

log local0. “Setting data to: [getfield $msrdp “\\” 3]”

set username [getfield $msrdp “\\” 3]

} else {

set username $msrdp

log local0. “Setting data to: $msrdp”

}

set finalusername [string tolower $username]

set finalusername [string trim $finalusername]

log local0. “User Being Persisted is: |$finalusername|”

persist uie $finalusername 10800

}

TCP::release

}

The only thing is that 9 character limit. I know this is an MS TS limitation but is ther a way around it. Thanks for all your input and help though. You have steered my in the right direction!

Hi all, thought I’d add something to the mix here, mainly for information purposes in case anyone else is in my boat - although if anyone does have a nugget of info then that would be great.

I’m on LTM v9.0.x - thought this iRule would save the day but doesn’t look like it works with this old code. Out of the 3 possible login situations username, username@company.org and domain\username, the only one it worked for was the username@company.org login type. The other 2 produced 2 persistence entries for one login and looking as follows: either username r??? or domain r??? (or some gobbledigook like that). In addition the persistence was not 100%.

One day I’ll be given shiny new v10 appliances…until then.

N