A lot of writers’ heads start to hurt where these two words are concerned.
Rack, as a noun, has a multitude of meanings, including an “instrument of torture” (The henchmen tied the hero to the rack. ), “a frame” (He placed his bag in the luggage rack. ), or “a state of deep anguish.” As a verb, rack holds a related meaning of to torture.
Wrack, as a noun, means “destruction” (He gazed at the storm-damaged harbor’s wrack and ruin.). As a verb, it means “to wreck,” and so to avoid confusion, you might as well just use wreck.
Given these definitions, you would write “rack your brain” (as you “torture your brain” not “wreck” it).
Finally, the torment is over.
No more anguish: Wrack vs. rack
