When bulldozers came to demolish Yophinalis Nyakundi's home five years ago, there was nothing he could do to stop them. The assault on his part of Nairobi's vast Mukuru slum, known to residents as "Gateway", started at around midnight and the wrecking machines were backed by armed police. The hundreds of families living in the tin shacks were given no time to retrieve anything. Furious residents were waved away with a court order obtained a fortnight before but never shown to the people whose lives it would ruin.

"There was no warning. The bulldozer even destroyed the house with all our things inside. We lost everything," says Mr Nyakundi, 38, who has grown up moving between the "villages" of Mukuru – a complex of shantytowns stretched across the Kenyan capital's industrial area. After the eviction in 2007, he shared a corrugated iron hovel with another family while he laboured in nearby factories or sold fruit and vegetables to put together enough money to build a new shack.

Mr Nyakundi's story is not exceptional in a city where 2.65 million of its four million inhabitants live in slums. He is part of what is known as the "invisible majority" of Nairobians who face long-term consequences of land-grabbing and murky title deeds, prey to slum lords who have made vast profits from building shantytowns on contested land and using the notoriously corrupt police and courts system to protect their investments. What is unusual is that he has turned to the same judicial system to put an end to the epidemic of forced evictions.

Along with 40 of his neighbours, he brought a petition this month naming some of Kenya's most powerful individuals, companies and banks, demanding rights to the land they live on and an end to forced evictions. Among the respondents, who must now answer the petition, are the former President Daniel arap Moi – who oversaw land grants in the area – and Cyrus Jirongo, a presidential contender who is among the landowners. The petitioners, who are being supported by an umbrella group for slum-dwellers, Muungano wa Wanavijiji, have succeeded in obtaining an injunction barring any further evictions in an area of Mukuru that is home to more than 100,000 people. The implications of the case are enormous in a city where 67 per cent of the population lives on less than 2 per cent of the land. The petition is being seen as a test for Kenya's much-vaunted new constitution that passed a referendum last year and is supposed to guarantee peoples' rights to adequate housing and secure tenure.

In many senses the history of Mukuru is the history of Nairobi itself. Before the country gained independence, it was a vast estate belonging to Jack Reuben, a British Army veteran whose service during the Second World War saw him awarded a huge tract of land. He established Villa Franca, which was divided between a sisal plantation and a depot for the Reuben haulage empire. Both businesses needed workers, so a labour camp was established.

Soon after independence from Britain in 1963, Mr Reuben left Kenya and his lands passed to the new state. Left without homes or jobs, Villa Franca's workforce squatted on the land, calling it Mukuru kwa Reuben, or "Reuben's place", in Swahili.

In the early 1980s, Kenya's land ministry came with promises and beacons which it used to measure the squatter camp and the surrounding area. Officials told residents that the beacons were the first step towards helping them to build permanent homes. Four years later, authorities began demolishing the shanties. There were to be no permanent homes.

In the years that followed, the petition alleges, the land was parcelled out to friends and allies of then-president Mr Moi. Under Kenyan, law the land was to be developed within two years or handed back to the state. In reality, it was either left vacant by its speculating owners or used as collateral for hefty bank loans.

When Kenya's Co-operative Bank tried to cash in the supposed security by selling one of the plots in Mukuru kwa Reuben in August this year, it found its headquarters and the auction house besieged by furious residents. They were determined not only to save their homes but to stop the destruction of a community school built on the land. The show of strength and the bank's eventual abandonment of efforts to sell has invigorated the slum-dwellers' movement.

Jane Weru, the director of the Union of Slum-Dwellers, says the land crisis is a "time-bomb" that could destroy Nairobi in the near future. "This is a national crisis that can cause a civil war," she warns. The looming court battle is so important, she says, because "in this case you have a direct link between high level corruption and its impact on the poorest people in the city".

When the results of the East African nation's last census arrived three years ago, it was found that the population had doubled in less than two decades to 40 million. It is expected to double again in the next 10 years. Nairobi is growing at an estimated 7 per cent each year and yet the vast majority of its residents live like Mr Nyakindi and the other petitioners.

They struggle to survive in a metropolis of fearsome statistics. Two-thirds of residents live in shacks measuring 10ft by 10ft. Some 92 per cent of them are under threat of eviction. While there are two households per acre in Nairobi's wealthy suburbs near the United Nations complex in Runda, its slum-dwellers live 318 households to an acre. Three-quarters of Nairobians are under 35 and, despite average economic growth of 5 per cent per year for most of the past decade, unemployment is estimated at 40 per cent.

Since 2000, the government has officially embarked on an $8bn slum upgrade plan. In reality, little of that money has been raised or spent and the persistent problem of land and tenure has hobbled any efforts to improve living conditions. Diana Mitlin, of the UK-based International Institute for Environment and Development, says that insecure land tenure is one of the main reasons for the failure. "People are living as tenants with no legal protection," she adds. "Consequently, there are very high levels of insecurity, which translates into an inability to improve their living conditions."

In Senegal's capital Dakar, an equivalent West African hub, eight out of 10 slum-dwellers have access to electricity, compared to one in five in Nairobi. Some 74 per cent of the same Dakarois own their shanties, while only 12 per cent do in the Kenyan capital. Secure tenure gives residents the incentive to invest in where they live.