Don't make this harder than it needs to be.
If the 12th man on defense knows he's the 12th man and is trying to get off the field when the ball is snapped, this is a live ball illegal substitution foul. 5 yards, previous spot. Doesn't matter how close he is.
If the 12th man on defense doesn't know he's the 12th man and is in formation and the snap is imminent, shut it down for dead ball illegal substitution, 5 yards.
If we don't kill it and he participates in the play, it turns into 15 yard illegal participation, previous spot.