A major terrorist attack was narrowly averted last night in Karlsruhe, West Germany, after police were alerted that 42 home-made rockets were aimed at the federal prosecutor's offices from an apartment house less than 50 yards away.

Just over four months ago, West Germany's former chief prosecutor, Siegried Buback, was machine-gunned to death in his car in Karlsruhe by two terrorists on a motorcycle.

The would-be barrage on the building housing the new prosector, Kurt Rebmann, was thwarted when an elderly couple who had been tied up and left behind in the apartment by the terrorists managed to get free and alert police.

The attempt has added to growing public tensions here over a new outbreak of terrorism and to the pressure on West German police, who still have a nation-wide dragnet under way for the terrorists who murdered Dresdner Bank President Juergen Ponto near Frankfurt earlier this month.

Since May 1972 15 attacks have been stages against West German judical authorities, but Rebmann told reporters today that this latest attempt would have raised to new heights the level of firepower used and was "especially treacherous" because it could have caused indiscriminate damage throughout the building.

Karlsruhe, the seat of federal high courts and headquarters of several law-enforcement agencies, has been the scene of a number of small bomb attacks.

This latest attempt comes in the third week of a hunger strike by more than 30 jailed urban guerrillas protesting conditions of their confinement.

Among the strikers are the three surviving members of the notorious Baader-Meinhof gang, which spread terror through several west German cities in the early 1970s.

The group associated with the planned rocket attack and the Buback killing is the "Red Army Faction" an off-shoot of the Baader-Meinhof group.

Police, fearing that all these episodes are linked, had been geared up for a new outburst of terrorism, especially in the Karlsruhe area, since the hunger strike began.

Police are seeking a young man and a young woman terrorist who entered the apartment of the elderly couple, tied them up and then took several hours to put the rocket cluster together.

The rockets were basically lengths of water piping, arrayed in a wide firing arc, similar to what authorities call "a Stalin organ." The missiles, fitted with contact fuses, were set to be fired sequentially by an electric clock.

Police said the material was homemade and did not come from military supplies. The rocket system was described as a sophisticated job, although there were some reports that the firing mechanism was faulty.

The terrorists also failed to do a proper job of tying up the apartment's occupants, a 69-year-old painter and his 74-year-old wife. The couple reportedly were able to make their way to a cupboard where there was a knife.