The Boeing YB-40 Flying Fortress was a modification of the United States B-17 Flying Fortress bomber aircraft, converted to act as a heavily armed escort for other bombers during World War II. At the time of its development, long-range fighter aircraft such as the P-51 Mustang were just entering quantity production, and thus were not yet available to accompany bombers all the way from England to Germany and back.
Work on the prototype, Project V-139, began in September, 1942 by converting the second production B-17F-1-BO (serial number 41-24341) built. Conversion work was done by Lockheed's Vega company.
The aircraft differed from the standard B-17 in that a second manned dorsal turret was installed in the former radio compartment, just behind the bomb bay and forward of the ventral ball turret's location. The single .50-calibre light-barrel (12.7 mm) Browning machine gun at each waist station was replaced by two of them mounted side-by-side as a twin-mount emplacement, with a mount for each pair of these being very much like the tail gun setup in general appearance. The bombardier's equipment was also replaced with two .50-calibre light-barrel Browning AN/M2 machine guns in a remotely operated Bendix designed "chin"-location turret.
The existing "cheek" machine guns (on the sides of the forward fuselage at the bombardier station), initially removed from the configuration, were restored in England to provide a total of sixteen guns, and the bomb bay was converted to an ammunition magazine. Additional armor plating was installed to protect crew positions. 
The aircraft's gross weight was some 4,000 pounds greater than a fully armed B-17. An indication of the burden this placed on the YB-40 is that while the B-17F on which it was based was rated to climb to 20,000 ft in 25 minutes, the YB-40 was rated at 48 minutes. Part of the decreased performance was due to the weight increase, and part was due to the greater aerodynamic drag of the gun stations.
The first flight of the XB-40 was on 10 November 1942. The first order of 13 YB-40s was made in October 1942. A follow-up order for 12 more was made in January 1943. The modifications were performed by Douglas Aircraft at their Tulsa, Oklahoma center, and the first aircraft were completed by the end of March 1943. Twenty service test aircraft were ordered, Vega Project V-140, as YB-40 along with four crew trainers designated TB-40. 
Because Vega had higher priority production projects, the YB-40/TB-40 assembly job was transferred to Douglas. A variety of different armament configurations was tried. Some YB-40s were fitted with four-gun nose and tail turrets. Some carried cannon of up to 40 mm in calibre, and a few carried up to as many as 30 guns of various calibres in multiple hand-held positions in the waist as well as in additional power turrets above and below the fuselage.
Externally, the XB-40 had the symmetrical waist windows of the standard B-17F and the second dorsal turret integrated into a dorsal fairing. In contrast, most of the YB-40s had staggered waist windows for better freedom of movement of the waist gunners, and the aft dorsal turret was moved slightly backwards, so that it stood clear of the dorsal fairing.
The YB-40's mission was to provide a heavily gunned escort capable of accompanying bombers all the way to the target and back. Of the initial order of 13, one (43-5732) was lost on the delivery flight from Iceland to UK in May 1943; it force-landed in a peat bog on an offshore Scottish island after running out of fuel. Although removed to Stornoway and repaired, it never flew in combat. The remaining 12 were allocated to the 92d Bombardment Group (Heavy), being assigned to the 327th Bombardment Squadron, stationed at RAF Alconbury (AAF-102) on 8 May 1943.
YB-40s flew in the following operational missions:
Altogether of the 59 aircraft dispatched, 48 sorties were credited. Five German fighter kills and two probables (likely kills) were claimed, and one YB-40 was lost, shot down on the 22 June mission to Hüls, Germany. Tactics were revised on the final five missions by placing a pair of YB-40s in the lead element of the strike to protect the mission commander.
Overall the concept proved a failure because the YB-40 could not keep up with standard B-17Fs, particularly after they had dropped their bombs. Despite the failure of the project as an operational aircraft, it led directly to modifications conspicuous on the final production variant of the B-17, the B-17G:
Once the test program ended, most of the surviving aircraft returned to the United States in November 1943 and were used as trainers. 42-5736 ("Tampa Tornado") was flown to RAF Kimbolton on 2 October 1943 where it was put on display and later used as a group transport. It was returned to the United States on 28 March 1944. All of the aircraft were sent to reclamation, mostly at RFC Ontario in May 1945, being broken up and smelted. (A couple of the YB-40s can be seen in the 1946 movie The Best Years of Our Lives, in the famous scene shot at the Ontario "graveyard".) No airframes were sold on the civil market.
XB-40: Conversion of B-17F-1-BO 41-24342 (Not deployed to ETO)
YB-40: Conversions of B-17F-10-VE 42-5732; 5733, "Peoria Prowler"; 5734, "Seymour Angel"; 5735, "Wango Wango"; 5736, "Tampa Tornado"; 5737, "Dakota Demon"; 5738, "Boston Tea Party"; 5739, "Lufkin Ruffian"; 5740, "Monticello"; 5741, "Chicago"; 5742, "Plain Dealing Express"; 5743, "Woolaroc"; 5744, "Dollie Madison" (All deployed to ETO)
YB-40: Conversions of B-17F-35-VEs 42-5920, 5921, 5923, 5924, 5925, and 5927 (Not deployed to ETO)
TB-40: Conversions of B-17F-25-VEs 42-5833 and 5834; B-17F-30-VE 42-5872, and B-17F-35-VE 42-5926 (5833 deployed to ETO, but not used in combat; remainder remained in the United States).