<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:

def impersonate_sid(sid, session_id=None, privs=None):
    '''
    Find an existing process token for the given sid and impersonate the token.
    '''
    for tok in enumerate_tokens(sid, session_id, privs):
        tok = dup_token(tok)
        elevate_token(tok)
        if win32security.ImpersonateLoggedOnUser(tok) == 0:
            raise WindowsError("Impersonation failure")  # pylint: disable=undefined-variable
        return tok
    raise WindowsError("Impersonation failure")