A ninja (忍者?) or shinobi (忍び?) was a covert agent or mercenary in feudal Japan who specialized in unorthodox warfare. The functions of the ninja included espionage, sabotage, infiltration, and assassination, and open combat in certain situations. Their covert methods of waging war contrasted the ninja with the samurai, who observed strict rules about honor and combat. The shinobi proper, a specially trained group of spies and mercenaries, appeared in the Sengoku or "warring states" period, in the 15th century, but antecedents may have existed in the 14th century, and possibly even in the 12th century (Heian or early Kamakura era).
In the unrest of the Sengoku period (15th–17th centuries), mercenaries and spies for hire became active in the Iga Province and the adjacent area around the village of Kōga, and it is from their ninja clans that much of our knowledge of the ninja is drawn. Following the unification of Japan under the Tokugawa shogunate (17th century), the ninja faded into obscurity. A number of shinobi manuals, often centered around Chinese military philosophy, were written in the 17th and 18th centuries, most notably the Bansenshukai (1676).
By the time of the Meiji Restoration (1868), the tradition of the shinobi had become a topic of popular imagination and mystery in Japan. Ninja figured prominently in folklore and legend, and as a result it is often difficult to separate historical fact from myth. Some legendary abilities purported to be in the province of ninja training include invisibility, walking on water, and control over the natural elements. As a consequence, their perception in western popular culture in the 20th century is often based more on such legend and folklore than on the historical spies of the Sengoku period.
Despite many popular folktales, historical accounts of the ninja are scarce. Historian Stephen Turnbull asserts that the ninja were mostly recruited from the lower class, and therefore little literary interest was taken in them. Instead, war epics such as the Tale of Hōgen (Hōgen Monogatari) and the Tale of the Heike (Heike Monogatari) focus mainly on the aristocratic samurai, whose deeds were apparently more appealing to the audience. Historian Kiyoshi Watatani states that the ninja were trained to be particularly secretive about their actions and existence:
- So-called ninjutsu techniques, in short are the skills of shinobi-no-jutsu and shinobijutsu, which have the aims of ensuring that one's opponent does not know of one's existence, and for which there was special training.