-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathjamRF_v1.py
More file actions
331 lines (284 loc) · 10.5 KB
/
jamRF_v1.py
File metadata and controls
331 lines (284 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# SPDX-License-Identifier: GPL-3.0
#
# GNU Radio Python Flow Graph
# Title: Not titled yet
# Author: Abubakar Sani Ali
# GNU Radio version: 3.8.1.0
###################################################################################
# Importing Libraries
###################################################################################
import platform
import time
import yaml
from gnuradio import gr
from gnuradio import blocks
from gnuradio import analog
from gnuradio import audio
from gnuradio import fft
from gnuradio.fft import window
from gnuradio import filter
from gnuradio.filter import firdes
from statistics import mean
import osmosdr
import sys
import numpy as np
from random import randint
###################################################################################
# Sensing Channel Activity Function
###################################################################################
def sense(freq, delay):
# print("Sense has received the call")
###############################################################################
# Variables
###############################################################################
samp_rate = 20e6 # Delay before hopping to the next channel in sec
sdr_bandwidth = 20e6 # Hackrf SDR Bandwidth
tb = gr.top_block()
###############################################################################
# Blocks
###############################################################################
# Source block
osmosdr_source = osmosdr.source(
args="numchan=" + str(1) + " " + ""
)
osmosdr_source.set_time_unknown_pps(osmosdr.time_spec_t())
osmosdr_source.set_sample_rate(samp_rate)
osmosdr_source.set_center_freq(freq, 0)
osmosdr_source.set_freq_corr(0, 0)
osmosdr_source.set_gain(0, 0)
osmosdr_source.set_if_gain(16, 0)
osmosdr_source.set_bb_gain(16, 0)
osmosdr_source.set_antenna('', 0)
osmosdr_source.set_bandwidth(sdr_bandwidth, 0)
# Inbetween blocks
low_pass_filter = filter.fir_filter_ccf(
1,
firdes.low_pass(
1,
samp_rate,
75e3,
25e3,
window.WIN_HAMMING,
6.76))
complex_to_mag_squared = blocks.complex_to_mag_squared(1)
# Sink block
file_sink = blocks.file_sink(gr.sizeof_float * 1, 'output.bin', False)
file_sink.set_unbuffered(True)
##############################################################################
# Connecting Blocks
##############################################################################
tb.connect(osmosdr_source, low_pass_filter)
tb.connect(low_pass_filter, complex_to_mag_squared)
tb.connect(complex_to_mag_squared, file_sink)
tb.start()
time.sleep(delay)
tb.stop()
tb.wait()
###################################################################################
# Jamming Channel Function
###################################################################################
def jam(freq, waveform, power, delay=1):
###############################################################################
# Variables
###############################################################################
print(f"\nThe frequency currently jammed is: {freq / (10e5)}MHz")
samp_rate = 20e6 # Sample Rate
sdr_bandwidth = 20e6 # Hackrf SDR Bandwidth
RF_gain, IF_gain = set_gains(power) # Hackrf SDR antenna gain
tb = gr.top_block()
##############################################################################
# waveform selection
##############################################################################
if waveform == 1:
source = analog.sig_source_c(samp_rate, analog.GR_SIN_WAVE, 1000, 1, 0, 0)
elif waveform == 2:
source = analog.sig_source_f(samp_rate, analog.GR_SIN_WAVE, 1000, 1, 0, 0)
elif waveform == 3:
source = analog.noise_source_c(analog.GR_GAUSSIAN, 1.0, 1)
else:
print("invalid selection")
#############################################################################
# Hackrf Trasmitter options
#############################################################################
freq_mod = analog.frequency_modulator_fc(1)
if platform.system() == "Windows":
osmosdr_sink = osmosdr.sink("hackrf=0")
print("Detected Windows OS")
else:
osmosdr_sink = osmosdr.sink(
args="numchan=" + str(1) + " " + ""
)
print("Detected Linux OS")
osmosdr_sink.set_time_unknown_pps(osmosdr.time_spec_t())
osmosdr_sink.set_sample_rate(samp_rate)
osmosdr_sink.set_center_freq(freq, 0)
osmosdr_sink.set_freq_corr(0, 0)
osmosdr_sink.set_gain(RF_gain, 0)
osmosdr_sink.set_if_gain(IF_gain, 0)
osmosdr_sink.set_bb_gain(20, 0)
osmosdr_sink.set_antenna('', 0)
osmosdr_sink.set_bandwidth(sdr_bandwidth, 0)
##############################################################################
# Connecting Blocks
##############################################################################
if waveform == 2:
tb.connect(source, freq_mod, osmosdr_sink)
else:
tb.connect(source, osmosdr_sink)
print(delay)
tb.start()
if delay != 0:
time.sleep(delay)
tb.stop()
tb.wait()
else:
tb.wait()
##################################################################################
# Set Frequency
##################################################################################
def set_frequency(channel, ch_dist):
if channel == 1:
freq = init_freq
else:
freq = init_freq + (channel - 1) * ch_dist
return freq
##################################################################################
# Set RF Gains
##################################################################################
def set_gains(power):
if -40 <= power <= 5:
RF_gain = 0
if power < -5:
IF_gain = power + 40
elif -5 <= power <= 2:
IF_gain = power + 41
elif 2 < power <= 5:
IF_gain = power + 42
elif power > 5:
RF_gain = 14
IF_gain = power + 34
else:
print("invalid Jammer Transmit power")
return RF_gain, IF_gain
##################################################################################
# Detect Activity Function
##################################################################################
def detect():
with open("output.bin", mode='rb') as file:
fileContent = file.read()
samples = np.memmap("output.bin", mode="r", dtype=np.float32)
p = 0.5 * mean(samples)
return p
##################################################################################
# Main Function
##################################################################################
if __name__ == "__main__":
# Global options
config_file = open("config_v1.yaml")
options = yaml.load(config_file, Loader=yaml.FullLoader)
jammer = options.get("jammer")
jamming = options.get("jamming")
waveform = options.get("waveform")
power = options.get("power")
band = options.get("band")
freq = options.get("freq")
ch_dist = options.get("ch_dist")
allocation = options.get("allocation")
t_jamming = options.get("t_jamming")
duration = options.get("duration")
print(jammer, t_jamming)
# Special options
if jammer != 1:
if band == 1:
ch_dist = ch_dist * 10e5
init_freq = 2412e6
lst_freq = 2484e6
elif band == 2:
ch_dist = 20e6
if allocation == 1:
init_freq = 5180e6
lst_freq = 5240e6
elif allocation == 2:
init_freq = 5260e6
lst_freq = 5320e6
elif allocation == 3:
init_freq = 5500e6
lst_freq = 5720e6
elif allocation == 4:
init_freq = 5745e6
lst_freq = 5825e6
else:
print('Invalid selection')
else:
print('Invalid selection')
n_channels = (lst_freq - init_freq) // ch_dist
if t_jamming > duration:
t_jamming = duration
if jamming == 2:
t_sensing = 0.05
threshold = 0.0002
# Starting RF Jamming
if jammer == 1:
freq = freq * 10e5
if jamming == 1:
print("JAM!")
jam(freq, waveform, power, t_jamming)
elif jamming == 2:
# Sensing Channel
sense(freq, t_sensing)
rx_power = detect()
print(rx_power)
# If channel is active then jam it
if rx_power > threshold:
jam(freq, waveform, power, t_jamming)
else:
print("Invalid jamming option selection")
elif jammer == 2:
channel = 1 # Initial Channel @ 2.412GHz
start_time = time.time()
while True:
freq = set_frequency(channel, ch_dist)
if jamming == 1:
# Jam
jam(freq, waveform, power, t_jamming)
elif jamming == 2:
# Sensing Channel
sense(freq, t_sensing)
rx_power = detect()
# If channel is active then jam it
if rx_power > threshold:
jam(freq, waveform, power, t_jamming)
else:
print("Invalid jamming option selection")
# Go to next channel
channel = 1 if channel > n_channels else channel + 1
# Checking elapsed time
jamming_time_per_run = time.time() - start_time
if jamming_time_per_run >= duration:
break
elif jammer == 3:
start_time = time.time()
while True:
channel = int(randint(1, int(n_channels) + 1))
freq = set_frequency(channel, ch_dist)
if jamming == 1:
# Jam
jam(freq, waveform, power, t_jamming)
elif jamming == 2:
# Sensing Channel
sense(freq, t_sensing)
rx_power = detect()
# If channel is active then jam it
if rx_power > threshold:
jam(freq, waveform, power, t_jamming)
else:
print("Invalid jamming option selection")
# Checking elapsed time
jamming_time_per_run = time.time() - start_time
if jamming_time_per_run >= duration:
break
else:
print("invalid jammer selection")