Media playback is unsupported on your device Media caption Security correspondent Gordon Corera has had a rare tour of GCHQ

The operations centre sits on one of the upper floors of GCHQ and runs 24/7. At any one time, a team of analysts might be monitoring the kidnap of a British citizen abroad or an ongoing counter-terrorist operation run jointly with MI5.

In one corner, a large globe visualises all the cyber attacks targeting the UK from around the world. The room is a reminder of the range of activity that GCHQ is involved in - as well as its global reach in monitoring communications and data flows.

Russian cyber attacks are high up the agenda, in the wake of claims Moscow interfered in the US election and is trying the same in Europe.

"We have been watching Russian cyber activity since the mid 1990s," GCHQ's outgoing director, Robert Hannigan, tells the BBC.

'Reckless and interfering'

"The scale has changed. They've invested a lot of money and people in offensive cyber behaviour and critically they've decided to do reckless and interfering things in European countries."

Mr Hannigan says that whilst it is impossible to be absolutely sure, the defences against such attacks seem to have held in the UK.

One of his legacies will be the creation of the National Cyber Security Centre, an arm of GCHQ which is based in London and is much more public facing in providing protective advice to the country about the threats in cyberspace.

Terrorism sits alongside cyber threats on the agenda. So-called Islamic State - or ISIL - has proved adept at exploiting the power of the internet.

Image copyright PA Image caption The Queen visits the National Cyber Security Centre - part of intelligence agency GCHQ

"It's one of their most important assets. As they are defeated on the ground, the 'online caliphate' will become more important.

"They will continue to try to use the media to crowd-source terrorism to get people around the world to go and commit acts of violence on their behalf...

"There are things we can do to contest ISIL in this media space... but it's not just for governments to do operations online. It's for the companies and for the rest of media and society to have the will to drive this material off the internet..."

When he took over as head of GCHQ in 2014, Mr Hannigan launched what was seen as a broadside against technology companies - arguing they were in denial about the way they were used by terrorist groups to communicate and spread their message.

No place to hide

In the wake of the Westminster attack, the Home Secretary Amber Rudd said that companies should not offer a safe space for terrorists to hide - a reference to the development of end-to-end encryption services which make it impossible to provide the content of communications, even on production of a warrant.

GCHQ, as well as trying to break codes, also works to secure communications and so treads a fine line.

"Encryption matters hugely to the safety of citizens and to the economy.... The home secretary is talking about a particular problem - that this strong encryption is being abused by terrorists and criminals...

"Our best way forward is to sit down with the tech companies..."

Image caption Robert Hannigan steps down as GCHQ boss on Friday after nearly three years in the job

The other area of tension with firms has been over extremist content hosted on websites.

Here, government has recently been placing pressure on the companies to be more proactive in taking down content rather than waiting for it to be reported to them.

"I think they have moved a long way [but] there's further to go," Mr Hannigan says.

"When I started the job in 2014 they really were reluctant to accept responsibility for anything they carried on their networks - whether that was terrorism, child sexual exploitation or any other kind of crime."

Image copyright PA Image caption GCHQ can detect the work of hackers around the globe

The threat from IS has been particularly acute in Europe in the last few years. That has driven increased security co-operation - so will Brexit be a problem?

"I don't think so, because the intelligence-sharing has never been through EU structures and national security has never been part of the European Union's remit.

"It's simply a statement of fact that we have very, very strong intelligence and security and defence capabilities and we bring a lot to Europe and to our European partners..."

The relationship with the US is by far the deepest, which he says will not change under the Trump administration.

"It's the most powerful weapon we have against terrorism in particular and has massively paid dividends in the last 10 years."

US spying claims

In recent weeks, there was controversy after reports claimed the Obama administration asked GCHQ to spy on President-elect Donald Trump.

GCHQ took the unusual step of publicly denying this.

"We get crazy conspiracy theories thrown at us every day," Mr Hannigan says. "We ignore most of them. On this occasion it was so crazy that we felt we should say so and we have said it's a ridiculous suggestion."

Image caption A globe in the operations centre visualises the cyber attacks targeting the UK

Deep underground, beneath the grass sit a series of cavernous computer halls. The noise is at points overwhelming.

Much effort goes into cooling the machines. Some of the endless racks contain off-the-shelf server technology but large specialist supercomputers sit alongside which are used by the cryptanalysts for code-breaking.

The exact specifications of these machines and just how much computing power sits in Cheltenham is classified largely to keep other states - primarily the Russians and Chinese - guessing.

"It's impossible to do counterterrorism or cyber security without that kind of power," Mr Hannigan explains, arguing that the challenge remains finding the small needle in the haystack of the massive volume of data on the internet.

Another aspect of Mr Hannigan's legacy will be the push for greater transparency and openness.

"It's very important in a democracy to have the consent of the public as well as the legislation in place and to explain that everything we do is under the law," Mr Hannigan says.

'Dark side'

He took over an agency bruised by the Edward Snowden revelations and allegations of "mass surveillance".

"Obviously a debate on privacy and greater transparency are good things - but it was perfectly possible to do that and indeed it was happening anyway without the damage that the Snowden revelations did. The same is true of the WikiLeaks disclosures."

Mr Hannigan says he and the organisation remain optimistic, rather than pessimistic, about the spread of technology.

"Technology and the internet are overwhelmingly brilliant things for human progress," Mr Hannigan says.

"Unfortunately there will always be people who want to abuse the latest technology. And it's our job to deal with that dark side."

Mr Hannigan's successor, Jeremy Fleming, formerly Deputy Director of MI5, takes over on Friday.