Meanwhile, the robbers returned to their cave and discovered the body was gone. Realizing their secret was known, they vowed to find the culprit. One robber went to town and, by chance, met old Mustapha, who told of sewing a corpse. The robber offered Mustapha gold to lead him to the house. Blindfolded, Mustapha retraced his steps and stopped at Ali Baba's house. The robber marked the door with chalk.
Morgiana saw the mark and, suspecting mischief, marked several neighboring doors the same way. When the robber captain came to find the house, he was confused by the many marks and ordered a retreat. The first robber was executed for his failure.
A second robber tried, marking the door with red chalk in a hidden spot. Morgiana again saw it and marked the other doors identically, foiling the robbers once more. The second robber was also put to death.
The captain then went himself. He memorized Ali Baba's house without marking it. He then devised a new plan: he bought nineteen mules and thirty-eight oil jars. He hid one robber in each empty jar, filled one jar with oil, and loaded them all onto the mules. Disguised as an oil merchant, he arrived at Ali Baba's house at dusk and asked for lodging. Ali Baba, being hospitable, agreed.
The captain unloaded the jars in the yard and whispered to his men to wait for his signal—a stone thrown from the window.
That night, Morgiana needed oil. Approaching a jar, she heard a whisper from inside: "Is it time?" Keeping her composure, she replied, "Not yet, but presently." She discovered a man in every jar except one, which held oil. Realizing the danger, she boiled oil from the full jar and poured it into each of the others, killing the hidden robbers.
Later, the captain threw the stone signal. Hearing no response, he went to the yard and found all his men dead. Enraged, he fled back to the cave.
The next morning, Morgiana told Ali Baba everything. They buried the robbers in the garden.
Determined for revenge, the captain disguised himself, rented a warehouse in town under the name Cogia Hassan, and posed as a merchant. He befriended Ali Baba's son, whose warehouse was nearby, and was soon invited to dine at Ali Baba's house.
The captain came, hiding a dagger, intending to kill Ali Baba. At dinner, he refused salt—a Persian custom meaning one must not harm a host whose salt one has eaten. Morgiana, who was serving, grew suspicious. Recognizing him as the false oil merchant, she devised a plan.
After the meal, Morgiana performed a dance with a tambourine and a dagger. As part of the dance, she playfully pointed the dagger at each guest. When she finished, she collected tips in her tambourine. As Cogia Hassan reached to give a coin, she plunged her dagger into his heart.
Ali Baba was horrified until Morgiana revealed the guest's true identity and the hidden dagger. Overwhelmed with gratitude for her shrewdness, courage, and loyalty, Ali Baba felt he could never thank her enough.
They buried the captain with his men. With all the robbers dead, the family was finally safe. In time, Ali Baba's son married Morgiana, and they all lived in peace and happiness.