The NHS became aware of a flaw in its IT systems more than two weeks before it was used to launch a cyber attack on a dozen Scottish health boards.

The incident on May 12 knocked out PCs and phones, forcing staff to rely on pen and paper.

The hacking group believed to be responsible has been linked to the 2014 attack on Sony Pictures and the theft of £64m from the Bangladesh Central Bank.

They exploited a flaw in outdated Windows operating systems to install malicious software on NHS computers.

The health service's cyber security wing first became aware of the danger on April 24 after receiving intelligence about a "specific threat".

It issued an update intended to fix the problem on April 25 and then sent a bulletin to more than 10,000 NHS IT staff on April 28 alerting them to the issue.

It is unclear how many took action but 12 NHS boards in Scotland were affected by the attack and 47 across the UK. No patient data is believed to have been accessed.

In a recent statement to NHS Digital's board, interim chief executive Rob Shaw said: "We are still working with the National Cyber Security Centre to establish the cause of the worldwide cyber attack.

"Since the attack happened we have been talking a number of further steps.

"We continue to work very closely with the National Cyber Security Centre and partner bodies to support NHS organisations.

"Our focus has been and remains to resolve any issues as quickly as possible and to learn any lessons in further strengthening the response of the system and the role we can play to forewarn, forearm and respond."

The attack involved the use of ransomware - software which invades computer systems and encrypts files so they cannot be used by their owners.

The hackers reportedly demanded Bitcoin payments of £230 to unlock individual PCs. The attack is believed to have earned them about £115,000, although no NHS boards paid the ransom.

More than 200,000 individuals and companies across 150 countries are thought to have been affected.