A theory about the origins of the story of Hiram Abiff is presented by Christopher Knight and Robert Lomas in their book The Hiram Key. It's a very strange story, but there seems to be incontrovertible material evidence in the Cairo Museum that attests to some of its assertions.
Around 1574 BC, at the height of the Second Intermediate Period when Egypt was dominated by the Hyksos king Apophis, a native-born Egyptian king named Sequenre Tao ruled a token Egyptian monarchy centered in Thebes. Joseph son of Jacob had been sold as a slave in Egypt some years prior, and eventually became Grand Vizier of Egypt, under Apophis, as the Bible says. The Hyksos were a race of asiatic people, just like Joseph, and would have more readily accepted him than native Egyptians, as he was of their race.
The theory goes that the true Egyptian kings like Sequenre Tao and his predecessors possessed knowledge of a secret ritual that was supposed to create a king by allowing the king-to-be to commune with the gods and gain from them his right to rule. Apophis and the previous Hyksos kings never had knowledge of this king-making rite, and as Apophis could feel his authority slipping, he wanted it desperately. So he enlisted his new Grand Vizier to come up with a plan that would allow him to obtain these secrets.
At the time, there was a great famine in Palestine, and Jacob's family, i.e. Joseph's brothers who had sold him into slavery many years ago, came into Egypt, as Joseph had the foresight to stockpile grain in anticipation of the famine. It would appear that one of the things Joseph had his errant brothers do, apart from the silver goblet incident involving Benjamin recorded in the Bible, was to get two of his brothers, probably Simeon and Levi, as we shall see later, to make contact with an ambitious priest (whom we shall call Jubelo) at the temple of Amen-Ra in Thebes who was willing to aid Apophis in obtaining the royal secrets from its custodians, probably only the two highest ranking priests and Sequenre Tao himself.
The trio probably wound up murdering the two priests in the end, after they refused to divulge the secrets, and the last desperate chance for success they had at that point was to attack the king himself. They took the King just as he emerged from the temple of Amen-Ra where they told him to give up the secrets under pain of death. Of course, King Sequenre refused and blows were struck, and so he lay dead on the ground. They failed spectacularly... Not only were the royal secrets lost forever, but the murder of the king would incite his sons Ahmose and Kamose (both literally "the Widow's sons") to wage a war that would eventually drive the Hyksos out of Egypt for good and inaugurate the New Kingdom. It was a harsh blow for Ahmose and Kamose as well; neither of them could become a true king now that the secrets were forever lost. No Egyptian ruler had ever dared call himself king ever after, instead using the title Pharoah, which means "Great House", implying that the Pharoah's council of ministers was what gave him the authority to rule.
Simeon and Levi somehow eventually escaped the wrath of the Thebans and made their way back to the Hyksos capital of Avaris. Of them, Jacob had this to say in Genesis 49:6:
O my soul, come not thou into their secret; unto their assembly, mine honour, be not thou united: for in their anger they slew a man, and in their self will they digged down a wall.
This strange Biblical verse has no real convincing explanation to date, except when viewed in the context of the above narration.
But what became of Jubelo? There is actual, physical evidence that suggests that he was eventually captured by the Thebans, and taken before Sequenre Tao's son Kamose, where he was tortured by bathing him in rancid milk and leaving him under the harsh Egyptian sun for a few days, making him smell evil, and easily recognizable to the forces of darkness which he served. He was then castrated and mummified alive. This seems to be borne out because Sequenre Tao's mummy was, surprisingly, found in a cache of royal mummies found by the Egyptologist Emil Brugsch in 1881 at Deir el Bahri, exhibiting very strange wounds that attest that he had suffered a violent death, probably from a heavy blow to his forehead that cracked his skull and broke his neck; this being the only royal mummy ever found to show marks of violence. Alongside Sequenre Tao's mummy was an unidentified mummy whose genitals were missing, and which, apparently, had not undergone a proper mummification as the internal organs and other parts normally removed in the process were still present. It also was covered with a cheese-like residue of rancid milk. The mummy also did not have the serene features and posture of most proper mummies, but on the contrary seems to be hunched backwards with the mouth open as if emitting a silent scream; all the signs that the person was actually mummified alive. This odd corpse had been dated to the end of the Second Intermediate Period, just as with the body of Sequenre Tao, but had not attracted much interest among Egyptologists, who tend to concentrate on the mummies of the famous. But to Knight and Lomas, this corpse would probably be that of Jubelo, one of the conspirators who murdered Sequenre Tao and brought about the loss of the Egyptian royal secrets. If that is so, the curse on Jubelo seems to have persisted to this very day: he is still doomed to be linked to the man he murdered, for in the Cairo Museum, Sequenre Tao is Catalogue No. 61051 while Jubelo is Catalogue No. 61023...
Eventually, this tradition about the murder of the king filtered down in garbled form into the secret traditions of the Jews, who had originated from Egypt proper and eventually assimilated much Egyptian cultural tradition. The secret tradition had been passed down over thousands of years to the Essenes in Qumran, who had left some of their most important and sacred writings in secret passages beneath the Temple of Jerusalem, shortly before the sack of Jerusalem by the Romans in 70 AD. In the 12th century, the Knights Templar dug up these scrolls and the substantial quantities of treasure in those secret passages, and began to practice the rites mentioned in the ancient scrolls, which they believed to be the purest form of Christianity before it was contaminated by Roman mystery religions. Among these was the tale of Hiram Abiff, which would be a garbled echo of the story of Sequenre Tao. After the Templars were suppressed in the early 14th century, their traditions spread in secret in proto-Masonic societies that are the forebears of modern Freemasonry.