The F.B.I. said on Monday that it was investigating the apparent hacking of the Trump campaign and what a senior law enforcement official also said was an effort to gain access to the accounts of top Democrats in a cyberattack possibly originating from Iran.

In a brief statement, an F.B.I. spokeswoman confirmed that the bureau was investigating “a campaign cyberintrusion,” days after former President Donald J. Trump said Iran had targeted his campaign. The bureau did not specifically name Iran or Mr. Trump. Nor did the bureau address the extent of the breach or the possibility that it encompassed other campaigns or political figures.

But investigators are also looking into an attempt to infiltrate accounts associated with the Democrats’ presidential campaign, according to the law enforcement official with knowledge of the situation. The timing of the attempt was unclear, though the official added that there was no indication that the effort had succeeded. Vice President Kamala Harris’s team, which carefully monitors cyberthreats, is not aware of any breach to its systems, according to a campaign official who spoke on the condition of anonymity to discuss security arrangements.

On Friday, Microsoft said a hacking group affiliated with Iran’s Islamic Revolutionary Guard Corps had infiltrated the account of a former top aide to an unnamed presidential campaign. Mr. Trump’s longtime adviser Roger J. Stone said Microsoft contacted him a few months ago, saying that his Hotmail email account had been compromised and that it believed the culprit to be Iran, he said in a phone interview on Monday evening.

A few weeks later, he got a call from the F.B.I. that his Gmail account had also been hacked — and the bureau believed that his account had been used by malign actors to successfully gain access to the internal communications of other people in Mr. Trump’s political operation, Mr. Stone added.

The F.B.I. has repeatedly warned about foreign countries meddling in the upcoming election, including using artificial intelligence to spread misinformation. It has singled out Iran, China and Russia in particular. Russia has a long history of trying to sow chaos in American elections, targeting the Democratic National Committee in 2016.