-
Notifications
You must be signed in to change notification settings - Fork 0
/
devsearch
292 lines (265 loc) · 9.46 KB
/
devsearch
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
##########################################################################
# File Name: devsearch.sh
# Author: soh0ro0t
# mail: [email protected]
# Created Time: Wed 23 Dec 2020 03:02:10 PM CST
#########################################################################
dev_type="none"
FILENAME_FOUND=0
DEVTYPE_FOUND=0
RAW_FOUND=0
PREBUILT_DEV_PATH=".*/kernel/drivers"
SHOW_WARNINGS_ONLY=0
QUIET=0
CRONJOB=0
# Normal color names
CYAN="$(printf '\033[0;36m')"
BLUE="$(printf '\033[0;34m')"
BROWN="$(printf '\033[0;33m')"
DARKGRAY="$(printf '\033[0;30m')"
GRAY="$(printf '\033[0;37m')"
GREEN="$(printf '\033[1;32m')"
LIGHTBLUE="$(printf '\033[0;94m')"
MAGENTA="$(printf '\033[1;35m')"
PURPLE="$(printf '\033[0;35m')"
RED="$(printf '\033[1;31m')"
YELLOW="$(printf '\033[1;33m')"
WHITE="$(printf '\033[1;37m')"
# Markup
BOLD="${WHITE}"
# With background
BG_BLUE="$(printf '\033[0;44m')"
# Semantic names
HEADER="${WHITE}"
NORMAL="$(printf '\033[0m')"
WARNING="${RED}"
SECTION="${YELLOW}"
NOTICE="${YELLOW}"
OK="${GREEN}"
BAD="${RED}"
################################################################################
# Name : RemoveTempFiles()
################################################################################
# Remove any temporary files
RemoveTempFiles() {
if [ ! "${TEMP_FILES}" = "" ]; then
echo "Temporary files: ${TEMP_FILES}"
# Clean up temp files
for FILE in ${TEMP_FILES}; do
# Temporary files should be in /tmp
TMPFILE=$(echo ${FILE} | egrep "^/tmp/lynis" | grep -v "\.\.")
if [ ! "${TMPFILE}" = "" ]; then
if [ -f ${TMPFILE} ]; then
echo "Action: removing temporary file ${TMPFILE}"
rm -f ${TMPFILE}
else
echo "Info: temporary file ${TMPFILE} was already removed"
fi
else
echo "Found invalid temporary file (${FILE}), not removed. Check your /tmp directory."
fi
done
else
echo "No temporary files to be deleted"
fi
}
################################################################################
# Name : RemovePIDFile()
################################################################################
# Remove PID file
RemovePIDFile() {
# Test if PIDFILE is defined, before checking file presence
if [ ! "${PIDFILE}" = "" ]; then
if [ -f ${PIDFILE} ]; then
rm -f $PIDFILE;
echo "PID file removed (${PIDFILE})"
else
echo "PID file not found (${PIDFILE})"
fi
fi
}
################################################################################
# Name : ExitFatal()
# Description : Perform exit of the program (with code 1), clean up resources
#
# Input : $1 = text string (optional)
# Returns : <nothing>
# Usage : ExitFatal
################################################################################
ExitFatal() {
RemovePIDFile
RemoveTempFiles
echo "${PROGRAM_NAME} ended with exit code 1."
if [ $# -eq 1 ]; then
${ECHOCMD} ""
${ECHOCMD} "${RED}Fatal error${NORMAL}: ${WHITE}$1${NORMAL}"
${ECHOCMD} ""
fi
exit 1
}
################################################################################
# Name : Display()
# Description : Show text on screen, with markup
#
# Input : <multiple parameters, see test>
# Returns : <nothing>
################################################################################
Display() {
INDENT=0; TEXT=""; RESULT=""; COLOR=""; SPACES=0; SHOWDEBUG=0
while [ $# -ge 1 ]; do
case $1 in
--color)
shift
case $1 in
GREEN) COLOR=$GREEN ;;
RED) COLOR=$RED ;;
WHITE) COLOR=$WHITE ;;
YELLOW) COLOR=$YELLOW ;;
esac
;;
--debug)
SHOWDEBUG=1
;;
--indent)
shift
INDENT=$1
;;
--result)
shift $(( $# > 0 ? 1 : 0 ))
RESULT=$1
;;
--text)
shift $(( $# > 0 ? 1 : 0 ))
TEXT=$1
;;
*)
echo "INVALID OPTION (Display): $1"
ExitFatal
;;
esac
# Go to next parameter
shift $(( $# > 0 ? 1 : 0 ))
done
if [ -z "${RESULT}" ]; then
RESULTPART=""
else
if [ ${CRONJOB} -eq 0 ]; then
RESULTPART=" [ ${COLOR}${RESULT}${NORMAL} ]"
else
RESULTPART=" [ ${RESULT} ]"
fi
fi
if [ ! -z "${TEXT}" ]; then
SHOW=0
TEXTPART="${BLUE}${TEXT}${NORMAL}"
if [ ${SHOW_WARNINGS_ONLY} == 1 ]; then
if [ "${RESULT}" = "WARNING" ]; then SHOW=1; fi
elif [ ${QUIET} == 0 ]; then SHOW=1
fi
if [ ${SHOW} -eq 1 ]; then
# Display:
# - for full shells, count with -m instead of -c, to support language locale (older busybox does not have -m)
# - wc needs LANG to deal with multi-bytes characters but LANG has been unset in include/consts
LINESIZE=$(export LC_ALL= ; export LANG="${Display_LANG}";echo "${TEXT}" | wc -m | tr -d ' ')
if [ ${SHOWDEBUG} -eq 1 ]; then DEBUGTEXT=" [${PURPLE}DEBUG${NORMAL}]"; else DEBUGTEXT=""; fi
if [ ${INDENT} -gt 0 ]; then SPACES=$((62 - INDENT - LINESIZE)); fi
if [ ${SPACES} -lt 0 ]; then SPACES=0; fi
if [ ${CRONJOB} -eq 0 ]; then
# Check if we already have already discovered a proper echo command tool. It not, set it default to 'echo'.
if [ "${ECHOCMD}" = "" ]; then ECHOCMD="echo"; fi
${ECHOCMD} -e "\033[${INDENT}C${TEXTPART}\033[${SPACES}C${RESULTPART}${DEBUGTEXT}"
else
echo "${TEXT}${RESULTPART}"
fi
fi
fi
}
usage() {
Display --indent 4 --text "Usage: devsearch.sh source_dir dev_name [dev_type]" --color RED --result ""
}
if [[ $# -gt 2 ]]; then
dir=$1
dev=$2
dev_type=$3
elif [[ $# -eq 2 ]];then
dir=$1
dev=$2
else
usage
fi
search_by_filename() {
filename=$1
command="find $dir -type f -iname $filename.c 2>/dev/null"
Display --indent 4 --text "[+] search by filename" --color RED --result ""
Display --indent 8 --text "[+] $command" --color RED --result ""
result="$(eval $command)"
if [ $? -eq 0 ];then
Display --indent 4 --text "[+] $result" --color RED --result ""
FILENAME_FOUND=1
return
fi
}
search_by_devtype() {
dev_type=$1
Display --indent 4 --text "[+] search by device type" --color BLUE --result ""
if [ "dev_type"== *"character"* ];then
# try to search registering code snippet of misc device
result=$(find $dir -path ".*/kernel/drivers" -exec grep -r -E 'misc_register\(&"$filename"' {} \;)
if [ $? -eq 0 ];then
Display --indent 4 --text "[+] $result" --color BLUE --result ""
DEVTYPE_FOUND=1
return
fi
else
Display --indent 4 --text "[!] cannot search by given type" --color RED --result ""
fi
}
search_by_force() {
target=$1
# search by device name assignment snippet code with given name
result=$(find $dir -type f -regextype posix-extended -regex '.*\.(c|h)' -exec grep 'name = "$target"' {} \;)
if [ $? -eq 0 ];then
Display --indent 4 --text "[+] $result" --color BLUE --result ""
RAW_FOUND=1
return
else
# some driver uses specific MACRO to imply its name
DEV=${dev^^}
result=$(find $dir -path ".*/kernel/drivers" -exec grep -r -l -E "$DEV"_DEVICE_NAME {} \;)
if [ $? -eq 0 ];then
Display --indent 4 --text "[+] $result" --color BLUE --result ""
RAW_FOUND=1
return
fi
# blute force searching
result=$(find $dir -type f -regextype posix-extended -regex '.*\.(c|h)' -exec grep 'name = \""$dev"\"' {} \;)
if [ $? -eq 0 ];then
Display --indent 4 --text "[+] $result" --color BLUE --result ""
RAW_FOUND=1
return
else
# some driver uses specific MACRO to imply its name
DEV=${dev^^}
result=$(find $dir -path ".*/kernel/drivers" -exec grep -r -l -E "$DEV"_DEVICE_NAME {} \;)
if [ $? -eq 0 ];then
Display --indent 4 --text "[+] $result" --color BLUE --result ""
RAW_FOUND=1
return
fi
# blute force searching
result=$(find $dir -type f -regextype posix-extended -regex '.*\.(c|h)' -exec grep 'name = \""$dev"\"' {} \;)
fi
fi
}
search_by_filename $dev
if [[ $FILENAME_FOUND -eq 1 ]];then
exit 0
fi
search_by_devtype $dev
if [[ $DEVTYPE_FOUND -eq 1 ]];then
exit 0
fi
search_by_force $dev
if [[ $RAW_FOUND -eq 1 ]];then
exit 0
fi