* Somalia struggles to rebuild after decades of lawlessness
* Pirates took up to $413 million in ransoms in 2005-2012
* African peacekeepers need support to take on al Shabaab
By Michelle Nichols
UNITED NATIONS, Oct 23 (Reuters) - Pirate attacks off the coast of Somalia are at the lowest level since 2006 because of tougher ship security and more Western naval patrols, while onshore al Shabaab militants have shifted tactics to guerrilla warfare, the United Nations said on Wednesday.
Somalia is struggling to rebuild after two decades of civil war and lawlessness sparked by the overthrow of President Siad Barre in 1991.
In a report to the U.N. Security Council, Secretary-General Ban Ki-moon said there were 17 attacks in the first nine months of 2013, compared to 99 attacks in the same period last year.
"As of 17 October, 2013, two small vessels and 60 seafarers are still held by Somali pirates, most of them ashore, and some of which the whereabouts are unknown," he said, adding that in 2012 pirates collected up to $40 million in ransom payments.
"The hostages held by Somali pirates endure dire conditions in captivity and are sometimes tortured and threatened by pirates in an effort to extract the maximum ransom," Ban said.
According to estimates by the U.N. Office on Drugs and Crime and the World Bank, pirates received up to $413 million in ransoms between April 2005 and December 2012.
A Somali pirate attack that was ended by U.S. Navy special forces and not a ransom payment has been turned into a Hollywood film starring actor Tom Hanks. "Captain Phillips" is based the seizure of a cargo ship by Somali pirates in 2009.
While the surviving pirate from that attack is serving a prison sentence in the United States, Ban noted that in general neither the Somali government nor any local authorities had seriously investigated or prosecuted pirate leaders, financiers, negotiators or facilitators.
In a separate letter to the Security Council, released on Wednesday, Ban said he was extremely concerned about Somalia's security situation and appealed for more international support to avoid gains being derailed by al Qaeda-linked al Shabaab.
A joint U.N. and African Union review of the U.N.-backed African peacekeeping mission in Somalia - known as AMISOM - found that al Shabaab had deliberately shifted tactics since May from conventional to guerrilla warfare, with targets including the government, state bodies and the United Nations.
A suicide bomber killed at least 16 people on Saturday in an attack on a cafe in a Somali town close to the Ethiopian border frequented by local and foreign soldiers fighting the al Qaeda-linked al Shabaab militants.
"In the face of these threats, and in the absence of enablers and force multipliers that would have permitted a sustained offensive against Al-Shabaab, the Somali National Army and AMISOM have now assumed a largely defensive, static posture," Ban said in the letter to the Security Council.
Ban warned that an al Shabaab attack on Nairobi's Westgate shopping center last month in neighboring Kenya, which killed at least 67 people, was "worrisome evidence that al Shabaab is able to plan, rehearse and implement complex attacks threatening peace and stability in Somalia and beyond."
He said there was an urgent need to resume and strengthen the military campaign against al Shabaab and backed the joint review's recommendation that the African force be increased by 2,550 troops to more than 20,000 for up to two years.
"I agree with the findings of the joint mission that it is not realistic for AMISOM to achieve the desired effect of resuming the military campaign without air assets," Ban said, urging all 193 U.N. member states to consider urgently providing helicopters and other assets that had already been approved by the U.N. Security Council, but not provided by anyone. (Reporting by Michelle Nichols; Editing by Vicki Allen)