The visitors went ahead after a poor David Raya clearance was punished by Joe Mason’s assured finish in the fifth minute.
The hosts were lacklustre in the first half but produced a much-improved second-half display that saw Nathan Delfouneso’s deflected effort hit the bar, only for Hanley to miss from point-blank range.
It looked as though the Bluebirds had weathered the storm, but the Rovers captain popped up in the 88th minute to spare his side’s blushes, before his defensive partner Shane Duffy saw a header cleared off the line in stoppage time.
Despite the pulsating end, Blackburn’s stuttering start continues, while Cardiff remain undefeated.
Rovers handed full debuts to Fode Koita and Hope Akpan, but Jordan Rhodes missed out through injury. David Marshall returned to the Cardiff side after suspension.
The hosts shot themselves in the foot in the fifth minute as Raya tried to play the ball out and his pass was intercepted by Lee Peltier, who fed Mason in the area and the forward flicked home from 12 yards.
Koita has proved a bright spark when he has appeared this season and he almost drew Rovers level five minutes later but his header from Craig Conway’s cross was straight at Marshall.
Cardiff had been quiet after the goal but threatened a second on the half-hour mark when Joe Ralls struck a crisp low drive that Raya fumbled before the ball was cleared.
Blackburn had two golden chances to equalise at the end of the half as a Tommy Spurr long throw was headed on by Duffy to Akpan, whose close-range header was clawed away by Marshall. Seconds later he should have been beaten when the ball fell to Duffy 10 yards out, but the defender’s effort was wild and missed the target.
After an uninspired first period, Rovers came out with real intent and were inches from equalising 11 minutes into the half as Delfouneso set himself up for a 20-yard volley which took a deflection before cannoning over off the crossbar.
And they should have been level minutes later when a wonderful Conway free-kick was flicked onto Hanley six yards out with the goal at his mercy but he headed wide.
Cardiff looked to have seen off the danger but Hanley made amends two minutes from the end as he got onto the end of another superb Conway cross to calmly slot home from close range.
Rovers went agonisingly close to a winner in stoppage time but Duffy’s header was brilliantly saved by Marshall and, moments later, the same player saw a header hacked off the line as Cardiff held on.